BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
logger.cpp
Go to the documentation of this file.
1 #include <fc/log/logger.hpp>
2 #include <fc/log/log_message.hpp>
3 #include <fc/thread/thread.hpp>
6 #include <fc/log/appender.hpp>
7 #include <fc/filesystem.hpp>
8 #include <unordered_map>
9 #include <string>
10 #include <fc/log/logger_config.hpp>
11 
12 namespace fc {
13 
14  class logger::impl {
15  public:
16  impl()
17  :_parent(nullptr),_enabled(true),_additivity(false),_level(log_level::warn){}
18  std::string _name;
20  bool _enabled;
23 
24  std::vector<appender::ptr> _appenders;
25  };
26 
27 
29  :my( new impl() ){}
30 
31  logger::logger(std::nullptr_t){}
32 
33  logger::logger( const string& name, const logger& parent )
34  :my( new impl() )
35  {
36  my->_name = name;
37  my->_parent = parent;
38  }
39 
40 
41  logger::logger( const logger& l )
42  :my(l.my){}
43 
45  :my(std::move(l.my)){}
46 
48 
50  my = l.my;
51  return *this;
52  }
54  std::swap(my,l.my);
55  return *this;
56  }
57  bool operator==( const logger& l, std::nullptr_t ) { return !(bool)l.my; }
58  bool operator!=( const logger& l, std::nullptr_t ) { return (bool)l.my; }
59 
60  bool logger::is_enabled( log_level e )const {
61  return e >= my->_level;
62  }
63 
65  m.get_context().append_context( my->_name );
66 
67  for( auto itr = my->_appenders.begin(); itr != my->_appenders.end(); ++itr )
68  (*itr)->log( m );
69 
70  if( my->_additivity && my->_parent != nullptr) {
71  my->_parent.log(m);
72  }
73  }
74  void logger::set_name( const std::string& n ) { my->_name = n; }
75  const std::string& logger::name()const { return my->_name; }
76 
77  extern bool do_default_config;
78 
79  std::unordered_map<std::string,logger>& get_logger_map() {
80  static bool force_link_default_config = fc::do_default_config;
81  //TODO: Atomic compare/swap set
82  static std::unordered_map<std::string,logger>* lm = new std::unordered_map<std::string, logger>();
83  (void)force_link_default_config; // hide warning;
84  return *lm;
85  }
86 
87  logger logger::get( const std::string& s ) {
88  static fc::spin_lock logger_spinlock;
89  scoped_lock<spin_lock> lock(logger_spinlock);
90  return get_logger_map()[s];
91  }
92 
93  logger logger::get_parent()const { return my->_parent; }
94  logger& logger::set_parent(const logger& p) { my->_parent = p; return *this; }
95 
96  log_level logger::get_log_level()const { return my->_level; }
97  logger& logger::set_log_level(log_level ll) { my->_level = ll; return *this; }
98 
100  { my->_appenders.push_back(a); }
101 
102  std::vector<appender::ptr> logger::get_appenders()const
103  {
104  return my->_appenders;
105  }
106 
107  bool configure_logging( const logging_config& cfg );
108  bool do_default_config = configure_logging( logging_config::default_config() );
109 
110 } // namespace fc
static logging_config default_config()
Defines types and helper macros necessary for generating log messages.
void set_name(const std::string &n)
Definition: logger.cpp:74
void add_appender(const appender::ptr &a)
Definition: logger.cpp:99
logger & set_parent(const logger &l)
Definition: logger.cpp:94
const std::string & name() const
Definition: logger.cpp:75
modified spin-lock that yields on failure, but becomes a &#39;spin lock&#39; if there are no other tasks to y...
Definition: spin_lock.hpp:20
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
logger _parent
Definition: logger.cpp:19
friend bool operator!=(const logger &, std::nullptr_t)
Definition: logger.cpp:58
logger get_parent() const
Definition: logger.cpp:93
friend bool operator==(const logger &, std::nullptr_t)
Definition: logger.cpp:57
std::vector< appender::ptr > _appenders
Definition: logger.cpp:24
std::shared_ptr< appender > ptr
Definition: appender.hpp:30
bool do_default_config
Definition: logger.cpp:108
log_level get_log_level() const
Definition: logger.cpp:96
void configure_logging(const fc::path &log_config)
void log(log_message m)
Definition: logger.cpp:64
std::string _name
Definition: logger.cpp:18
log_context get_context() const
void append_context(const std::string &c)
Definition: log_message.cpp:84
logger & set_log_level(log_level e)
Definition: logger.cpp:97
std::vector< appender::ptr > get_appenders() const
Definition: logger.cpp:102
aggregates a message along with the context and associated meta-information.
bool is_enabled(log_level e) const
Definition: logger.cpp:60
Definition: api.hpp:15
logger & operator=(const logger &)
Definition: logger.cpp:49
log_level _level
Definition: logger.cpp:22
bool _additivity
Definition: logger.cpp:21