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.
This commit is contained in:
Andrei Polushin 2022-08-13 16:10:00 +07:00 committed by GitHub
parent a1b6164ef1
commit 1fb4cc1958
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 27 deletions

View File

@ -455,6 +455,9 @@ DECLARE_bool(colorlogtostderr);
// stderr in addition to log files. // stderr in addition to log files.
DECLARE_int32(stderrthreshold); 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. // Set whether the log prefix should be prepended to each line of output.
DECLARE_bool(log_prefix); DECLARE_bool(log_prefix);

View File

@ -146,6 +146,8 @@ DEFINE_int32(stderrthreshold,
GLOG_DEFINE_string(alsologtoemail, "", GLOG_DEFINE_string(alsologtoemail, "",
"log messages go to these email addresses " "log messages go to these email addresses "
"in addition to logfiles"); "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, 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, 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 // Write a header message into the log file
ostringstream file_header_stream; if (FLAGS_log_file_header) {
file_header_stream.fill('0'); ostringstream file_header_stream;
file_header_stream << "Log file created at: " file_header_stream.fill('0');
<< 1900+tm_time.tm_year << '/' file_header_stream << "Log file created at: "
<< setw(2) << 1+tm_time.tm_mon << '/' << 1900+tm_time.tm_year << '/'
<< setw(2) << tm_time.tm_mday << 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_hour << ':'
<< setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n") << setw(2) << tm_time.tm_min << ':'
<< "Running on machine: " << setw(2) << tm_time.tm_sec << (FLAGS_log_utc_time ? " UTC\n" : "\n")
<< LogDestination::hostname() << '\n'; << "Running on machine: "
<< LogDestination::hostname() << '\n';
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): "
<< PrettyDuration(static_cast<int>(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<int>(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 // Write to LOG file