BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
103  {
104  auto item = std::find(my->_appenders.begin(), my->_appenders.end(), a);
105  if (item != my->_appenders.end())
106  my->_appenders.erase(item);
107  }
108 
109  std::vector<appender::ptr> logger::get_appenders()const
110  {
111  return my->_appenders;
112  }
113 
114  bool configure_logging( const logging_config& cfg );
116 
117 } // namespace fc
appender.hpp
fc::logger::operator=
logger & operator=(const logger &)
Definition: logger.cpp:49
fc::logger::impl::_name
std::string _name
Definition: logger.cpp:18
fc::logger::impl::_additivity
bool _additivity
Definition: logger.cpp:21
fc::configure_logging
void configure_logging(const fc::path &log_config)
Definition: logger_config.cpp:18
fc::logger
Definition: logger.hpp:21
fc::scoped_lock
Definition: scoped_lock.hpp:5
fc::log_context::append_context
void append_context(const std::string &c)
Definition: log_message.cpp:84
fc::spin_lock
modified spin-lock that yields on failure, but becomes a 'spin lock' if there are no other tasks to y...
Definition: spin_lock.hpp:20
fc::logger::~logger
~logger()
Definition: logger.cpp:47
fc
Definition: api.hpp:15
fc::logger::log
void log(log_message m)
Definition: logger.cpp:64
fc::logger::set_log_level
logger & set_log_level(log_level e)
Definition: logger.cpp:97
scoped_lock.hpp
fc::logger::name
const std::string & name() const
Definition: logger.cpp:75
filesystem.hpp
fc::do_default_config
bool do_default_config
Definition: logger.cpp:115
spin_lock.hpp
fc::logger::impl::_parent
logger _parent
Definition: logger.cpp:19
fc::logging_config
Definition: logger_config.hpp:35
fc::logger::impl::_appenders
std::vector< appender::ptr > _appenders
Definition: logger.cpp:24
fc::logger::get_log_level
log_level get_log_level() const
Definition: logger.cpp:96
logger_config.hpp
fc::logger::set_parent
logger & set_parent(const logger &l)
Definition: logger.cpp:94
fc::get_logger_map
std::unordered_map< std::string, logger > & get_logger_map()
Definition: logger.cpp:79
thread.hpp
fc::logging_config::default_config
static logging_config default_config()
Definition: logger_config.cpp:58
fc::logger::is_enabled
bool is_enabled(log_level e) const
Definition: logger.cpp:60
fc::logger::set_name
void set_name(const std::string &n)
Definition: logger.cpp:74
fc::logger::get_appenders
std::vector< appender::ptr > get_appenders() const
Definition: logger.cpp:109
fc::log_message
aggregates a message along with the context and associated meta-information.
Definition: log_message.hpp:106
fc::logger::add_appender
void add_appender(const appender::ptr &a)
Definition: logger.cpp:99
fc::appender::ptr
std::shared_ptr< appender > ptr
Definition: appender.hpp:30
fc::logger::remove_appender
void remove_appender(const appender::ptr &a)
Definition: logger.cpp:102
fc::logger::get
static logger get(const std::string &name="default")
Definition: logger.cpp:87
std
Definition: zeroed_array.hpp:76
logger.hpp
fc::operator!=
bool operator!=(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:257
fc::logger::impl
Definition: logger.cpp:14
fc::logger::impl::impl
impl()
Definition: logger.cpp:16
fc::log_level
Definition: log_message.hpp:23
fc::logger::impl::_enabled
bool _enabled
Definition: logger.cpp:20
fc::logger::logger
logger()
Definition: logger.cpp:28
fc::logger::impl::_level
log_level _level
Definition: logger.cpp:22
fc::log_message::get_context
log_context get_context() const
Definition: log_message.cpp:209
log_message.hpp
Defines types and helper macros necessary for generating log messages.
fc::operator==
bool operator==(const optional< T > &left, const optional< T > &right)
Definition: optional.hpp:249
fc::logger::get_parent
logger get_parent() const
Definition: logger.cpp:93