Logo ROOT   6.12/07
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
6 /// is welcome!
7 
8 /*************************************************************************
9  * Copyright (C) 1995-2015, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 #ifndef ROOT7_TLog
17 #define ROOT7_TLog
18 
19 #include <array>
20 #include <memory>
21 #include <sstream>
22 #include "RStringView.h"
23 #include <vector>
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 class TLogManager: public TLogHandler {
53 private:
54  std::vector<std::unique_ptr<TLogHandler>> fHandlers;
55 
56  /// Initialize taking a TLogHandlerDefault.
57  TLogManager(std::unique_ptr<TLogHandler> &&lh) { fHandlers.emplace_back(std::move(lh)); }
58 
59 public:
60  static TLogManager &Get();
61 
62  /// Add a TLogHandler in the front - to be called before all others.
63  void PushFront(std::unique_ptr<TLogHandler> handler) { fHandlers.insert(fHandlers.begin(), std::move(handler)); }
64 
65  /// Add a TLogHandler in the back - to be called after all others.
66  void PushBack(std::unique_ptr<TLogHandler> handler) { fHandlers.emplace_back(std::move(handler)); }
67 
68  // Emit a `TLogEntry` to the TLogHandlers.
69  // Returns false if further emission of this Log should be suppressed.
70  bool Emit(const TLogEntry &entry) override
71  {
72  for (auto &&handler: fHandlers)
73  if (!handler->Emit(entry))
74  return false;
75  return true;
76  }
77 };
78 
79 class TLogEntry: public std::ostringstream {
80 public:
81  std::string fGroup;
82  std::string fFile;
83  std::string fFuncName;
84  int fLine = 0;
86 
87 public:
88  TLogEntry() = default;
89  TLogEntry(ELogLevel level, std::string_view group): fGroup(group), fLevel(level) {}
91  : fGroup(group), fFile(filename), fFuncName(funcname), fLine(line), fLevel(level)
92  {}
93 
94  TLogEntry &SetFile(const std::string &file)
95  {
96  fFile = file;
97  return *this;
98  }
99  TLogEntry &SetFunction(const std::string &func)
100  {
101  fFuncName = func;
102  return *this;
103  }
105  {
106  fLine = line;
107  return *this;
108  }
109 
111 };
112 
113 } // namespace Experimental
114 } // namespace ROOT
115 
116 #if defined(_MSC_VER)
117 #define R__LOG_PRETTY_FUNCTION __FUNCSIG__
118 #else
119 #define R__LOG_PRETTY_FUNCTION __PRETTY_FUNCTION__
120 #endif
121 
122 #define R__LOG_HERE(LEVEL, GROUP) \
123  ROOT::Experimental::TLogEntry(LEVEL, GROUP).SetFile(__FILE__).SetLine(__LINE__).SetFunction(R__LOG_PRETTY_FUNCTION)
124 
125 #define R__FATAL_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kFatal, GROUP)
126 #define R__ERROR_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kError, GROUP)
127 #define R__WARNING_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kWarning, GROUP)
128 #define R__INFO_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kInfo, GROUP)
129 #define R__DEBUG_HERE(GROUP) R__LOG_HERE(ROOT::Experimental::ELogLevel::kDebug, GROUP)
130 
131 #endif
TLogEntry & SetLine(int line)
Definition: TLogger.hxx:104
basic_string_view< char > string_view
Definition: RStringView.h:35
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
TLogEntry(ELogLevel level, std::string_view group)
Definition: TLogger.hxx:89
TLine * line
void PushBack(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the back - to be called after all others.
Definition: TLogger.hxx:66
void PushFront(std::unique_ptr< TLogHandler > handler)
Add a TLogHandler in the front - to be called before all others.
Definition: TLogger.hxx:63
TLogEntry(ELogLevel level, std::string_view group, std::string_view filename, int line, std::string_view funcname)
Definition: TLogger.hxx:90
bool Emit(const TLogEntry &entry) override
Definition: TLogger.hxx:70
TLogEntry & SetFile(const std::string &file)
Definition: TLogger.hxx:94
TLogManager(std::unique_ptr< TLogHandler > &&lh)
Initialize taking a TLogHandlerDefault.
Definition: TLogger.hxx:57
Informational messages; used for instance for tracing.
static TLogManager & Get()
Definition: TLogger.cxx:47
std::vector< std::unique_ptr< TLogHandler > > fHandlers
Definition: TLogger.hxx:54
Warnings about likely unexpected behavior.
Abstract TLogHandler base class.
Definition: TLogger.hxx:45
Definition: file.py:1
TLogEntry & SetFunction(const std::string &func)
Definition: TLogger.hxx:99
Debug information; only useful for developers.
ELogLevel
Kinds of diagnostics.
Definition: TLogger.hxx:31