Add log_year_in_prefix flag

The flag allows suppressing the year in the log line prefix. At the same
time, the flag allows to generate log output compatible to glog releases
prior to version 0.5.
This commit is contained in:
M Samoila 2021-12-22 12:46:38 -08:00 committed by Sergiu Deitsch
parent 42d509b1c3
commit 43fc3bf91c
2 changed files with 14 additions and 5 deletions

View File

@ -430,6 +430,9 @@ DECLARE_int32(stderrthreshold);
// Set whether the log prefix should be prepended to each line of output. // Set whether the log prefix should be prepended to each line of output.
DECLARE_bool(log_prefix); DECLARE_bool(log_prefix);
// Set whether the year should be included in the log prefix.
DECLARE_bool(log_year_in_prefix);
// Log messages at a level <= this flag are buffered. // Log messages at a level <= this flag are buffered.
// Log messages at a higher level are flushed immediately. // Log messages at a higher level are flushed immediately.
DECLARE_int32(logbuflevel); DECLARE_int32(logbuflevel);

View File

@ -144,6 +144,8 @@ GLOG_DEFINE_string(alsologtoemail, "",
"in addition to logfiles"); "in addition to logfiles");
GLOG_DEFINE_bool(log_prefix, true, GLOG_DEFINE_bool(log_prefix, true,
"Prepend the log prefix to the start of each log line"); "Prepend the log prefix to the start of each log line");
GLOG_DEFINE_bool(log_year_in_prefix, true,
"Include the year in the log prefix");
GLOG_DEFINE_int32(minloglevel, 0, "Messages logged at a lower level than this don't " GLOG_DEFINE_int32(minloglevel, 0, "Messages logged at a lower level than this don't "
"actually get logged anywhere"); "actually get logged anywhere");
GLOG_DEFINE_int32(logbuflevel, 0, GLOG_DEFINE_int32(logbuflevel, 0,
@ -1224,10 +1226,12 @@ void LogFileObject::Write(bool force_flush,
if(!g_application_fingerprint.empty()) { if(!g_application_fingerprint.empty()) {
file_header_stream << "Application fingerprint: " << g_application_fingerprint << '\n'; file_header_stream << "Application fingerprint: " << g_application_fingerprint << '\n';
} }
const char* const date_time_format = FLAGS_log_year_in_prefix
? "yyyymmdd hh:mm:ss.uuuuuu"
: "mmdd hh:mm:ss.uuuuuu";
file_header_stream << "Running duration (h:mm:ss): " file_header_stream << "Running duration (h:mm:ss): "
<< PrettyDuration(static_cast<int>(WallTime_Now() - start_time_)) << '\n' << PrettyDuration(static_cast<int>(WallTime_Now() - start_time_)) << '\n'
<< "Log line format: [IWEF]yyyymmdd hh:mm:ss.uuuuuu " << "Log line format: [IWEF]" << date_time_format << " "
<< "threadid file:line] msg" << '\n'; << "threadid file:line] msg" << '\n';
const string& file_header_string = file_header_stream.str(); const string& file_header_string = file_header_stream.str();
@ -1620,9 +1624,11 @@ void LogMessage::Init(const char* file,
#ifdef GLOG_CUSTOM_PREFIX_SUPPORT #ifdef GLOG_CUSTOM_PREFIX_SUPPORT
if (custom_prefix_callback == NULL) { if (custom_prefix_callback == NULL) {
#endif #endif
stream() << LogSeverityNames[severity][0] stream() << LogSeverityNames[severity][0];
<< setw(4) << 1900 + logmsgtime_.year() if (FLAGS_log_year_in_prefix) {
<< setw(2) << 1 + logmsgtime_.month() stream() << setw(4) << 1900 + logmsgtime_.year();
}
stream() << setw(2) << 1 + logmsgtime_.month()
<< setw(2) << logmsgtime_.day() << setw(2) << logmsgtime_.day()
<< ' ' << ' '
<< setw(2) << logmsgtime_.hour() << ':' << setw(2) << logmsgtime_.hour() << ':'