-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.h
112 lines (97 loc) · 2.38 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//(c) 2013-2016 by Authors
//This file is a part of Ragout program.
//Released under the BSD license (see LICENSE file)
#pragma once
#include <iostream>
#include <fstream>
#include <ctime>
#include <stdexcept>
class Logger
{
public:
static Logger& get()
{
static Logger instance;
return instance;
}
void setOutputFile(const std::string& filename)
{
_logFile.open(filename, std::ofstream::out | std::ofstream::app);
_logFileSet = true;
if (!_logFile.is_open())
{
throw std::runtime_error("Can't open log file");
}
}
void setDebugging(bool debug) {_debug = debug;}
class StreamWriter
{
public:
StreamWriter(const std::string& level,
std::ostream* consoleStream = nullptr,
std::ostream* fileStream = nullptr):
_fileStream(fileStream), _consoleStream(consoleStream)
{
if (_fileStream) *_fileStream << timestamp() << " " << level << " ";
if (_consoleStream) *_consoleStream << timestamp()
<< " " << level << " ";
}
~StreamWriter()
{
if (_fileStream) *_fileStream << std::endl;
if (_consoleStream) *_consoleStream << std::endl;
}
template <class T>
Logger::StreamWriter& operator<< (const T& val)
{
if (_fileStream) *_fileStream << val;
if (_consoleStream) *_consoleStream << val;
return *this;
}
private:
std::ostream* _fileStream;
std::ostream* _consoleStream;
};
StreamWriter info()
{
std::ostream* logPtr = _logFileSet ? &_logFile : nullptr;
return StreamWriter("INFO:", &std::cerr, logPtr);
}
StreamWriter warning()
{
std::ostream* logPtr = _logFileSet ? &_logFile : nullptr;
return StreamWriter("WARNING:", &std::cerr, logPtr);
}
StreamWriter error()
{
std::ostream* logPtr = _logFileSet ? &_logFile : nullptr;
return StreamWriter("ERROR:", &std::cerr, logPtr);
}
StreamWriter debug()
{
std::ostream* logPtr = _logFileSet ? &_logFile : nullptr;
std::ostream* consolePtr = _debug ? &std::cerr : nullptr;
return StreamWriter("DEBUG:", consolePtr, logPtr);
}
private:
static std::string timestamp(const char* format = "[%Y-%m-%d %H:%M:%S]")
{
std::time_t t = std::time(0);
char cstr[256];
std::strftime(cstr, sizeof(cstr), format, std::localtime(&t));
return cstr;
}
Logger():
_debug(false), _logFileSet(false)
{}
~Logger()
{
if (_logFileSet)
{
_logFile << "-----------End assembly log------------\n";
}
}
bool _debug;
bool _logFileSet;
std::ofstream _logFile;
};