-
Notifications
You must be signed in to change notification settings - Fork 6
/
Logger.cpp
178 lines (168 loc) · 3.95 KB
/
Logger.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
#include "Logger.h"
Logger::Logger(void)
{
fileOpen = false;
hLog = INVALID_HANDLE_VALUE;
}
Logger::~Logger(void)
{
instanceCreated = false;
}
Logger*
Logger::getInstance()
{
if(!instanceCreated)
{
logger = new Logger();
instanceCreated = true;
}
return logger;
}
char*
Logger::convertToMultiByteString(wstring* message, size_t* charsConverted)
{
*charsConverted = 0;
int length = static_cast<int>(message->length());
int mbsize = WideCharToMultiByte(CP_UTF8, 0, message->c_str(), length, NULL, 0, NULL, NULL);
if (mbsize > 0)
{
char* szMessage = (char*)malloc(mbsize);
int bytes = WideCharToMultiByte(CP_UTF8, 0, message->c_str(), length, szMessage, mbsize, NULL, NULL);
if(bytes == 0)
{
DebugPrint(L"Logger-convertToMultiByteString: WideCharToMultiByte ERROR - %08x allocated: %i\n", GetLastError(), mbsize);
}
*charsConverted = bytes;
return szMessage;
} else {
DebugPrint(L"Logger-convertToMultiByteString: WideCharToMultiByte ERROR - %08x allocated: %i\n", GetLastError(), mbsize);
}
return NULL;
}
void
Logger::writeSystemEventToLog(wstring* type, wstring* time,
wstring* process, wstring* action,
wstring* object, vector<wstring>* extra)
{
if(isFileOpen())
{
wstring message = L"\"";
if(type->compare(L"process") == 0){
message += *time;
message += L"\",\"";
message += *type;
message += L"\",\"";
message += *action;
message += L"\",\"";
message += extra->at(0); //Parent process ID
message += L"\",\"";
message += *process;
message += L"\",\"";
message += extra->at(1); //Child process ID
message += L"\",\"";
message += *object;
}
else if (type->compare(L"registry") == 0){
message += *time;
message += L"\",\"";
message += *type;
message += L"\",\"";
message += *action;
message += L"\",\"";
message += extra->at(0); //process ID
message += L"\",\"";
message += *process;
message += L"\",\"";
message += *object;
vector<wstring>::const_iterator itr = extra->begin();
itr++;
for(itr; itr != extra->end(); itr++ ) {
message += L"\",\"";
message += *itr;
}
}
else if (type->compare(L"file") == 0) {
message += *time;
message += L"\",\"";
message += *type;
message += L"\",\"";
message += *action;
message += L"\",\"";
message += extra->at(0); //process ID
message += L"\",\"";
message += *process;
message += L"\",\"";
message += *object;
}
else{
message += L"UNKNOWN EVENT TYPE IN writeSystemEventToLog()";
}
message += L"\"\r\n";
writeToLog(&message);
}
}
void
Logger::writeToLog(wstring* message)
{
if(isFileOpen())
{
size_t charsConverted;
char* szMessage = convertToMultiByteString(message, &charsConverted);
if(szMessage != NULL)
{
DWORD bytesWritten;
WriteFile(
hLog,
szMessage,
static_cast<unsigned long>(charsConverted), // Ignore null charater
&bytesWritten,
NULL
);
free(szMessage);
}
}
}
void
Logger::openLogFile(wstring file)
{
if(!isFileOpen())
{
wchar_t* szFullPath = new wchar_t[4096];
wchar_t* pFileName;
GetFullPathName(
file.c_str(),
4096,
szFullPath,
&pFileName
);
logFullPath = szFullPath;
logFileName = pFileName;
std::transform(logFullPath.begin(),logFullPath.end(),logFullPath.begin(), towlower);
hLog = CreateFile(
szFullPath,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if(hLog != INVALID_HANDLE_VALUE)
{
DebugPrint(L"Logger-openLogFile: Log file successfully opened - %ls\n", szFullPath);
fileOpen = true;
} else {
printf("Logger: ERROR %08x - Could not open log file %ls\n", GetLastError(), szFullPath);
}
delete [] szFullPath;
}
}
void
Logger::closeLogFile()
{
if(isFileOpen())
{
CloseHandle(hLog);
fileOpen = false;
}
}