Logs are now rotated on startup if rotation is enabled
This commit is contained in:
parent
39c7f05ea9
commit
cc1ef61cd6
2 changed files with 53 additions and 59 deletions
|
|
@ -1,7 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <fc/filesystem.hpp>
|
||||
#include <fc/log/appender.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <fc/filesystem.hpp>
|
||||
#include <fc/time.hpp>
|
||||
|
||||
namespace fc {
|
||||
|
|
@ -20,6 +21,7 @@ class file_appender : public appender {
|
|||
bool rotate;
|
||||
microseconds rotation_interval;
|
||||
microseconds rotation_limit;
|
||||
bool rotation_compression;
|
||||
};
|
||||
file_appender( const variant& args );
|
||||
~file_appender();
|
||||
|
|
@ -32,4 +34,5 @@ class file_appender : public appender {
|
|||
} // namespace fc
|
||||
|
||||
#include <fc/reflect/reflect.hpp>
|
||||
FC_REFLECT( fc::file_appender::config, (format)(filename)(flush)(truncate)(rotate)(rotation_interval)(rotation_limit) )
|
||||
FC_REFLECT( fc::file_appender::config,
|
||||
(format)(filename)(flush)(truncate)(rotate)(rotation_interval)(rotation_limit)(rotation_compression) )
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
#include <fc/log/file_appender.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <fc/thread/scoped_lock.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <fc/io/fstream.hpp>
|
||||
#include <fc/variant.hpp>
|
||||
#include <fc/log/file_appender.hpp>
|
||||
#include <fc/reflect/variant.hpp>
|
||||
#include <fc/thread/scoped_lock.hpp>
|
||||
#include <fc/variant.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <fc/filesystem.hpp>
|
||||
|
||||
|
||||
namespace fc {
|
||||
class file_appender::impl : public fc::retainable {
|
||||
|
|
@ -24,10 +21,49 @@ namespace fc {
|
|||
const auto file_number = timestamp.sec_since_epoch() / interval_seconds;
|
||||
return time_point_sec( file_number * interval_seconds );
|
||||
}
|
||||
|
||||
void rotate_files( bool initializing = false )
|
||||
{
|
||||
if( !cfg.rotate ) return;
|
||||
const auto now = time_point::now();
|
||||
const auto start_time = get_file_start_time( now, cfg.rotation_interval );
|
||||
if( !initializing )
|
||||
{
|
||||
if( start_time <= current_file_start_time ) return;
|
||||
out.close();
|
||||
}
|
||||
|
||||
auto log_filename = cfg.filename.string();
|
||||
|
||||
/* Delete old log files */
|
||||
const auto limit_time = now - cfg.rotation_limit;
|
||||
auto itr = directory_iterator( fc::path( log_filename ).parent_path() );
|
||||
for( ; itr != directory_iterator(); itr++ )
|
||||
{
|
||||
const auto current_filename = itr->string();
|
||||
const auto current_pos = current_filename.find( log_filename );
|
||||
if( current_pos != 0 ) continue;
|
||||
const auto current_timestamp = current_filename.substr( log_filename.size() + 1 ); // TODO SKI{P EXTENSION
|
||||
try
|
||||
{
|
||||
if( std::stoi( current_timestamp ) < limit_time.sec_since_epoch() )
|
||||
remove_all( current_filename );
|
||||
//else compress if not already compressed
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
file_appender::config::config( const fc::path& p )
|
||||
:format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
|
||||
filename(p),flush(true),truncate(true),rotate(false){}
|
||||
filename(p),flush(true),truncate(true),rotate(false),rotation_compression(false){}
|
||||
|
||||
file_appender::file_appender( const variant& args )
|
||||
:my( new impl() )
|
||||
|
|
@ -40,19 +76,12 @@ namespace fc {
|
|||
|
||||
fc::create_directories( fc::path( log_filename ).parent_path() );
|
||||
|
||||
if( my->cfg.rotate )
|
||||
{
|
||||
const auto start_time = my->get_file_start_time( time_point::now(), my->cfg.rotation_interval );
|
||||
// TODO: Convert to proper timestamp string
|
||||
log_filename += "." + std::to_string( start_time.sec_since_epoch() );
|
||||
my->current_file_start_time = start_time;
|
||||
}
|
||||
|
||||
my->out.open( log_filename.c_str() );
|
||||
if( !my->cfg.rotate ) my->out.open( log_filename.c_str() );
|
||||
else my->rotate_files( true );
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
std::cerr << "error opening log file: " << my->cfg.filename.string() << "\n";
|
||||
std::cerr << "error opening log file: " << log_filename << "\n";
|
||||
//elog( "%s", fc::except_str().c_str() );
|
||||
}
|
||||
}
|
||||
|
|
@ -61,7 +90,6 @@ namespace fc {
|
|||
// MS THREAD METHOD MESSAGE \t\t\t File:Line
|
||||
void file_appender::log( const log_message& m )
|
||||
{
|
||||
const auto now = time_point::now();
|
||||
std::stringstream line;
|
||||
//line << (m.get_context().get_timestamp().time_since_epoch().count() % (1000ll*1000ll*60ll*60))/1000 <<"ms ";
|
||||
line << std::string(m.get_context().get_timestamp()) << " ";
|
||||
|
|
@ -91,44 +119,7 @@ namespace fc {
|
|||
/* Write to log file (rotating file beforehand if necessary) */
|
||||
{
|
||||
fc::scoped_lock<boost::mutex> lock( my->slock );
|
||||
|
||||
if( my->cfg.rotate )
|
||||
{
|
||||
const auto start_time = my->get_file_start_time( now, my->cfg.rotation_interval );
|
||||
if( start_time > my->current_file_start_time )
|
||||
{
|
||||
my->out.close();
|
||||
auto log_filename = my->cfg.filename.string();
|
||||
|
||||
/* Delete old log files */
|
||||
// TODO: Delete on startup as well
|
||||
const auto limit_time = now - my->cfg.rotation_limit;
|
||||
auto itr = directory_iterator( fc::path( log_filename ).parent_path() );
|
||||
for( ; itr != directory_iterator(); itr++ )
|
||||
{
|
||||
const auto current_filename = itr->string();
|
||||
const auto current_pos = current_filename.find( log_filename );
|
||||
if( current_pos != 0 ) continue;
|
||||
const auto current_timestamp = current_filename.substr( log_filename.size() + 1 );
|
||||
try
|
||||
{
|
||||
if( std::stoi( current_timestamp ) < limit_time.sec_since_epoch() )
|
||||
remove_all( current_filename );
|
||||
//else compress if not already compressed
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Convert to proper timestamp string
|
||||
log_filename += "." + std::to_string( start_time.sec_since_epoch() );
|
||||
my->out.open( log_filename.c_str() );
|
||||
|
||||
my->current_file_start_time = start_time;
|
||||
}
|
||||
}
|
||||
|
||||
if( my->cfg.rotate ) my->rotate_files();
|
||||
my->out << line.str() << "\t\t\t" << m.get_context().get_file() <<":"<<m.get_context().get_line_number()<<"\n";
|
||||
if( my->cfg.flush ) my->out.flush();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue