Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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>
12
13namespace fc {
14
15 log_config& log_config::get() {
16 // allocate dynamically which will leak on exit but allow loggers to be used until the very end of execution
17 static log_config* the = new log_config;
18 return *the;
19 }
20
22 {
23 std::lock_guard g( log_config::get().log_mutex );
24 log_config::get().appender_factory_map[type] = f;
25 return true;
26 }
27
29 std::lock_guard g( log_config::get().log_mutex );
30 return log_config::get().logger_map[name];
31 }
32
34 std::lock_guard g( log_config::get().log_mutex );
35 if( log_config::get().logger_map.find( name ) != log_config::get().logger_map.end() ) {
36 log = log_config::get().logger_map[name];
37 } else {
38 // no entry for logger, so setup with default logger if it exists, otherwise do nothing since default logger not configured
39 if( log_config::get().logger_map.find( DEFAULT_LOGGER ) != log_config::get().logger_map.end() ) {
40 log = log_config::get().logger_map[DEFAULT_LOGGER];
41 log_config::get().logger_map.emplace( name, log );
42 }
43 }
44 }
45
46 void log_config::initialize_appenders( boost::asio::io_service& ios ) {
47 std::lock_guard g( log_config::get().log_mutex );
48 for( auto& iter : log_config::get().appender_map )
49 iter.second->initialize( ios );
50 }
51
55 bool configure_logging( const logging_config& cfg ) {
57 }
58
60 try {
61 static bool reg_console_appender = log_config::register_appender<console_appender>( "console" );
62 static bool reg_gelf_appender = log_config::register_appender<gelf_appender>( "gelf" );
63 static bool reg_dmlog_appender = log_config::register_appender<dmlog_appender>( "dmlog" );
64
65 std::lock_guard g( log_config::get().log_mutex );
66 log_config::get().logger_map.clear();
67 log_config::get().appender_map.clear();
68
69 for( size_t i = 0; i < cfg.appenders.size(); ++i ) {
70 // create appender
71 auto fact_itr = log_config::get().appender_factory_map.find( cfg.appenders[i].type );
72 if( fact_itr == log_config::get().appender_factory_map.end() ) {
73 //wlog( "Unknown appender type '%s'", type.c_str() );
74 continue;
75 }
76 auto ap = fact_itr->second->create( cfg.appenders[i].args );
77 log_config::get().appender_map[cfg.appenders[i].name] = ap;
78 }
79 for( size_t i = 0; i < cfg.loggers.size(); ++i ) {
80 auto lgr = log_config::get().logger_map[cfg.loggers[i].name];
81
82 // TODO: finish configure logger here...
83 if( cfg.loggers[i].parent ) {
84 lgr.set_parent( log_config::get().logger_map[*cfg.loggers[i].parent] );
85 }
86 lgr.set_name(cfg.loggers[i].name);
87 if( cfg.loggers[i].level ) lgr.set_log_level( *cfg.loggers[i].level );
88
89
90 for( auto a = cfg.loggers[i].appenders.begin(); a != cfg.loggers[i].appenders.end(); ++a ){
91 auto ap_it = log_config::get().appender_map.find(*a);
92 if( ap_it != log_config::get().appender_map.end() ) {
93 lgr.add_appender( ap_it->second );
94 }
95 }
96 }
97 return reg_console_appender || reg_gelf_appender || reg_dmlog_appender;
98 } catch ( exception& e )
99 {
100 std::cerr<<e.to_detail_string()<<"\n";
101 }
102 return false;
103 }
104
106 //slog( "default cfg" );
107 logging_config cfg;
108
109 variants c;
110 c.push_back( mutable_variant_object( "level","debug")("color", "green") );
111 c.push_back( mutable_variant_object( "level","warn")("color", "brown") );
112 c.push_back( mutable_variant_object( "level","error")("color", "red") );
113
114 cfg.appenders.push_back(
115 appender_config( "stderr", "console",
117 ( "stream","std_error")
118 ( "level_colors", c )
119 ) );
120 cfg.appenders.push_back(
121 appender_config( "stdout", "console",
123 ( "stream","std_out")
124 ( "level_colors", c )
125 ) );
126
127 logger_config dlc;
128 dlc.name = DEFAULT_LOGGER;
130 dlc.appenders.push_back("stderr");
131 cfg.loggers.push_back( dlc );
132 return cfg;
133 }
134
135 static thread_local std::string thread_name;
136 void set_os_thread_name( const string& name ) {
137#ifdef FC_USE_PTHREAD_NAME_NP
138 pthread_setname_np( pthread_self(), name.c_str() );
139#endif
140 }
141 void set_thread_name( const string& name ) {
142 thread_name = name;
143 }
144 const string& get_thread_name() {
145 if( thread_name.empty() ) {
146#ifdef FC_USE_PTHREAD_NAME_NP
147 char thr_name[64];
148 int rc = pthread_getname_np( pthread_self(), thr_name, 64 );
149 if( rc == 0 ) {
150 thread_name = thr_name;
151 }
152#else
153 static int thread_count = 0;
154 thread_name = string( "thread-" ) + fc::to_string( thread_count++ );
155#endif
156 }
157 return thread_name;
158 }
159}
std::string name
std::shared_ptr< appender_factory > ptr
Definition appender.hpp:19
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
std::string to_detail_string(log_level ll=log_level::all) const
static variant from_file(const fc::path &p, const parse_type ptype=parse_type::legacy_parser, const uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:797
An order-preserving dictionary of variants.
wraps boost::filesystem::path to provide platform independent path manipulation.
Defines exception's used by fc.
#define DEFAULT_LOGGER
Definition logger.hpp:7
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
fc::string to_string(double)
Definition string.cpp:131
void set_thread_name(const string &name)
const string & get_thread_name()
std::vector< fc::variant > variants
Definition variant.hpp:173
void set_os_thread_name(const string &name)
void configure_logging(const fc::path &log_config)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
static void initialize_appenders(boost::asio::io_service &ios)
static logger get_logger(const fc::string &name)
static bool configure_logging(const logging_config &l)
static void update_logger(const fc::string &name, logger &log)
static bool register_appender(const fc::string &type)
std::vector< string > appenders
std::optional< log_level > level
if not set, then parents level is used.
std::vector< logger_config > loggers
static logging_config default_config()
std::vector< appender_config > appenders
yh_object_type type
Definition yubihsm.h:672
yh_rc rc