From b522f12a0e71e57eefcbe5601844e3afde8ad94d Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Thu, 3 Jul 2014 02:17:03 -0400 Subject: [PATCH] Use ISO format timestamps for rotated log files --- src/log/file_appender.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/log/file_appender.cpp b/src/log/file_appender.cpp index 82e8f0b..246521a 100644 --- a/src/log/file_appender.cpp +++ b/src/log/file_appender.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,17 @@ namespace fc { return time_point_sec( file_number * interval_seconds ); } + string timestamp_to_string( const time_point_sec& timestamp ) + { + auto ptime = boost::posix_time::from_time_t( time_t ( timestamp.sec_since_epoch() ) ); + return boost::posix_time::to_iso_string( ptime ); + } + + time_point_sec string_to_timestamp( const string& str ) + { + return time_point::from_iso_string( str ); + } + void rotate_files( bool initializing = false ) { if( !cfg.rotate ) return; @@ -35,6 +47,7 @@ namespace fc { } auto log_filename = cfg.filename.string(); + const auto timestamp_string = timestamp_to_string( start_time ); /* Delete old log files */ const auto limit_time = now - cfg.rotation_limit; @@ -45,10 +58,11 @@ namespace fc { auto current_pos = current_filename.find( log_filename ); if( current_pos != 0 ) continue; current_pos = log_filename.size() + 1; - const auto current_timestamp = current_filename.substr( current_pos, current_pos + 10 ); + const auto current_timestamp = string( current_filename.begin() + current_pos, + current_filename.begin() + current_pos + timestamp_string.size() ); try { - if( std::stoi( current_timestamp ) < limit_time.sec_since_epoch() ) + if( string_to_timestamp( current_timestamp ) < limit_time ) { remove_all( current_filename ); } @@ -64,10 +78,9 @@ namespace fc { } } - // TODO: Convert to proper timestamp string - log_filename += "." + std::to_string( start_time.sec_since_epoch() ); - out.open( log_filename.c_str() ); current_file_start_time = start_time; + log_filename += "." + timestamp_string; + out.open( log_filename.c_str() ); } }; file_appender::config::config( const fc::path& p )