55#ifndef _PFASST__LOGGING_HPP_
66#define _PFASST__LOGGING_HPP_
77
8+ #include < iomanip>
89#ifndef NDEBUG
910 #include < iostream> // used by pfasst::log::test_logging_levels()
1011#endif
1112#include < string>
1213using namespace std ;
1314
1415#ifdef WITH_MPI
15- #include < iomanip>
1616 #include < sstream>
17-
1817 #include < mpi.h>
1918#endif
2019
@@ -52,21 +51,6 @@ struct OUT
5251 static const string reset;
5352};
5453
55- #ifdef NO_COLOR
56- const string OUT::black = " " ;
57- const string OUT::red = " " ;
58- const string OUT::green = " " ;
59- const string OUT::yellow = " " ;
60- const string OUT::blue = " " ;
61- const string OUT::magenta = " " ;
62- const string OUT::cyan = " " ;
63- const string OUT::white = " " ;
64-
65- const string OUT::bold = " " ;
66- const string OUT::underline = " " ;
67-
68- const string OUT::reset = " " ;
69- #else
7054const string OUT::black = " \033 [30m" ;
7155const string OUT::red = " \033 [31m" ;
7256const string OUT::green = " \033 [32m" ;
@@ -80,7 +64,6 @@ const string OUT::bold = "\033[1m";
8064const string OUT::underline = " \033 [4m" ;
8165
8266const string OUT::reset = " \033 [0m" ;
83- #endif
8467
8568// enable easy logging of STL containers
8669#define ELPP_STL_LOGGING
@@ -194,6 +177,86 @@ namespace pfasst
194177 */
195178 static size_t stack_position;
196179
180+ /* *
181+ * Formats the local MPI world rank as a string.
182+ *
183+ * The local rank number as given by pfasst::config::get_rank() is filled from the left with the
184+ * @p fill character.
185+ *
186+ * @param[in] width width of the formatted rank
187+ * @param[in] fill character used for left-filling
188+ * @returns formatted rank
189+ *
190+ * @since v1.0.0
191+ *
192+ * @ingroup Internals
193+ */
194+ inline string format_mpi_rank (const size_t width = 4 , const char fill = ' ' )
195+ {
196+ ostringstream frmter;
197+ frmter << std::setw (width) << std::setfill (fill) << pfasst::config::get_rank ();
198+ return frmter.str ();
199+ }
200+
201+ /* *
202+ * Composes the log file name for the local rank.
203+ *
204+ * The rank-local log file name is composed of a potentially given _log_prefix_ as given from
205+ * the command line.
206+ * In case MPI is enabled this prefix gets extended by `_mpi-rank-<RANK>` where `<RANK>` is
207+ * the formatted MPI rank as determined by format_mpi_rank().
208+ * The file extension is always `.log`.
209+ *
210+ * @returns rank-local log file name
211+ *
212+ * @since v1.0.0
213+ *
214+ * @ingroup Internals
215+ */
216+ inline string get_log_file_name ()
217+ {
218+ string log_name = config::get_value<string>(" log_prefix" , " " );
219+ #ifdef WITH_MPI
220+ if (log_name.size () > 0 ) {
221+ log_name += " _" ;
222+ }
223+ log_name += " mpi-rank-" + format_mpi_rank (' 0' );
224+ #endif
225+ log_name += " .log" ;
226+ return log_name;
227+ }
228+
229+ /* *
230+ * Sets global logging options.
231+ *
232+ * @param[in,out] conf configuration to extend and set
233+ * @param[in] default_conf optional, default configuration set to be used to retreive
234+ * default options
235+ *
236+ * @since v1.0.0
237+ *
238+ * @ingroup Internals
239+ */
240+ inline void set_global_logging_options (el::Configurations* conf,
241+ const el::Configurations* default_conf = nullptr )
242+ {
243+ string milliseconds_width, to_stdout;
244+ if (default_conf) {
245+ el::Configurations* default_conf_nc = const_cast <el::Configurations*>(default_conf);
246+ milliseconds_width = default_conf_nc->get (el::Level::Info,
247+ el::ConfigurationType::MillisecondsWidth)->value ();
248+ to_stdout = default_conf_nc->get (el::Level::Info,
249+ el::ConfigurationType::ToStandardOutput)->value ();
250+ } else {
251+ milliseconds_width = PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH;
252+ to_stdout = (pfasst::config::options::get_instance ().get_variables_map ()
253+ .count (" quiet" )) ? " false" : " true" ;
254+ }
255+
256+ conf->setGlobally (el::ConfigurationType::ToStandardOutput, to_stdout);
257+ conf->setGlobally (el::ConfigurationType::Filename, get_log_file_name ());
258+ }
259+
197260 /* *
198261 * Provides convenient way of adding additional named loggers.
199262 *
@@ -222,63 +285,53 @@ namespace pfasst
222285 */
223286 inline static void add_custom_logger (const string& id)
224287 {
225- const string TIMESTAMP = OUT::white + " %datetime{%H:%m:%s,%g}" + OUT::reset + " " ;
288+ bool colorize = pfasst::config::options::get_instance ().get_variables_map ()
289+ .count (" nocolor" ) ? false : true ;
290+
291+ const string INFO_COLOR = (colorize) ? OUT::blue : " " ;
292+ const string DEBG_COLOR = (colorize) ? " " : " " ;
293+ const string WARN_COLOR = (colorize) ? OUT::magenta : " " ;
294+ const string ERRO_COLOR = (colorize) ? OUT::red : " " ;
295+ const string FATA_COLOR = (colorize) ? OUT::red + OUT::bold : " " ;
296+ const string VERB_COLOR = (colorize) ? OUT::white : " " ;
297+ const string TIMESTAMP_COLOR = (colorize) ? OUT::white : " " ;
298+ const string RESET = (colorize) ? OUT::reset : " " ;
299+
300+ const string TIMESTAMP = TIMESTAMP_COLOR + " %datetime{%H:%m:%s,%g}" + RESET + " " ;
226301 const string LEVEL = " %level" ;
227302 const string VLEVEL = " VERB%vlevel" ;
228303 const string POSITION = " %fbase:%line" ;
229304 const string MESSAGE = " %msg" ;
230305#ifdef WITH_MPI
231- const int rank = pfasst::config::get_rank ();
232- ostringstream frmter;
233- frmter << std::setw (3 ) << rank;
234- const string MPI_RANK = " , rank " + frmter.str ();
306+ const string MPI_RANK = " , rank " + format_mpi_rank ();
235307#else
236308 const string MPI_RANK = " " ;
237309#endif
238310
239- const string INFO_COLOR = OUT::blue;
240- const string DEBG_COLOR = " " ;
241- const string WARN_COLOR = OUT::magenta;
242- const string ERRO_COLOR = OUT::red;
243- const string FATA_COLOR = OUT::red + OUT::bold;
244- const string VERB_COLOR = OUT::white;
245-
246311 const size_t id_length = id.size ();
247312 string id2print = id.substr (0 , LOGGER_ID_LENGTH);
248313 boost::to_upper (id2print);
249314 if (id_length < LOGGER_ID_LENGTH) {
250315 id2print.append (LOGGER_ID_LENGTH - id_length, ' ' );
251316 }
252317
253- el::Configurations* default_conf = \
254- const_cast <el::Configurations*>(el::Loggers::defaultConfigurations ());
255-
256318 el::Logger* logger = el::Loggers::getLogger (id);
257319 el::Configurations* conf = logger->configurations ();
258- conf->setGlobally (el::ConfigurationType::MillisecondsWidth,
259- default_conf->get (el::Level::Info,
260- el::ConfigurationType::MillisecondsWidth)->value ());
261- conf->setGlobally (el::ConfigurationType::ToStandardOutput,
262- default_conf->get (el::Level::Info,
263- el::ConfigurationType::ToStandardOutput)->value ());
320+ const el::Configurations* default_conf = el::Loggers::defaultConfigurations ();
321+ set_global_logging_options (conf, default_conf);
264322
265- #ifdef WITH_MPI
266- conf->setGlobally (el::ConfigurationType::ToFile, " true" );
267- conf->setGlobally (el::ConfigurationType::Filename,
268- string (" mpi_run_" ) + to_string (rank) + string (" .log" ));
269- #endif
270323 conf->set (el::Level::Info, el::ConfigurationType::Format,
271- TIMESTAMP + INFO_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + OUT::reset );
324+ TIMESTAMP + INFO_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + RESET );
272325 conf->set (el::Level::Debug, el::ConfigurationType::Format,
273- TIMESTAMP + DEBG_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + POSITION + " " + MESSAGE + OUT::reset );
326+ TIMESTAMP + DEBG_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + POSITION + " " + MESSAGE + RESET );
274327 conf->set (el::Level::Warning, el::ConfigurationType::Format,
275- TIMESTAMP + WARN_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + OUT::reset );
328+ TIMESTAMP + WARN_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + RESET );
276329 conf->set (el::Level::Error, el::ConfigurationType::Format,
277- TIMESTAMP + ERRO_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + OUT::reset );
330+ TIMESTAMP + ERRO_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + MESSAGE + RESET );
278331 conf->set (el::Level::Fatal, el::ConfigurationType::Format,
279- TIMESTAMP + FATA_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + POSITION + " " + MESSAGE + OUT::reset );
332+ TIMESTAMP + FATA_COLOR + " [" + id2print + " , " + LEVEL + MPI_RANK + " ] " + POSITION + " " + MESSAGE + RESET );
280333 conf->set (el::Level::Verbose, el::ConfigurationType::Format,
281- TIMESTAMP + VERB_COLOR + " [" + id2print + " , " + VLEVEL + MPI_RANK + " ] " + MESSAGE + OUT::reset );
334+ TIMESTAMP + VERB_COLOR + " [" + id2print + " , " + VLEVEL + MPI_RANK + " ] " + MESSAGE + RESET );
282335 el::Loggers::reconfigureLogger (logger, *conf);
283336 }
284337
@@ -294,19 +347,8 @@ namespace pfasst
294347 el::Configurations defaultConf;
295348 defaultConf.setToDefault ();
296349
297- if (!pfasst::config::options::get_instance ().get_variables_map ().count (" quiet" )) {
298- defaultConf.setGlobally (el::ConfigurationType::ToStandardOutput, " true" );
299- } else {
300- defaultConf.setGlobally (el::ConfigurationType::ToStandardOutput, " false" );
301- }
302- #ifdef WITH_MPI
303- int rank = pfasst::config::get_rank ();
304- defaultConf.setGlobally (el::ConfigurationType::ToFile, " true" );
305- defaultConf.setGlobally (el::ConfigurationType::Filename,
306- string (" mpi_run_" ) + to_string (rank) + string (" .log" ));
307- #endif
308- defaultConf.setGlobally (el::ConfigurationType::MillisecondsWidth,
309- PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);
350+ set_global_logging_options (&defaultConf);
351+
310352 el::Loggers::setDefaultConfigurations (defaultConf, true );
311353
312354 add_custom_logger (" default" );
0 commit comments