-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cpp
More file actions
executable file
·154 lines (122 loc) · 3.48 KB
/
Copy pathLog.cpp
File metadata and controls
executable file
·154 lines (122 loc) · 3.48 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
// Log.cpp: implementation of the Log class.
//
//////////////////////////////////////////////////////////////////////
#include "stdhdrs.h"
#include "Log.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
const int Log::ToDebug = 1;
const int Log::ToFile = 2;
const int Log::ToConsole = 4;
const static int LINE_BUFFER_SIZE = 1024;
Log::Log(int mode, int level, LPTSTR filename, bool append)
{
hlogfile = NULL;
m_todebug = false;
m_toconsole = false;
m_tofile = false;
SetMode(mode);
if (mode & ToFile) {
SetFile(filename, append);
}
}
void Log::SetMode(int mode) {
if (mode & ToDebug)
m_todebug = true;
else
m_todebug = false;
if (mode & ToFile) {
m_tofile = true;
} else {
CloseFile();
m_tofile = false;
}
#ifdef _WIN32_WCE
m_toconsole = false;
#else
if (mode & ToConsole) {
if (!m_toconsole)
AllocConsole();
m_toconsole = true;
} else {
m_toconsole = false;
}
#endif
}
void Log::SetLevel(int level) {
m_level = level;
}
void Log::SetFile(LPTSTR filename, bool append)
{
// if a log file is open, close it now.
CloseFile();
m_tofile = true;
// If filename is NULL or invalid we should throw an exception here
hlogfile = CreateFile(
filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if (hlogfile == NULL) {
// We should throw an exception here
m_todebug = true;
m_tofile = false;
Print(0, _T("Error opening log file %s\n"), filename);
}
if (append) {
SetFilePointer( hlogfile, 0, NULL, FILE_END );
} else {
SetEndOfFile( hlogfile );
}
}
// if a log file is open, close it now.
void Log::CloseFile() {
if (hlogfile != NULL) {
CloseHandle(hlogfile);
hlogfile = NULL;
}
}
#ifndef UNDER_CE
// Non-CE version
void Log::ReallyPrint(LPTSTR format, va_list ap)
{
TCHAR line[LINE_BUFFER_SIZE];
_vstprintf(line, format, ap);
if (m_todebug) OutputDebugString(line);
if (m_toconsole) {
DWORD byteswritten;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), line, _tcslen(line)*sizeof(TCHAR), &byteswritten, NULL);
};
if (m_tofile && (hlogfile != NULL)) {
DWORD byteswritten;
WriteFile(hlogfile, line, _tcslen(line)*sizeof(TCHAR), &byteswritten, NULL);
}
}
#else
// CE version
void Log::ReallyPrint(LPTSTR format, va_list ap)
{
TCHAR line[LINE_BUFFER_SIZE];
_vstprintf(line, format, ap);
if (m_todebug) OutputDebugString(line);
if (m_tofile && (hlogfile != NULL)) {
DWORD byteswritten;
// Log file is more readable if non-unicode!
char ansiline[LINE_BUFFER_SIZE];
int origlen = _tcslen(line);
int newlen = WideCharToMultiByte(
CP_ACP, // code page
0, // performance and mapping flags
line, // address of wide-character string
origlen, // number of characters in string
ansiline, // address of buffer for new string
255, // size of buffer
NULL, NULL );
WriteFile(hlogfile, ansiline, newlen, &byteswritten, NULL);
}
}
#endif
Log::~Log()
{
CloseFile();
}
Log theLog;