Elements 6.1.2
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/Exception.h" // for Exception
38#include "ElementsKernel/Memory.h" // for make_unique
39#include "ElementsKernel/Path.h" // for Path::Item
40
41using log4cpp::Category;
42using log4cpp::Layout;
43using log4cpp::Priority;
44using std::string;
45using std::unique_ptr;
46
47namespace Elements {
48
49static const std::map<string, const int> LOG_LEVEL{{"FATAL", Priority::FATAL},
50 {"ERROR", Priority::ERROR},
51 {"WARN", Priority::WARN},
52 {"INFO", Priority::INFO},
53 {"DEBUG", Priority::DEBUG}};
54
56 auto layout = make_unique<log4cpp::PatternLayout>();
57 layout->setConversionPattern("%d{%FT%T%Z} %c %5p : %m%n");
58 return layout;
59}
60
61Logging::Logging(Category& log4cppLogger) : m_log4cppLogger(log4cppLogger) {}
62
63Logging Logging::getLogger(const string& name) {
64 if (Category::getRoot().getAppender("console") == nullptr) {
65 log4cpp::OstreamAppender* consoleAppender = new log4cpp::OstreamAppender{"console", &std::cerr};
66 consoleAppender->setLayout(getLogLayout().release());
67 Category::getRoot().addAppender(consoleAppender);
68 if (Category::getRoot().getPriority() == Priority::NOTSET) {
69 Category::setRootPriority(Priority::INFO);
70 }
71 }
72 return Logging{Category::getInstance(name)};
73}
74
75void Logging::setLevel(string level) {
76 boost::to_upper(level);
77 auto it = LOG_LEVEL.find(level);
78 if (it != LOG_LEVEL.end()) {
79 Category::setRootPriority(it->second);
80 } else {
81 std::stringstream error_buffer;
82 error_buffer << "Unrecognized logging level: " << level << std::endl;
83 throw Exception(error_buffer.str());
84 }
85}
86
87void Logging::setLogFile(const Path::Item& fileName) {
88 Category& root = Category::getRoot();
89 root.removeAppender(root.getAppender("file"));
90 if (fileName.has_filename()) {
91 log4cpp::FileAppender* fileAppender = new log4cpp::FileAppender("file", fileName.string());
92 fileAppender->setLayout(getLogLayout().release());
93 root.addAppender(fileAppender);
94 }
95 root.setPriority(root.getPriority());
96}
97
99Logging::LogMessageStream::LogMessageStream(Category& logger, P_log_func log_func)
100 : m_logger(logger), m_log_func{log_func} {}
102
104 : m_logger(other.m_logger), m_log_func{other.m_log_func} {}
105
107 : m_logger(other.m_logger), m_log_func{other.m_log_func} {}
108
110 (m_logger.*m_log_func)(m_message.str());
111}
112
113} // namespace Elements
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:63
static void setLogFile(const Path::Item &fileName)
Sets the file to store the log messages.
Definition Logging.cpp:87
static void setLevel(std::string level)
Sets the global message level.
Definition Logging.cpp:75
Logging(log4cpp::Category &log4cppLogger)
Definition Logging.cpp:61
T endl(T... args)
unique_ptr< Layout > getLogLayout()
Definition Logging.cpp:55
static const std::map< string, const int > LOG_LEVEL
Definition Logging.cpp:49
T str(T... args)