-
Notifications
You must be signed in to change notification settings - Fork 0
/
logviewer_html.cpp
235 lines (181 loc) · 6.08 KB
/
logviewer_html.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
227
228
229
230
231
232
233
234
235
/******************************************************************************
* logviewer_Html.cpp
*
* Utility to display log files in real time on and HTML file.
*
* Copyright (C) 2012-2016 Pietro Mele
* Released under a GPL 3 license.
*
*
*****************************************************************************/
#include "logviewer.hpp"
#include "CSS_default.h"
#include <cassert>
#include <fstream>
#include <iostream>
using std::endl;
namespace log_viewer {
int LogViewer::CheckLogFilesDiagnostic() const
{
int status = 0;
if(htmlOutStream.good() == true) status = 0;
if(htmlOutStream.is_open() == false) status -= 1;
if(htmlOutStream.fail() == true) status -= 10;
if(htmlOutStream.bad() == true) status -= 100;
if(htmlOutStream.eof() == true) status -= 1000;
return status;
}
int LogViewer::WriteHeader_html()
{
int n = 0;
// Write the header only if the HTML file is shorter than a fixed size
htmlOutStream.seekg(0, std::ios_base::end);
const std::streamoff size = htmlOutStream.tellg();
if(size < 10) {
htmlOutStream.seekp(0, std::ios_base::beg);
htmlOutStream << logFormatter.HeaderHTML() << endl;
//+TODO AddHtmlControls();
htmlOutStream << logFormatter.TitleHTML() << endl;
++n;
}
// Add a CSS file, if not available
const std::string outLogFilePath = outLogFile.substr(0, outLogFile.find_last_of("\\/"));
const std::string cssPath = outLogFilePath + "/logviewer.css";
{
std::ifstream check(cssPath);
if(check.good() == false) {
std::ofstream css(cssPath);
css << css_default << endl;
if(verbose > 0)
std::cout << "Written new default CSS file: " << cssPath << std::endl;
}
else
if(verbose > 0)
std::cout << "Default CSS file already exists: " << cssPath << std::endl;
}
return n;
}
int LogViewer::WriteFooter_html()
{
MoveBackToEndLogsBlock_html();
//+TODO AddHtmlControls();
htmlOutStream << logFormatter.FooterHTML() << endl;
return 1;
}
/// Log footer: move before HTML output file's closing tag
int LogViewer::MoveBackToEndLogsBlock_html()
{
// Search backwards for the end of the logs block
// Look for the last log message
/** Assumed end of HTML file structure:
...logs...
<br> <span style="color:red;">Log message</span>
<table> ... </table>
</body>
</html>
*/
using namespace std;
string token, line;
const string logsEndToken_span("</span>");
const string logsBegToken_table("<table>");
const string logsEndToken_body("</body>");
size_t posTokenLine = 0; // token position in the current line
streamoff pos = 0; // current position in the file
streamoff posNewLogs = 0; // position for the next new log
streamoff posEndToken_span = 0; // </span> token position in the file
streamoff posBegToken_table = 0; // <table> token position in the file
streamoff posBegToken_body = 0; // </body> token position in the file
long assumedFooterLength = 400; // approximation in excess
// Start from the end of the log file
htmlOutStream.seekg(0, ios_base::end);
htmlOutStream.seekp(0, ios_base::end);
// Length of the current log file
const streamsize size = htmlOutStream.tellg();
if(size < assumedFooterLength)
assumedFooterLength = size;
bool logsShown = false; // true when at least one log has been shown on the HTML page
bool lastTable = false; // to discriminate between first and last table
// Go back a fixed number of characters
htmlOutStream.seekg(-assumedFooterLength, ios_base::end);
/// Find the point where to start adding new logs
while(!htmlOutStream.eof()) //+TODO Update
{
pos = htmlOutStream.tellg();
getline(htmlOutStream, line);
//cerr << "pos: " << pos << " log: " << line << endl; //+T+
if(line.find(logsEndToken_span) != string::npos) {
posEndToken_span = htmlOutStream.tellg(); // move after the element
logsShown = true;
}
if(line.find(logsBegToken_table) != string::npos) {
if(logsShown) {
lastTable = true;
posBegToken_table = pos; // move before the element
}
}
if(line.find(logsEndToken_body) != string::npos) {
posBegToken_body = pos; // move before the element
}
}
if(posBegToken_table && lastTable)
posNewLogs = posBegToken_table;
else if(posBegToken_body)
posNewLogs = posBegToken_body;
else if(posEndToken_span)
posNewLogs = posEndToken_span;
else
posNewLogs = 0; // end of the logs block not found
//cerr << "</span> = " << posEndToken_span << "; <table> = " << posBegToken_table << "; </body> = " << posBegToken_body << endl; //+T+
//cerr << "Pos new logs = " << posNewLogs << endl; //+T+
/// Move to posNewLogs //+TODO
// Reset htmlOutStream error state flags //+?
htmlOutStream.clear();
if(posNewLogs != 0) {
//+TEST
//cerr << "Status 1 = " << CheckLogFilesDiagnostic() << endl; //+B+ EOF & FAIL
htmlOutStream.seekg(posNewLogs, ios_base::beg);
htmlOutStream.seekp(posNewLogs, ios_base::beg);
//+TEST
//cerr << "Status 2 = " << CheckLogFilesDiagnostic() << endl; //+B+ EOF & FAIL
const streamoff posg = htmlOutStream.tellg();
const streamoff posp = htmlOutStream.tellp();
//cerr << posg << " = " << posp << " = " << posNewLogs << " ???" << endl;
assert(posg == posNewLogs);
assert(posp == posNewLogs);
}
else {
// End of the logs block not found
htmlOutStream.seekg(-1, ios_base::end);
htmlOutStream.seekp(-1, ios_base::end);
}
//htmlOutStream << "xxx" << flush; //+T+ OK
// Reset htmlOutStream error state flags
htmlOutStream.clear();
return 0;
}
/// Add widgets to control/filter the HTML output
int LogViewer::AddHtmlControls()
{
//+TODO - Put real controls here
htmlOutStream
<< "\t\t<table style=\"width:100%\">"
<< " <tr>"
<< " <th>Reload</th>"
<< " <th>Min log level = </th>"
<< " <th>Number</th>"
<< " <th>Verbose</th>"
<< " <th>Show file name</th>"
<< " <th>Include</th>"
<< " <th>Exclude</th>"
<< " <th>Context</th>"
<< " <th>Width</th>"
<< " <th>Min level for context</th>"
<< " <th>Min context level</th>"
<< " <th></th>"
<< " </tr>"
<< "</table>"
<< endl;
return -1;
}
} // log_viewer