Skip to content

Commit

Permalink
Merge pull request #162 from pmconrad/1982_rotate_logs
Browse files Browse the repository at this point in the history
Check + rotate logs while logging not by scheduled task
  • Loading branch information
pmconrad authored Sep 17, 2019
2 parents f052ac9 + e168b64 commit bae416a
Showing 1 changed file with 41 additions and 37 deletions.
78 changes: 41 additions & 37 deletions src/log/file_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ namespace fc {
boost::mutex slock;

private:
future<void> _rotation_task;
time_point_sec _current_file_start_time;

time_point_sec get_file_start_time( const time_point_sec& timestamp, const microseconds& interval )
{
int64_t interval_seconds = interval.to_seconds();
int64_t file_number = timestamp.sec_since_epoch() / interval_seconds;
return time_point_sec( (uint32_t)(file_number * interval_seconds) );
}
future<void> _deletion_task;
boost::atomic<int64_t> _current_file_number;
const int64_t _interval_seconds;
time_point _next_file_time;

public:
impl( const config& c) : cfg( c )
impl( const config& c) : cfg( c ), _interval_seconds( cfg.rotation_interval.to_seconds() )
{
try
{
Expand All @@ -44,6 +39,7 @@ namespace fc {
FC_ASSERT( cfg.rotation_limit >= cfg.rotation_interval );

rotate_files( true );
delete_files();
} else {
out.open( cfg.filename, std::ios_base::out | std::ios_base::app);
}
Expand All @@ -58,7 +54,7 @@ namespace fc {
{
try
{
_rotation_task.cancel_and_wait("file_appender is destructing");
_deletion_task.cancel_and_wait("file_appender is destructing");
}
catch( ... )
{
Expand All @@ -67,9 +63,22 @@ namespace fc {

void rotate_files( bool initializing = false )
{
FC_ASSERT( cfg.rotate );
if( !cfg.rotate ) return;

fc::time_point now = time_point::now();
fc::time_point_sec start_time = get_file_start_time( now, cfg.rotation_interval );
if( now < _next_file_time ) return;

int64_t new_file_number = now.sec_since_epoch() / _interval_seconds;
if( initializing )
_current_file_number.store( new_file_number );
else
{
int64_t prev_file_number = _current_file_number.load();
if( prev_file_number >= new_file_number ) return;
if( !_current_file_number.compare_exchange_weak( prev_file_number, new_file_number ) ) return;
}
fc::time_point_sec start_time = time_point_sec( (uint32_t)(new_file_number * _interval_seconds) );
_next_file_time = start_time + _interval_seconds;
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);
Expand All @@ -79,44 +88,41 @@ namespace fc {

if( !initializing )
{
if( start_time <= _current_file_start_time )
{
_rotation_task = schedule( [this]() { rotate_files(); },
_current_file_start_time + cfg.rotation_interval.to_seconds(),
"rotate_files(2)" );
return;
}

out.flush();
out.close();
}
remove_all(link_filename); // on windows, you can't delete the link while the underlying file is opened for writing
out.open( log_filename, std::ios_base::out | std::ios_base::app );
create_hard_link(log_filename, link_filename);
}
}

void delete_files()
{
/* Delete old log files */
fc::time_point limit_time = now - cfg.rotation_limit;
auto current_file = _current_file_number.load();
fc::time_point_sec start_time = time_point_sec( (uint32_t)(current_file * _interval_seconds) );
fc::time_point limit_time = time_point::now() - cfg.rotation_limit;
fc::path link_filename = cfg.filename;
string link_filename_string = link_filename.filename().string();
directory_iterator itr(link_filename.parent_path());
string timestamp_string = start_time.to_non_delimited_iso_string();
for( ; itr != directory_iterator(); itr++ )
{
try
{
string current_filename = itr->filename().string();
if (current_filename.compare(0, link_filename_string.size(), link_filename_string) != 0 ||
current_filename.size() <= link_filename_string.size() + 1)
continue;
if( current_filename.compare(0, link_filename_string.size(), link_filename_string) != 0
|| current_filename.size() <= link_filename_string.size() + 1 )
continue;
string current_timestamp_str = current_filename.substr(link_filename_string.size() + 1,
timestamp_string.size());
fc::time_point_sec current_timestamp = fc::time_point_sec::from_iso_string( current_timestamp_str );
if( current_timestamp < start_time )
if( current_timestamp < start_time
&& ( current_timestamp < limit_time || file_size( current_filename ) <= 0 ) )
{
if( current_timestamp < limit_time || file_size( current_filename ) <= 0 )
{
remove_all( *itr );
continue;
}
remove_all( *itr );
continue;
}
}
catch (const fc::canceled_exception&)
Expand All @@ -127,11 +133,8 @@ namespace fc {
{
}
}

_current_file_start_time = start_time;
_rotation_task = schedule( [this]() { rotate_files(); },
_current_file_start_time + cfg.rotation_interval.to_seconds(),
"rotate_files(3)" );
_deletion_task = schedule( [this]() { delete_files(); }, start_time + _interval_seconds,
"delete_files(3)" );
}
};

Expand All @@ -151,8 +154,9 @@ namespace fc {
// MS THREAD METHOD MESSAGE \t\t\t File:Line
void file_appender::log( const log_message& m )
{
my->rotate_files();

std::stringstream line;
//line << (m.get_context().get_timestamp().time_since_epoch().count() % (1000ll*1000ll*60ll*60))/1000 <<"ms ";
line << string(m.get_context().get_timestamp()) << " ";
line << std::setw( 21 ) << (m.get_context().get_thread_name().substr(0,9) + string(":") + m.get_context().get_task_name()).c_str() << " ";

Expand Down

0 comments on commit bae416a

Please sign in to comment.