BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
logger_config.cpp
Go to the documentation of this file.
2 #include <fc/log/appender.hpp>
3 #include <fc/io/json.hpp>
4 #include <fc/filesystem.hpp>
5 #include <unordered_map>
6 #include <string>
9 #include <fc/reflect/variant.hpp>
11 #include <fc/io/stdio.hpp>
12 
13 namespace fc {
14  extern std::unordered_map<std::string,logger>& get_logger_map();
15  extern std::unordered_map<std::string,appender::ptr>& get_appender_map();
16  logger_config& logger_config::add_appender( const string& s ) { appenders.push_back(s); return *this; }
17 
18  void configure_logging( const fc::path& lc )
19  {
20  configure_logging( fc::json::from_file<logging_config>(lc) );
21  }
22  bool configure_logging( const logging_config& cfg )
23  {
24  try {
25  static bool reg_console_appender = appender::register_appender<console_appender>( "console" );
26  static bool reg_file_appender = appender::register_appender<file_appender>( "file" );
27  get_logger_map().clear();
28  get_appender_map().clear();
29 
30  for( size_t i = 0; i < cfg.appenders.size(); ++i ) {
31  appender::create( cfg.appenders[i].name, cfg.appenders[i].type, cfg.appenders[i].args );
32  // TODO... process enabled
33  }
34  for( size_t i = 0; i < cfg.loggers.size(); ++i ) {
35  auto lgr = logger::get( cfg.loggers[i].name );
36 
37  // TODO: finish configure logger here...
38  if( cfg.loggers[i].parent.valid() ) {
39  lgr.set_parent( logger::get( *cfg.loggers[i].parent ) );
40  }
41  lgr.set_name(cfg.loggers[i].name);
42  if( cfg.loggers[i].level.valid() ) lgr.set_log_level( *cfg.loggers[i].level );
43 
44 
45  for( auto a = cfg.loggers[i].appenders.begin(); a != cfg.loggers[i].appenders.end(); ++a ){
46  auto ap = appender::get( *a );
47  if( ap ) { lgr.add_appender(ap); }
48  }
49  }
50  return reg_console_appender || reg_file_appender;
51  } catch ( exception& e )
52  {
53  fc::cerr<<e.to_detail_string()<<"\n";
54  }
55  return false;
56  }
57 
59  //slog( "default cfg" );
60  logging_config cfg;
61 
62  variants c;
63  c.push_back( mutable_variant_object( "level","debug")("color", "green") );
64  c.push_back( mutable_variant_object( "level","warn")("color", "brown") );
65  c.push_back( mutable_variant_object( "level","error")("color", "red") );
66 
67  cfg.appenders.push_back(
68  appender_config( "stderr", "console",
70  ( "stream","std_error")
71  ( "level_colors", c )
72  ) );
73  cfg.appenders.push_back(
74  appender_config( "stdout", "console",
76  ( "stream","std_out")
77  ( "level_colors", c )
78  ) );
79 
80  logger_config dlc;
81  dlc.name = "default";
82  dlc.level = log_level::debug;
83  dlc.appenders.push_back("stderr");
84  cfg.loggers.push_back( dlc );
85  return cfg;
86  }
87 }
std::vector< logger_config > loggers
static logging_config default_config()
static logger get(const std::string &name="default")
Definition: logger.cpp:87
std::unordered_map< std::string, logger > & get_logger_map()
Definition: logger.cpp:79
static appender::ptr create(const std::string &name, const std::string &type, const variant &args)
Definition: appender.cpp:35
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
std::vector< variant > variants
Definition: variant.hpp:170
std::vector< string > appenders
fc::optional< log_level > level
if not set, then parents level is used.
static appender::ptr get(const std::string &name)
Definition: appender.cpp:25
void configure_logging(const fc::path &log_config)
std::vector< appender_config > appenders
Defines exception&#39;s used by fc.
std::unordered_map< std::string, appender::ptr > & get_appender_map()
Definition: appender.cpp:17
Definition: api.hpp:15
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
An order-perserving dictionary of variant&#39;s.
logger_config & add_appender(const string &s)
cerr_t & cerr
Definition: iostream.cpp:176