Skip to content

Commit

Permalink
replace cout with easylogging custom logger to avoid race conditions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterTea authored Jul 30, 2020
1 parent 36a5821 commit bfbe72c
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 232 deletions.
10 changes: 6 additions & 4 deletions src/base/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ bool ClientConnection::connect() {
// survived.
STERROR << "Error connecting to server: " << response.status() << ": "
<< response.error();
cout << "Error connecting to server: " << response.status() << ": "
<< response.error() << endl;
CLOG(INFO, "stdout") << "Error connecting to server: "
<< response.status() << ": " << response.error()
<< endl;
string s = string("Error connecting to server: ") +
to_string(response.status()) + string(": ") + response.error();
throw std::runtime_error(s.c_str());
Expand Down Expand Up @@ -114,8 +115,9 @@ void ClientConnection::pollReconnect() {
if (response.status() != RETURNING_CLIENT) {
STERROR << "Error reconnecting to server: " << response.status()
<< ": " << response.error();
cout << "Error reconnecting to server: " << response.status()
<< ": " << response.error() << endl;
CLOG(INFO, "stdout")
<< "Error reconnecting to server: " << response.status() << ": "
<< response.error() << endl;
socketHandler->close(newSocketFd);
} else {
recover(newSocketFd);
Expand Down
15 changes: 14 additions & 1 deletion src/base/LogHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ INITIALIZE_EASYLOGGINGPP
namespace et {
void interruptSignalHandler(int signum) {
STERROR << "Got interrupt";
cout << endl << "Got interrupt (perhaps ctrl+c?). Exiting." << endl;
CLOG(INFO, "stdout") << endl
<< "Got interrupt (perhaps ctrl+c?). Exiting." << endl;
::exit(signum);
}

Expand Down Expand Up @@ -63,4 +64,16 @@ string LogHandler::stderrToFile(const string &pathPrefix) {
setvbuf(stderr_stream, NULL, _IOLBF, BUFSIZ); // set to line buffering
return stderrFilename;
}

void LogHandler::setupStdoutLogger() {
el::Logger *stdoutLogger = el::Loggers::getLogger("stdout");
// Easylogging configurations
el::Configurations stdoutConf;
stdoutConf.setToDefault();
// Values are always std::string
stdoutConf.setGlobally(el::ConfigurationType::Format, "%msg");
stdoutConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
stdoutConf.setGlobally(el::ConfigurationType::ToFile, "false");
el::Loggers::reconfigureLogger(stdoutLogger, stdoutConf);
}
} // namespace et
1 change: 1 addition & 0 deletions src/base/LogHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class LogHandler {
string maxlogsize = "20971520");
static void rolloutHandler(const char *filename, std::size_t size);
static string stderrToFile(const string &pathPrefix);
static void setupStdoutLogger();
};
} // namespace et
#endif // __ET_LOG_HANDLER__
6 changes: 3 additions & 3 deletions src/base/TcpSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
STERROR << "Error binding " << p->ai_family << "/" << p->ai_socktype
<< "/" << p->ai_protocol << ": " << localErrno << " "
<< strerror(localErrno);
cout << "Error binding " << p->ai_family << "/" << p->ai_socktype << "/"
<< p->ai_protocol << ": " << localErrno << " "
<< strerror(localErrno) << endl;
CLOG(INFO, "stdout") << "Error binding " << p->ai_family << "/"
<< p->ai_socktype << "/" << p->ai_protocol << ": "
<< localErrno << " " << strerror(localErrno) << endl;
stringstream oss;
oss << "Error binding port " << port << ": " << localErrno << " "
<< strerror(localErrno);
Expand Down
5 changes: 3 additions & 2 deletions src/htm/HtmClientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main(int argc, char** argv) {

auto result = options.parse(argc, argv);
if (result.count("help")) {
cout << options.help({}) << endl;
CLOG(INFO, "stdout") << options.help({}) << endl;
exit(0);
}

Expand Down Expand Up @@ -94,7 +94,8 @@ int main(int argc, char** argv) {
}

// This means we are the client to the daemon
std::this_thread::sleep_for(std::chrono::microseconds(10*1000)); // Sleep for 10ms to let the daemon come alive
std::this_thread::sleep_for(std::chrono::microseconds(
10 * 1000)); // Sleep for 10ms to let the daemon come alive
shared_ptr<SocketHandler> socketHandler(new PipeSocketHandler());
SocketEndpoint pipeEndpoint;
pipeEndpoint.set_name(HtmServer::getPipeName());
Expand Down
Loading

0 comments on commit bfbe72c

Please sign in to comment.