Logo ROOT   6.10/09
Reference Guide
TLogger.hxx
Go to the documentation of this file.
1 /// \file ROOT/TLogger.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 "RStringView.h"
22 #include <vector>
23 
24 
25 namespace ROOT {
26 namespace Experimental {
27 
28  /**
29  Kinds of diagnostics.
30  */
31  enum class ELogLevel {
32  kDebug, ///< Debug information; only useful for developers
33  kInfo, ///< Informational messages; used for instance for tracing
34  kWarning, ///< Warnings about likely unexpected behavior
35  kError,
36  kFatal
37  };
38 
39  class TLogEntry;
40 
41  /**
42  Abstract TLogHandler base class. ROOT logs everything from info to error
43  to entities of this class.
44  */
45  class TLogHandler {
46  public:
47  virtual ~TLogHandler();
48  // Returns false if further emission of this Log should be suppressed.
49  virtual bool Emit(const TLogEntry& entry) = 0;
50  };
51 
52 
53  class TLogManager: public TLogHandler {
54  private:
55  std::vector<std::unique_ptr<TLogHandler>> fHandlers;
56 
57  /// Initialize taking a TLogHandlerDefault.
58  TLogManager(std::unique_ptr<TLogHandler>&& lh) {
59  fHandlers.emplace_back(std::move(lh));
60  }
61 
62  public:
63  static TLogManager& Get();
64 
65  /// Add a TLogHandler in the front - to be called before all others.
66  void PushFront(std::unique_ptr<TLogHandler> handler) {
67  fHandlers.insert(fHandlers.begin(), std::move(handler));
68  }
69 
70  /// Add a TLogHandler in the back - to be called after all others.
71  void PushBack(std::unique_ptr<TLogHandler> handler) {
72  fHandlers.emplace_back(std::move(handler));
73  }
74 
75  // Emit a `TLogEntry` to the TLogHandlers.
76  // Returns false if further emission of this Log should be suppressed.
77  bool Emit(const TLogEntry& entry) override {
78  for (auto&& handler: fHandlers)
79  if (!handler->Emit(entry))
80  return false;
81  return true;
82  }
83  };
84 
85 
86  class TLogEntry: public std::ostringstream {
87  public:
88  std::string fGroup;
89  std::string fFile;
90  std::string fFuncName;
91  int fLine = 0;
93 
94  public:
95  TLogEntry() = default;
96  TLogEntry(ELogLevel level, std::string_view group):
97  fGroup(group), fLevel(level) {}
98  TLogEntry(ELogLevel level, std::string_view group, std::string_view filename,
99  int line, std::string_view funcname):
100  fGroup(group), fFile(filename), fFuncName(funcname), fLine(line),
101  fLevel(level) {}
102 
103  TLogEntry& SetFile(const std::string& file) { fFile = file; return *this; }
104  TLogEntry& SetFunction(const std::string& func) {
105  fFuncName = func;
106  return *this;
107  }
108  TLogEntry& SetLine(int line) { fLine = line; return *this; }
109 
111  TLogManager::Get().Emit(*this);
112  }
113  };
114 
115 } // namespace Experimental
116 } // namespace ROOT
117 
118 #define R__LOG_HERE(LEVEL, GROUP) \
119  ROOT::Experimental::TLogEntry(LEVEL, GROUP).SetFile(__FILE__).SetLine(__LINE__).SetFunction(__PRETTY_FUNCTION__)
120 
121 #define R__FATAL_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kFatal, GROUP)
122 #define R__ERROR_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kError, GROUP)
123 #define R__WARNING_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kWarning, GROUP)
124 #define R__INFO_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kInfo, GROUP)
125 #define R__DEBUG_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kDebug, GROUP)
126 
127 #endif
TLogEntry & SetLine(int line)
Definition: TLogger.hxx:108
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TLogEntry(ELogLevel level, std::string_view group)
Definition: TLogger.hxx:96
TLine * line
void PushBack(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the back - to be called after all others.
Definition: TLogger.hxx:71
void PushFront(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the front - to be called before all others.
Definition: TLogger.hxx:66
TLogEntry(ELogLevel level, std::string_view group, std::string_view filename, int line, std::string_view funcname)
Definition: TLogger.hxx:98
bool Emit(const TLogEntry &entry) override
Definition: TLogger.hxx:77
TLogEntry & SetFile(const std::string &file)
Definition: TLogger.hxx:103
TLogManager(std::unique_ptr< TLogHandler > &&lh)
Initialize taking a TLogHandlerDefault.
Definition: TLogger.hxx:58
Informational messages; used for instance for tracing.
static TLogManager & Get()
Definition: TLogger.cxx:50
std::vector< std::unique_ptr< TLogHandler > > fHandlers
Definition: TLogger.hxx:55
Warnings about likely unexpected behavior.
Abstract TLogHandler base class.
Definition: TLogger.hxx:45
double func(double *x, double *p)
Definition: stressTF1.cxx:213
Definition: file.py:1
TLogEntry & SetFunction(const std::string &func)
Definition: TLogger.hxx:104
Debug information; only useful for developers.
ELogLevel
Kinds of diagnostics.
Definition: TLogger.hxx:31