Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check + rotate logs while logging not by scheduled task #162

Merged
merged 4 commits into from
Sep 17, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/log/file_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ namespace fc {
private:
future<void> _deletion_task;
boost::atomic<int64_t> _current_file_number;
const int64_t _interval_seconds;

public:
impl( const config& c) : cfg( c )
impl( const config& c) : cfg( c ), _interval_seconds( cfg.rotation_interval.to_seconds() )
{
try
{
Expand Down Expand Up @@ -63,9 +64,8 @@ namespace fc {
{
if( !cfg.rotate ) return;

int64_t interval_seconds = cfg.rotation_interval.to_seconds();
fc::time_point now = time_point::now();
int64_t new_file_number = now.sec_since_epoch() / interval_seconds;
int64_t new_file_number = now.sec_since_epoch() / _interval_seconds;
if( initializing )
_current_file_number.store( new_file_number );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _next_file_time is atomic, we can build logic around it to replace _current_file_number, to save one member variable. ;-) The current code would work too though.

else
Expand All @@ -74,7 +74,7 @@ namespace fc {
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) );
fc::time_point_sec start_time = time_point_sec( (uint32_t)(new_file_number * _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 @@ -97,8 +97,7 @@ namespace fc {
{
/* Delete old log files */
auto current_file = _current_file_number.load();
int64_t interval_seconds = cfg.rotation_interval.to_seconds();
fc::time_point_sec start_time = time_point_sec( (uint32_t)(current_file * interval_seconds) );
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();
Expand Down Expand Up @@ -130,8 +129,7 @@ namespace fc {
{
}
}
_deletion_task = schedule( [this]() { delete_files(); },
start_time + cfg.rotation_interval.to_seconds(),
_deletion_task = schedule( [this]() { delete_files(); }, start_time + _interval_seconds,
"delete_files(3)" );
}
};
Expand Down