Skip to content

Commit dbb1e6a

Browse files
committed
Merge pull request #123 from torbjoernk/feature/more-awesome-logging
logging: now with awesome color and fine-tunable verbosity levels
2 parents 3419ae2 + f363dfe commit dbb1e6a

File tree

3 files changed

+101
-60
lines changed

3 files changed

+101
-60
lines changed

include/pfasst.hpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,9 @@ namespace pfasst
1414
SDC<>::enable_config_options(0);
1515
Quadrature::enable_config_options(0);
1616
config::init_config();
17-
config::read_commandline(argc, argv);
1817
log::start_log(argc, argv);
18+
config::read_commandline(argc, argv);
1919
}
2020
} // ::pfasst
2121

22-
#include <string>
23-
using namespace std;
24-
25-
struct OUT
26-
{
27-
public:
28-
static const string black;
29-
static const string red;
30-
static const string green;
31-
static const string yellow;
32-
static const string blue;
33-
static const string magenta;
34-
static const string cyan;
35-
static const string white;
36-
37-
static const string bold;
38-
static const string underline;
39-
40-
static const string reset;
41-
};
42-
43-
const string OUT::black = "\033[30m";
44-
const string OUT::red = "\033[31m";
45-
const string OUT::green = "\033[32m";
46-
const string OUT::yellow = "\033[33m";
47-
const string OUT::blue = "\033[34m";
48-
const string OUT::magenta = "\033[35m";
49-
const string OUT::cyan = "\033[36m";
50-
const string OUT::white = "\033[37m";
51-
const string OUT::bold = "\033[1m";
52-
const string OUT::underline = "\033[4m";
53-
const string OUT::reset = "\033[0m";
54-
5522
#endif

include/pfasst/config.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@ namespace pfasst
125125
static string pretty_print()
126126
{
127127
stringstream s;
128-
s << Options::get_instance().get_all_options();
128+
s << "Logging Options:" << endl
129+
<< " -v [ --verbose ] activates maximum verbosity" << endl
130+
<< " --v=arg activates verbosity upto verbose level 2" << endl
131+
<< " (valid range: 0-9)" << endl
132+
<< " -vmodule=arg actives verbose logging for specific module" << endl
133+
<< " (see [1] for details)" << endl;
134+
s << Options::get_instance().get_all_options() << endl;
135+
s << "[1]: https://github.com/easylogging/easyloggingpp#vmodule";
129136
return s.str();
130137
}
131138

include/pfasst/logging.hpp

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

9+
struct OUT
10+
{
11+
public:
12+
static const string black;
13+
static const string red;
14+
static const string green;
15+
static const string yellow;
16+
static const string blue;
17+
static const string magenta;
18+
static const string cyan;
19+
static const string white;
20+
21+
static const string bold;
22+
static const string underline;
23+
24+
static const string reset;
25+
};
26+
27+
const string OUT::black = "\033[30m";
28+
const string OUT::red = "\033[31m";
29+
const string OUT::green = "\033[32m";
30+
const string OUT::yellow = "\033[33m";
31+
const string OUT::blue = "\033[34m";
32+
const string OUT::magenta = "\033[35m";
33+
const string OUT::cyan = "\033[36m";
34+
const string OUT::white = "\033[37m";
35+
const string OUT::bold = "\033[1m";
36+
const string OUT::underline = "\033[4m";
37+
const string OUT::reset = "\033[0m";
38+
39+
940
#include "config.hpp"
1041

1142
// enable easy logging of STL containers
@@ -20,6 +51,10 @@ using namespace std;
2051
#define _ELPP_STACKTRACE_ON_CRASH
2152
#endif
2253

54+
#ifdef PFASST_NO_LOGGING
55+
#define _ELPP_DISABLE_LOGS
56+
#endif
57+
2358
#include <easylogging++.h>
2459

2560
#ifndef PFASST_LOGGER_INITIALIZED
@@ -38,42 +73,54 @@ using namespace std;
3873
#define PFASST_LOGGER_INITIALIZED
3974
#endif
4075

41-
#ifndef PFASST_LOGGER_DEFAULT_GLOBAL_FORMAT
42-
//! format for the default global logger
43-
#define PFASST_LOGGER_DEFAULT_GLOBAL_FORMAT "[%level] %msg"
76+
#ifndef PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH
77+
//! precision of milliseconds to be printed
78+
#define PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH "4"
4479
#endif
4580

46-
#ifndef PFASST_LOGGER_DEFAULT_GLOBAL_TOFILE
47-
//! whether to log to file by default
48-
#define PFASST_LOGGER_DEFAULT_GLOBAL_TOFILE "false"
49-
#endif
50-
#ifndef PFASST_LOGGER_DEFAULT_GLOBAL_TOSTDOUT
51-
//! whether to log to stdout by default
52-
#define PFASST_LOGGER_DEFAULT_GLOBAL_TOSTDOUT "true"
53-
#endif
54-
55-
#ifndef PFASST_LOGGER_DEFAULT_DEBUG_FORMAT
56-
//! format for the default global debug logger
57-
#define PFASST_LOGGER_DEFAULT_DEBUG_FORMAT "[%datetime{%Y-%M-%d %H:%m:%s,%g}] %level - %fbase:%line - %msg"
58-
#endif
5981

6082
namespace pfasst
6183
{
6284
namespace log
6385
{
64-
6586
/**
6687
* sets default configuration for default loggers
6788
*/
6889
static void load_default_config()
6990
{
91+
const string TIMESTAMP = OUT::white + "%datetime{%H:%m:%s,%g}" + OUT::reset + " ";
92+
const string LEVEL = "[%level]";
93+
const string VLEVEL = "[VERB%vlevel]";
94+
const string POSITION = "%fbase:%line";
95+
const string MESSAGE = "%msg";
96+
7097
el::Configurations defaultConf;
7198
defaultConf.setToDefault();
72-
defaultConf.set(el::Level::Global, el::ConfigurationType::Format, PFASST_LOGGER_DEFAULT_GLOBAL_FORMAT);
73-
defaultConf.set(el::Level::Global, el::ConfigurationType::ToFile, PFASST_LOGGER_DEFAULT_GLOBAL_TOFILE);
74-
defaultConf.set(el::Level::Global, el::ConfigurationType::ToStandardOutput, PFASST_LOGGER_DEFAULT_GLOBAL_TOSTDOUT);
75-
defaultConf.set(el::Level::Debug, el::ConfigurationType::Format, PFASST_LOGGER_DEFAULT_DEBUG_FORMAT);
76-
el::Loggers::reconfigureLogger("default", defaultConf);
99+
100+
defaultConf.setGlobally(el::ConfigurationType::Format, "%msg");
101+
defaultConf.setGlobally(el::ConfigurationType::ToFile, "false");
102+
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
103+
defaultConf.setGlobally(el::ConfigurationType::MillisecondsWidth, PFASST_LOGGER_DEFAULT_GLOBAL_MILLISECOND_WIDTH);
104+
105+
defaultConf.set(el::Level::Info, el::ConfigurationType::Format,
106+
TIMESTAMP + OUT::blue + LEVEL + " " + MESSAGE + OUT::reset);
107+
108+
defaultConf.set(el::Level::Debug, el::ConfigurationType::Format,
109+
TIMESTAMP + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
110+
111+
defaultConf.set(el::Level::Warning, el::ConfigurationType::Format,
112+
TIMESTAMP + OUT::magenta + LEVEL + " " + MESSAGE + OUT::reset);
113+
114+
defaultConf.set(el::Level::Error, el::ConfigurationType::Format,
115+
TIMESTAMP + OUT::red + LEVEL + " " + MESSAGE + OUT::reset);
116+
117+
defaultConf.set(el::Level::Fatal, el::ConfigurationType::Format,
118+
TIMESTAMP + OUT::red + OUT::bold + LEVEL + " " + POSITION + " " + MESSAGE + OUT::reset);
119+
120+
defaultConf.set(el::Level::Verbose, el::ConfigurationType::Format,
121+
TIMESTAMP + OUT::white + VLEVEL + " " + MESSAGE + OUT::reset);
122+
123+
el::Loggers::reconfigureAllLoggers(defaultConf);
77124
}
78125

79126
/**
@@ -83,9 +130,9 @@ namespace pfasst
83130
*
84131
* - NewLineForContainer
85132
* - LogDetailedCrashReason
133+
* - DisableApplicationAbortOnFatalLog
86134
* - ColoredTerminalOutput
87135
* - MultiLoggerSupport
88-
* - HierarchicalLogging
89136
* - AutoSpacing
90137
*
91138
* \see https://github.com/easylogging/easyloggingpp#logging-flags
@@ -94,12 +141,32 @@ namespace pfasst
94141
{
95142
el::Loggers::addFlag(el::LoggingFlag::NewLineForContainer);
96143
el::Loggers::addFlag(el::LoggingFlag::LogDetailedCrashReason);
144+
el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
97145
el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput);
98146
el::Loggers::addFlag(el::LoggingFlag::MultiLoggerSupport);
99-
el::Loggers::addFlag(el::LoggingFlag::HierarchicalLogging);
147+
el::Loggers::addFlag(el::LoggingFlag::CreateLoggerAutomatically);
100148
el::Loggers::addFlag(el::LoggingFlag::AutoSpacing);
101149
}
102150

151+
#ifdef NDEBUG
152+
static void test_logging_levels() {}
153+
#else
154+
static void test_logging_levels()
155+
{
156+
cout << "### Example of different Logging Levels:" << endl;
157+
LOG(INFO) << "info";
158+
LOG(DEBUG) << "debug";
159+
LOG(WARNING) << "warning";
160+
LOG(ERROR) << "error";
161+
LOG(FATAL) << "fatal error";
162+
LOG(TRACE) << "trace";
163+
for (size_t level = 0; level <= 9; ++level) {
164+
VLOG(level) << "verbose level" << level;
165+
}
166+
cout << "### End Example Logging Levels" << endl << endl;
167+
}
168+
#endif
169+
103170
/**
104171
* starts easylogging++ with given arguments and loads configuration
105172
*
@@ -112,8 +179,8 @@ namespace pfasst
112179
static void start_log(int argc, char** argv)
113180
{
114181
_START_EASYLOGGINGPP(argc, argv);
115-
load_default_config();
116182
set_logging_flags();
183+
load_default_config();
117184
}
118185
} // ::pfasst::log
119186
} // ::pfasst

0 commit comments

Comments
 (0)