added convenience macros, improved output
This commit is contained in:
parent
5272dc7edd
commit
0cc1d98579
12
logger.cpp
12
logger.cpp
@ -10,13 +10,12 @@
|
||||
|
||||
using namespace sc;
|
||||
|
||||
logger::logger(loglevel level) : _level(level) {
|
||||
}
|
||||
logger::logger(loglevel level) : _level(level) {}
|
||||
|
||||
void logger::msg(loglevel level) {
|
||||
void logger::msg(loglevel level, const char* file, int line, const char* func) {
|
||||
if (level >= _level) {
|
||||
if (_topcall) {
|
||||
write_prefix();
|
||||
write_prefix(level, file, line, func);
|
||||
} else {
|
||||
_topcall = true;
|
||||
}
|
||||
@ -24,6 +23,7 @@ void logger::msg(loglevel level) {
|
||||
}
|
||||
}
|
||||
|
||||
void logger::write_prefix() {
|
||||
std::cerr << loglevel_desc(_level) << ": ";
|
||||
void logger::write_prefix(loglevel level, const char* file, int line, const char* func) {
|
||||
std::cerr << file << ":" << line << ", " << func << "() ";
|
||||
std::cerr << '[' << loglevel_desc(level) << "] ";
|
||||
}
|
||||
|
20
logger.hpp
20
logger.hpp
@ -42,17 +42,18 @@ namespace sc {
|
||||
public:
|
||||
logger(loglevel level);
|
||||
|
||||
void msg(loglevel level);
|
||||
void msg(loglevel level, const char* file, int line, const char* func);
|
||||
|
||||
template<typename T, typename... Args>
|
||||
void msg(loglevel level, T value, Args... args) {
|
||||
void msg(loglevel level, const char* file, int line, const char* func,
|
||||
T value, Args... args) {
|
||||
if (level >= _level) {
|
||||
if (_topcall) {
|
||||
_topcall = false;
|
||||
write_prefix();
|
||||
write_prefix(level, file, line, func);
|
||||
}
|
||||
std::cerr << value;
|
||||
msg(level, args...);
|
||||
msg(level, file, line, func, args...);
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +61,10 @@ namespace sc {
|
||||
void level(loglevel level) { _level = level; }
|
||||
|
||||
private:
|
||||
void write_prefix();
|
||||
void write_prefix(loglevel level,
|
||||
const char* file,
|
||||
int line,
|
||||
const char* func);
|
||||
|
||||
loglevel _level {loglevel::info};
|
||||
bool _topcall {true};
|
||||
@ -68,4 +72,10 @@ namespace sc {
|
||||
|
||||
}
|
||||
|
||||
#define _debug(logger, ...) logger.msg(sc::loglevel::debug, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define _info(logger, ...) logger.msg(sc::loglevel::info, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define _warning(logger, ...) logger.msg(sc::loglevel::warning, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define _error(logger, ...) logger.msg(sc::loglevel::error, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
#define _critical(logger, ...) logger.msg(sc::loglevel::critical, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
|
||||
|
||||
#endif // _logger_H_
|
||||
|
Loading…
x
Reference in New Issue
Block a user