66#include < string>
77using namespace std ;
88
9+ #include < boost/algorithm/string.hpp>
10+
911struct OUT
1012{
1113 public:
@@ -97,6 +99,8 @@ const string OUT::reset = "\033[0m";
9799
98100#define LOG_INDENT string (pfasst::log::stack_position * 2 , ' ' )
99101
102+ // ! length of logger ID to print
103+ #define LOGGER_ID_LENGTH 8
100104
101105namespace pfasst
102106{
@@ -120,43 +124,82 @@ namespace pfasst
120124 static size_t stack_position;
121125
122126 /* *
123- * sets default configuration for default loggers
127+ * \brief provides convenient way of adding additional named loggers
128+ * \details With this function one can easily create additional named loggers distinctable by the `id`.
129+ * The first \ref LOGGER_ID_LENGTH characters of the ID (as uppercase) will be included in every line of the log.
130+ * The ID is used in the actual logging calls.
131+ *
132+ * \code{.cpp}
133+ * add_custom_logger("MyCustomLogger")
134+ * // somewhere else in the code
135+ * CLOG(INFO, "MyCustomLogger") << "a logging message";
136+ * \endcode
137+ *
138+ * This results in the log line (for the default value of \ref LOGGER_ID_LENGTH):
139+ *
140+ * <TIME> [MYCUSTOM, INFO ] a logging message
141+ * \note Please make sure to use `CLOG` (and `CVLOG` for verbose logging) to be able to specify a specific logger.
142+ * Otherwise the default logger will be used.
143+ * \param[in] id The ID of the logger. This is used in logging calls.
124144 */
125- inline static void load_default_config ( )
145+ inline static void add_custom_logger ( const string& id )
126146 {
127147 const string TIMESTAMP = OUT::white + " %datetime{%H:%m:%s,%g}" + OUT::reset + " " ;
128- const string LEVEL = " [ %level]" ;
129- const string VLEVEL = " [ VERB%vlevel]" ;
148+ const string LEVEL = " %level]" ;
149+ const string VLEVEL = " VERB%vlevel]" ;
130150 const string POSITION = " %fbase:%line" ;
131151 const string MESSAGE = " %msg" ;
132152
153+ const string INFO_COLOR = OUT::blue;
154+ const string DEBG_COLOR = " " ;
155+ const string WARN_COLOR = OUT::magenta;
156+ const string ERRO_COLOR = OUT::red;
157+ const string FATA_COLOR = OUT::red + OUT::bold;
158+ const string VERB_COLOR = OUT::white;
159+
160+ const size_t id_length = id.size ();
161+ string id2print = id.substr (0 , LOGGER_ID_LENGTH);
162+ boost::to_upper (id2print);
163+ if (id_length < LOGGER_ID_LENGTH) {
164+ id2print.append (LOGGER_ID_LENGTH - id_length, ' ' );
165+ }
166+
167+ el::Logger* logger = el::Loggers::getLogger (id);
168+ el::Configurations* conf = logger->configurations ();
169+ conf->set (el::Level::Info, el::ConfigurationType::Format,
170+ TIMESTAMP + INFO_COLOR + " [" + id2print + " , " + LEVEL + " " + MESSAGE + OUT::reset);
171+ conf->set (el::Level::Debug, el::ConfigurationType::Format,
172+ TIMESTAMP + DEBG_COLOR + " [" + id2print + " , " + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
173+ conf->set (el::Level::Warning, el::ConfigurationType::Format,
174+ TIMESTAMP + WARN_COLOR + " [" + id2print + " , " + LEVEL + " " + MESSAGE + OUT::reset);
175+ conf->set (el::Level::Error, el::ConfigurationType::Format,
176+ TIMESTAMP + ERRO_COLOR + " [" + id2print + " , " + LEVEL + " " + MESSAGE + OUT::reset);
177+ conf->set (el::Level::Fatal, el::ConfigurationType::Format,
178+ TIMESTAMP + FATA_COLOR + " [" + id2print + " , " + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
179+ conf->set (el::Level::Verbose, el::ConfigurationType::Format,
180+ TIMESTAMP + VERB_COLOR + " [" + id2print + " , " + VLEVEL + " " + MESSAGE + OUT::reset);
181+ el::Loggers::reconfigureLogger (logger, *conf);
182+ }
183+
184+ /* *
185+ * sets default configuration for default loggers
186+ */
187+ inline static void load_default_config ()
188+ {
133189 el::Configurations defaultConf;
134190 defaultConf.setToDefault ();
135191
136- defaultConf.setGlobally (el::ConfigurationType::Format, " %msg" );
137192 defaultConf.setGlobally (el::ConfigurationType::ToFile, " false" );
138193 defaultConf.setGlobally (el::ConfigurationType::ToStandardOutput, " true" );
139194 defaultConf.setGlobally (el::ConfigurationType::MillisecondsWidth, PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);
140-
141- defaultConf.set (el::Level::Info, el::ConfigurationType::Format,
142- TIMESTAMP + OUT::blue + LEVEL + " " + MESSAGE + OUT::reset);
143-
144- defaultConf.set (el::Level::Debug, el::ConfigurationType::Format,
145- TIMESTAMP + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
146-
147- defaultConf.set (el::Level::Warning, el::ConfigurationType::Format,
148- TIMESTAMP + OUT::magenta + LEVEL + " " + MESSAGE + OUT::reset);
149-
150- defaultConf.set (el::Level::Error, el::ConfigurationType::Format,
151- TIMESTAMP + OUT::red + LEVEL + " " + MESSAGE + OUT::reset);
152-
153- defaultConf.set (el::Level::Fatal, el::ConfigurationType::Format,
154- TIMESTAMP + OUT::red + OUT::bold + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
155-
156- defaultConf.set (el::Level::Verbose, el::ConfigurationType::Format,
157- TIMESTAMP + OUT::white + VLEVEL + " " + MESSAGE + OUT::reset);
158-
159195 el::Loggers::reconfigureAllLoggers (defaultConf);
196+
197+ add_custom_logger (" default" );
198+ add_custom_logger (" Controller" );
199+ add_custom_logger (" Sweeper" );
200+ add_custom_logger (" Encap" );
201+ add_custom_logger (" Quadrature" );
202+ add_custom_logger (" User" );
160203 }
161204
162205 /* *
0 commit comments