Let's make (git) history!
此提交包含在:
71
logger.h
一般檔案
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
|
新增問題並參考
封鎖使用者