Let's make (git) history!

此提交包含在:
2018-11-03 17:12:20 +01:00
當前提交 3bfebfe8a8
共有 212 個檔案被更改,包括 11970 行新增0 行删除

71
logger.h 一般檔案
查看文件

@@ -0,0 +1,71 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <iostream>
#include <chrono>
class Logger
{
private:
class LogEntry
{
bool headerSent;
std::ostream *out;
std::string prefix;
public:
LogEntry(std::ostream *out, std::string prefix) : out(out), prefix(prefix)
{
}
template <class T> LogEntry &operator<<(const T &val)
{
if(out == nullptr)
return *this;
if(!headerSent)
{
(*out) << time(0) << " " << prefix;
}
(*out) << val;
headerSent = true;
return *this; // or maybe out itself? probably not.
}
~LogEntry()
{
if(out != nullptr)
{
(*out) << std::endl;
(*out).flush();
}
}
};
public:
static std::ostream *out;
static int logLevel;
static void setStream(std::ostream *out)
{
Logger::out = out;
}
static LogEntry debug()
{
if(Logger::logLevel >= 3)
return LogEntry(out, "Debug: ");
return LogEntry(nullptr, "");
}
static LogEntry error()
{
return LogEntry(out, "Error: ");
}
static LogEntry log()
{
if(Logger::logLevel >= 2)
return LogEntry(out, "Log: ");
return LogEntry(nullptr, "");
}
};
#endif // LOGGER_H