diff --git a/Logger.h b/Logger.h index cdd92db..4c5e269 100644 --- a/Logger.h +++ b/Logger.h @@ -61,7 +61,7 @@ extern pid_t gPid; #define _LOG(level) \ Log(LOG_##level).get() << "pid(" << gPid << "), " \ << "tid(" << gettid() << ") " \ - << Utils::timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " + << Utils::timestr(21, true) << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " // (pat) If you '#define LOG_GROUP groupname' before including Logger.h, then you can set Log.Level.groupname as well as Log.Level.filename. #ifdef LOG_GROUP diff --git a/Utils.cpp b/Utils.cpp index 6c72046..d4f10c0 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -265,14 +265,20 @@ double timef() return tv.tv_usec / 1000000.0 + tv.tv_sec; } -const std::string timestr(unsigned fieldwidth) // Use to pick the number of chars in the output. +const std::string timestr(unsigned fieldwidth, bool addDate) // Use to pick the number of chars in the output. { struct timeval tv; struct tm tm; gettimeofday(&tv,NULL); localtime_r(&tv.tv_sec,&tm); unsigned tenths = tv.tv_usec / 100000; // Rounding down is ok. - std::string result = format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); + std::string result; + if (addDate) + result = format(" %04d/%02d/%02d %02d:%02d:%02d.%1d", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec, tenths); + else + result = format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); return result.substr(fieldwidth >= result.size() ? 0 : result.size() - fieldwidth); //switch (maxfield) { //case 'h': case 'H': return format("%02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); diff --git a/Utils.h b/Utils.h index 2c228c7..5e19c2b 100644 --- a/Utils.h +++ b/Utils.h @@ -32,7 +32,7 @@ using namespace std; extern double timef(); // high resolution time // We dont use a default arg here timestr(unsigned fieldwidth=12) because g++ complains about duplicate decl in Logger.h extern const std::string timestr(); // A timestamp to print in messages. -extern const std::string timestr(unsigned fieldwidth); // A timestamp to print in messages. +extern const std::string timestr(unsigned fieldwidth, bool addDate = false); // A timestamp to print in messages. extern void sleepf(double howlong); // high resolution sleep extern int gcd(int x, int y);