Mantid
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <Poco/Logger.h>
10#include <Poco/NullStream.h>
11
12#include <algorithm>
13#include <exception>
14#include <iostream>
15#include <sstream>
16
17namespace Mantid::Kernel {
18namespace {
19// We only need a single NullStream object
20Poco::NullOutputStream NULL_STREAM;
21} // namespace
22
23static const std::string PriorityNames_data[] = {"NOT_USED", "PRIO_FATAL", "PRIO_CRITICAL",
24 "PRIO_ERROR", "PRIO_WARNING", "PRIO_NOTICE",
25 "PRIO_INFORMATION", "PRIO_DEBUG", "PRIO_TRACE"};
26const std::string *Logger::PriorityNames = PriorityNames_data;
27
31Logger::Logger(const std::string &name)
32 : m_log(&Poco::Logger::get(name)), m_logStream(std::make_unique<ThreadSafeLogStream>(*m_log)), m_levelOffset(0),
33 m_enabled(true) {}
34
38void Logger::setName(const std::string &name) {
39 auto *logger = &Poco::Logger::get(name);
40 auto logStream = std::make_unique<ThreadSafeLogStream>(*logger); // don't swap if this throws
41
42 using std::swap;
43 swap(m_log, logger);
44 swap(m_logStream, logStream);
45}
46
52bool Logger::getEnabled() const { return m_enabled; }
53
59void Logger::setEnabled(const bool enabled) { m_enabled = enabled; }
60
68void Logger::fatal(const std::string &msg) { log(msg, Poco::Message::PRIO_FATAL); }
69
77void Logger::error(const std::string &msg) { log(msg, Poco::Message::PRIO_ERROR); }
78
86void Logger::warning(const std::string &msg) { log(msg, Poco::Message::PRIO_WARNING); }
87
95void Logger::notice(const std::string &msg) { log(msg, Poco::Message::PRIO_NOTICE); }
96
105void Logger::information(const std::string &msg) { log(msg, Poco::Message::PRIO_INFORMATION); }
106
114void Logger::debug(const std::string &msg) { log(msg, Poco::Message::PRIO_DEBUG); }
115
129void Logger::dump(const std::string &msg, const void *buffer, std::size_t length) {
130 if (m_enabled) {
131 try {
132 m_log->dump(msg, buffer, length);
133 } catch (std::exception &e) {
134 // failures in logging are not allowed to throw exceptions out of the
135 // logging class
136 std::cerr << e.what();
137 }
138 }
139}
140
146bool Logger::is(int level) const {
147 bool retVal = false;
148 try {
149 retVal = m_log->is(level);
150 } catch (std::exception &e) {
151 // failures in logging are not allowed to throw exceptions out of the
152 // logging class
153 std::cerr << e.what();
154 }
155 return retVal;
156}
157
158void Logger::setLevel(int level) {
159 try {
160 m_log->setLevel(level);
161 } catch (std::exception &e) {
162 // failures in logging are not allowed to throw exceptions out of the
163 // logging class
164 std::cerr << e.what();
165 }
166}
167
172void Logger::setLevel(const std::string &level) {
173 try {
174 m_log->setLevel(level);
175 } catch (std::exception &e) {
176 // failures in logging are not allowed to throw exceptions out of the
177 // logging class
178 std::cerr << e.what();
179 }
180}
181
182int Logger::getLevel() const { return m_log->getLevel(); }
183
191std::ostream &Logger::fatal() { return getLogStream(Priority::PRIO_FATAL); }
192
200std::ostream &Logger::error() { return getLogStream(Priority::PRIO_ERROR); }
201
210std::ostream &Logger::warning() { return getLogStream(Priority::PRIO_WARNING); }
211
220std::ostream &Logger::notice() { return getLogStream(Priority::PRIO_NOTICE); }
221
230std::ostream &Logger::information() { return getLogStream(Priority::PRIO_INFORMATION); }
231
239std::ostream &Logger::debug() { return getLogStream(Priority::PRIO_DEBUG); }
240
245void Logger::accumulate(const std::string &msg) { m_logStream->accumulate(msg); }
246
251
256void Logger::flush(Priority priority) { log(m_logStream->flush(), priority); }
257
259void Logger::flushDebug() { flush(Poco::Message::PRIO_DEBUG); }
260
262void Logger::flushInformation() { flush(Poco::Message::PRIO_INFORMATION); }
263
265void Logger::flushNotice() { flush(Poco::Message::PRIO_NOTICE); }
266
268void Logger::flushWarning() { flush(Poco::Message::PRIO_WARNING); }
269
271void Logger::flushError() { flush(Poco::Message::PRIO_ERROR); }
272
274void Logger::flushFatal() { flush(Poco::Message::PRIO_FATAL); }
275
277void Logger::purge() { m_logStream->flush(); }
278
283 try {
284 // Release the POCO loggers
285 Poco::Logger::shutdown();
286 } catch (std::exception &e) {
287 // failures in logging are not allowed to throw exceptions out of the
288 // logging class
289 std::cerr << e.what();
290 }
291}
292
297void Logger::setLevelForAll(const int level) {
298 // "" is the root logger
299 Poco::Logger::setLevel("", level);
300}
301
306void Logger::log(const std::string &message, const Logger::Priority &priority) {
307 if (!m_enabled)
308 return;
309
310 try {
311 switch (applyLevelOffset(priority)) {
312 case Poco::Message::PRIO_FATAL:
313 m_log->fatal(message);
314 break;
315 case Poco::Message::PRIO_CRITICAL:
316 m_log->critical(message);
317 break;
318 case Poco::Message::PRIO_ERROR:
319 m_log->error(message);
320 break;
321 case Poco::Message::PRIO_WARNING:
322 m_log->warning(message);
323 break;
324 case Poco::Message::PRIO_NOTICE:
325 m_log->notice(message);
326 break;
327 case Poco::Message::PRIO_INFORMATION:
328 m_log->information(message);
329 break;
330 case Poco::Message::PRIO_DEBUG:
331 m_log->debug(message);
332 break;
333 case Poco::Message::PRIO_TRACE:
334 m_log->trace(message);
335 break;
336 default:
337 break;
338 }
339 } catch (std::exception &e) {
340 // Failures in logging are not allowed to throw exceptions out of the
341 // logging class
342 std::cerr << "Error in logging framework: " << e.what();
343 }
344}
345
351std::ostream &Logger::getLogStream(const Logger::Priority &priority) {
352 if (!m_enabled)
353 return NULL_STREAM;
354
355 switch (applyLevelOffset(priority)) {
356 case Poco::Message::PRIO_FATAL:
357 return m_logStream->fatal();
358 break;
359 case Poco::Message::PRIO_CRITICAL:
360 return m_logStream->critical();
361 break;
362 case Poco::Message::PRIO_ERROR:
363 return m_logStream->error();
364 break;
365 case Poco::Message::PRIO_WARNING:
366 return m_logStream->warning();
367 break;
368 case Poco::Message::PRIO_NOTICE:
369 return m_logStream->notice();
370 break;
371 case Poco::Message::PRIO_INFORMATION:
372 return m_logStream->information();
373 break;
374 case Poco::Message::PRIO_DEBUG:
375 return m_logStream->debug();
376 break;
377 default:
378 return NULL_STREAM;
379 }
380}
381
388 int retVal = proposedLevel;
389 // fast exit if offset is 0
390 if (m_levelOffset == 0) {
391 return proposedLevel;
392 } else {
393 retVal += m_levelOffset;
394 if (retVal < static_cast<int>(Priority::PRIO_FATAL)) {
395 retVal = Priority::PRIO_FATAL;
396 } else if (retVal > static_cast<int>(Priority::PRIO_TRACE)) {
397 retVal = Priority::PRIO_TRACE;
398 }
399 }
400 // Logger::Priority p(retVal);
401 return static_cast<Logger::Priority>(retVal);
402}
403
408void Logger::setLevelOffset(int level) { m_levelOffset = level; }
409
415
416} // namespace Mantid::Kernel
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void flushInformation()
flushes the accumulated message with information priority
Definition: Logger.cpp:262
void flushFatal()
flushes the accumulated message with fatal priority
Definition: Logger.cpp:274
void dump(const std::string &msg, const void *buffer, std::size_t length)
Logs the given message at debug level, followed by the data in buffer.
Definition: Logger.cpp:129
static const std::string * PriorityNames
Definition: Logger.h:57
void setName(const std::string &name)
Update the name of the logger.
Definition: Logger.cpp:38
std::ostream & error()
Logs at error level.
Definition: Logger.cpp:200
int getLevelOffset() const
Gets the Logger's log offset level.
Definition: Logger.cpp:414
static void setLevelForAll(const int level)
Sets the log level for all Loggers created so far, including the root logger.
Definition: Logger.cpp:297
bool m_enabled
The state of this logger, disabled loggers send no messages.
Definition: Logger.h:162
static void shutdown()
Shuts down the logging framework and releases all Loggers.
Definition: Logger.cpp:282
void flushDebug()
flushes the accumulated message with debug priority
Definition: Logger.cpp:259
Priority applyLevelOffset(Priority proposedLevel)
Return a log stream set with the given priority.
Definition: Logger.cpp:387
void setEnabled(const bool enabled)
set if the logging is enabled
Definition: Logger.cpp:59
void flushError()
flushes the accumulated message with error priority
Definition: Logger.cpp:271
Poco::Message::Priority Priority
Definition: Logger.h:55
void log(const std::string &message, const Priority &priority)
Log a message at a given priority.
Definition: Logger.cpp:306
void setLevel(int level)
Sets the Logger's log level.
Definition: Logger.cpp:158
std::ostream & getLogStream(const Priority &priority)
gets the correct log stream for a priority
Definition: Logger.cpp:351
Poco::Logger * m_log
Internal handle to third party logging objects.
Definition: Logger.h:155
int getLevel() const
Returns the Logger's log level.
Definition: Logger.cpp:182
bool getEnabled() const
returns true if the log is enabled
Definition: Logger.cpp:52
std::ostream & warning()
Logs at warning level.
Definition: Logger.cpp:210
bool is(int level) const
Returns true if at least the given log level is set.
Definition: Logger.cpp:146
std::ostream & debug()
Logs at debug level.
Definition: Logger.cpp:239
void setLevelOffset(int level)
Sets the Logger's log offset level.
Definition: Logger.cpp:408
std::ostream & notice()
Logs at notice level.
Definition: Logger.cpp:220
std::ostream & information()
Logs at information level.
Definition: Logger.cpp:230
std::unique_ptr< ThreadSafeLogStream > m_logStream
Allows stream operators for a logger.
Definition: Logger.h:157
std::ostream & fatal()
Logs at Fatal level.
Definition: Logger.cpp:191
void purge()
flushes the accumulated messages without logging them
Definition: Logger.cpp:277
void flush()
flushes accumulated messages to the current level
Definition: Logger.cpp:250
void flushWarning()
flushes the accumulated message with warning priority
Definition: Logger.cpp:268
int m_levelOffset
The offset of the logger.
Definition: Logger.h:160
void flushNotice()
flushes the accumulated message with notice priority
Definition: Logger.cpp:265
void accumulate(const std::string &msg)
accumulates a message
Definition: Logger.cpp:245
The main log stream class implementing an ostream interface to a Logger.
static const std::string PriorityNames_data[]
Definition: Logger.cpp:23
void swap(MultiFileValidator &obj1, MultiFileValidator &obj2)
Definition: Algorithm.h:30
STL namespace.