Monitoring  3.3.4
O2 Monitoring library
MonLogger.h
Go to the documentation of this file.
1 // Copyright CERN and copyright holders of ALICE O2. This software is
2 // distributed under the terms of the GNU General Public License v3 (GPL
3 // Version 3), copied verbatim in the file "COPYING".
4 //
5 // See http://alice-o2.web.cern.ch/license for full licensing information.
6 //
7 // In applying this license CERN does not waive the privileges and immunities
8 // granted to it by virtue of its status as an Intergovernmental Organization
9 // or submit itself to any jurisdiction.
10 
15 
16 #ifndef MONITORING_MONINFOLOGGER_H
17 #define MONITORING_MONINFOLOGGER_H
18 
19 #include <chrono>
20 #include <iomanip>
21 #include <iostream>
22 
23 namespace o2
24 {
26 namespace monitoring
27 {
28 
30 enum class Severity { Error = 31,
31  Warn = 33,
32  Info = 49 };
33 
35 class MonLogger
36 {
37  public:
41  template <typename T>
42  MonLogger& operator<<(const T& log)
43  {
44  mStream << log;
45  return *this;
46  }
47 
52  static MonLogger& Get(Severity severity = Severity::Info)
53  {
54  static MonLogger loggerInstance;
55  loggerInstance.setDate();
56  loggerInstance.setSeverity(severity);
57  return loggerInstance;
58  }
59 
62  static auto End() -> decltype("\033[0m\n")
63  {
64  return "\033[0m\n";
65  }
66 
67  private:
69  std::ostream& mStream;
70 
72  MonLogger(std::ostream& oStream = std::cout) : mStream(oStream)
73  {
74  }
75 
77  void setDate()
78  {
79  auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
80  mStream << std::put_time(std::localtime(&now), "%Y-%m-%d %X") << "\t";
81  }
82 
85  void setSeverity(Severity severity)
86  {
87  mStream << "\033[1;" << static_cast<int>(severity) << "m";
88  }
89 
91  MonLogger& operator=(const MonLogger&) = delete;
92  MonLogger(const MonLogger&) = delete;
93  MonLogger(MonLogger&&) = delete;
94  MonLogger& operator=(MonLogger&&) = delete;
95 };
96 
97 } // namespace monitoring
98 } // namespace o2
99 
100 #endif //MONITORING_MONINFOLOGGER_H
static auto End() -> decltype("\033[0m\n")
Definition: MonLogger.h:62
Definition: Backend.h:23
Simple Monitoring logging class.
Definition: MonLogger.h:35
Severity
List of possible log severities.
Definition: MonLogger.h:30
MonLogger & operator<<(const T &log)
Definition: MonLogger.h:42
static MonLogger & Get(Severity severity=Severity::Info)
Definition: MonLogger.h:52