enhance console appender
This commit is contained in:
parent
e2e416bf80
commit
36d657fb62
2 changed files with 44 additions and 21 deletions
|
|
@ -48,9 +48,17 @@ namespace fc
|
||||||
|
|
||||||
|
|
||||||
console_appender( const variant& args );
|
console_appender( const variant& args );
|
||||||
|
console_appender( const config& cfg );
|
||||||
|
console_appender();
|
||||||
|
|
||||||
~console_appender();
|
~console_appender();
|
||||||
virtual void log( const log_message& m );
|
virtual void log( const log_message& m );
|
||||||
|
|
||||||
|
void print( const std::string& text_to_print,
|
||||||
|
color::type text_color = color::console_default );
|
||||||
|
|
||||||
|
void configure( const config& cfg );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class impl;
|
class impl;
|
||||||
std::unique_ptr<impl> my;
|
std::unique_ptr<impl> my;
|
||||||
|
|
|
||||||
|
|
@ -27,16 +27,27 @@ namespace fc {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
console_appender::console_appender( const variant& args ) :
|
console_appender::console_appender( const variant& args )
|
||||||
my(new impl)
|
:my(new impl)
|
||||||
{
|
{
|
||||||
|
configure( args.as<config>() );
|
||||||
|
}
|
||||||
|
|
||||||
|
console_appender::console_appender( const config& cfg )
|
||||||
|
:my(new impl)
|
||||||
|
{
|
||||||
|
configure( cfg );
|
||||||
|
}
|
||||||
|
console_appender::console_appender()
|
||||||
|
:my(new impl){}
|
||||||
|
|
||||||
|
|
||||||
|
void console_appender::configure( const config& console_appender_config )
|
||||||
|
{ try {
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
my->console_handle = INVALID_HANDLE_VALUE;
|
my->console_handle = INVALID_HANDLE_VALUE;
|
||||||
#endif
|
#endif
|
||||||
try
|
my->cfg = console_appender_config;
|
||||||
{
|
|
||||||
my->cfg = args.as<config>();//fc::variant_cast<config>(args);
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (my->cfg.stream = stream::std_error)
|
if (my->cfg.stream = stream::std_error)
|
||||||
my->console_handle = GetStdHandle(STD_ERROR_HANDLE);
|
my->console_handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
|
@ -48,13 +59,7 @@ namespace fc {
|
||||||
my->lc[i] = color::console_default;
|
my->lc[i] = color::console_default;
|
||||||
for( auto itr = my->cfg.level_colors.begin(); itr != my->cfg.level_colors.end(); ++itr )
|
for( auto itr = my->cfg.level_colors.begin(); itr != my->cfg.level_colors.end(); ++itr )
|
||||||
my->lc[itr->level] = itr->color;
|
my->lc[itr->level] = itr->color;
|
||||||
}
|
} FC_CAPTURE_AND_RETHROW( (console_appender_config) ) }
|
||||||
catch ( exception& e )
|
|
||||||
{
|
|
||||||
fc::cerr<<e.to_detail_string()<<"\n";
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console_appender::~console_appender() {}
|
console_appender::~console_appender() {}
|
||||||
|
|
||||||
|
|
@ -113,19 +118,29 @@ namespace fc {
|
||||||
line << "] ";
|
line << "] ";
|
||||||
fc::string message = fc::format_string( m.get_format(), m.get_data() );
|
fc::string message = fc::format_string( m.get_format(), m.get_data() );
|
||||||
line << message;//.c_str();
|
line << message;//.c_str();
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
fc::unique_lock<boost::mutex> lock(log_mutex());
|
fc::unique_lock<boost::mutex> lock(log_mutex());
|
||||||
|
|
||||||
|
print( line.str(), my->lc[m.get_context().get_log_level()] );
|
||||||
|
|
||||||
|
fprintf( out, "\n" );
|
||||||
|
|
||||||
|
if( my->cfg.flush ) fflush( out );
|
||||||
|
}
|
||||||
|
|
||||||
|
void console_appender::print( const std::string& text, color::type text_color )
|
||||||
|
{
|
||||||
|
FILE* out = stream::std_error ? stderr : stdout;
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (my->console_handle != INVALID_HANDLE_VALUE)
|
if (my->console_handle != INVALID_HANDLE_VALUE)
|
||||||
SetConsoleTextAttribute(my->console_handle, get_console_color( my->lc[m.get_context().get_log_level()] ));
|
SetConsoleTextAttribute(my->console_handle, get_console_color(text_color));
|
||||||
#else
|
#else
|
||||||
if(isatty(fileno(out))) fprintf( out, "\r%s", get_console_color( my->lc[m.get_context().get_log_level()] ) );
|
if(isatty(fileno(out))) fprintf( out, "\r%s", get_console_color( text_color ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fprintf( out, "%s", line.str().c_str()); //fmt_str.c_str() );
|
if( text.size() )
|
||||||
|
fprintf( out, "%s", text.c_str() ); //fmt_str.c_str() );
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (my->console_handle != INVALID_HANDLE_VALUE)
|
if (my->console_handle != INVALID_HANDLE_VALUE)
|
||||||
|
|
@ -133,7 +148,7 @@ namespace fc {
|
||||||
#else
|
#else
|
||||||
if(isatty(fileno(out))) fprintf( out, "\r%s", CONSOLE_DEFAULT );
|
if(isatty(fileno(out))) fprintf( out, "\r%s", CONSOLE_DEFAULT );
|
||||||
#endif
|
#endif
|
||||||
fprintf( out, "\n" );
|
|
||||||
if( my->cfg.flush ) fflush( out );
|
if( my->cfg.flush ) fflush( out );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue