Skip to content

Commit 31ea461

Browse files
committed
log: adding ability to add further named loggers
this should enable better 'grep'-ing of the log for special data given the user has added and used several different named loggers for different parts of his code (I'll provide a more extensive example within the Boris example)
1 parent 8792d7b commit 31ea461

File tree

5 files changed

+82
-27
lines changed

5 files changed

+82
-27
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ namespace pfasst
9999

100100
virtual ~AdvectionDiffusionSweeper()
101101
{
102-
LOG(INFO) << "number of f1 evals: " << this->nf1evals;
102+
CLOG(INFO, "Advec") << "number of f1 evals: " << this->nf1evals;
103103
}
104104
//! @}
105105

@@ -141,7 +141,7 @@ namespace pfasst
141141

142142
auto n = this->get_controller()->get_step();
143143
auto k = this->get_controller()->get_iteration();
144-
LOG(INFO) << "err: " << n << " " << k << " " << max << " (" << qend.size() << "," << predict << ")";
144+
CLOG(INFO, "Advec") << "err: " << n << " " << k << " " << max << " (" << qend.size() << "," << predict << ")";
145145

146146
this->errors.insert(vtype(ktype(n, k), max));
147147
}
@@ -163,7 +163,7 @@ namespace pfasst
163163

164164
auto n = this->get_controller()->get_step();
165165
auto k = this->get_controller()->get_iteration();
166-
LOG(INFO) << "res: " << n << " " << k << " " << rmax << " (" << residuals.size() << ")";
166+
CLOG(INFO, "Advec") << "res: " << n << " " << k << " " << rmax << " (" << residuals.size() << ")";
167167

168168
this->residuals[ktype(n, k)] = rmax;
169169
}

examples/advection_diffusion/vanilla_sdc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ int main(int argc, char** argv)
6666
{
6767
pfasst::examples::advection_diffusion::AdvectionDiffusionSweeper<>::enable_config_options();
6868
pfasst::init(argc, argv);
69+
pfasst::log::add_custom_logger("Advec");
6970

7071
pfasst::examples::advection_diffusion::run_vanilla_sdc(0.0);
7172
}

include/pfasst/encap/imex_sweeper.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <memory>
99

1010
#include "../globals.hpp"
11+
#include "../logging.hpp"
1112
#include "../quadrature.hpp"
1213
#include "encapsulation.hpp"
1314
#include "encap_sweeper.hpp"
@@ -292,6 +293,8 @@ namespace pfasst
292293
{
293294
time dt = this->get_controller()->get_time_step();
294295
time t = this->get_controller()->get_time();
296+
CLOG(INFO, "Sweeper") << "predicting step " << this->get_controller()->get_step() + 1
297+
<< " (t=" << t << ", dt=" << dt << ")";
295298

296299
if (initial) {
297300
this->state[0]->copy(this->start_state);
@@ -319,6 +322,8 @@ namespace pfasst
319322
UNUSED(initial);
320323
time dt = this->get_controller()->get_time_step();
321324
time t = this->get_controller()->get_time();
325+
CLOG(INFO, "Sweeper") << "predicting step " << this->get_controller()->get_step() + 1
326+
<< " (t=" << t << ", dt=" << dt << ")";
322327
time ds;
323328

324329
shared_ptr<Encapsulation<time>> rhs = this->get_factory()->create(pfasst::encap::solution);
@@ -349,6 +354,8 @@ namespace pfasst
349354
auto const nodes = this->quadrature->get_nodes();
350355
auto const dt = this->get_controller()->get_time_step();
351356
auto const s_mat = this->quadrature->get_s_mat().block(1, 0, nodes.size()-1, nodes.size());
357+
CLOG(INFO, "Sweeper") << "sweeping on step " << this->get_controller()->get_step() + 1
358+
<< " in iteration " << this->get_controller()->get_iteration() << " (dt=" << dt << ")";
352359
time ds;
353360

354361
this->s_integrals[0]->mat_apply(this->s_integrals, dt, s_mat, this->fs_expl, true);
@@ -384,6 +391,8 @@ namespace pfasst
384391
auto const nodes = this->quadrature->get_nodes();
385392
auto const dt = this->get_controller()->get_time_step();
386393
auto const s_mat = this->quadrature->get_s_mat();
394+
CLOG(INFO, "Sweeper") << "sweeping on step " << this->get_controller()->get_step() + 1
395+
<< " in iteration " << this->get_controller()->get_iteration() << " (dt=" << dt << ")";
387396
time ds;
388397

389398
this->s_integrals[0]->mat_apply(this->s_integrals, dt, s_mat, this->fs_expl, true);

include/pfasst/logging.hpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <string>
77
using namespace std;
88

9+
#include <boost/algorithm/string.hpp>
10+
911
struct 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

101105
namespace 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
/**

tests/examples/advection_diffusion/test_advection_diffusion.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,7 @@ TEST(FASTest, SerialMLSDC)
130130
int main(int argc, char** argv)
131131
{
132132
testing::InitGoogleTest(&argc, argv);
133+
pfasst::log::start_log(argc, argv);
134+
pfasst::log::add_custom_logger("Advec");
133135
return RUN_ALL_TESTS();
134136
}

0 commit comments

Comments
 (0)