-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlogger.h
76 lines (67 loc) · 1.74 KB
/
logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#pragma once
#include <string>
#include <fstream>
#include <sstream>
#include <mutex>
struct tm;
namespace logger
{
enum class Level { Debug, Info, Warning, Error, Fatal };
class FileLogger;
class ConsoleLogger;
class BaseLogger;
class BaseLogger
{
class LogStream;
public:
BaseLogger() = default;
virtual ~BaseLogger() = default;
virtual LogStream operator()(Level nLevel = Level::Debug);
private:
const tm* getLocalTime();
void endline(Level nLevel, std::string&& oMessage);
virtual void output(const tm *p_tm,
const char *str_level,
const char *str_message) = 0;
private:
std::mutex _lock;
tm _localTime;
};
class BaseLogger::LogStream : public std::ostringstream
{
BaseLogger& m_oLogger;
Level m_nLevel;
public:
LogStream(BaseLogger& oLogger, Level nLevel)
: m_oLogger(oLogger), m_nLevel(nLevel) {};
LogStream(const LogStream& ls)
: m_oLogger(ls.m_oLogger), m_nLevel(ls.m_nLevel) {};
~LogStream()
{
m_oLogger.endline(m_nLevel, std::move(str()));
}
};
class ConsoleLogger : public BaseLogger
{
using BaseLogger::BaseLogger;
virtual void output(const tm *p_tm,
const char *str_level,
const char *str_message);
};
class FileLogger : public BaseLogger
{
public:
FileLogger(std::string filename) noexcept;
FileLogger(const FileLogger&) = delete;
FileLogger(FileLogger&&) = delete;
virtual ~FileLogger();
private:
virtual void output(const tm *p_tm,
const char *str_level,
const char *str_message);
private:
std::ofstream _file;
};
extern ConsoleLogger debug;
extern FileLogger record;
} // namespace logger