Added color output, added dependency on libscterm

This commit is contained in:
Bob Polis 2023-08-08 12:35:37 +02:00
parent fc75a9a6f3
commit 9925f44eea
3 changed files with 43 additions and 14 deletions

View File

@ -1,4 +1,4 @@
LDLIBS := LDLIBS := -lscterm
MAJOR := 1 MAJOR := 1
MINOR := 0 MINOR := 0

View File

@ -9,18 +9,23 @@ using namespace sc;
logger::logger(const std::string& name, loglevel level) logger::logger(const std::string& name, loglevel level)
: _level(level), _name(name) {} : _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 (level >= _level) {
if (_topcall) { if (_topcall) {
write_prefix(level, file, line, func); write_prefix(level, color, file, line, func);
} else { } else {
_topcall = true; _topcall = true;
} }
std::cerr << '\n'; 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 // retrieve accurate time for milliseconds display
struct timeval secs; struct timeval secs;
(void)::gettimeofday(&secs, nullptr); (void)::gettimeofday(&secs, nullptr);
@ -29,6 +34,10 @@ void logger::write_prefix(loglevel level, const char* file, int line, const char
char dtstamp[20]; char dtstamp[20];
std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec)); std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec));
if (color) {
sc::io::rgbf col {sc::io::rgbf(const_cast<sc::io::trgb&>(*color))};
std::cerr << col;
}
std::cerr << dtstamp << "." << std::setfill('0') << std::setw(6) << secs.tv_usec << " "; std::cerr << dtstamp << "." << std::setfill('0') << std::setw(6) << secs.tv_usec << " ";
std::cerr << _name << "[" << ::getpid() << "] "; std::cerr << _name << "[" << ::getpid() << "] ";
std::cerr << file << ":" << line << " " << func << "() "; std::cerr << file << ":" << line << " " << func << "() ";

View File

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <libscterm.hpp>
namespace sc { namespace sc {
@ -30,23 +31,25 @@ namespace sc {
} }
return ""; return "";
} }
class logger { class logger {
public: public:
logger(const std::string& name, loglevel level); 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<typename T, typename... Args> template<typename T, typename... Args>
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) { T value, Args... args) {
if (level >= _level) { if (level >= _level) {
if (_topcall) { if (_topcall) {
_topcall = false; _topcall = false;
write_prefix(level, file, line, func); write_prefix(level, color, file, line, func);
} }
std::cerr << value; std::cerr << value;
msg(level, file, line, func, args...); msg(level, color, file, line, func, args...);
} }
} }
@ -55,6 +58,7 @@ namespace sc {
private: private:
void write_prefix(loglevel level, void write_prefix(loglevel level,
const sc::io::trgb* color,
const char* file, const char* file,
int line, int line,
const char* func); const char* func);
@ -66,10 +70,26 @@ namespace sc {
} }
#define SCDebug(logger, ...) logger.msg(sc::loglevel::debug, __FILE__, __LINE__, __func__, __VA_ARGS__) #define SCDebug(logger, ...) \
#define SCInfo(logger, ...) logger.msg(sc::loglevel::info, __FILE__, __LINE__, __func__, __VA_ARGS__) logger.msg(sc::loglevel::debug, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SCWarning(logger, ...) logger.msg(sc::loglevel::warning, __FILE__, __LINE__, __func__, __VA_ARGS__) #define SCInfo(logger, ...) \
#define SCError(logger, ...) logger.msg(sc::loglevel::error, __FILE__, __LINE__, __func__, __VA_ARGS__) logger.msg(sc::loglevel::info, nullptr, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define SCCritical(logger, ...) logger.msg(sc::loglevel::critical, __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_ #endif // _logger_H_