# HG changeset patch # User Sebastien Jodogne # Date 1591435299 -7200 # Node ID 55710d73780f73a2d9e2d2f7d5faa537d64cc085 # Parent 7f9909062d9c46b21671a3218d3927353220fedf reorganizing Logging.cpp diff -r 7f9909062d9c -r 55710d73780f Core/Logging.cpp --- a/Core/Logging.cpp Sat Jun 06 11:18:12 2020 +0200 +++ b/Core/Logging.cpp Sat Jun 06 11:21:39 2020 +0200 @@ -531,6 +531,84 @@ } + static void CheckFile(std::unique_ptr& f) + { + if (loggingContext_->file_.get() == NULL || + !loggingContext_->file_->is_open()) + { + throw OrthancException(ErrorCode_CannotWriteFile); + } + } + + + static void GetLinePrefix(std::string& prefix, + LogLevel level, + const char* file, + int line) + { + boost::filesystem::path path(file); + boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); + boost::posix_time::time_duration duration = now.time_of_day(); + + /** + From Google Log documentation: + + "Log lines have this form: + + Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... + + where the fields are defined as follows: + + L A single character, representing the log level (eg 'I' for INFO) + mm The month (zero padded; ie May is '05') + dd The day (zero padded) + hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds + threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) + file The file name + line The line number + msg The user-supplied message" + + In this implementation, "threadid" is not printed. + **/ + + char c; + switch (level) + { + case LogLevel_ERROR: + c = 'E'; + break; + + case LogLevel_WARNING: + c = 'W'; + break; + + case LogLevel_INFO: + c = 'I'; + break; + + case LogLevel_TRACE: + c = 'T'; + break; + + default: + throw OrthancException(ErrorCode_InternalError); + } + + char date[64]; + sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", + c, + now.date().month().as_number(), + now.date().day().as_number(), + static_cast(duration.hours()), + static_cast(duration.minutes()), + static_cast(duration.seconds()), + static_cast(duration.fractional_seconds())); + + prefix = (std::string(date) + path.filename().string() + ":" + + boost::lexical_cast(line) + "] "); + } + + void Initialize() { boost::mutex::scoped_lock lock(loggingMutex_); @@ -626,15 +704,6 @@ } - static void CheckFile(std::unique_ptr& f) - { - if (loggingContext_->file_.get() == NULL || - !loggingContext_->file_->is_open()) - { - throw OrthancException(ErrorCode_CannotWriteFile); - } - } - void SetTargetFolder(const std::string& path) { boost::mutex::scoped_lock lock(loggingMutex_); @@ -690,75 +759,13 @@ return; } - // Compute the header of the line, temporary release the lock as + // Compute the prefix of the line, temporary release the lock as // this is a time-consuming operation lock_.unlock(); - std::string header; - - { - boost::filesystem::path path(file); - boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); - boost::posix_time::time_duration duration = now.time_of_day(); - - /** - From Google Log documentation: - - "Log lines have this form: - - Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg... - - where the fields are defined as follows: - - L A single character, representing the log level (eg 'I' for INFO) - mm The month (zero padded; ie May is '05') - dd The day (zero padded) - hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds - threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux) - file The file name - line The line number - msg The user-supplied message" - - In this implementation, "threadid" is not printed. - **/ + std::string prefix; + GetLinePrefix(prefix, level, file, line); - char prefix; - switch (level) - { - case LogLevel_ERROR: - prefix = 'E'; - break; - - case LogLevel_WARNING: - prefix = 'W'; - break; - - case LogLevel_INFO: - prefix = 'I'; - break; - - case LogLevel_TRACE: - prefix = 'T'; - break; - - default: - throw OrthancException(ErrorCode_InternalError); - } - - char date[64]; - sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ", - prefix, - now.date().month().as_number(), - now.date().day().as_number(), - static_cast(duration.hours()), - static_cast(duration.minutes()), - static_cast(duration.seconds()), - static_cast(duration.fractional_seconds())); - - header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast(line) + "] "; - } - - - // The header is computed, we now re-lock the mutex to access + // The prefix is computed, we now re-lock the mutex to access // the stream objects. Pay attention that "loggingContext_", // "infoEnabled_" or "traceEnabled_" might have changed while // the mutex was unlocked. @@ -808,7 +815,7 @@ lock_.unlock(); } - (*stream_) << header; + (*stream_) << prefix; } catch (...) {