From 1fb4cc1958a423a1bd6eeee01eb36d3b3b035b93 Mon Sep 17 00:00:00 2001 From: Andrei Polushin Date: Sat, 13 Aug 2022 16:10:00 +0700 Subject: [PATCH] log_file_header: add option to disable log file headers. (#850) Log lines can be customized for parsing by an external tool. To simplify such customization and parsing, there should be an option to customize the file header, or at least to disable adding it. --- src/glog/logging.h.in | 3 +++ src/logging.cc | 60 +++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/glog/logging.h.in b/src/glog/logging.h.in index 8a26cbc46..b0c86a3e0 100644 --- a/src/glog/logging.h.in +++ b/src/glog/logging.h.in @@ -455,6 +455,9 @@ DECLARE_bool(colorlogtostderr); // stderr in addition to log files. DECLARE_int32(stderrthreshold); +// Set whether the log file header should be written upon creating a file. +DECLARE_bool(log_file_header); + // Set whether the log prefix should be prepended to each line of output. DECLARE_bool(log_prefix); diff --git a/src/logging.cc b/src/logging.cc index 1df1034ae..0ccd8fefc 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -146,6 +146,8 @@ DEFINE_int32(stderrthreshold, GLOG_DEFINE_string(alsologtoemail, "", "log messages go to these email addresses " "in addition to logfiles"); +GLOG_DEFINE_bool(log_file_header, true, + "Write the file header at the start of each log file"); GLOG_DEFINE_bool(log_prefix, true, "Prepend the log prefix to the start of each log line"); GLOG_DEFINE_bool(log_year_in_prefix, true, @@ -1244,35 +1246,37 @@ void LogFileObject::Write(bool force_flush, } // Write a header message into the log file - ostringstream file_header_stream; - file_header_stream.fill('0'); - file_header_stream << "Log file created at: " - << 1900+tm_time.tm_year << '/' - << setw(2) << 1+tm_time.tm_mon << '/' - << setw(2) << tm_time.tm_mday - << ' ' - << setw(2) << tm_time.tm_hour << ':' - << setw(2) << tm_time.tm_min << ':' - << setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n") - << "Running on machine: " - << LogDestination::hostname() << '\n'; - - if(!g_application_fingerprint.empty()) { - file_header_stream << "Application fingerprint: " << g_application_fingerprint << '\n'; + if (FLAGS_log_file_header) { + ostringstream file_header_stream; + file_header_stream.fill('0'); + file_header_stream << "Log file created at: " + << 1900+tm_time.tm_year << '/' + << setw(2) << 1+tm_time.tm_mon << '/' + << setw(2) << tm_time.tm_mday + << ' ' + << setw(2) << tm_time.tm_hour << ':' + << setw(2) << tm_time.tm_min << ':' + << setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n") + << "Running on machine: " + << LogDestination::hostname() << '\n'; + + if(!g_application_fingerprint.empty()) { + 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): " + << PrettyDuration(static_cast(WallTime_Now() - start_time_)) << '\n' + << "Log line format: [IWEF]" << date_time_format << " " + << "threadid file:line] msg" << '\n'; + const string& file_header_string = file_header_stream.str(); + + const size_t header_len = file_header_string.size(); + fwrite(file_header_string.data(), 1, header_len, file_); + file_length_ += header_len; + bytes_since_flush_ += header_len; } - 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): " - << PrettyDuration(static_cast(WallTime_Now() - start_time_)) << '\n' - << "Log line format: [IWEF]" << date_time_format << " " - << "threadid file:line] msg" << '\n'; - const string& file_header_string = file_header_stream.str(); - - const size_t header_len = file_header_string.size(); - fwrite(file_header_string.data(), 1, header_len, file_); - file_length_ += header_len; - bytes_since_flush_ += header_len; } // Write to LOG file