-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform log.cpp
More file actions
executable file
·76 lines (64 loc) · 2.47 KB
/
platform log.cpp
File metadata and controls
executable file
·76 lines (64 loc) · 2.47 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
// Include statements
#include <windows.h>
#include <windef.h>
#include <atlstr.h>
#include <shlobj.h>
#include "resource.h"
#include "program.h"
#include "class.h"
#include "function.h"
// Takes a name like "Backup" without the trailing ".html" extension
// Composes the path to the log file with the given name next to this running exe
string LogPath(read name) {
WCHAR bay[MAX_PATH];
lstrcpy(bay, L"");
GetModuleFileName(NULL, bay, MAX_PATH);
return make(before(bay, L"\\", Reverse), L"\\", string(name), L".html");
}
// Create a new log file at the given path and open it
HANDLE LogOpen(read path, read title) {
// Open the file there or create one there and open it
HANDLE file = CreateFile(
LongPath(path), // Path and file name
GENERIC_WRITE, // Only need to write
0, // No sharing
NULL,
CREATE_ALWAYS, // Create a new file here or overwrite one already here
FILE_ATTRIBUTE_NORMAL, // Normal attributes
NULL);
if (file == INVALID_HANDLE_VALUE) return NULL;
// Write the headers
LogAppend(file, L"\ufeff"); // Start with the 2 bytes ff fe, with ff first, to tell programs that unicode characters follow
LogAppend(file, L"<html><head><title>Backup</title></head><body><pre style=\"font: 8pt Courier New\">\r\n"); // HTML header for web browsers
LogAppend(file, make(title, L"\r\n")); // Text headers for the user
LogAppend(file, L"\r\n");
LogAppend(file, make(saydate(L":"), L" start\r\n"));
LogAppend(file, L"----------------------------------------\r\n");
// Return the open file handle
return file;
}
// Takes the handle to an open log file and a line of text that ends with "\r\n"
// Appends the text to the end of the file and moves the file pointer past it
bool LogAppend(HANDLE file, read r) {
// Write it to the file
DWORD bytes = length(r) * sizeof(WCHAR);
DWORD written = 0;
int result = WriteFile(
file, // Open file handle
(LPVOID)r, // Pointer to data
bytes, // Number of bytes there to write
&written, // Number of bytes written
NULL);
if (!result || written != bytes) return false;
return true;
}
// Close the given open log file
bool LogClose(HANDLE file) {
// Write the footers
LogAppend(file, L"----------------------------------------\r\n");
LogAppend(file, make(saydate(L":"), L" end\r\n"));
LogAppend(file, L"</pre></body></html>\r\n");
// Close the file
if (!CloseHandle(file)) return false;
return true;
}