Log/Log.h

64 lines
1.4 KiB
C++

#pragma once
#include <sstream> // std::stringstream (buffer for log entries)
#include <string>
namespace Log {
enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE };
// set up the logger with a ConsoleOutput
void init();
// close all output streams
void stop();
void addLogfile(const std::string& filename, Level max);
void addLogfile(const std::string& filename, Level min, Level max);
void setConsoleLogLevel(Level lvl);
void setColoredOutput(bool enabled);
// Log entry that can be formed with various mixed data types
// by concatenation with the << operator
// Inspired from https://stackoverflow.com/a/8337882
class Entry {
private:
std::stringstream ss;
Level lvl;
void addMetadataHeader();
public:
Entry(Level lvl);
Entry(Entry&&) = default;
~Entry();
template <typename T> Entry& operator<<(const T& msg) {
ss << msg;
return *this;
}
};
class LeveledSink {
private:
Level level;
public:
LeveledSink(Level level);
template <typename T> Entry operator<<(const T& msg) {
Entry entry(level);
entry << msg;
return entry;
}
};
// LeveledSinks
static LeveledSink fatal(Level::FATAL);
static LeveledSink error(Level::ERROR);
static LeveledSink warn(Level::WARN);
static LeveledSink note(Level::NOTE);
static LeveledSink info(Level::INFO);
static LeveledSink debug(Level::DEBUG);
static LeveledSink trace(Level::TRACE);
} // namespace Log