Added color output, added dependency on libscterm
This commit is contained in:
parent
fc75a9a6f3
commit
9925f44eea
@ -1,4 +1,4 @@
|
||||
LDLIBS :=
|
||||
LDLIBS := -lscterm
|
||||
|
||||
MAJOR := 1
|
||||
MINOR := 0
|
||||
|
@ -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<sc::io::trgb&>(*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 << "() ";
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <libscterm.hpp>
|
||||
|
||||
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<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) {
|
||||
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_
|
||||
|
Loading…
x
Reference in New Issue
Block a user