Skip to content

Commit

Permalink
new optional feature: print logs to stdout with files.
Browse files Browse the repository at this point in the history
  • Loading branch information
SwimmingTiger committed Feb 27, 2017
1 parent 400d90b commit ba38232
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -27,3 +27,7 @@
*.exe
*.out
*.app

# build dir & files
/build*/
/CMakeLists.txt
19 changes: 19 additions & 0 deletions CMakeLists4Windows.txt
Expand Up @@ -86,6 +86,25 @@ else()
message("(-DPOOLAGENT__USE_GLOG=ON switching to GLog)")
endif()

###
# Print logs to stdout with files
###
option(POOLAGENT__GLOG_TO_STDOUT
"Print logs to stdout with files" OFF)

if(POOLAGENT__GLOG_TO_STDOUT)
message("-- Print logs to stdout with files")
set(CompilerFlags CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE)
foreach(CompilerFlag ${CompilerFlags})
set(${CompilerFlag} "${${CompilerFlag}} /DGLOG_TO_STDOUT")
message("${CompilerFlag}=${${CompilerFlag}}")
endforeach()
else()
message("-- Only logs to file")
message("(-DPOOLAGENT__GLOG_TO_STDOUT=ON printing logs to stdout with files)")
endif()


SET(CMAKE_CXX_COMPILER_ARG1 "-std=c++98")
SET(CMAKE_C_COMPILER_ARG1 "-std=c99")
Expand Down
3 changes: 3 additions & 0 deletions README-Windows.md
Expand Up @@ -73,6 +73,9 @@ POOLAGENT__USE_IOCP:BOOL=OFF
# Use GLog for logging replace stdout
POOLAGENT__USE_GLOG:BOOL=OFF
# Print logs to stdout with files
POOLAGENT__GLOG_TO_STDOUT:BOOL=OFF
```

## Static linking with VC++ runtime library
Expand Down
28 changes: 28 additions & 0 deletions src/Utils.cc
Expand Up @@ -21,7 +21,35 @@
#include <stdarg.h>

#include <algorithm>
#include <iostream>
#include <iomanip>

#if defined(SUPPORT_GLOG) && defined(GLOG_TO_STDOUT)
void GLogToStdout::send(google::LogSeverity severity, const char* full_filename,
const char* base_filename, int line, const struct ::tm* tm_time,
const char* message, size_t message_len) {

std::cout << ToString(severity, base_filename, line, tm_time, message, message_len) << std::endl;
}

std::string GLogToStdout::ToString(google::LogSeverity severity, const char* file, int line,
const struct ::tm* tm_time, const char* message, size_t message_len) {

std::ostringstream stream;

stream << '[' << 1900 + tm_time->tm_year << '-'
<< std::setfill('0')
<< std::setw(2) << 1 + tm_time->tm_mon << '-'
<< std::setw(2) << tm_time->tm_mday << ' '
<< std::setw(2) << tm_time->tm_hour << ':'
<< std::setw(2) << tm_time->tm_min << ':'
<< std::setw(2) << tm_time->tm_sec << "] ";

stream << string(message, message_len);

return stream.str();
}
#endif

string Strings::Format(const char * fmt, ...) {
char tmp[512];
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.h
Expand Up @@ -68,6 +68,20 @@ using std::vector;
//
#define BTCCOM_MINER_AGENT "btccom-agent/0.1"

#if defined(SUPPORT_GLOG) && defined(GLOG_TO_STDOUT)
// Print logs to stdout with glog
class GLogToStdout : public google::LogSink {
public:
virtual void send(google::LogSeverity severity, const char* full_filename,
const char* base_filename, int line,
const struct ::tm* tm_time,
const char* message, size_t message_len);

virtual std::string ToString(google::LogSeverity severity, const char* file, int line,
const struct ::tm* tm_time,
const char* message, size_t message_len);
};
#endif

class Strings {
public:
Expand Down
8 changes: 8 additions & 0 deletions src/agent/AgentMain.cc
Expand Up @@ -20,6 +20,7 @@

#include <fstream>
#include <streambuf>
#include "Utils.h"

#include "Server.h"

Expand Down Expand Up @@ -81,6 +82,13 @@ int main(int argc, char **argv) {
FLAGS_max_log_size = 10; // max log file size 10 MB
FLAGS_logbuflevel = -1;
FLAGS_stop_logging_if_full_disk = true;

#if defined(GLOG_TO_STDOUT)
// Print logs to stdout with glog
GLogToStdout glogToStdout;
google::AddLogSink(&glogToStdout);
#endif

#endif

signal(SIGTERM, handler);
Expand Down

0 comments on commit ba38232

Please sign in to comment.