Jpp  master_rocky-43-ge265d140c
the software that should make you happy
log.hh
Go to the documentation of this file.
1 #ifndef DATAQUEUE_LOG_HH
2 #define DATAQUEUE_LOG_HH
3 
4 #include <sstream>
5 #include <string>
6 #include <map>
7 #include <boost/thread/mutex.hpp>
8 
9 /**
10  * \author cpellegrino
11  */
12 
13 namespace Log {
14 
16 
17 class Logger
18 {
19  std::ostringstream m_stream;
20 
22 
23  public:
24 
26  :
27  m_level(level)
28  {}
29 
30  std::ostringstream& stream()
31  {
32  return m_stream;
33  }
34 
35  ~Logger();
36 };
37 
38 class Counter
39 {
41 
43  mutable boost::mutex m_mutex;
44 
45  Counter() {}
46 
47  Counter(Counter const&);
49 
50  public:
51 
52  static
54  {
55  static Counter c;
56 
57  return c;
58  }
59 
60  void add(std::string const& tag)
61  {
62  boost::mutex::scoped_lock lock(m_mutex);
63 
64  container_t::iterator const it = m_counters.find(tag);
65 
66  if (it != m_counters.end()) {
67  ++it->second;
68  } else {
69  m_counters.insert(std::make_pair(tag, 1));
70  }
71  }
72 
73  void reset()
74  {
75  boost::mutex::scoped_lock lock(m_mutex);
76  m_counters.clear();
77  }
78 
79  void reset(std::string const& tag)
80  {
81  boost::mutex::scoped_lock lock(m_mutex);
82 
83  container_t::iterator const it = m_counters.find(tag);
84 
85  if (it != m_counters.end()) {
86  it->second = 0;
87  }
88  }
89 
90  friend
91  std::ostream& operator <<(std::ostream& stream, Counter const& c)
92  {
93  boost::mutex::scoped_lock lock(c.m_mutex);
94 
95  for (
96  container_t::const_iterator it = c.m_counters.begin(), et = c.m_counters.end();
97  it != et;
98  ++it
99  ) {
100  stream << it->first << ": " << it->second << ", ";
101  }
102 
103  return stream;
104  }
105 };
106 
107 } // ns Log
108 
109 #define LOG_DEBUG Log::Logger(Log::cDebug ).stream()
110 #define LOG_WARNING Log::Logger(Log::cWarning).stream()
111 #define LOG_ERROR Log::Logger(Log::cError ).stream()
112 #define LOG_NOTICE Log::Logger(Log::cNotice ).stream()
113 #define LOG_STATUS Log::Logger(Log::cDebug ).stream()
114 
115 #endif // DATAQUEUE_LOG_HH
container_t m_counters
Definition: log.hh:42
Counter()
Definition: log.hh:45
Counter & operator=(Counter const &)
boost::mutex m_mutex
Definition: log.hh:43
void reset()
Definition: log.hh:73
void add(std::string const &tag)
Definition: log.hh:60
void reset(std::string const &tag)
Definition: log.hh:79
friend std::ostream & operator<<(std::ostream &stream, Counter const &c)
Definition: log.hh:91
std::map< std::string, unsigned int > container_t
Definition: log.hh:40
static Counter & get()
Definition: log.hh:53
Counter(Counter const &)
std::ostringstream m_stream
Definition: log.hh:19
~Logger()
Logger without Jpp.
Definition: jlog.cc:50
std::ostringstream & stream()
Definition: log.hh:30
LogLevel m_level
Definition: log.hh:21
Logger(LogLevel level)
Definition: log.hh:25
Definition: log.hh:13
LogLevel
Definition: log.hh:15
@ cStatus
Definition: log.hh:15
@ cWarning
Definition: log.hh:15
@ cError
Definition: log.hh:15
@ cDebug
Definition: log.hh:15
@ cNotice
Definition: log.hh:15