BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 }
fc::appender::create
static appender::ptr create(const std::string &name, const std::string &type, const variant &args)
Definition: appender.cpp:35
appender.hpp
stdio.hpp
fc::configure_logging
void configure_logging(const fc::path &log_config)
Definition: logger_config.cpp:18
fc::get_appender_map
std::unordered_map< std::string, appender::ptr > & get_appender_map()
Definition: appender.cpp:17
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
fc::mutable_variant_object
An order-perserving dictionary of variant's.
Definition: variant_object.hpp:108
variant.hpp
fc::log_level::debug
@ debug
Definition: log_message.hpp:35
fc
Definition: api.hpp:15
fc::logger_config::add_appender
logger_config & add_appender(const string &s)
Definition: logger_config.cpp:16
filesystem.hpp
fc::appender::get
static appender::ptr get(const std::string &name)
Definition: appender.cpp:25
fc::logger_config
Definition: logger_config.hpp:21
fc::logging_config
Definition: logger_config.hpp:35
fc::logger_config::level
fc::optional< log_level > level
if not set, then parents level is used.
Definition: logger_config.hpp:26
logger_config.hpp
fc::path
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
file_appender.hpp
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
fc::get_logger_map
std::unordered_map< std::string, logger > & get_logger_map()
Definition: logger.cpp:79
fc::logging_config::default_config
static logging_config default_config()
Definition: logger_config.cpp:58
fc::exception::to_detail_string
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
fc::logging_config::appenders
std::vector< appender_config > appenders
Definition: logger_config.hpp:38
fc::cerr
cerr_t & cerr
Definition: iostream.cpp:176
fc::logger_config::appenders
std::vector< string > appenders
Definition: logger_config.hpp:30
json.hpp
console_appender.hpp
fc::logger::get
static logger get(const std::string &name="default")
Definition: logger.cpp:87
exception.hpp
Defines exception's used by fc.
fc::appender_config
Definition: logger_config.hpp:6
fc::logging_config::loggers
std::vector< logger_config > loggers
Definition: logger_config.hpp:39
fc::logger_config::name
string name
Definition: logger_config.hpp:23