MarlinMT  0.1.0
LoggerManager.cc
Go to the documentation of this file.
2 
3 // -- marlinmt headers
4 #include <marlinmt/Application.h>
5 
6 namespace marlinmt {
7 
9  Component("LoggerManager") {
10  setDescription( "The logger manager manages all the logging facility whithin the application" ) ;
11  // temporary name before initialization
12  mainLogger()->setName( "main" ) ;
13  }
14 
15  //--------------------------------------------------------------------------
16 
18  auto config = application().configuration() ;
19  if( config.hasSection("logging") ) {
20  setParameters( config.section("logging") ) ;
21  }
22  streamlog::logsink_list sinks {} ;
23  if ( _coloredConsole.get() ) {
24  sinks.push_back( streamlog::logstream::coloredConsole<Logging::mutex_type>() ) ;
25  }
26  else {
27  sinks.push_back( streamlog::logstream::console<Logging::mutex_type>() ) ;
28  }
29  if ( not _logfile.get().empty() ) {
30  auto logfilename = _logfile.get() ;
31  sinks.push_back( streamlog::logstream::simpleFile<Logging::mutex_type>( logfilename ) ) ;
32  }
33  mainLogger()->setName( application().programName() ) ;
34  mainLogger()->setSinks( sinks ) ;
35  streamlog::logstream::global().setName( application().programName() ) ;
36  streamlog::logstream::global().setSinks( sinks ) ;
37  }
38 
39  //--------------------------------------------------------------------------
40 
42  return _logger ;
43  }
44 
45  //--------------------------------------------------------------------------
46 
48  auto logger = Logging::createLogger( name ) ;
49  logger->setSinks( mainLogger()->sinks() ) ;
50  logger->setLevel( mainLogger()->levelName() ) ;
51  return logger ;
52  }
53 
54 } // namespace marlinmt
static Logger createLogger(const std::string &name)
Create a standalone logger instance.
Definition: Logging.cc:5
StringParameter _logfile
The name of the log file (optional)
Definition: LoggerManager.h:55
const std::string & name() const
Get the component name.
Definition: Component.cc:23
void setParameters(const ConfigSection &section, bool throwIfNotFound=false)
Set the parameters from the configuration section.
Definition: Component.cc:124
LoggerPtr _logger
The logger instance.
Definition: Component.h:173
LoggerManager()
Constructor.
Definition: LoggerManager.cc:8
Logger createLogger(const std::string &name) const
Create a new logger instance.
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
Component class.
Definition: Component.h:22
Logging::Logger Logger
Definition: LoggerManager.h:23
const Application & application() const
Get the application in which the component is registered.
Definition: Component.cc:83
Logger mainLogger() const
Get the main logger instance (not the global one)
T get() const
Get the parameter value.
Definition: Parameter.h:556
BoolParameter _coloredConsole
Whether to use a colored console printout.
Definition: LoggerManager.h:57
void initialize() override
Initialize the logger manager.
const Configuration & configuration() const
Get the main application configuration object.
Definition: Application.cc:278