forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileIO.cpp
More file actions
355 lines (319 loc) · 10 KB
/
Copy pathFileIO.cpp
File metadata and controls
355 lines (319 loc) · 10 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <fstream>
#include <algorithm>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include "Win32/Win32Lib.h"
#include "Win32/Utilities.h"
#include "CobaltFusion/stringbuilder.h"
#include "DebugViewppLib/LogFile.h"
#include "DebugViewppLib/FileIO.h"
#include "DebugViewppLib/Conversions.h"
#include <boost/lexical_cast.hpp>
namespace fusion {
namespace debugviewpp {
class TabSplitter
{
public:
explicit TabSplitter(const std::string& text) :
m_it(text.begin()),
m_end(text.end())
{
}
std::string GetNext()
{
auto it = std::find(m_it, m_end, '\t');
std::string s(m_it, it);
m_it = it == m_end ? it : it + 1;
return s;
}
std::string GetTail() const
{
return std::string(m_it, m_end);
}
private:
std::string::const_iterator m_it;
std::string::const_iterator m_end;
};
FILETIME MakeFileTime(uint64_t t)
{
uint32_t mask = ~0U;
FILETIME ft;
ft.dwHighDateTime = (t >> 32) & mask;
ft.dwLowDateTime = t & mask;
return ft;
}
// very old debugview++ version wrote the clocktime time as a 64-bit number
FILETIME MakeFileTimeFromV1TimeStamp(const std::string& text)
{
return MakeFileTime(boost::lexical_cast<uint64_t>(text));
}
FILETIME MakeFileTimeFromDateTime(const std::string& text)
{
auto st = SYSTEMTIME();
std::istringstream is(text);
char c1;
char c2;
char c3;
char c4;
char c5;
char c6;
if (!((is >> st.wYear >> c1 >> st.wMonth >> c2 >> st.wDay >> std::noskipws >> c3 >> st.wHour >> c4 >> st.wMinute >> c5 >> st.wSecond >> c6 >> st.wMilliseconds) && c1 == '/' && c2 == '/' && c3 == ' ' && c4 == ':' && c5 == ':' && c6 == '.'))
{
st = Win32::FileTimeToSystemTime(FILETIME());
}
return Win32::LocalFileTimeToFileTime(Win32::SystemTimeToFileTime(st));
}
FILETIME MakeFileTimeBackwardsCompatible(const std::string& text)
{
if (text.find('/') != std::string::npos)
{
return MakeFileTimeFromDateTime(text);
}
return MakeFileTimeFromV1TimeStamp(text);
}
bool ReadTime(const std::string& s, double& time)
{
std::istringstream is(s);
return is >> time && is.eof();
}
bool FileExists(const char* filename)
{
std::ifstream ifile(filename);
return ifile.is_open();
}
std::string FileTypeToString(FileType::type value)
{
switch (value)
{
case FileType::DebugViewPP1: return "DebugView++ Logfile v1";
case FileType::DebugViewPP2: return "DebugView++ Logfile v2";
case FileType::Sysinternals: return "Sysinternals Debugview Logfile";
case FileType::AsciiText: return "ASCII text file";
case FileType::UTF8: return "Unicode UTF-8";
case FileType::UTF16BE: return "Unicode UTF16BE";
case FileType::UTF16LE: return "Unicode UTF16LE";
default: break;
}
return "Unimplemented file type";
}
FileType::type IdentifyFile(const std::wstring& filename)
{
{
// Encoding detection is very complex, see #107
std::ifstream fs(filename, std::ios::binary);
std::vector<unsigned char> buffer(3);
fs.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
if (buffer[0] == 0xfe && buffer[1] == 0xff)
{
return FileType::UTF16BE;
}
if (buffer[0] == 0xff && buffer[1] == 0xfe)
{
return FileType::UTF16LE;
}
if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
{
return FileType::UTF8;
}
}
std::ifstream is(filename);
std::string line;
if (!std::getline(is, line))
{
return FileType::Unknown;
}
// first we check for our own header
auto trimmed = boost::trim_copy_if(line, boost::is_any_of(" \r\n\t"));
if (boost::ends_with(trimmed, g_debugViewPPIdentification1))
{
return FileType::DebugViewPP1;
}
if (boost::ends_with(trimmed, g_debugViewPPIdentification2))
{
return FileType::DebugViewPP2;
}
// if the extension is .txt (and we did not find our own header)
// we say it is an ascii-file.
if (boost::iends_with(filename, ".txt"))
{
return FileType::AsciiText;
}
// .log files are potentially sysinternals dbgview files
if (boost::iends_with(filename, ".log"))
{
// to test for sysinternals debugview-logfiles, we need to check the second line
// since the first line contains the computer-name in some cases (depending on how it was saved)
// logfiles with only one line are not that interesting anyway.
if (!std::getline(is, line))
{
return FileType::AsciiText;
}
// if the second line contains 2 or 3 tabs characters, we say it's a sysinternals debugview-logfile
// two kinds of lines are logged, kernel message lines and process message lines, the latter have an extra PID Columnn
auto tabs = std::count(line.begin(), line.end(), '\t');
if (tabs == 2 || tabs == 3)
{
return FileType::Sysinternals;
}
}
return FileType::AsciiText;
}
bool IsBinaryFileType(FileType::type filetype)
{
switch (filetype)
{
case FileType::UTF16BE:
case FileType::UTF16LE:
case FileType::UTF8:
return true;
default:
return false;
}
}
// read localtime in format "HH:mm:ss.ms"
bool ReadLocalTimeMs(const std::string& text, FILETIME& ft)
{
std::istringstream is(text);
WORD h;
WORD m;
WORD s;
WORD ms;
char c1;
char c2;
char d1;
if (!((is >> h >> c1 >> m >> c2 >> s >> d1 >> ms) && c1 == ':' && c2 == ':' && d1 == '.'))
{
return false;
}
SYSTEMTIME st = Win32::FileTimeToSystemTime(FILETIME());
st.wHour = h;
st.wMinute = m;
st.wSecond = s;
st.wMilliseconds = ms;
ft = Win32::LocalFileTimeToFileTime(Win32::SystemTimeToFileTime(st));
return true;
}
// read localtime in format "HH:mm:ss"
bool ReadLocalTime(const std::string& text, FILETIME& ft)
{
std::istringstream is(text);
WORD h;
WORD m;
WORD s;
char c1;
char c2;
if (!((is >> h >> c1 >> m >> c2 >> s) && c1 == ':' && c2 == ':'))
{
return false;
}
SYSTEMTIME st = Win32::FileTimeToSystemTime(FILETIME());
st.wHour = h;
st.wMinute = m;
st.wSecond = s;
st.wMilliseconds = 0;
ft = Win32::LocalFileTimeToFileTime(Win32::SystemTimeToFileTime(st));
return true;
}
std::istream& ReadLogFileMessage(std::istream& is, Line& line)
{
std::string data;
if (!std::getline(is, data))
{
return is;
}
if (!ReadLogFileMessage(data, line))
{
is.setstate(std::ios_base::failbit);
}
return is;
}
bool ReadSysInternalsLogFileMessage(const std::string& data, Line& line, USTimeConverter& converter)
{
TabSplitter split(data);
auto col1 = split.GetNext();
auto col2 = split.GetNext();
auto col3 = split.GetTail();
// depending on regional settings Sysinternals debugview logs time differently.
// we support the four most common formats
// 'hh:MM:SS.mmm tt', 'hh:MM:SS tt', 'HH:MM:SS.mmm' and 'HH:MM:SS'
if (!converter.ReadLocalTimeUSRegionMs(col2, line.systemTime))
{ // try hh:MM:SS.mmm tt
if (!converter.ReadLocalTimeUSRegion(col2, line.systemTime))
{ // try hh:MM:SS tt
if (!ReadLocalTimeMs(col2, line.systemTime))
{ // try HH:MM:SS.mmm
if (!ReadLocalTime(col2, line.systemTime))
{ // try HH:MM:SS
ReadTime(col2, line.time); // otherwise assume relative time: S.mmmmmm
}
}
}
}
if (!col3.empty() && col3[0] == '[') // messages from processes are preceeded by [pid], but kernel messages do not have a prefix
{
line.processName = "[unavailable]";
std::istringstream is3(col3);
char c1;
char c2;
char c3;
if (is3 >> std::noskipws >> c1 >> line.pid >> c2 >> c3 && c1 == '[' && c2 == ']' && c3 == ' ' && std::getline(is3, line.message))
{
return true;
}
}
else
{
line.processName = "[kernel]";
}
line.message = split.GetTail();
return true;
}
bool ReadLogFileMessage(const std::string& data, Line& line)
{
try
{
TabSplitter split(data);
line.time = std::stod(split.GetNext());
line.systemTime = MakeFileTimeBackwardsCompatible(split.GetNext());
line.pid = std::stoul(split.GetNext());
line.processName = split.GetNext();
line.message = split.GetTail();
}
catch (std::exception& ex)
{
line.message = stringbuilder() << "Exception: '" << ex.what() << "' occurred processing line: " << data;
}
return true;
}
std::ostream& operator<<(std::ostream& os, const FILETIME& ft)
{
uint64_t hi = ft.dwHighDateTime;
uint64_t lo = ft.dwLowDateTime;
return os << ((hi << 32) | lo);
}
void OpenLogFile(std::ofstream& ofstream, const std::wstring& filename, OpenMode::type mode)
{
ofstream.open(filename, mode == OpenMode::Truncate ? std::ofstream::trunc : std::ofstream::app);
if (mode == OpenMode::Truncate)
{
// intentionally maintain the same amount of Columnns, so it is always easy to parse by csv import tools
WriteLogFileMessage(ofstream, 0, Win32::GetSystemTimeAsFileTime(), 0, "DebugView++.exe", g_debugViewPPIdentification1);
}
}
std::string GetOffsetText(double time) // todo: use libfmt ? consider speed here, so maybe not ?
{
char buf[32];
sprintf_s(buf, "%.06f", time);
return buf;
}
void WriteLogFileMessage(std::ofstream& ofstream, double time, FILETIME filetime, DWORD pid, const std::string& processName, std::string message)
{
boost::trim_right_if(message, boost::is_any_of(" \r\n\t"));
ofstream << GetOffsetText(time) << "\t" << GetDateTimeText(filetime) << "\t" << pid << "\t" << processName << "\t" << message << "\n";
}
} // namespace debugviewpp
} // namespace fusion