-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.cpp
162 lines (152 loc) · 6.04 KB
/
log.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "log.hpp"
#include <boost/filesystem.hpp>
#include <chrono>
#include <sstream>
#include <cstdio>
//#####################################################################################################################
static Logger logger;
//---------------------------------------------------------------------------------------------------------------------
std::string makeLogName(std::string const& filename, int rotation)
{
return filename + "_" + std::to_string(rotation) + ".txt";
}
//---------------------------------------------------------------------------------------------------------------------
void shiftLog(std::string const& filename, int rotation)
{
if (rotation <= 0)
return;
for (int i = 0; i != rotation; ++i)
{
int fromEnd = rotation - i - 1;
auto from = makeLogName(filename, fromEnd);
auto to = makeLogName(filename, fromEnd + 1);
if (boost::filesystem::exists(from))
boost::filesystem::copy_file(from, to, boost::filesystem::copy_option::overwrite_if_exists);
}
}
//---------------------------------------------------------------------------------------------------------------------
void setLogTerminalEnabled(bool enabled)
{
logger.setTerminalEnabled(enabled);
}
//#####################################################################################################################
void Logger::open(std::string const& filename, int rotation)
{
if (rotation < 0)
return;
shiftLog(filename, rotation);
auto parentDir = boost::filesystem::path{filename}.parent_path();
if (!boost::filesystem::exists(parentDir))
boost::filesystem::create_directories(parentDir);
file_.open(makeLogName(filename, 0));
}
//---------------------------------------------------------------------------------------------------------------------
void Logger::configureProjectMainFile(std::string const& directory)
{
root_ = boost::filesystem::path{directory}.parent_path().string();
}
//---------------------------------------------------------------------------------------------------------------------
void Logger::stamp(char const* file, char const* func, int line)
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::string fileStr = file;
if (!root_.empty() && fileStr.size() > root_.size() && fileStr.substr(0, root_.size()) == root_)
fileStr = fileStr.substr(root_.size(), fileStr.size() - root_.size());
std::stringstream prefixStream;
prefixStream
<< "[" << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X") << "] "
<< "(" << fileStr << ":" << line << ") "
<< "{" << func << "} "
;
write(prefixStream.str());
}
//---------------------------------------------------------------------------------------------------------------------
void Logger::manipulate(std::ios_base&(*manip)(std::ios_base&))
{
if (file_.is_open())
file_ << manip;
if (terminal_)
std::cout << manip;
}
//---------------------------------------------------------------------------------------------------------------------
void Logger::setTerminalEnabled(bool enabled)
{
terminal_ = enabled;
}
//---------------------------------------------------------------------------------------------------------------------
bool Logger::terminalEnabled()
{
return terminal_;
}
//#####################################################################################################################
Logger& LogProxy::log()
{
return logger;
}
//---------------------------------------------------------------------------------------------------------------------
void LogProxy::disable()
{
disabled_ = true;
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy::~LogProxy()
{
//if (!disabled_)
//logger.write('\n');
}
//#####################################################################################################################
LogProxy logImpl(char const* file, char const* func, int line, bool stamp)
{
if (stamp)
logger.stamp(file, func, line);
auto proxy = LogProxy{};
return {};
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, std::ios_base&(*manip)(std::ios_base&))
{
logger.manipulate(manip);
return std::move(proxy);
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, std::_Setw)
{
return std::move(proxy);
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, std::string const& value)
{
logger.write(value);
return std::move(proxy);
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, char value)
{
logger.write(value);
if (value == '\n')
proxy.disable();
return std::move(proxy);
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, std::string_view const& value)
{
logger.write(value);
return std::move(proxy);
}
//---------------------------------------------------------------------------------------------------------------------
LogProxy&& operator<<(LogProxy&& proxy, char const* value)
{
if (strcmp(value, "\n") == 0)
{
logger.write('\n');
proxy.disable();
return std::move(proxy);
}
else
{
logger.write(value);
return std::move(proxy);
}
}
//#####################################################################################################################