2019-06-01 23:51:14 +02:00
|
|
|
#include "Log.h"
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
#include <chrono> // date/time
|
|
|
|
#include <fstream> // ofstream (logging to file)
|
|
|
|
#include <iomanip> // std::put_time
|
|
|
|
#include <iostream> // std::ostream, std::cout, std::cin
|
|
|
|
#include <vector> // list of outputs
|
|
|
|
|
2020-11-02 23:51:44 +01:00
|
|
|
#if LOG_USEMUTEX == 1
|
|
|
|
#include <mutex>
|
|
|
|
#endif
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
namespace Log {
|
|
|
|
|
2019-06-01 23:51:14 +02:00
|
|
|
/*
|
2020-09-23 20:26:44 +02:00
|
|
|
* abstract class Output
|
|
|
|
* default implementations
|
2020-09-23 18:52:49 +02:00
|
|
|
*/
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
// abstract base class for a log sink
|
|
|
|
class Output {
|
|
|
|
public:
|
|
|
|
Output() {}
|
|
|
|
Output(Level lvl_max) : lvl_max(lvl_max) {}
|
|
|
|
virtual ~Output() {}
|
|
|
|
|
|
|
|
virtual void log(Level lvl, std::stringbuf* sbuf) {
|
2020-11-02 23:51:44 +01:00
|
|
|
//aquire lock
|
|
|
|
#if LOG_USEMUTEX == 1
|
|
|
|
std::unique_lock<std::mutex> lock(ostreamLock);
|
|
|
|
#endif
|
2020-12-14 19:37:39 +01:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
std::ostream* os = getOs(lvl);
|
|
|
|
if (os) {
|
2022-07-08 01:44:32 +02:00
|
|
|
*os << sbuf << '\n';
|
2020-09-23 20:26:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void setLogLevel(Level lvl) { lvl_max = lvl; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Level lvl_max = INFO;
|
|
|
|
// returns the correct ostream for the given log-level
|
|
|
|
// or returns nullptr if no ostream is set/enabled for this level
|
|
|
|
virtual std::ostream* getOs(Level lvl) = 0; // abstract
|
2020-11-02 23:51:44 +01:00
|
|
|
|
|
|
|
#if LOG_USEMUTEX == 1
|
|
|
|
std::mutex ostreamLock; //used for both streams
|
|
|
|
#endif
|
2020-09-23 20:26:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// logging to stdout/stderr
|
|
|
|
class ConsoleOutput : public Output {
|
|
|
|
public:
|
|
|
|
ConsoleOutput() : Output() {}
|
|
|
|
|
|
|
|
virtual bool setColoredOutput(bool enabled) {
|
|
|
|
// TODO: check the terminal's compatibility for colors
|
|
|
|
coloredOutput = enabled;
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
private:
|
|
|
|
std::ostream* osStd = &std::cout;
|
|
|
|
std::ostream* osErr = &std::cerr;
|
|
|
|
bool coloredOutput;
|
2020-09-23 18:52:49 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
virtual void log(Level lvl, std::stringbuf* sbuf) {
|
|
|
|
// OFF FATAL ERROR WARN NOTE INFO DEBUG TRACE
|
|
|
|
static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"};
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-11-02 23:51:44 +01:00
|
|
|
//aquire lock
|
|
|
|
#if LOG_USEMUTEX == 1
|
|
|
|
std::unique_lock<std::mutex> lock(ostreamLock);
|
|
|
|
#endif
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
std::ostream* os = getOs(lvl);
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
if (os) {
|
|
|
|
// print colors if enabled
|
|
|
|
if (coloredOutput)
|
|
|
|
*os << "\x1B[" << color_codes[lvl] << sbuf << "\x1B[0m";
|
|
|
|
else
|
|
|
|
*os << sbuf;
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2022-07-08 01:44:32 +02:00
|
|
|
*os << '\n';
|
2020-09-23 20:26:44 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
virtual std::ostream* getOs(Level lvl) {
|
|
|
|
// out of scope?
|
|
|
|
if (!lvl || lvl > lvl_max)
|
|
|
|
return nullptr;
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
// stderr for FATAL, ERROR, WARN
|
|
|
|
if (lvl <= Level::WARN)
|
|
|
|
return osErr;
|
|
|
|
else
|
|
|
|
return osStd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileOutput : public Output {
|
|
|
|
public:
|
2022-07-04 21:09:52 +02:00
|
|
|
FileOutput(const std::string& filename, Level lvl_max, bool truncate)
|
|
|
|
: Output(lvl_max), filename(filename), ofs(filename, truncate ? std::ostream::trunc : std::ostream::app) {}
|
2020-09-23 20:26:44 +02:00
|
|
|
|
2022-07-04 21:09:52 +02:00
|
|
|
FileOutput(const std::string& filename, Level lvl_min, Level lvl_max, bool truncate)
|
2020-09-23 20:26:44 +02:00
|
|
|
: Output(lvl_max),
|
|
|
|
filename(filename),
|
2022-07-04 21:09:52 +02:00
|
|
|
ofs(filename, truncate ? std::ostream::trunc : std::ostream::app),
|
2020-09-23 20:26:44 +02:00
|
|
|
lvl_min(lvl_min) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string filename;
|
|
|
|
std::ofstream ofs;
|
|
|
|
Level lvl_min = FATAL;
|
|
|
|
|
2020-11-02 23:51:44 +01:00
|
|
|
#if LOG_USEMUTEX == 1
|
|
|
|
std::mutex ostreamLock; //used for both streams
|
|
|
|
#endif
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
virtual std::ostream* getOs(Level lvl) {
|
|
|
|
if (lvl_min <= lvl && lvl <= lvl_max)
|
|
|
|
return &ofs;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<Output*> outputs;
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void log(Level lvl, std::stringbuf* strb) {
|
2019-06-01 23:51:14 +02:00
|
|
|
for (Output* out : outputs) {
|
|
|
|
out->log(lvl, strb);
|
|
|
|
// reset stringbuffer read pointer to the beginning
|
|
|
|
strb->pubseekpos(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 21:02:31 +02:00
|
|
|
LeveledSink fatal(Level::FATAL);
|
|
|
|
LeveledSink error(Level::ERROR);
|
|
|
|
LeveledSink warn(Level::WARN);
|
|
|
|
LeveledSink note(Level::NOTE);
|
|
|
|
LeveledSink info(Level::INFO);
|
|
|
|
LeveledSink debug(Level::DEBUG);
|
|
|
|
LeveledSink trace(Level::TRACE);
|
|
|
|
|
2019-06-01 23:51:14 +02:00
|
|
|
/*
|
2020-09-23 20:26:44 +02:00
|
|
|
* class Entry
|
2019-06-01 23:51:14 +02:00
|
|
|
*/
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
Entry::Entry(Level lvl) : lvl(lvl) {
|
|
|
|
addMetadataHeader();
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
Entry::~Entry() {
|
|
|
|
log(lvl, ss.rdbuf());
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void Entry::addMetadataHeader() {
|
2019-06-01 23:51:14 +02:00
|
|
|
static const char* LevelTag[] = {"", "[FATAL] ", "[ERROR] ", "[WARN ] ",
|
|
|
|
"[NOTE ] ", "[INFO ] ", "[DEBUG] ", "[TRACE] "};
|
|
|
|
|
|
|
|
// get current date/time
|
2020-12-14 19:37:39 +01:00
|
|
|
std::time_t now = std::time(NULL);
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
// datetime
|
2020-12-14 19:37:39 +01:00
|
|
|
ss << "[" << std::put_time(std::localtime(&now), "%F %T") << "]";
|
2020-09-23 20:26:44 +02:00
|
|
|
// log level
|
|
|
|
ss << LevelTag[lvl];
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void init() {
|
|
|
|
// add default console logger
|
|
|
|
if (outputs.empty())
|
|
|
|
outputs.push_back(new ConsoleOutput());
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void stop() {
|
|
|
|
for (auto output : outputs)
|
|
|
|
delete output;
|
|
|
|
outputs.clear();
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 21:09:52 +02:00
|
|
|
void addLogfile(const std::string& filename, Level max, bool truncate) {
|
|
|
|
outputs.push_back(new FileOutput(filename, max, truncate));
|
2019-06-01 23:51:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 21:09:52 +02:00
|
|
|
void addLogfile(const std::string& filename, Level min, Level max, bool truncate) {
|
|
|
|
outputs.push_back(new FileOutput(filename, min, max, truncate));
|
2020-09-23 20:26:44 +02:00
|
|
|
}
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void setConsoleLogLevel(Level lvl) {
|
|
|
|
outputs.at(0)->setLogLevel(lvl); // has to exist
|
|
|
|
}
|
2019-06-01 23:51:14 +02:00
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
void setColoredOutput(bool enabled) {
|
|
|
|
((ConsoleOutput*) outputs.at(0))->setColoredOutput(enabled); // has to exist
|
2020-09-23 18:52:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:26:44 +02:00
|
|
|
LeveledSink::LeveledSink(Level level) : level(level) {}
|
|
|
|
|
|
|
|
} // namespace Log
|