forked from mlghuskie/NoBastian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileLogger.h
49 lines (43 loc) · 983 Bytes
/
FileLogger.h
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
#pragma once
#include "Include.h"
using namespace std;
namespace asmjs
{
class FileLogger
{
string LogFilePath;
public:
bool Enable;
HANDLE hLogFile;
string Prefix, Postfix;
FileLogger(string logFilePath) : LogFilePath(logFilePath) {}
void OpenOutput()
{
hLogFile = CreateFileA
(
LogFilePath.c_str(),
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
null,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
null
);
if (hLogFile == INVALID_HANDLE_VALUE) throw exception("Couldn't open log file.");
}
void Log(const char* format...)
{
if (Enable)
{
char buff[1024];
va_list va_alist;
va_start(va_alist, format);
vsprintf_s(buff, format, va_alist);
va_end(va_alist);
string newbuff;
newbuff = Prefix + buff + Postfix;
if (!WriteFile(hLogFile, newbuff.c_str(), newbuff.size(), null, null)) throw exception("WriteFile failed");
}
}
};
}