diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 22ace58..b23af16 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -47,8 +47,8 @@ namespace fc { static time_point now(); static time_point maximum() { return time_point( microseconds::maximum() ); } static time_point min() { return time_point(); } - operator fc::string()const; + operator fc::string()const; static time_point from_iso_string( const fc::string& s ); const microseconds& time_since_epoch()const { return elapsed; } @@ -109,8 +109,11 @@ namespace fc { friend microseconds operator - ( const time_point_sec& t, const time_point_sec& m ) { return time_point(t) - time_point(m); } friend microseconds operator - ( const time_point& t, const time_point_sec& m ) { return time_point(t) - time_point(m); } + fc::string to_non_delimited_iso_string()const; fc::string to_iso_string()const; - fc::string to_iso_extended_string()const; + + operator fc::string()const; + static time_point_sec from_iso_string( const fc::string& s ); private: uint32_t utc_seconds; @@ -121,11 +124,11 @@ namespace fc { /** return a human-readable approximate time, relative to now() * e.g., "4 hours ago", "2 months ago", etc. */ - string get_approximate_relative_time_string(const time_point_sec& event_time, - const time_point_sec& relative_to_time = fc::time_point::now(), + string get_approximate_relative_time_string(const time_point_sec& event_time, + const time_point_sec& relative_to_time = fc::time_point::now(), const std::string& ago = " ago"); string get_approximate_relative_time_string(const time_point& event_time, - const time_point& relative_to_time = fc::time_point::now(), + const time_point& relative_to_time = fc::time_point::now(), const std::string& ago = " ago"); } diff --git a/src/log/file_appender.cpp b/src/log/file_appender.cpp index 283ebf9..58f3f81 100644 --- a/src/log/file_appender.cpp +++ b/src/log/file_appender.cpp @@ -15,7 +15,7 @@ namespace fc { static const string compression_extension( ".lzma" ); - class file_appender::impl : public fc::retainable + class file_appender::impl : public fc::retainable { public: config cfg; @@ -34,16 +34,6 @@ namespace fc { return time_point_sec( (uint32_t)(file_number * interval_seconds) ); } - string timestamp_to_string( const time_point_sec& timestamp ) - { - return timestamp.to_iso_string(); - } - - time_point_sec string_to_timestamp( const string& str ) - { - return time_point::from_iso_string( str ); - } - void compress_file( const fc::path& filename ) { FC_ASSERT( cfg.rotate && cfg.rotation_compression ); @@ -95,7 +85,7 @@ namespace fc { FC_ASSERT( cfg.rotate ); fc::time_point now = time_point::now(); fc::time_point_sec start_time = get_file_start_time( now, cfg.rotation_interval ); - string timestamp_string = timestamp_to_string( start_time ); + string timestamp_string = start_time.to_non_delimited_iso_string(); fc::path link_filename = cfg.filename; fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string); @@ -106,8 +96,8 @@ namespace fc { { if( start_time <= _current_file_start_time ) { - _rotation_task = schedule( [this]() { rotate_files(); }, - _current_file_start_time + cfg.rotation_interval.to_seconds(), + _rotation_task = schedule( [this]() { rotate_files(); }, + _current_file_start_time + cfg.rotation_interval.to_seconds(), "rotate_files(2)" ); return; } @@ -134,7 +124,7 @@ namespace fc { continue; string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1, timestamp_string.size()); - fc::time_point_sec current_timestamp = string_to_timestamp( current_timestamp_str ); + fc::time_point_sec current_timestamp = fc::time_point_sec::from_iso_string( current_timestamp_str ); if( current_timestamp < start_time ) { if( current_timestamp < limit_time || file_size( current_filename ) <= 0 ) @@ -143,9 +133,9 @@ namespace fc { continue; } - if( !cfg.rotation_compression ) + if( !cfg.rotation_compression ) continue; - if( current_filename.find( compression_extension ) != string::npos ) + if( current_filename.find( compression_extension ) != string::npos ) continue; compress_file( *itr ); } @@ -160,8 +150,8 @@ namespace fc { } _current_file_start_time = start_time; - _rotation_task = schedule( [this]() { rotate_files(); }, - _current_file_start_time + cfg.rotation_interval.to_seconds(), + _rotation_task = schedule( [this]() { rotate_files(); }, + _current_file_start_time + cfg.rotation_interval.to_seconds(), "rotate_files(3)" ); } }; @@ -182,7 +172,7 @@ namespace fc { { fc::create_directories(my->cfg.filename.parent_path()); - if(!my->cfg.rotate) + if(!my->cfg.rotate) my->out.open(my->cfg.filename); } catch( ... ) @@ -190,7 +180,7 @@ namespace fc { std::cerr << "error opening log file: " << my->cfg.filename.preferred_string() << "\n"; } } - + file_appender::~file_appender(){} // MS THREAD METHOD MESSAGE \t\t\t File:Line @@ -211,7 +201,7 @@ namespace fc { if( method_name[i] == ':' ) p = i; } - if( method_name[p] == ':' ) + if( method_name[p] == ':' ) ++p; line << std::setw( 20 ) << m.get_context().get_method().substr(p,20).c_str() <<" "; } @@ -227,7 +217,7 @@ namespace fc { { fc::scoped_lock lock( my->slock ); my->out << line.str() << "\t\t\t" << m.get_context().get_file() << ":" << m.get_context().get_line_number() << "\n"; - if( my->cfg.flush ) + if( my->cfg.flush ) my->out.flush(); } } diff --git a/src/time.cpp b/src/time.cpp index 06c4494..20d003e 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -8,58 +8,70 @@ #include namespace fc { + namespace bch = boost::chrono; - time_point time_point::now() { - return time_point(microseconds(bch::duration_cast(bch::system_clock::now().time_since_epoch()).count())); - } - time_point::operator fc::string()const { - bch::system_clock::time_point tp; - tp += bch::microseconds( elapsed._count); - time_t tt = bch::system_clock::to_time_t(tp); - - return boost::posix_time::to_iso_string( boost::posix_time::from_time_t(tt) + boost::posix_time::microseconds( elapsed._count % 1000000 ) ); - } - time_point time_point::from_iso_string( const fc::string& s ) + + time_point time_point::now() { - try - { - auto pt = boost::posix_time::from_iso_string(s); - //return fc::time_point(fc::seconds( (pt - boost::posix_time::from_time_t(0)).total_seconds() )); - return fc::time_point(fc::microseconds( (pt - boost::posix_time::from_time_t(0)).total_microseconds() )); - } - FC_RETHROW_EXCEPTIONS(warn, "unable to convert ISO-formatted string to fc::time_point") + return time_point( microseconds( bch::duration_cast( bch::system_clock::now().time_since_epoch() ).count() ) ); + } + + fc::string time_point_sec::to_non_delimited_iso_string()const + { + const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) ); + return boost::posix_time::to_iso_string( ptime ); } fc::string time_point_sec::to_iso_string()const { - const auto ptime = boost::posix_time::from_time_t( time_t ( sec_since_epoch() ) ); - return boost::posix_time::to_iso_string( ptime ); - } - - fc::string time_point_sec::to_iso_extended_string()const - { - const auto ptime = boost::posix_time::from_time_t( time_t ( sec_since_epoch() ) ); + const auto ptime = boost::posix_time::from_time_t( time_t( sec_since_epoch() ) ); return boost::posix_time::to_iso_extended_string( ptime ); } + time_point_sec::operator fc::string()const + { + return this->to_iso_string(); + } + + time_point_sec time_point_sec::from_iso_string( const fc::string& s ) + { try { + static boost::posix_time::ptime epoch = boost::posix_time::from_time_t( 0 ); + boost::posix_time::ptime pt; + if( s.size() >= 5 && s.at( 4 ) == '-' ) // http://en.wikipedia.org/wiki/ISO_8601 + pt = boost::date_time::parse_delimited_time( s, 'T' ); + else + pt = boost::posix_time::from_iso_string( s ); + return fc::time_point_sec( (pt - epoch).total_seconds() ); + } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point_sec" ) } + + time_point::operator fc::string()const + { + return fc::string( time_point_sec( *this ) ); + } + + time_point time_point::from_iso_string( const fc::string& s ) + { try { + return time_point( time_point_sec::from_iso_string( s ) ); + } FC_RETHROW_EXCEPTIONS( warn, "unable to convert ISO-formatted string to fc::time_point" ) } + void to_variant( const fc::time_point& t, variant& v ) { - v = fc::string(t); + v = fc::string( t ); } void from_variant( const fc::variant& v, fc::time_point& t ) { - t = fc::time_point::from_iso_string(v.as_string()); + t = fc::time_point::from_iso_string( v.as_string() ); } void to_variant( const fc::time_point_sec& t, variant& v ) { - v = fc::string(fc::time_point(t)); + v = fc::string( t ); } void from_variant( const fc::variant& v, fc::time_point_sec& t ) { - t = fc::time_point::from_iso_string(v.as_string()); + t = fc::time_point_sec::from_iso_string( v.as_string() ); } // inspired by show_date_relative() in git's date.c - string get_approximate_relative_time_string(const time_point_sec& event_time, - const time_point_sec& relative_to_time /* = fc::time_point::now() */, + string get_approximate_relative_time_string(const time_point_sec& event_time, + const time_point_sec& relative_to_time /* = fc::time_point::now() */, const std::string& default_ago /* = " ago" */) { - + string ago = default_ago; int32_t seconds_ago = relative_to_time.sec_since_epoch() - event_time.sec_since_epoch(); @@ -116,8 +128,8 @@ namespace fc { result << ago; return result.str(); } - string get_approximate_relative_time_string(const time_point& event_time, - const time_point& relative_to_time /* = fc::time_point::now() */, + string get_approximate_relative_time_string(const time_point& event_time, + const time_point& relative_to_time /* = fc::time_point::now() */, const std::string& ago /* = " ago" */) { return get_approximate_relative_time_string(time_point_sec(event_time), time_point_sec(relative_to_time), ago); }