Skip to content

Commit

Permalink
Add extra information in log file header (application build/version, …
Browse files Browse the repository at this point in the history
…actual duration time)
  • Loading branch information
d-uspenskiy authored and sergiud committed Mar 25, 2020
1 parent 7da49d4 commit 4f9a8c4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());
// Enable/Disable old log cleaner.
GOOGLE_GLOG_DLL_DECL void EnableLogCleaner(int overdue_days);
GOOGLE_GLOG_DLL_DECL void DisableLogCleaner();

GOOGLE_GLOG_DLL_DECL void SetApplicationFingerprint(const std::string& fingerprint);

class LogSink; // defined below

Expand Down
29 changes: 27 additions & 2 deletions src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ class LogFileObject : public base::Logger {
uint32 file_length_;
unsigned int rollover_attempt_;
int64 next_flush_time_; // cycle count at which to flush log
WallTime start_time_;

// Actually create a logfile using the value of base_filename_ and the
// optional argument time_pid_string
Expand Down Expand Up @@ -916,11 +917,27 @@ vector<string> GetOverdueLogNames(string log_directory, int days) {
bool log_cleaner_enabled_;
int log_cleaner_overdue_days_ = 7;

std::string g_application_fingerprint;

} // namespace

void SetApplicationFingerprint(const std::string& fingerprint) {
g_application_fingerprint = fingerprint;
}

namespace {

string PrettyDuration(int secs) {
std::stringstream result;
int mins = secs / 60;
int hours = mins / 60;
mins = mins % 60;
secs = secs % 60;
result.fill('0');
result << hours << ':' << setw(2) << mins << ':' << setw(2) << secs;
return result.str();
}

LogFileObject::LogFileObject(LogSeverity severity,
const char* base_filename)
: base_filename_selected_(base_filename != NULL),
Expand All @@ -933,7 +950,8 @@ LogFileObject::LogFileObject(LogSeverity severity,
dropped_mem_length_(0),
file_length_(0),
rollover_attempt_(kRolloverAttemptFrequency-1),
next_flush_time_(0) {
next_flush_time_(0),
start_time_(WallTime_Now()) {
assert(severity >= 0);
assert(severity < NUM_SEVERITIES);
}
Expand Down Expand Up @@ -1208,7 +1226,14 @@ void LogFileObject::Write(bool force_flush,
<< setw(2) << tm_time.tm_min << ':'
<< setw(2) << tm_time.tm_sec << '\n'
<< "Running on machine: "
<< LogDestination::hostname() << '\n'
<< LogDestination::hostname() << '\n';

if(!g_application_fingerprint.empty()) {
file_header_stream << "Application fingerprint: " << g_application_fingerprint << '\n';
}

file_header_stream << "Running duration (h:mm:ss): "
<< PrettyDuration(static_cast<int>(WallTime_Now() - start_time_)) << '\n'
<< "Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu "
<< "threadid file:line] msg" << '\n';
const string& file_header_string = file_header_stream.str();
Expand Down

0 comments on commit 4f9a8c4

Please sign in to comment.