Elements  6.2
A C++ base framework for the Euclid Software.
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 
41 using log4cpp::Category;
42 using log4cpp::Layout;
43 using log4cpp::Priority;
44 using std::string;
45 using std::unique_ptr;
46 
47 namespace Elements {
48 
49 static 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 
61 Logging::Logging(Category& log4cppLogger) : m_log4cppLogger(log4cppLogger) {}
62 
63 Logging 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 
75 void 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 
87 void 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 
99 Logging::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
Elements::LOG_LEVEL
static const std::map< string, const int > LOG_LEVEL
Definition: Logging.cpp:49
Elements::Logging::setLogFile
static void setLogFile(const Path::Item &fileName)
Sets the file to store the log messages.
Definition: Logging.cpp:87
Elements::Kernel::Path::Item
boost::filesystem::path Item
Definition: Path.h:56
std::string
STL class.
Path.h
provide functions to retrieve resources pointed by environment variables
Elements::Logging::LogMessageStream::~LogMessageStream
~LogMessageStream()
Definition: Logging.cpp:109
Elements::Logging
Logging API of the Elements framework.
Definition: Logging.h:93
std::stringstream
STL class.
Elements::Logging::Logging
Logging(log4cpp::Category &log4cppLogger)
Definition: Logging.cpp:61
Exception.h
defines the base Elements exception class
std::cerr
Elements::getLogLayout
unique_ptr< Layout > getLogLayout()
Definition: Logging.cpp:55
Elements::Exception
Elements base exception class.
Definition: Exception.h:47
std::map
STL class.
Elements::Logging::getLogger
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
Elements::Logging::LogMessageStream::LogMessageStream
LogMessageStream(log4cpp::Category &logger, P_log_func log_func)
Memory.h
provide functions to retrieve configuration files
Elements::Logging::LogMessageStream
A helper class for logging messages using the "<<" operator.
Definition: Logging.h:309
std::endl
T endl(T... args)
Elements::Logging::LogMessageStream::m_log_func
P_log_func m_log_func
Definition: Logging.h:328
std::stringstream::str
T str(T... args)
Logging.h
Logging facility.
std::unique_ptr
STL class.
Elements::Logging::setLevel
static void setLevel(std::string level)
Sets the global message level.
Definition: Logging.cpp:75
Elements
Definition: callBackExample.h:35