ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include <ChimeraTK/cppext/future_queue.hpp>
6
7#include <boost/thread.hpp>
8
9#include <sstream>
10
11namespace ChimeraTK {
12
13 /********************************************************************************************************************/
14
15 class Logger {
16 public:
20 static Logger& getInstance();
21
27 enum class Severity { trace, debug, info, warning, error };
28
34 void setMinSeverity(Severity minSeverity) { _minSeverity = minSeverity; }
35
39 class StreamProxy : public std::ostream {
40 public:
41 StreamProxy(Logger* logger, Severity severity, std::string context);
42 ~StreamProxy() override;
43
44 private:
45 std::stringbuf _buf;
46 Severity _severity;
47 std::string _context;
48 Logger& _logger;
49 };
50
69 StreamProxy getStream(Severity severity, std::string context);
70
71 ~Logger();
72
73 private:
74 Logger() = default;
75
81 static std::shared_ptr<Logger>& getSharedPtr();
82
89 void log(Severity severity, std::string context, std::string message) noexcept;
90
91 // The mainLoop() is executed in a dedicated thread waits for incoming log messages and prints them
92 void mainLoop();
93
94 // Convert Severity to string
95 static std::string severityToString(Severity severity);
96
97 // Struct with all arguments of log(), to be placed on to the _messageQueue
98 struct LogMessage {
99 Severity severity{Severity::info};
100 std::string context;
101 std::string text;
102 };
103
104 // This queue is filled in log() and read in mainLoop()
105 cppext::future_queue<LogMessage> _messageQueue{10};
106
107 // Minimum severity to be sent to the queue. This allows filtering lower severity messages at sender side, even
108 // before the message text has been (fully) composed.
109 std::atomic<Severity> _minSeverity{Severity::info};
110
111 // Thread executing mainLoop().
112 // Note: The thread must be started only after all other data members have been initialised, so this line must
113 // come last (or the thread must be only started in the constructor body).
114 boost::thread _mainLoopThread{[this] { mainLoop(); }};
115
116 friend class Application;
117 };
118
119 /********************************************************************************************************************/
120
124 inline Logger::StreamProxy logger(Logger::Severity severity, std::string context) {
125 return Logger::getInstance().getStream(severity, std::move(context));
126 }
127
128 /********************************************************************************************************************/
129 /********************************************************************************************************************/
130
132 return *getSharedPtr();
133 }
134
135 /********************************************************************************************************************/
136
137 inline std::shared_ptr<Logger>& Logger::getSharedPtr() {
138 static std::shared_ptr<Logger> instance(new Logger());
139 return instance;
140 }
141
142 /********************************************************************************************************************/
143
144} // namespace ChimeraTK
Proxy for output stream, handed out to the log sources by the Logger::Module.
Definition Logger.h:39
Severity
Severity levels used by the Logger.
Definition Logger.h:27
void setMinSeverity(Severity minSeverity)
Set the minimum severity level to be passed to the logger.
Definition Logger.h:34
StreamProxy getStream(Severity severity, std::string context)
Return an output stream object for the given severity.
Definition Logger.cc:43
static Logger & getInstance()
Obtain global instance of Logger singleton.
Definition Logger.h:131
InvalidityTracer application module.
Logger::StreamProxy logger(Logger::Severity severity, std::string context)
Convenience function to obtain the logger stream.
Definition Logger.h:124