-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNLog.cpp
More file actions
114 lines (104 loc) · 1.88 KB
/
NLog.cpp
File metadata and controls
114 lines (104 loc) · 1.88 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
#include "stdafx.h"
#include "NLog.h"
NLog::NLog(CString fname, bool isFullPath, bool forDebug)
{
log = 0;
logging = false;
#ifndef DEBUG
if (forDebug) return;
#endif // DEBUG
if (!isFullPath) {
if (SUCCEEDED(SHGetFolderPath(NULL,
CSIDL_PERSONAL | CSIDL_FLAG_CREATE,
NULL,
0,
szPath)))
{
PathAppend(szPath, fname.GetBuffer());
}
}
else {
_tcscpy_s(szPath, MAX_PATH, fname.GetBuffer());
}
try {
log = new CAtlFile();
HRESULT res = log->Create(
szPath,
GENERIC_WRITE,
FILE_SHARE_READ,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL);
if (!SUCCEEDED(res)) {
delete log;
log = 0;
logging = false;
}
}
catch (...) {
logging = false;
log = 0;
}
}
NLog::~NLog()
{
Close();
}
void NLog::Start() {
if (log) {
logging = true;
SYSTEMTIME st;
GetLocalTime(&st);
CString str;
str.Format(TEXT("%04d.%02d.%02d %02d:%02d:%02d.%04d "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
Log(str);
}
}
void NLog::Stop() {
logging = false;
}
void NLog:: Log(CString msg)
{
if (logging)
{
CString str;
SYSTEMTIME st;
GetLocalTime(&st);
str.Format(TEXT("%02d:%02d:%02d.%04d "), st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
log->Write(str.GetBuffer(), str.GetLength()*sizeof(TCHAR));
log->Write(msg.GetBuffer(), msg.GetLength()*sizeof(TCHAR));
log->Write(TEXT("\r\n"), 4);
log->Flush();
}
else {
}
}
void NLog::LogF(CString msg, ...)
{
if (logging) {
va_list vl;
va_start(vl, msg);
CString str;
str.FormatV(msg, vl);
va_end(vl);
Log(str);
}
}
void NLog::Write(byte * buffer, DWORD size)
{
if (logging) {
CString str, temp;
for (DWORD i = 0; i < size; i++) {
temp.Format(TEXT("%02x "), buffer[i]);
str = str + temp;
}
CString msg;
LogF(TEXT("%d: [%s]"), size, str);
}
}
void NLog :: Close() {
if (log) {
log->Close();
log = 0;
logging = false;
}
}