Skip to content

Commit d6ea835

Browse files
committed
Merge pull request #203 from torbjoernk/feature/config-log-file-name
add config option for logfile name
2 parents 072c30f + 8c87728 commit d6ea835

File tree

3 files changed

+111
-74
lines changed

3 files changed

+111
-74
lines changed

CMakeLists.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ CMAKE_DEPENDENT_OPTION(pfasst_INSTALL_EXAMPLES "Install example programs." ON
2020
"pfasst_BUILD_EXAMPLES" OFF)
2121
option(pfasst_BUILD_TESTS "Build test suite for PFASST." ON )
2222
option(pfasst_WITH_MPI "Build with MPI enabled." ON )
23-
option(pfasst_WITH_COLOR "Enable colorful logging output." ON )
2423
option(pfasst_WITH_GCC_PROF "Enable excessive debugging & profiling output with GCC." OFF)
2524
option(pfasst_DEFAULT_RAND_SEED "Using a hardcoded random seed" ON )
2625

@@ -44,10 +43,6 @@ if(pfasst_DEFAULT_RAND_SEED)
4443
set(pfasst_RANDOM_SEED "42")
4544
endif()
4645

47-
if(NOT ${pfasst_WITH_COLOR})
48-
add_definitions(-DNO_COLOR)
49-
endif()
50-
5146
# Check for C++11 support
5247
if(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
5348
check_cxx_compiler_flag(-std=c++11 HAVE_STD11)
@@ -118,10 +113,6 @@ add_feature_info(MPI
118113
pfasst_WITH_MPI
119114
"build with MPI"
120115
)
121-
add_feature_info(COLOR
122-
pfasst_WITH_COLOR
123-
"colorful logging"
124-
)
125116
if(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
126117
add_feature_info(Profiling
127118
pfasst_WITH_GCC_PROF

include/pfasst/config.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ namespace pfasst
287287
* Global | `h`, `help` | `bool`
288288
* Global | `q`, `quiet` | `bool`
289289
* Global | `input` | `string`
290+
* Global | `log_prefix` | `string`
291+
* Global | `c`,`nocolor` | `bool`
290292
* Duration | `dt` | `double`
291293
* Duration | `tend` | `double`
292294
* Duration | `num_iters` | `size_t`
@@ -295,9 +297,11 @@ namespace pfasst
295297
*/
296298
static inline void init()
297299
{
298-
options::add_option("Global", "help,h", "display this help message");
299-
options::add_option("Global", "quiet,q", "don't log to stdout");
300+
options::add_option ("Global", "help,h", "display this help message");
301+
options::add_option ("Global", "quiet,q", "don't log to stdout");
300302
options::add_option<string>("Global", "input", "INI file with configuration options");
303+
options::add_option<string>("Global", "log_prefix", "a prefix for the log files");
304+
options::add_option ("Global", "nocolor,c", "disable colorful logging");
301305

302306
options::add_option<double>("Duration", "dt", "time step size");
303307
options::add_option<double>("Duration", "tend", "final time of simulation");

include/pfasst/logging.hpp

Lines changed: 105 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
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>
1213
using 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
7054
const string OUT::black = "\033[30m";
7155
const string OUT::red = "\033[31m";
7256
const string OUT::green = "\033[32m";
@@ -80,7 +64,6 @@ const string OUT::bold = "\033[1m";
8064
const string OUT::underline = "\033[4m";
8165

8266
const 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

Comments
 (0)