Elements 6.3.3
A C++ base framework for the Euclid Software.
Loading...
Searching...
No Matches
Logging.cpp
Go to the documentation of this file.
1
20
22
23#include <iostream> // for cerr
24#include <map> // for map, operator!=, _Rb_tree_const_iterator
25#include <memory> // for unique_ptr, make_unique
26#include <sstream> // for basic_ostream, operator<<, endl, stringstream
27#include <string> // for char_traits, allocator, basic_string, operator<, operator<<, string
28#include <utility> // for pair
29
30#include <boost/algorithm/string.hpp> // for to_upper
31
32#include <log4cpp/Category.hh> // for Category
33#include <log4cpp/FileAppender.hh> // for FileAppender
34#include <log4cpp/OstreamAppender.hh> // for OstreamAppender
35#include <log4cpp/PatternLayout.hh> // for PatternLayout
36#include <log4cpp/Priority.hh> // for Priority, Priority::INFO, Priority::DEBUG, Priority::ERROR, Priority::FATAL, Priority::WARN, Priority::NOTSET
37
38#include "ElementsKernel/Compat.h" // for NON_REDUNDANT_MOVE
39#include "ElementsKernel/Exception.h" // for Exception
40#include "ElementsKernel/Memory.h" // for make_unique
41#include "ElementsKernel/Path.h" // for Item
42
43namespace log4cpp {
44class Layout;
45}
46
47using log4cpp::Category;
48using log4cpp::Layout;
49using log4cpp::Priority;
50using std::string;
51using std::unique_ptr;
52
53namespace Elements {
54
55// clang-format off
56
58 {"FATAL", Priority::FATAL},
59 {"ERROR", Priority::ERROR},
60 {"WARN", Priority::WARN},
61 {"INFO", Priority::INFO},
62 {"DEBUG", Priority::DEBUG}
63};
64
65// clang-format on
66
69 layout->setConversionPattern("%d{%FT%T%Z} %c %5p : %m%n");
70 return NON_REDUNDANT_MOVE(layout);
71}
72
73Logging::Logging(Category& log4cppLogger) : m_log4cppLogger(log4cppLogger) {}
74
75Logging Logging::getLogger(const string& name) {
76 if (Category::getRoot().getAppender("console") == nullptr) {
77 auto* consoleAppender = new log4cpp::OstreamAppender{"console", &std::cerr};
78 consoleAppender->setLayout(getLogLayout().release());
79 Category::getRoot().addAppender(consoleAppender);
80 if (Category::getRoot().getPriority() == Priority::NOTSET) {
81 Category::setRootPriority(Priority::INFO);
82 }
83 }
84 return Logging{Category::getInstance(name)};
85}
86
87void Logging::setLevel(string level) {
88 boost::to_upper(level);
89 auto it = LOG_LEVEL.find(level);
90 if (it != LOG_LEVEL.end()) {
91 Category::setRootPriority(it->second);
92 } else {
93 std::stringstream error_buffer;
94 error_buffer << "Unrecognized logging level: " << level << std::endl;
95 throw Exception(error_buffer.str());
96 }
97}
98
99void Logging::setLogFile(const Path::Item& fileName) {
100 Category& root = Category::getRoot();
101 root.removeAppender(root.getAppender("file"));
102 if (fileName.has_filename()) {
103 auto* fileAppender = new log4cpp::FileAppender("file", fileName.string());
104 fileAppender->setLayout(getLogLayout().release());
105 root.addAppender(fileAppender);
106 }
107 root.setPriority(root.getPriority());
108}
109
110void Logging::debug(const std::string& logMessage) {
111 m_log4cppLogger.debug(logMessage);
112}
113
117
118void Logging::info(const std::string& logMessage) {
119 m_log4cppLogger.info(logMessage);
120}
121
125
126void Logging::warn(const std::string& logMessage) {
127 m_log4cppLogger.warn(logMessage);
128}
129
133
134void Logging::error(const std::string& logMessage) {
135 m_log4cppLogger.error(logMessage);
136}
140
141void Logging::fatal(const std::string& logMessage) {
142 m_log4cppLogger.fatal(logMessage);
143}
144
148
149void Logging::log(log4cpp::Priority::Value level, const std::string& logMessage) {
150 m_log4cppLogger.log(level, logMessage);
151}
152
154Logging::LogMessageStream::LogMessageStream(log4cpp::Category& logger, P_log_func log_func)
155 : m_logger(logger), m_log_func{log_func} {}
157
159 : m_logger(other.m_logger), m_log_func{other.m_log_func} {}
160
163
167
168} // 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:44
A helper class for logging messages using the "<<" operator.
Definition Logging.h:275
LogMessageStream(log4cpp::Category &logger, P_log_func log_func)
log4cpp::Category & m_logger
Definition Logging.h:291
static Logging getLogger(const std::string &name="")
Definition Logging.cpp:75
LogMessageStream fatal()
Definition Logging.cpp:145
static void setLogFile(const Path::Item &fileName)
Sets the file to store the log messages.
Definition Logging.cpp:99
LogMessageStream error()
Definition Logging.cpp:137
LogMessageStream debug()
Definition Logging.cpp:114
static void setLevel(std::string level)
Sets the global message level.
Definition Logging.cpp:87
log4cpp::Category & m_log4cppLogger
Definition Logging.h:263
void log(log4cpp::Priority::Value level, const std::string &logMessage)
Definition Logging.cpp:149
Logging(log4cpp::Category &log4cppLogger)
Definition Logging.cpp:73
LogMessageStream info()
Definition Logging.cpp:122
LogMessageStream warn()
Definition Logging.cpp:130
T endl(T... args)
#define NON_REDUNDANT_MOVE(x)
Definition Compat.h:38
ELEMENTS_API std::unique_ptr< T > make_unique(Args &&... args)
Constructs an object of type T and wraps it in a std::unique_ptr using args as the parameter list for...
Definition Memory.h:55
unique_ptr< Layout > getLogLayout()
Definition Logging.cpp:67
static const std::map< string, const int > LOG_LEVEL
Definition Logging.cpp:57
T str(T... args)