Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RLogger.cxx
Go to the documentation of this file.
1/// \file RLogger.cxx
2/// \ingroup Base ROOT7
3/// \author Axel Naumann <axel@cern.ch>
4/// \date 2015-07-07
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-2020, 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#include "ROOT/RLogger.hxx"
17
18#include "TError.h"
19
20#include <algorithm>
21#include <array>
22#include <memory>
23#include <vector>
24
25// pin vtable
27
28namespace {
29class RLogHandlerDefault : public ROOT::RLogHandler {
30public:
31 // Returns false if further emission of this log entry should be suppressed.
32 bool Emit(const ROOT::RLogEntry &entry) override;
33};
34
35inline bool RLogHandlerDefault::Emit(const ROOT::RLogEntry &entry)
36{
37 constexpr static int numLevels = static_cast<int>(ROOT::ELogLevel::kDebug) + 1;
38 int cappedLevel = std::min(static_cast<int>(entry.fLevel), numLevels - 1);
39 constexpr static std::array<const char *, numLevels> sTag{
40 {"{unset-error-level please report}", "FATAL", "Error", "Warning", "Info", "Debug"}};
41
42 std::stringstream strm;
43 auto channel = entry.fChannel;
44 if (channel && !channel->GetName().empty())
45 strm << '[' << channel->GetName() << "] ";
47
48 if (!entry.fLocation.fFile.empty())
49 strm << " " << entry.fLocation.fFile << ':' << entry.fLocation.fLine;
50 if (!entry.fLocation.fFuncName.empty())
51 strm << " in " << entry.fLocation.fFuncName;
52
53 static constexpr const int errorLevelOld[] = {kFatal /*unset*/, kFatal, kError, kWarning, kInfo, kInfo /*debug*/};
55 entry.fMessage.c_str());
56 return true;
57}
58} // unnamed namespace
59
61{
62 static RLogManager instance(std::make_unique<RLogHandlerDefault>());
63 return instance;
64}
65
66std::unique_ptr<ROOT::RLogHandler> ROOT::RLogManager::Remove(RLogHandler *handler)
67{
68 auto iter = std::find_if(fHandlers.begin(), fHandlers.end(), [&](const std::unique_ptr<RLogHandler> &handlerPtr) {
69 return handlerPtr.get() == handler;
70 });
71 if (iter != fHandlers.end()) {
72 std::unique_ptr<RLogHandler> ret;
73 swap(*iter, ret);
74 fHandlers.erase(iter);
75 return ret;
76 }
77 return {};
78}
79
81{
82 auto channel = entry.fChannel;
83
84 Increment(entry.fLevel);
85 if (channel != this)
86 channel->Increment(entry.fLevel);
87
88 // Is there a specific level for the channel? If so, take that,
89 // overruling the global one.
90 if (channel->GetEffectiveVerbosity(*this) < entry.fLevel)
91 return true;
92
93 // Lock-protected extraction of handlers, such that they don't get added during the
94 // handler iteration.
95 std::vector<RLogHandler *> handlers;
96
97 {
98 std::lock_guard<std::mutex> lock(fMutex);
99
100 handlers.resize(fHandlers.size());
101 std::transform(fHandlers.begin(), fHandlers.end(), handlers.begin(),
102 [](const std::unique_ptr<RLogHandler> &handlerUPtr) { return handlerUPtr.get(); });
103 }
104
105 for (auto &&handler : handlers)
106 if (!handler->Emit(entry))
107 return false;
108 return true;
109}
static Roo_reg_AGKInteg1D instance
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
ErrorHandlerFunc_t GetErrorHandler()
Returns the current error handler function.
Definition TError.cxx:100
A diagnostic that can be emitted by the RLogManager.
Definition RLogger.hxx:175
Abstract RLogHandler base class.
Definition RLogger.hxx:82
virtual ~RLogHandler()
Definition RLogger.cxx:26
A RLogHandler that multiplexes diagnostics to different client RLogHandlers and keeps track of the su...
Definition RLogger.hxx:133
std::unique_ptr< RLogHandler > Remove(RLogHandler *handler)
Remove and return the given log handler. Returns nullptr if not found.
Definition RLogger.cxx:66
bool Emit(const RLogEntry &entry) override
Emit a log entry.
Definition RLogger.cxx:80
static RLogManager & Get()
Definition RLogger.cxx:60
@ kInfo
Informational messages; used for instance for tracing.
@ kDebug
Debug information; only useful for developers; can have added verbosity up to 255-kDebug.
@ kError
An error.
@ kFatal
An error which causes further processing to be unreliable.
@ kWarning
Warnings about likely unexpected behavior.