Skip to content

Commit

Permalink
Streamline logging config and add extra validation to stop on a bad c…
Browse files Browse the repository at this point in the history
…onfiguration steemit#2612
  • Loading branch information
mvandeberg committed Jul 11, 2018
1 parent cb7255c commit e49b2da
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 48 deletions.
14 changes: 6 additions & 8 deletions libraries/utilities/include/steem/utilities/logging_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@

namespace steem { namespace utilities {

struct console_appender_args
struct appender_args
{
std::string appender;
std::string file;
std::string stream;
};

struct file_appender_args
{
std::string appender;
std::string file;
void validate();
};

struct logger_args
{
std::string name;
std::string level;
std::string appender;

void validate();
};

void set_logging_program_options( boost::program_options::options_description& options );
Expand All @@ -37,6 +36,5 @@ fc::optional<fc::logging_config> load_logging_config( const boost::program_optio

} } // steem::utilities

FC_REFLECT( steem::utilities::console_appender_args, (appender)(stream) )
FC_REFLECT( steem::utilities::file_appender_args, (appender)(file) )
FC_REFLECT( steem::utilities::appender_args, (appender)(file)(stream) )
FC_REFLECT( steem::utilities::logger_args, (name)(level)(appender) )
95 changes: 57 additions & 38 deletions libraries/utilities/logging_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,36 @@ namespace steem { namespace utilities {
using std::string;
using std::vector;

void set_logging_program_options( boost::program_options::options_description& options )
void appender_args::validate()
{
std::vector< std::string > default_console_appender( { "{\"appender\":\"stderr\",\"stream\":\"std_error\"}" } );
std::string str_default_console_appender = boost::algorithm::join( default_console_appender, " " );
FC_ASSERT( appender.length(), "Must specify an appender name" );
FC_ASSERT( ( file.length() > 0 ) ^ ( stream.length() > 0 ), "Must specify either a file or a stream" );
}

std::vector< std::string > default_file_appender( { "{\"appender\":\"p2p\",\"file\":\"logs/p2p/p2p.log\"}" } );
std::string str_default_file_appender = boost::algorithm::join( default_file_appender, " " );
void logger_args::validate()
{
FC_ASSERT( name.length(), "Must specify a logger name" );
FC_ASSERT( level.length(), "Must specify a logger level" );
FC_ASSERT( appender.length(), "Must specify an appender name" );
}

void set_logging_program_options( boost::program_options::options_description& options )
{
std::vector< std::string > default_appender(
{ "{\"appender\":\"stderr\",\"stream\":\"std_error\"}\n",
"log-appender = {\"appender\":\"p2p\",\"file\":\"logs/p2p/p2p.log\"}" } );
std::string str_default_appender = boost::algorithm::join( default_appender, "" );

std::vector< std::string > default_logger(
{ "{\"name\":\"default\",\"level\":\"info\",\"appender\":\"stderr\"}\n",
"log-logger = {\"name\":\"p2p\",\"level\":\"warn\",\"appender\":\"p2p\"}" } );
std::string str_default_logger = boost::algorithm::join( default_logger, "" );

options.add_options()
("log-console-appender", boost::program_options::value< std::vector< std::string > >()->composing()->default_value( default_console_appender, str_default_console_appender ),
"Console appender definition json: {\"appender\", \"stream\"}" )
("log-file-appender", boost::program_options::value< std::vector< std::string > >()->composing()->default_value( default_file_appender, str_default_file_appender ),
"File appender definition json: {\"appender\", \"file\"}" )
("log-appender", boost::program_options::value< std::vector< std::string > >()->composing()->default_value( default_appender, str_default_appender ),
"Appender definition json: {\"appender\", \"stream\", \"file\"} Can only specify a file OR a stream" )
("log-console-appender", boost::program_options::value< std::vector< std::string > >()->composing() )
("log-file-appender", boost::program_options::value< std::vector< std::string > >()->composing() )
("log-logger", boost::program_options::value< std::vector< std::string > >()->composing()->default_value( default_logger, str_default_logger ),
"Logger definition json: {\"name\", \"level\", \"appender\"}" )
;
Expand All @@ -43,44 +55,51 @@ fc::optional<fc::logging_config> load_logging_config( const boost::program_optio
fc::logging_config logging_config;
bool found_logging_config = false;

std::vector< string > all_appenders;

if( args.count( "log-appender" ) )
{
std::vector< string > appenders = args["log-appender"].as< vector< string > >();
all_appenders.insert( all_appenders.end(), appenders.begin(), appenders.end() );
}

if( args.count( "log-console-appender" ) )
{
std::vector< string > console_appenders = args["log-console-appender"].as< vector< string > >();

for( string& s : console_appenders )
{
try
{
auto console_appender = fc::json::from_string( s ).as< console_appender_args >();

fc::console_appender::config console_appender_config;
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::debug,
fc::console_appender::color::green));
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::warn,
fc::console_appender::color::brown));
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::error,
fc::console_appender::color::red));
console_appender_config.stream = fc::variant( console_appender.stream ).as< fc::console_appender::stream::type >();
logging_config.appenders.push_back(
fc::appender_config( console_appender.appender, "console", fc::variant( console_appender_config ) ) );
found_logging_config = true;
}
catch( ... ) {}
}
all_appenders.insert( all_appenders.end(), console_appenders.begin(), console_appenders.end() );
}

if( args.count( "log-file-appender" ) )
{
std::vector< string > file_appenders = args["log-file-appender"].as< vector< string > >();
all_appenders.insert( all_appenders.end(), file_appenders.begin(), file_appenders.end() );
}

for( string& s : file_appenders )
{
auto file_appender = fc::json::from_string( s ).as< file_appender_args>();
for( string& s : all_appenders )
{
auto appender = fc::json::from_string( s ).as< appender_args >();
appender.validate();

fc::path file_name = file_appender.file;
if( appender.stream.length() )
{
fc::console_appender::config console_appender_config;
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::debug,
fc::console_appender::color::green));
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::warn,
fc::console_appender::color::brown));
console_appender_config.level_colors.emplace_back(
fc::console_appender::level_color( fc::log_level::error,
fc::console_appender::color::red));
console_appender_config.stream = fc::variant( appender.stream ).as< fc::console_appender::stream::type >();
logging_config.appenders.push_back(
fc::appender_config( appender.appender, "console", fc::variant( console_appender_config ) ) );
found_logging_config = true;
}
else // validate ensures the is either a stream or file configured
{
fc::path file_name = appender.file;
if( file_name.is_relative() )
file_name = fc::absolute( pwd ) / file_name;

Expand All @@ -93,7 +112,7 @@ fc::optional<fc::logging_config> load_logging_config( const boost::program_optio
file_appender_config.rotation_interval = fc::hours(1);
file_appender_config.rotation_limit = fc::days(1);
logging_config.appenders.push_back(
fc::appender_config( file_appender.appender, "file", fc::variant( file_appender_config ) ) );
fc::appender_config( appender.appender, "file", fc::variant( file_appender_config ) ) );
found_logging_config = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions programs/steemd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ int main( int argc, char** argv )
if( logging_config )
fc::configure_logging( *logging_config );
}
catch( const fc::exception& )
catch( const fc::exception& e )
{
wlog( "Error parsing logging config" );
wlog( "Error parsing logging config. ${e}", ("e", e.to_string()) );
}

if( args.at( "backtrace" ).as< string >() == "yes" )
Expand Down

0 comments on commit e49b2da

Please sign in to comment.