-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogFormatter.cpp
226 lines (164 loc) · 5.17 KB
/
LogFormatter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/******************************************************************************
* LogFormatter.cpp
*
* Get a plain log message and format it according to its destination (e.g. text, HTML, ...).
*
* Copyright (C) 2012-2016 Pietro Mele
* Released under a GPL 3 license.
*
*
*****************************************************************************/
#include "LogFormatter.hpp"
#include "textModeFormatting.h"
#include <sstream>
namespace log_viewer {
const std::string LogFormatter::availableFormats = "plain console HTML markdown";
std::string LogFormatter::defaultFormats = "console";
LogFormatter::LogFormatter()
: formatPlain(false), formatConsole(false), formatHtml(false), formatMarkdown(false)
{
SetFormats(defaultFormats);
}
LogFormatter::LogFormatter(std::string &_formats)
: formatPlain(false), formatConsole(false), formatHtml(false), formatMarkdown(false)
{
SetFormats(_formats);
}
LogFormatter::LogFormatter(std::string &_formats,
const std::string &_title)
: formatPlain(false), formatConsole(false), formatHtml(false), formatMarkdown(false)
{
SetFormats(_formats);
SetTitle(_title);
}
int LogFormatter::SetFormats(std::string &_formats)
{
int nValidFormats = CheckFormats(_formats);
if(nValidFormats > 0)
formats = _formats;
else
{
formats = defaultFormats;
nValidFormats = -1;
}
if(formats.find("plain") != std::string::npos) formatPlain = true;
else formatPlain = false;
if(formats.find("console") != std::string::npos) formatConsole = true;
else formatConsole = false;
if(formats.find("HTML") != std::string::npos ||
formats.find("html") != std::string::npos) formatHtml = true;
else formatHtml = false;
if(formats.find("markdown") != std::string::npos) formatMarkdown = true;
else formatMarkdown = false;
return nValidFormats;
}
int LogFormatter::SetFormats(std::string &_formats, bool &_consoleOutput, bool &_textFileOutput, bool &_htmlOutput, bool &_markdownOutput)
{
int r = SetFormats(_formats);
_consoleOutput = formatConsole;
_textFileOutput = formatPlain;
_htmlOutput = formatHtml;
_markdownOutput = formatMarkdown;
return r;
}
int LogFormatter::SetFormats(const char *_formats)
{
std::string f(_formats);
return SetFormats(f);
}
// Filter invalid formats; return number of valid formats
int LogFormatter::CheckFormats(std::string &_formats) const
{
int nValidFormats = 0;
std::string validFormats;
std::stringstream formats(_formats);
std::string format;
while(formats >> format)
{
if(availableFormats.find(format) != std::string::npos) {
++nValidFormats;
validFormats.append(format);
validFormats.append(" ");
}
}
_formats = validFormats;
return nValidFormats;
}
// Log message formatters
std::string LogFormatter::Format(const std::string &_log,
int _level,
const std::string &_file,
char _tag,
int _logNumber) const
{
if(formatPlain) return FormatPlain(_log, _level, _file, _tag, _logNumber);
else if(formatConsole) return FormatConsole(_log, _level, _file, _tag, _logNumber);
else if(formatHtml) return FormatHTML(_log, _level, _file, _tag, _logNumber);
else if(formatMarkdown) return FormatMarkdown(_log, _level, _file, _tag, _logNumber);
return FormatConsole(_log, _level, _file, _tag, _logNumber);
}
std::string LogFormatter::FormatPlain(const std::string &_log,
int _level,
const std::string &_file,
char _tag,
int _logNumber) const
{
std::string fLog;
if(!_file.empty())
fLog += _file + ": ";
if(_logNumber > 0)
fLog += std::to_string(_logNumber) + ": ";
fLog += _tag + _log;
return fLog;
}
std::string LogFormatter::FormatConsole(const std::string &_log,
int _level,
const std::string &_file,
char _tag,
int _logNumber) const
{
using namespace textModeFormatting;
using textModeFormatting::Format;
std::string fLog(Reset());
if(!_file.empty())
fLog += _file + ": ";
if(_logNumber > 0)
fLog += std::to_string(_logNumber) + ": ";
#ifndef _WIN32
fLog += std::string(Format(ny));
#endif
fLog += _tag
+ std::string(Format(_level))
+ _log
+ std::string(Reset());
return fLog;
}
std::string LogFormatter::FormatMarkdown(const std::string &_log,
int _level,
const std::string &_file,
char _tag,
int _logNumber) const
{
//+TODO
return _log;
}
// Headers
std::string LogFormatter::Header() const
{
if(formats == "plain") return HeaderPlain();
else if(formats == "console") return HeaderConsole();
else if(formats == "HTML") return HeaderHTML();
else if(formats == "markdown") return HeaderMarkdown();
return HeaderPlain();
}
// Footers
std::string LogFormatter::Footer() const
{
if(formats == "plain") return FooterPlain();
else if(formats == "console") return FooterConsole();
else if(formats == "HTML") return FooterHTML();
else if(formats == "markdown") return FooterMarkdown();
return FooterPlain();
}
} // log_viewer