MarlinMT  0.1.0
Component.cc
Go to the documentation of this file.
1 #include <marlinmt/Component.h>
2 #include <marlinmt/Exceptions.h>
3 #include <marlinmt/Application.h>
4 #include <marlinmt/Utils.h>
5 
6 namespace marlinmt {
7 
8  Component::Component( const std::string &type ) :
9  _type(type),
10  _name(details::convert<void*>::to_string(this)) {
11  _logger = Logging::createLogger( this->type() + "_" + this->name() ) ;
12  _logger->setLevel( "MESSAGE" ) ;
13  }
14 
15  //--------------------------------------------------------------------------
16 
17  const std::string &Component::type() const {
18  return _type ;
19  }
20 
21  //--------------------------------------------------------------------------
22 
23  const std::string &Component::name() const {
24  return _name ;
25  }
26 
27  //--------------------------------------------------------------------------
28 
29  void Component::setName( const std::string &n ) {
30  _name = n ;
31  }
32 
33  //--------------------------------------------------------------------------
34 
35  const std::string &Component::description() const {
36  return _description ;
37  }
38 
39  //--------------------------------------------------------------------------
40 
41  void Component::setDescription( const std::string &desc ) {
42  _description = desc ;
43  }
44 
45  //--------------------------------------------------------------------------
46 
48  return log<loglevel::DEBUG>() ;
49  }
50 
51  //--------------------------------------------------------------------------
52 
54  return log<loglevel::MESSAGE>() ;
55  }
56 
57  //--------------------------------------------------------------------------
58 
60  return log<loglevel::WARNING>() ;
61  }
62 
63  //--------------------------------------------------------------------------
64 
66  return log<loglevel::ERROR>() ;
67  }
68 
69  //--------------------------------------------------------------------------
70 
71  void Component::setVerbosity( const std::string &level ) {
72  _logger->setLevel( level ) ;
73  }
74 
75  //--------------------------------------------------------------------------
76 
77  const std::string &Component::verbosity() const {
78  return _logger->levelName() ;
79  }
80 
81  //--------------------------------------------------------------------------
82 
84  if( nullptr == _application ) {
85  MARLINMT_THROW( "Application not set" ) ;
86  }
87  return *_application ;
88  }
89 
90  //--------------------------------------------------------------------------
91 
93  if( nullptr == _application ) {
94  MARLINMT_THROW( "Application not set" ) ;
95  }
96  return *_application ;
97  }
98 
99  //--------------------------------------------------------------------------
100 
102  _application = app ;
104  if( _verbosity.isSet() ) {
105  _logger->setLevel( _verbosity.get() ) ;
106  }
107  initialize() ;
108  }
109 
110  //--------------------------------------------------------------------------
111 
113  return ( nullptr == _application ) ;
114  }
115 
116  //--------------------------------------------------------------------------
117 
119  printParameters<MESSAGE>() ;
120  }
121 
122  //--------------------------------------------------------------------------
123 
124  void Component::setParameters( const ConfigSection &section, bool throwIfNotFound ) {
125  auto names = section.parameterNames() ;
126  for( auto n : names ) {
127  auto iter = _parameters.find( n ) ;
128  if( _parameters.end() != iter ) {
129  iter->second->str( section.parameter<std::string>( n ) ) ;
130  }
131  else if( throwIfNotFound ) {
132  MARLINMT_THROW( "Input parameter '" + n + "' from section '" + section.name() + "' can't be set (not found)" ) ;
133  }
134  }
135  }
136 
137  //--------------------------------------------------------------------------
138 
139  void Component::getParameters( ConfigSection &section, const std::set<std::string> &exclude ) const {
140  auto &metadata = section.metadata() ;
141  metadata["description"] = description() ;
142  metadata["name"] = name() ;
143  metadata["type"] = type() ;
144  for( auto iter : _parameters ) {
145  if( exclude.end() != exclude.find( iter.first ) ) {
146  continue ;
147  }
148  auto value = iter.second->hasDefault() ? iter.second->defaultStr() : "" ;
149  section.setParameter( iter.first, value ) ;
150  metadata[ iter.first + ".description" ] = iter.second->description() ;
151  metadata[ iter.first + ".optional" ] = iter.second->hasDefault() ? "true" : "false" ;
152  }
153  }
154 
155 }
const Metadata & metadata() const
Get the section metadata.
Logging::StreamType error() const
Shortcut for log<ERROR>()
Definition: Component.cc:65
static Logger createLogger(const std::string &name)
Create a standalone logger instance.
Definition: Logging.cc:5
T parameter(const std::string &n) const
Get a parameter value as type T.
std::string _type
The component type.
Definition: Component.h:165
Logging::StreamType debug() const
Shortcut for log<DEBUG>()
Definition: Component.cc:47
const std::string & name() const
Get the component name.
Definition: Component.cc:23
Application * _application
The application in which the component has been registered.
Definition: Component.h:171
const std::string & name() const
Get the section name.
std::vector< std::string > parameterNames() const
Get the list of parameter names.
Logger createLogger(const std::string &name) const
Create a new logger instance.
Definition: Application.cc:189
void setup(Application *app)
Setup the component.
Definition: Component.cc:101
void setName(const std::string &n)
Set the component name.
Definition: Component.cc:29
std::string _name
The component name.
Definition: Component.h:167
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
constexpr unsigned long long value(const Flag_t &flag)
Definition: Flags.h:106
Logging::StreamType message() const
Shortcut for log<MESSAGE>()
Definition: Component.cc:53
Component()=delete
No default constructor.
const std::string & type() const
Get the component name.
Definition: Component.cc:17
Logger::element_type::stream_type StreamType
Definition: Logging.h:74
std::string _description
The component description.
Definition: Component.h:169
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
const std::string & description() const
Get the component description.
Definition: Component.cc:35
#define MARLINMT_THROW(message)
Definition: Exceptions.h:8
Application class Base application interface for running a Marlin application.
Definition: Application.h:25
void setVerbosity(const std::string &level)
Set the verbosity level.
Definition: Component.cc:71
const Application & application() const
Get the application in which the component is registered.
Definition: Component.cc:83
bool isInitialized() const
Whether the component has been initialized.
Definition: Component.cc:112
StringParameter _verbosity
The verbosity level of the logger (parameter)
Definition: Component.h:175
T get() const
Get the parameter value.
Definition: Parameter.h:556
ConfigSection & setParameter(const std::string &n, const T &val)
Set a parameter value.
Definition: Configuration.h:97
const std::string & verbosity() const
Get the verbosity level.
Definition: Component.cc:77
virtual void initialize()
Initialize the component.
Definition: Component.h:72
ConfigSection class Holds a set of parameters and subsection.
Definition: Configuration.h:20
void getParameters(ConfigSection &section, const std::set< std::string > &exclude={}) const
Get the parameters from configurable object and populate the config section with. ...
Definition: Component.cc:139
void printParameters() const
Print the component parameters.
Definition: Component.cc:118
ParameterMap _parameters
The parameter map.
Definition: Parameter.h:416
bool isSet() const
Whether the parameter has been set.
Definition: Parameter.h:506
Logging::StreamType warning() const
Shortcut for log<WARNING>()
Definition: Component.cc:59