This commit is contained in:
Oliver 2019-06-02 18:54:58 +02:00
parent 762cc48220
commit 8169db351e
1 changed files with 11 additions and 11 deletions

22
Log.h
View File

@ -8,7 +8,7 @@
#include <vector> // list of outputs #include <vector> // list of outputs
class Log { class Log {
public: public:
enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE }; enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE };
// delete ctors as this class is used via static methods only // delete ctors as this class is used via static methods only
@ -22,17 +22,17 @@ class Log {
// close all output streams // close all output streams
static void stop(); static void stop();
private: private:
// abstract base class for a log sink // abstract base class for a log sink
class Output { class Output {
public: public:
Output(); Output();
Output(Log::Level lvl_max); Output(Log::Level lvl_max);
virtual ~Output(); virtual ~Output();
virtual void log(Log::Level lvl, std::stringbuf* sbuf); virtual void log(Log::Level lvl, std::stringbuf* sbuf);
virtual void setLogLevel(Log::Level lvl); virtual void setLogLevel(Log::Level lvl);
protected: protected:
Log::Level lvl_max = INFO; Log::Level lvl_max = INFO;
// returns the correct ostream for the given log-level // returns the correct ostream for the given log-level
// or returns nullptr if no ostream is set/enabled for this level // or returns nullptr if no ostream is set/enabled for this level
@ -42,11 +42,11 @@ class Log {
// logging to stdout/stderr // logging to stdout/stderr
class ConsoleOutput : public Output { class ConsoleOutput : public Output {
public: public:
ConsoleOutput(); ConsoleOutput();
virtual bool setColoredOutput(bool enabled); virtual bool setColoredOutput(bool enabled);
private: private:
std::ostream* osStd = &std::cout; std::ostream* osStd = &std::cout;
std::ostream* osErr = &std::cerr; std::ostream* osErr = &std::cerr;
bool coloredOutput; bool coloredOutput;
@ -55,11 +55,11 @@ class Log {
}; };
class FileOutput : public Output { class FileOutput : public Output {
public: public:
FileOutput(const std::string& filename, Log::Level lvl_max); FileOutput(const std::string& filename, Log::Level lvl_max);
FileOutput(const std::string& filename, Log::Level lvl_min, Log::Level lvl_max); FileOutput(const std::string& filename, Log::Level lvl_min, Log::Level lvl_max);
private: private:
std::string filename; std::string filename;
std::ofstream ofs; std::ofstream ofs;
Log::Level lvl_min = FATAL; Log::Level lvl_min = FATAL;
@ -68,7 +68,7 @@ class Log {
static std::vector<Output*> outputs; static std::vector<Output*> outputs;
public: public:
static void addLogfile(const std::string& filename, Level max); static void addLogfile(const std::string& filename, Level max);
static void addLogfile(const std::string& filename, Level min, Level max); static void addLogfile(const std::string& filename, Level min, Level max);
@ -79,11 +79,11 @@ class Log {
// by concatenation with the << operator // by concatenation with the << operator
// Inspired from https://stackoverflow.com/a/8337882 // Inspired from https://stackoverflow.com/a/8337882
class Entry { class Entry {
private: private:
std::stringstream ss; std::stringstream ss;
Log::Level lvl; Log::Level lvl;
public: public:
Entry(Log::Level lvl) : lvl(lvl) {} Entry(Log::Level lvl) : lvl(lvl) {}
Entry(Entry&&) = default; Entry(Entry&&) = default;
~Entry() { Log::log(lvl, ss.rdbuf()); } ~Entry() { Log::log(lvl, ss.rdbuf()); }