added convenience macros, improved output

This commit is contained in:
Bob Polis 2020-04-29 16:40:04 +02:00
parent 5272dc7edd
commit 0cc1d98579
2 changed files with 21 additions and 11 deletions

View File

@ -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) << "] ";
}

View File

@ -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_