Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include <boost/filesystem/path.hpp> // for path
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, etc
37 
38 #include "ElementsKernel/Exception.h" // for Exception
39 
40 using std::string;
41 using std::unique_ptr;
42 using log4cpp::Category;
43 using log4cpp::Priority;
44 using log4cpp::Layout;
45 
46 namespace Elements {
47 
48 static const std::map<string, const int> LOG_LEVEL {{"FATAL", Priority::FATAL},
49  {"ERROR", Priority::ERROR},
50  {"WARN", Priority::WARN},
51  {"INFO", Priority::INFO},
52  {"DEBUG", Priority::DEBUG}};
53 
55  log4cpp::PatternLayout* layout = new log4cpp::PatternLayout {};
56  layout->setConversionPattern("%d{%FT%T%Z} %c %5p : %m%n");
57  return unique_ptr<Layout>(layout);
58 }
59 
60 Logging::Logging(Category& log4cppLogger)
61  : m_log4cppLogger(log4cppLogger) { }
62 
63 Logging Logging::getLogger(const string& name) {
64  if (Category::getRoot().getAppender("console") == NULL) {
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 
76 void Logging::setLevel(string level) {
77  boost::to_upper(level);
78  auto it = LOG_LEVEL.find(level);
79  if ( it != LOG_LEVEL.end() ) {
80  Category::setRootPriority(it->second);
81  } else {
82  std::stringstream error_buffer;
83  error_buffer << "Unrecognized logging level: " << level << std::endl;
84  throw Exception(error_buffer.str());
85  }
86 }
87 
89  Category& root = Category::getRoot();
90  root.removeAppender(root.getAppender("file"));
91  if (fileName.has_filename()) {
92  log4cpp::FileAppender* fileAppender = new log4cpp::FileAppender("file", fileName.string());
93  fileAppender->setLayout(getLogLayout().release());
94  root.addAppender(fileAppender);
95  }
96  root.setPriority(root.getPriority());
97 }
98 
100 Logging::LogMessageStream::LogMessageStream(Category& logger, P_log_func log_func)
101  : m_logger(logger), m_log_func{log_func} { }
103 
105  : m_logger(other.m_logger), m_log_func{other.m_log_func} { }
106 
108  (m_logger.*m_log_func) (m_message.str());
109 }
110 
111 
112 } // namespace Elements
Logging facility.
T endl(T...args)
STL class.
STL class.
unique_ptr< Layout > getLogLayout()
Definition: Logging.cpp:54
A helper class for logging messages using the &quot;&lt;&lt;&quot; operator.
Definition: Logging.h:313
Logging(log4cpp::Category &log4cppLogger)
Definition: Logging.cpp:60
Logging API of the Elements framework.
Definition: Logging.h:93
boost::filesystem::path path
Definition: DataSyncUtils.h:33
T str(T...args)
static void setLogFile(const boost::filesystem::path &fileName)
Sets the file to store the log messages.
Definition: Logging.cpp:88
STL class.
static void setLevel(std::string level)
Sets the global message level.
Definition: Logging.cpp:76
static const std::map< string, const int > LOG_LEVEL
Definition: Logging.cpp:48
static Logging getLogger(const std::string &name="")
Definition: Logging.cpp:63
defines the base Elements exception class
LogMessageStream(log4cpp::Category &logger, P_log_func log_func)