diff options
author | Chris Wilson <chris+github@qwirx.com> | 2008-01-31 23:44:54 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2008-01-31 23:44:54 +0000 |
commit | 86f0df155e183924693f6bacfc0a158a14f05bed (patch) | |
tree | d7931c3b160f54dbd6e20dcd9841fdb4579da12b | |
parent | 2ccdaf9a575f4cc0d758a87300b7cf51d96c9779 (diff) |
Add support for microsecond timestamps and PID logging on console log
for daemons.
-rw-r--r-- | lib/common/Logging.cpp | 50 | ||||
-rw-r--r-- | lib/common/Logging.h | 2 | ||||
-rw-r--r-- | lib/server/Daemon.cpp | 19 |
3 files changed, 55 insertions, 16 deletions
diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index 2b81b52b..7da7409d 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -182,6 +182,7 @@ Logger::~Logger() bool Console::sShowTime = false; bool Console::sShowTimeMicros = false; bool Console::sShowTag = false; +bool Console::sShowPID = false; std::string Console::sTag; void Console::SetTag(const std::string& rTag) @@ -200,6 +201,11 @@ void Console::SetShowTimeMicros(bool enabled) sShowTimeMicros = enabled; } +void Console::SetShowPID(bool enabled) +{ + sShowPID = enabled; +} + bool Console::Log(Log::Level level, const std::string& rFile, int line, std::string& rMessage) { @@ -215,7 +221,7 @@ bool Console::Log(Log::Level level, const std::string& rFile, target = stderr; } - std::string msg; + std::ostringstream buf; if (sShowTime) { @@ -231,8 +237,6 @@ bool Console::Log(Log::Level level, const std::string& rFile, if (localtime_r(&seconds, &tm_now) != NULL) #endif { - std::ostringstream buf; - buf << std::setfill('0') << std::setw(2) << tm_ptr->tm_hour << ":" << std::setw(2) << tm_ptr->tm_min << ":" << @@ -244,40 +248,58 @@ bool Console::Log(Log::Level level, const std::string& rFile, } buf << " "; - msg += buf.str(); } else { - msg += strerror(errno); - msg += " "; + buf << strerror(errno); + buf << " "; } } if (sShowTag) { - msg += "[" + sTag + "] "; + if (sShowPID) + { + buf << "[" << sTag << " " << getpid() << "] "; + } + else + { + buf << "[" << sTag << "] "; + } + } + else if (sShowPID) + { + buf << "[" << getpid() << "] "; } if (level <= Log::FATAL) { - msg += "FATAL: "; + buf << "FATAL: "; } else if (level <= Log::ERROR) { - msg += "ERROR: "; + buf << "ERROR: "; } else if (level <= Log::WARNING) { - msg += "WARNING: "; + buf << "WARNING: "; } else if (level <= Log::NOTICE) { - msg += "NOTICE: "; + buf << "NOTICE: "; } - - msg += rMessage; + else if (level <= Log::INFO) + { + buf << "INFO: "; + } + else if (level <= Log::TRACE) + { + buf << "TRACE: "; + } + + buf << rMessage; - fprintf(target, "%s\n", msg.c_str()); + fprintf(target, "%s\n", buf.str().c_str()); return true; } diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 78db2bea..f26fd217 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -121,6 +121,7 @@ class Console : public Logger static bool sShowTimeMicros; static bool sShowTag; static std::string sTag; + static bool sShowPID; public: virtual bool Log(Log::Level level, const std::string& rFile, @@ -131,6 +132,7 @@ class Console : public Logger static void SetTag(const std::string& rTag); static void SetShowTime(bool enabled); static void SetShowTimeMicros(bool enabled); + static void SetShowPID(bool enabled); }; // -------------------------------------------------------------------------- diff --git a/lib/server/Daemon.cpp b/lib/server/Daemon.cpp index bdbbb433..0f07eac3 100644 --- a/lib/server/Daemon.cpp +++ b/lib/server/Daemon.cpp @@ -106,7 +106,7 @@ std::string Daemon::GetOptionString() #ifndef WIN32 "DFk" #endif - "hqvVt:T"; + "hPqvVt:TU"; } void Daemon::Usage() @@ -125,11 +125,13 @@ void Daemon::Usage() " -F Do not fork into background, but fork to serve multiple clients\n" " -k Keep console open after fork, keep writing log messages to it\n" #endif + " -P Show process ID (PID) in console output\n" " -q Run more quietly, reduce verbosity level by one, can repeat\n" " -v Run more verbosely, increase verbosity level by one, can repeat\n" " -V Run at maximum verbosity\n" " -t <tag> Tag console output with specified marker\n" - " -T Timestamp console output\n"; + " -T Timestamp console output\n" + " -U Timestamp console output with microseconds\n"; } // -------------------------------------------------------------------------- @@ -181,6 +183,12 @@ int Daemon::ProcessOption(signed int option) } break; + case 'P': + { + Console::SetShowPID(true); + } + break; + case 'q': { if(mLogLevel == Log::NOTHING) @@ -225,6 +233,13 @@ int Daemon::ProcessOption(signed int option) } break; + case 'U': + { + Console::SetShowTime(true); + Console::SetShowTimeMicros(true); + } + break; + case '?': { BOX_FATAL("Unknown option on command line: " |