From ccc0bce45b99b3fcc73b4c217ef6c2088abde4dd Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 15 Dec 2007 20:03:50 +0000 Subject: Allow logging with microsecond timestamps. --- lib/common/Logging.cpp | 36 +++++++++++++++++++++++++----------- lib/common/Logging.h | 2 ++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/common/Logging.cpp b/lib/common/Logging.cpp index d22db238..2b81b52b 100644 --- a/lib/common/Logging.cpp +++ b/lib/common/Logging.cpp @@ -20,6 +20,8 @@ #include +#include "BoxTime.h" + bool Logging::sLogToSyslog = false; bool Logging::sLogToConsole = false; bool Logging::sContextSet = false; @@ -178,7 +180,8 @@ Logger::~Logger() } bool Console::sShowTime = false; -bool Console::sShowTag = false; +bool Console::sShowTimeMicros = false; +bool Console::sShowTag = false; std::string Console::sTag; void Console::SetTag(const std::string& rTag) @@ -192,6 +195,11 @@ void Console::SetShowTime(bool enabled) sShowTime = enabled; } +void Console::SetShowTimeMicros(bool enabled) +{ + sShowTimeMicros = enabled; +} + bool Console::Log(Log::Level level, const std::string& rFile, int line, std::string& rMessage) { @@ -211,25 +219,31 @@ bool Console::Log(Log::Level level, const std::string& rFile, if (sShowTime) { - struct tm time_now, *tm_ptr = &time_now; - time_t time_t_now = time(NULL); + box_time_t time_now = GetCurrentBoxTime(); + time_t seconds = BoxTimeToSeconds(time_now); + int micros = BoxTimeToMicroSeconds(time_now) % MICRO_SEC_IN_SEC; + + struct tm tm_now, *tm_ptr = &tm_now; - if (time_t_now == ((time_t)-1)) - { - msg += strerror(errno); - msg += " "; - } #ifdef WIN32 - else if ((tm_ptr = localtime(&time_t_now)) != NULL) + if ((tm_ptr = localtime(&seconds)) != NULL) #else - else if (localtime_r(&time_t_now, &time_now) != NULL) + 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 << ":" << - std::setw(2) << tm_ptr->tm_sec << " "; + std::setw(2) << tm_ptr->tm_sec; + + if (sShowTimeMicros) + { + buf << "." << std::setw(6) << micros; + } + + buf << " "; msg += buf.str(); } else diff --git a/lib/common/Logging.h b/lib/common/Logging.h index 9753fc88..78db2bea 100644 --- a/lib/common/Logging.h +++ b/lib/common/Logging.h @@ -118,6 +118,7 @@ class Console : public Logger { private: static bool sShowTime; + static bool sShowTimeMicros; static bool sShowTag; static std::string sTag; @@ -129,6 +130,7 @@ class Console : public Logger static void SetTag(const std::string& rTag); static void SetShowTime(bool enabled); + static void SetShowTimeMicros(bool enabled); }; // -------------------------------------------------------------------------- -- cgit v1.2.3