From 9925f44eeadc0979fc3e1b8eb97734327c9ed511 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Tue, 8 Aug 2023 12:35:37 +0200 Subject: [PATCH] Added color output, added dependency on libscterm --- premake.make | 2 +- src/logger.cpp | 15 ++++++++++++--- src/logger.hpp | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/premake.make b/premake.make index e9bdcf6..df62bc8 100644 --- a/premake.make +++ b/premake.make @@ -1,4 +1,4 @@ -LDLIBS := +LDLIBS := -lscterm MAJOR := 1 MINOR := 0 diff --git a/src/logger.cpp b/src/logger.cpp index 0b5ad20..02ca7f6 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -9,18 +9,23 @@ using namespace sc; logger::logger(const std::string& name, loglevel level) : _level(level), _name(name) {} -void logger::msg(loglevel level, const char* file, int line, const char* func) { +void logger::msg(loglevel level, const sc::io::trgb* color, + const char* file, int line, const char* func) { if (level >= _level) { if (_topcall) { - write_prefix(level, file, line, func); + write_prefix(level, color, file, line, func); } else { _topcall = true; } std::cerr << '\n'; + if (color) { + std::cerr << sc::io::reset; + } } } -void logger::write_prefix(loglevel level, const char* file, int line, const char* func) { +void logger::write_prefix(loglevel level, const sc::io::trgb* color, + const char* file, int line, const char* func) { // retrieve accurate time for milliseconds display struct timeval secs; (void)::gettimeofday(&secs, nullptr); @@ -29,6 +34,10 @@ void logger::write_prefix(loglevel level, const char* file, int line, const char char dtstamp[20]; std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec)); + if (color) { + sc::io::rgbf col {sc::io::rgbf(const_cast(*color))}; + std::cerr << col; + } std::cerr << dtstamp << "." << std::setfill('0') << std::setw(6) << secs.tv_usec << " "; std::cerr << _name << "[" << ::getpid() << "] "; std::cerr << file << ":" << line << " " << func << "() "; diff --git a/src/logger.hpp b/src/logger.hpp index 8eb18ac..c29d456 100644 --- a/src/logger.hpp +++ b/src/logger.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace sc { @@ -30,23 +31,25 @@ namespace sc { } return ""; } - + class logger { public: logger(const std::string& name, loglevel level); - void msg(loglevel level, const char* file, int line, const char* func); + void msg(loglevel level, const sc::io::trgb* color, + const char* file, int line, const char* func); template - void msg(loglevel level, const char* file, int line, const char* func, + void msg(loglevel level, const sc::io::trgb* color, + const char* file, int line, const char* func, T value, Args... args) { if (level >= _level) { if (_topcall) { _topcall = false; - write_prefix(level, file, line, func); + write_prefix(level, color, file, line, func); } std::cerr << value; - msg(level, file, line, func, args...); + msg(level, color, file, line, func, args...); } } @@ -55,6 +58,7 @@ namespace sc { private: void write_prefix(loglevel level, + const sc::io::trgb* color, const char* file, int line, const char* func); @@ -66,10 +70,26 @@ namespace sc { } -#define SCDebug(logger, ...) logger.msg(sc::loglevel::debug, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define SCInfo(logger, ...) logger.msg(sc::loglevel::info, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define SCWarning(logger, ...) logger.msg(sc::loglevel::warning, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define SCError(logger, ...) logger.msg(sc::loglevel::error, __FILE__, __LINE__, __func__, __VA_ARGS__) -#define SCCritical(logger, ...) logger.msg(sc::loglevel::critical, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCDebug(logger, ...) \ + logger.msg(sc::loglevel::debug, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCInfo(logger, ...) \ + logger.msg(sc::loglevel::info, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCWarning(logger, ...) \ + logger.msg(sc::loglevel::warning, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCError(logger, ...) \ + logger.msg(sc::loglevel::error, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCCritical(logger, ...) \ + logger.msg(sc::loglevel::critical, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__) + +#define SCDebugC(logger, color, ...) \ + logger.msg(sc::loglevel::debug, &color, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCInfoC(logger, color, ...) \ + logger.msg(sc::loglevel::info, &color, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCWarningC(logger, color, ...) \ + logger.msg(sc::loglevel::warning, &color, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCErrorC(logger, color, ...) \ + logger.msg(sc::loglevel::error, &color, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define SCCriticalC(logger, color, ...) \ + logger.msg(sc::loglevel::critical, &color, __FILE__, __LINE__, __func__, __VA_ARGS__) #endif // _logger_H_