2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/log/file_appender.hpp>
|
2013-02-05 05:06:16 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/thread/scoped_lock.hpp>
|
2013-02-05 05:06:16 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/io/fstream.hpp>
|
|
|
|
|
#include <fc/variant.hpp>
|
|
|
|
|
#include <fc/reflect/variant.hpp>
|
|
|
|
|
|
2013-02-05 05:06:16 +00:00
|
|
|
|
|
|
|
|
namespace fc {
|
|
|
|
|
class file_appender::impl : public fc::retainable {
|
|
|
|
|
public:
|
|
|
|
|
config cfg;
|
|
|
|
|
ofstream out;
|
|
|
|
|
boost::mutex slock;
|
|
|
|
|
};
|
|
|
|
|
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}" ),
|
2013-02-05 05:06:16 +00:00
|
|
|
filename(p),flush(true),truncate(true){}
|
|
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
file_appender::file_appender( const variant& args )
|
2013-02-05 05:06:16 +00:00
|
|
|
:my( new impl() )
|
|
|
|
|
{
|
|
|
|
|
try {
|
2013-06-05 19:19:00 +00:00
|
|
|
my->cfg = args.as<config>();
|
2013-02-05 05:06:16 +00:00
|
|
|
my->out.open( my->cfg.filename.string().c_str() );
|
|
|
|
|
} catch ( ... ) {
|
2013-06-05 19:19:00 +00:00
|
|
|
//elog( "%s", fc::except_str().c_str() );
|
2013-02-05 05:06:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file_appender::~file_appender(){}
|
|
|
|
|
|
|
|
|
|
void file_appender::log( const log_message& m )
|
|
|
|
|
{
|
2013-06-05 19:19:00 +00:00
|
|
|
fc::string message = fc::format_string( m.get_format(), m.get_data() );
|
|
|
|
|
fc::variant lmsg(m);
|
2013-02-05 05:06:16 +00:00
|
|
|
|
2013-06-05 19:19:00 +00:00
|
|
|
fc::string fmt_str = fc::format_string( my->cfg.format, mutable_variant_object(lmsg.get_object())( "message", message) );
|
2013-02-05 05:06:16 +00:00
|
|
|
{
|
|
|
|
|
fc::scoped_lock<boost::mutex> lock(my->slock);
|
|
|
|
|
my->out << fmt_str << "\n";
|
|
|
|
|
}
|
|
|
|
|
if( my->cfg.flush ) my->out.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|