netxsimdg
Loading...
Searching...
No Matches
Logged.cpp
Go to the documentation of this file.
1
7#include "include/Logged.hpp"
8
10
11#include <boost/log/common.hpp>
12#include <boost/log/utility/setup/console.hpp>
13#include <boost/log/utility/setup/file.hpp>
14#include <iostream>
15
16namespace Nextsim {
17
18const std::map<std::string, Logged::level> Logged::levelNames = {
19 { "all", level::ALL },
20 { "All", level::ALL },
21 { "ALL", level::ALL },
22 { "trace", level::TRACE },
23 { "TRACE", level::TRACE },
24 { "debug", level::DEBUG_LVL },
25 { "DEBUG", level::DEBUG_LVL },
26 { "info", level::INFO },
27 { "INFO", level::INFO },
28 { "warning", level::WARNING },
29 { "WARNING", level::WARNING },
30 { "error", level::ERROR },
31 { "ERROR", level::ERROR },
32 { "critical", level::CRITICAL },
33 { "CRITICAL", level::CRITICAL },
34 { "fatal", level::CRITICAL },
35 { "FATAL", level::CRITICAL },
36 { "alert", level::ALERT },
37 { "ALERT", level::ALERT },
38 { "emergency", level::EMERGENCY },
39 { "EMERGENCY", level::EMERGENCY },
40 { "none", level::NONE },
41 { "None", level::NONE },
42 { "NONE", level::NONE },
43};
44
45const std::map<int, std::string> keyMap = {
46 { Logged::MINIMUM_LOG_LEVEL_KEY, "Logged.minimum_log_level" },
47 { Logged::FILE_NAME_PATTERN_KEY, "Logged.file_name_pattern" },
48 { Logged::CONSOLE_LOG_LEVEL_KEY, "Logged.console_log_level" },
49};
50BOOST_LOG_ATTRIBUTE_KEYWORD(Severity, "Severity", Logged::level)
51
52boost::log::sources::severity_logger<Logged::level> sl;
53
54// Initialize the logger, that is set up boost::log how we want it
56{
57 level minimumLogLevel = levelNames.at(Configured<Logged>::getConfiguration(
58 keyMap.at(MINIMUM_LOG_LEVEL_KEY), std::string("info")));
59 std::string fileNamePattern = Configured<Logged>::getConfiguration(
60 keyMap.at(FILE_NAME_PATTERN_KEY), std::string("nextsim.%T.log"));
61 boost::log::add_file_log(boost::log::keywords::file_name = fileNamePattern,
62 // All logs go to file above the minimum level
63 boost::log::keywords::filter = (Severity >= minimumLogLevel));
64
65 level consoleLogLevel = levelNames.at(Configured<Logged>::getConfiguration(
66 keyMap.at(CONSOLE_LOG_LEVEL_KEY), std::string("none")));
67 boost::log::add_console_log(
68 std::cout, boost::log::keywords::filter = (Severity >= consoleLogLevel));
69}
70
71void Logged::log(const std::string& message, Logged::level lvl)
72{
73 BOOST_LOG_SEV(sl, lvl) << message;
74}
75
76} /* namespace Nextsim */
virtual ConfigMap getConfiguration() const
Returns the current configuration of the object.
static void configure()
Static function that configures the logger.
Definition Logged.cpp:55
static void log(const std::string &message, const level lvl=level::NOTICE)
Logs a message at the given log level, or default to level::NOTICE.
Definition Logged.cpp:71