diff --git a/logger.cpp b/logger.cpp index 3254a8a..5dc1bc0 100644 --- a/logger.cpp +++ b/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) << "] "; } diff --git a/logger.hpp b/logger.hpp index 3e4a4b4..5a2062d 100644 --- a/logger.hpp +++ b/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 - 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_