2014-07-03 05:27:58 +00:00
|
|
|
#include <fc/compress/lzma.hpp>
|
2014-07-03 14:02:40 +00:00
|
|
|
#include <fc/exception/exception.hpp>
|
2014-07-03 05:16:46 +00:00
|
|
|
#include <fc/io/fstream.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/log/file_appender.hpp>
|
2014-07-03 05:16:46 +00:00
|
|
|
#include <fc/reflect/variant.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/thread/scoped_lock.hpp>
|
2014-07-03 14:02:40 +00:00
|
|
|
#include <fc/thread/thread.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/variant.hpp>
|
2014-07-03 05:16:46 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2013-07-03 17:26:23 +00:00
|
|
|
#include <iomanip>
|
2014-07-03 14:02:40 +00:00
|
|
|
#include <queue>
|
2013-07-03 17:26:23 +00:00
|
|
|
#include <sstream>
|
2013-02-05 05:06:16 +00:00
|
|
|
|
|
|
|
|
namespace fc {
|
2014-07-03 14:02:40 +00:00
|
|
|
|
|
|
|
|
static const string compression_extension( ".lzma" );
|
|
|
|
|
|
2013-02-05 05:06:16 +00:00
|
|
|
class file_appender::impl : public fc::retainable {
|
|
|
|
|
public:
|
2014-07-03 14:02:40 +00:00
|
|
|
config cfg;
|
|
|
|
|
ofstream out;
|
|
|
|
|
boost::mutex slock;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
future<void> _rotation_task;
|
|
|
|
|
time_point_sec _current_file_start_time;
|
|
|
|
|
std::unique_ptr<thread> _compression_thread;
|
2014-07-02 21:36:21 +00:00
|
|
|
|
|
|
|
|
time_point_sec get_file_start_time( const time_point_sec& timestamp, const microseconds& interval )
|
|
|
|
|
{
|
|
|
|
|
const auto interval_seconds = interval.to_seconds();
|
|
|
|
|
const auto file_number = timestamp.sec_since_epoch() / interval_seconds;
|
|
|
|
|
return time_point_sec( file_number * interval_seconds );
|
|
|
|
|
}
|
2014-07-03 05:16:46 +00:00
|
|
|
|
2014-07-03 06:17:03 +00:00
|
|
|
string timestamp_to_string( const time_point_sec& timestamp )
|
|
|
|
|
{
|
2014-07-27 01:05:11 +00:00
|
|
|
return timestamp.to_iso_string();
|
2014-07-03 06:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time_point_sec string_to_timestamp( const string& str )
|
|
|
|
|
{
|
|
|
|
|
return time_point::from_iso_string( str );
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:02:40 +00:00
|
|
|
void compress_file( const string& filename )
|
|
|
|
|
{
|
|
|
|
|
FC_ASSERT( cfg.rotate && cfg.rotation_compression );
|
|
|
|
|
FC_ASSERT( _compression_thread );
|
|
|
|
|
if( !_compression_thread->is_current() )
|
|
|
|
|
{
|
2014-07-27 04:08:35 +00:00
|
|
|
_compression_thread->async( [this, filename]() { compress_file( filename ); }, "compress_file" ).wait();
|
2014-07-03 14:02:40 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
lzma_compress_file( filename, filename + compression_extension );
|
|
|
|
|
remove_all( filename );
|
|
|
|
|
}
|
|
|
|
|
catch( ... )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
impl( const config& c) : cfg( c )
|
|
|
|
|
{
|
|
|
|
|
if( cfg.rotate )
|
|
|
|
|
{
|
2014-07-05 20:14:25 +00:00
|
|
|
FC_ASSERT( cfg.rotation_interval >= seconds( 1 ) );
|
|
|
|
|
FC_ASSERT( cfg.rotation_limit >= cfg.rotation_interval );
|
|
|
|
|
|
2014-07-03 14:02:40 +00:00
|
|
|
if( cfg.rotation_compression )
|
|
|
|
|
_compression_thread.reset( new thread( "compression") );
|
|
|
|
|
|
2014-07-27 04:08:35 +00:00
|
|
|
_rotation_task = async( [this]() { rotate_files( true ); }, "rotate_files" );
|
2014-07-03 14:02:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~impl()
|
|
|
|
|
{
|
2014-07-26 19:05:26 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_rotation_task.cancel_and_wait();
|
|
|
|
|
}
|
|
|
|
|
catch( ... )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if( _compression_thread )
|
|
|
|
|
_compression_thread->quit();
|
|
|
|
|
}
|
|
|
|
|
catch( ... )
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-07-03 14:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 05:16:46 +00:00
|
|
|
void rotate_files( bool initializing = false )
|
|
|
|
|
{
|
2014-07-03 14:02:40 +00:00
|
|
|
FC_ASSERT( cfg.rotate );
|
2014-07-03 05:16:46 +00:00
|
|
|
const auto now = time_point::now();
|
|
|
|
|
const auto start_time = get_file_start_time( now, cfg.rotation_interval );
|
2014-07-03 14:02:40 +00:00
|
|
|
const auto timestamp_string = timestamp_to_string( start_time );
|
|
|
|
|
const auto link_filename = cfg.filename.string();
|
|
|
|
|
const auto log_filename = link_filename + "." + timestamp_string;
|
|
|
|
|
|
2014-07-03 05:16:46 +00:00
|
|
|
{
|
2014-07-16 17:39:14 +00:00
|
|
|
fc::scoped_lock<boost::mutex> lock( slock );
|
2014-07-05 20:14:25 +00:00
|
|
|
|
2014-07-16 17:39:14 +00:00
|
|
|
if( !initializing )
|
|
|
|
|
{
|
|
|
|
|
if( start_time <= _current_file_start_time )
|
|
|
|
|
{
|
2014-07-26 19:05:26 +00:00
|
|
|
_rotation_task = schedule( [this]() { rotate_files(); }, _current_file_start_time + cfg.rotation_interval.to_seconds(), "log_rotation_task" );
|
2014-07-16 17:39:14 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2014-07-03 05:16:46 +00:00
|
|
|
|
2014-07-16 17:39:14 +00:00
|
|
|
out.flush();
|
|
|
|
|
out.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out.open( log_filename.c_str() );
|
|
|
|
|
}
|
2014-07-03 06:35:18 +00:00
|
|
|
remove_all( link_filename );
|
2014-07-03 14:02:40 +00:00
|
|
|
create_hard_link( log_filename, link_filename );
|
2014-07-03 05:16:46 +00:00
|
|
|
|
|
|
|
|
/* Delete old log files */
|
|
|
|
|
const auto limit_time = now - cfg.rotation_limit;
|
2014-07-03 14:02:40 +00:00
|
|
|
auto itr = directory_iterator( fc::path( link_filename ).parent_path() );
|
2014-07-03 05:16:46 +00:00
|
|
|
for( ; itr != directory_iterator(); itr++ )
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-07-03 14:02:40 +00:00
|
|
|
const auto current_filename = itr->string();
|
|
|
|
|
auto current_pos = current_filename.find( link_filename );
|
|
|
|
|
if( current_pos != 0 ) continue;
|
|
|
|
|
current_pos = link_filename.size() + 1;
|
|
|
|
|
const auto current_timestamp_str = string( current_filename.begin() + current_pos, /* substr not working */
|
|
|
|
|
current_filename.begin() + current_pos + timestamp_string.size() );
|
|
|
|
|
const auto current_timestamp = string_to_timestamp( current_timestamp_str );
|
|
|
|
|
if( current_timestamp < start_time )
|
2014-07-03 05:27:58 +00:00
|
|
|
{
|
2014-07-03 14:02:40 +00:00
|
|
|
if( current_timestamp < limit_time || file_size( current_filename ) <= 0 )
|
|
|
|
|
{
|
|
|
|
|
remove_all( current_filename );
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( !cfg.rotation_compression ) continue;
|
|
|
|
|
if( current_filename.find( compression_extension ) != string::npos ) continue;
|
|
|
|
|
compress_file( current_filename );
|
2014-07-03 05:27:58 +00:00
|
|
|
}
|
2014-07-03 05:16:46 +00:00
|
|
|
}
|
|
|
|
|
catch( ... )
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:02:40 +00:00
|
|
|
_current_file_start_time = start_time;
|
2014-07-05 20:14:25 +00:00
|
|
|
_rotation_task = schedule( [this]() { rotate_files(); }, _current_file_start_time + cfg.rotation_interval.to_seconds() );
|
2014-07-03 05:16:46 +00:00
|
|
|
}
|
2013-02-05 05:06:16 +00:00
|
|
|
};
|
|
|
|
|
file_appender::config::config( const fc::path& p )
|
2013-06-05 19:19:00 +00:00
|
|
|
:format( "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}" ),
|
2014-07-03 14:02:40 +00:00
|
|
|
filename(p),flush(true),truncate(true),rotate(false),rotation_compression(true){}
|
2013-02-05 05:06:16 +00:00
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
file_appender::file_appender( const variant& args )
|
2014-07-03 14:02:40 +00:00
|
|
|
:my( new impl( args.as<config>() ) )
|
2013-02-05 05:06:16 +00:00
|
|
|
{
|
2014-07-02 21:36:21 +00:00
|
|
|
std::string log_filename;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
log_filename = my->cfg.filename.string();
|
|
|
|
|
|
|
|
|
|
fc::create_directories( fc::path( log_filename ).parent_path() );
|
|
|
|
|
|
2014-07-03 05:16:46 +00:00
|
|
|
if( !my->cfg.rotate ) my->out.open( log_filename.c_str() );
|
2014-07-02 21:36:21 +00:00
|
|
|
}
|
|
|
|
|
catch( ... )
|
|
|
|
|
{
|
2014-07-03 05:16:46 +00:00
|
|
|
std::cerr << "error opening log file: " << log_filename << "\n";
|
2013-02-05 05:06:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file_appender::~file_appender(){}
|
|
|
|
|
|
2013-07-03 17:26:23 +00:00
|
|
|
// MS THREAD METHOD MESSAGE \t\t\t File:Line
|
2013-02-05 05:06:16 +00:00
|
|
|
void file_appender::log( const log_message& m )
|
|
|
|
|
{
|
2013-07-03 17:26:23 +00:00
|
|
|
std::stringstream line;
|
2014-06-24 20:29:59 +00:00
|
|
|
//line << (m.get_context().get_timestamp().time_since_epoch().count() % (1000ll*1000ll*60ll*60))/1000 <<"ms ";
|
|
|
|
|
line << std::string(m.get_context().get_timestamp()) << " ";
|
2013-07-03 17:26:23 +00:00
|
|
|
line << std::setw( 10 ) << m.get_context().get_thread_name().substr(0,9).c_str() <<" ";
|
|
|
|
|
|
|
|
|
|
auto me = m.get_context().get_method();
|
|
|
|
|
// strip all leading scopes...
|
|
|
|
|
if( me.size() )
|
|
|
|
|
{
|
|
|
|
|
uint32_t p = 0;
|
|
|
|
|
for( uint32_t i = 0;i < me.size(); ++i )
|
|
|
|
|
{
|
|
|
|
|
if( me[i] == ':' ) p = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( me[p] == ':' ) ++p;
|
|
|
|
|
line << std::setw( 20 ) << m.get_context().get_method().substr(p,20).c_str() <<" ";
|
|
|
|
|
}
|
|
|
|
|
line << "] ";
|
2013-06-05 19:19:00 +00:00
|
|
|
fc::string message = fc::format_string( m.get_format(), m.get_data() );
|
2013-07-03 17:26:23 +00:00
|
|
|
line << message.c_str();
|
|
|
|
|
|
|
|
|
|
//fc::variant lmsg(m);
|
2013-02-05 05:06:16 +00:00
|
|
|
|
2014-07-02 21:36:21 +00:00
|
|
|
// fc::string fmt_str = fc::format_string( my->cfg.format, mutable_variant_object(m.get_context())( "message", message) );
|
|
|
|
|
|
2013-02-05 05:06:16 +00:00
|
|
|
{
|
2014-07-02 21:36:21 +00:00
|
|
|
fc::scoped_lock<boost::mutex> lock( my->slock );
|
2013-07-03 17:26:23 +00:00
|
|
|
my->out << line.str() << "\t\t\t" << m.get_context().get_file() <<":"<<m.get_context().get_line_number()<<"\n";
|
2014-07-02 21:36:21 +00:00
|
|
|
if( my->cfg.flush ) my->out.flush();
|
2013-02-05 05:06:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-03 14:02:40 +00:00
|
|
|
|
|
|
|
|
} // fc
|