ROOT  6.06/09
Reference Guide
TLogger.h
Go to the documentation of this file.
1 /// \file TDirectory.h
2 /// \ingroup Base ROOT7
3 /// \author Axel Naumann <axel@cern.ch>
4 /// \date 2015-03-29
5 /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
6 
7 /*************************************************************************
8  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
9  * All rights reserved. *
10  * *
11  * For the licensing terms see $ROOTSYS/LICENSE. *
12  * For the list of contributors see $ROOTSYS/README/CREDITS. *
13  *************************************************************************/
14 
15 #ifndef ROOT7_TLog
16 #define ROOT7_TLog
17 
18 #include <array>
19 #include <memory>
20 #include <sstream>
21 #include <experimental/string_view>
22 #include <vector>
23 
24 namespace std {
26 }
27 
28 namespace ROOT {
29 
30  /**
31  Kinds of diagnostics.
32  */
33  enum class ELogLevel {
34  kDebug, ///< Debug information; only useful for developers
35  kInfo, ///< Informational messages; used for instance for tracing
36  kWarning, ///< Warnings about likely unexpected behavior
37  kError,
38  kFatal
39  };
40 
41  class TLogEntry;
42 
43  /**
44  Abstract TLogHandler base class. ROOT logs everything from info to error
45  to entities of this class.
46  */
47  class TLogHandler {
48  public:
49  virtual ~TLogHandler();
50  // Returns false if further emission of this Log should be suppressed.
51  virtual bool Emit(const TLogEntry& entry) = 0;
52  };
53 
54 
55  class TLogManager: public TLogHandler {
56  private:
57  std::vector<std::unique_ptr<TLogHandler>> fHandlers;
58 
59  /// Initialize taking a TLogHandlerDefault.
60  TLogManager(std::unique_ptr<TLogHandler>&& lh) {
61  fHandlers.emplace_back(std::move(lh));
62  }
63 
64  public:
65  static TLogManager& Get();
66 
67  /// Add a TLogHandler in the front - to be called before all others.
68  void PushFront(std::unique_ptr<TLogHandler> handler) {
69  fHandlers.insert(fHandlers.begin(), std::move(handler));
70  }
71 
72  /// Add a TLogHandler in the back - to be called after all others.
73  void PushBack(std::unique_ptr<TLogHandler> handler) {
74  fHandlers.emplace_back(std::move(handler));
75  }
76 
77  // Emit a `TLogEntry` to the TLogHandlers.
78  // Returns false if further emission of this Log should be suppressed.
79  bool Emit(const TLogEntry& entry) override {
80  for (auto&& handler: fHandlers)
81  if (!handler->Emit(entry))
82  return false;
83  return true;
84  }
85  };
86 
87 
88  class TLogEntry: public std::ostringstream {
89  public:
90  std::string fGroup;
91  std::string fFile;
92  std::string fFuncName;
93  int fLine = 0;
95 
96  public:
97  TLogEntry() = default;
98  TLogEntry(ELogLevel level, std::string_view group):
99  fGroup(group), fLevel(level) {}
100  TLogEntry(ELogLevel level, std::string_view group, std::string_view filename,
101  int line, std::string_view funcname):
102  fGroup(group), fFile(filename), fFuncName(funcname), fLine(line),
103  fLevel(level) {}
104 
105  TLogEntry& SetFile(const std::string& file) { fFile = file; return *this; }
106  TLogEntry& SetFunction(const std::string& func) {
107  fFuncName = func;
108  return *this;
109  }
110  TLogEntry& SetLine(int line) { fLine = line; return *this; }
111 
113  TLogManager::Get().Emit(*this);
114  }
115  };
116 
117 
118 } // namespace ROOT
119 
120 #define R__LOG_HERE(LEVEL, GROUP) \
121  TLogEntry(LEVEL, GROUP).SetFile(__FILE__).SetLine(__LINE__).SetFunction(__PRETTY_FUNCTION__)
122 
123 
124 #define R__ERROR_HERE(GROUP) R__LOG_HERE(ELogLevel::kError, GROUP)
125 #define R__ERROR_HERE(GROUP) R__LOG_HERE(ELogLevel::kError, GROUP)
126 
127 #endif
std::string fFuncName
Definition: TLogger.h:92
ELogLevel fLevel
Definition: TLogger.h:94
Informational messages; used for instance for tracing.
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
TLine * line
std::string fFile
Definition: TLogger.h:91
TLogManager(std::unique_ptr< TLogHandler > &&lh)
Initialize taking a TLogHandlerDefault.
Definition: TLogger.h:60
static TLogManager & Get()
Definition: TLogger.cxx:50
void PushBack(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the back - to be called after all others.
Definition: TLogger.h:73
std::vector< std::unique_ptr< TLogHandler > > fHandlers
Definition: TLogger.h:57
static const char * filename()
TLogEntry & SetFile(const std::string &file)
Definition: TLogger.h:105
STL namespace.
TLogEntry()=default
TLogEntry(ELogLevel level, std::string_view group, std::string_view filename, int line, std::string_view funcname)
Definition: TLogger.h:100
TLogEntry(ELogLevel level, std::string_view group)
Definition: TLogger.h:98
basic_string_view< char > string_view
TLogEntry & SetFunction(const std::string &func)
Definition: TLogger.h:106
std::string fGroup
Definition: TLogger.h:90
Long64_t entry
virtual bool Emit(const TLogEntry &entry)=0
virtual ~TLogHandler()
Definition: TLogger.cxx:19
bool Emit(const TLogEntry &entry) override
Definition: TLogger.h:79
Debug information; only useful for developers.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
ELogLevel
Kinds of diagnostics.
Definition: TLogger.h:33
Abstract TLogHandler base class.
Definition: TLogger.h:47
TLogEntry & SetLine(int line)
Definition: TLogger.h:110
void PushFront(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the front - to be called before all others.
Definition: TLogger.h:68
Warnings about likely unexpected behavior.