Elements 6.2.1
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Logging.cpp
Go to the documentation of this file.
1
21#include "ElementsKernel/Logging.h" // for Logging, etc
22
23#include <iostream> // for operator<<, stringstream, etc
24#include <map> // for map
25#include <memory> // for unique_ptr
26#include <sstream> // for stringstream
27#include <string> // for char_traits, string
28
29#include <boost/algorithm/string/case_conv.hpp> // for to_upper
30
31#include <log4cpp/Category.hh> // for Category
32#include <log4cpp/FileAppender.hh> // for FileAppender
33#include <log4cpp/OstreamAppender.hh> // for OstreamAppender
34#include <log4cpp/PatternLayout.hh> // for PatternLayout
35#include <log4cpp/Priority.hh> // for Priority, Priority::::INFO, etc
36
37#include "ElementsKernel/Compat.h" // NON_REDUNDANT_MOVE
38#include "ElementsKernel/Exception.h" // for Exception
39#include "ElementsKernel/Memory.h" // for make_unique
40#include "ElementsKernel/Path.h" // for Path::Item
41
42using log4cpp::Category;
43using log4cpp::Layout;
44using log4cpp::Priority;
45using std::string;
46using std::unique_ptr;
47
48namespace Elements {
49
50// clang-format off
51
53 {"FATAL", Priority::FATAL},
54 {"ERROR", Priority::ERROR},
55 {"WARN", Priority::WARN},
56 {"INFO", Priority::INFO},
57 {"DEBUG", Priority::DEBUG}
58};
59
60// clang-format on
61
63 auto layout = make_unique<log4cpp::PatternLayout>();
64 layout->setConversionPattern("%d{%FT%T%Z} %c %5p : %m%n");
65 return NON_REDUNDANT_MOVE(layout);
66}
67
68Logging::Logging(Category& log4cppLogger) : m_log4cppLogger(log4cppLogger) {}
69
70Logging Logging::getLogger(const string& name) {
71 if (Category::getRoot().getAppender("console") == nullptr) {
72 log4cpp::OstreamAppender* consoleAppender = new log4cpp::OstreamAppender{"console", &std::cerr};
73 consoleAppender->setLayout(getLogLayout().release());
74 Category::getRoot().addAppender(consoleAppender);
75 if (Category::getRoot().getPriority() == Priority::NOTSET) {
76 Category::setRootPriority(Priority::INFO);
77 }
78 }
79 return Logging{Category::getInstance(name)};
80}
81
82void Logging::setLevel(string level) {
83 boost::to_upper(level);
84 auto it = LOG_LEVEL.find(level);
85 if (it != LOG_LEVEL.end()) {
86 Category::setRootPriority(it->second);
87 } else {
88 std::stringstream error_buffer;
89 error_buffer << "Unrecognized logging level: " << level << std::endl;
90 throw Exception(error_buffer.str());
91 }
92}
93
94void Logging::setLogFile(const Path::Item& fileName) {
95 Category& root = Category::getRoot();
96 root.removeAppender(root.getAppender("file"));
97 if (fileName.has_filename()) {
98 log4cpp::FileAppender* fileAppender = new log4cpp::FileAppender("file", fileName.string());
99 fileAppender->setLayout(getLogLayout().release());
100 root.addAppender(fileAppender);
101 }
102 root.setPriority(root.getPriority());
103}
104
106Logging::LogMessageStream::LogMessageStream(Category& logger, P_log_func log_func)
107 : m_logger(logger), m_log_func{log_func} {}
109
111 : m_logger(other.m_logger), m_log_func{other.m_log_func} {}
112
114 : m_logger(other.m_logger), m_log_func{other.m_log_func} {}
115
117 (m_logger.*m_log_func)(m_message.str());
118}
119
120} // namespace Elements
This file is intended to iron out all the differences between different C++ standards.
defines the base Elements exception class
Logging facility.
provide functions to retrieve configuration files
provide functions to retrieve resources pointed by environment variables
Elements base exception class.
Definition Exception.h:47
A helper class for logging messages using the "<<" operator.
Definition Logging.h:309
LogMessageStream(log4cpp::Category &logger, P_log_func log_func)
Logging API of the Elements framework.
Definition Logging.h:93
static Logging getLogger(const std::string &name="")
Definition Logging.cpp:70
static void setLogFile(const Path::Item &fileName)
Sets the file to store the log messages.
Definition Logging.cpp:94
static void setLevel(std::string level)
Sets the global message level.
Definition Logging.cpp:82
Logging(log4cpp::Category &log4cppLogger)
Definition Logging.cpp:68
T end(T... args)
T endl(T... args)
#define NON_REDUNDANT_MOVE(x)
Definition Compat.h:38
unique_ptr< Layout > getLogLayout()
Definition Logging.cpp:62
static const std::map< string, const int > LOG_LEVEL
Definition Logging.cpp:52
T str(T... args)