Repository navigation
Expand file tree
/
Copy pathdebug.cpp
More file actions
249 lines (215 loc) · 6.17 KB
/
Copy pathdebug.cpp
File metadata and controls
249 lines (215 loc) · 6.17 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//
// debug.cpp
// ~~~~~~~~~
//
// Copyright (c) 2011 achellies (achellies at 163 dot com)
//
// This code may be used in compiled form in any way you desire. This
// source file may be redistributed by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Beware of bugs.
//
#include "stdafx.h"
#include "debug.hpp"
#include <winbase.h>
#if defined(DBG_THREADSAFE)
class OS_CLockable
{
public:
// ## Constructor / Destructor
OS_CLockable();
~OS_CLockable();
// ....................................................................
// METHOD NAME: Acquire
// DESCRIPTION:
// Acquires a lock, blocking.
// PARAMETERS:
// none
// RETURN VALUE:
// none
// ....................................................................
void Acquire();
// ....................................................................
// METHOD NAME: Release
// DESCRIPTION:
// Releases the lock.
// object.
// PARAMETERS:
// none
// RETURN VALUE:
// none
// ....................................................................
void Release();
private:
DWORD dwVersion;
DWORD dwWindowsMajorVersion;
DWORD dwWindowsMinorVersion;
// ## Instance Variables
CRITICAL_SECTION m_critSec;
// ## declarations with no implementations (prevent accidental use)
OS_CLockable (const OS_CLockable&);
OS_CLockable& operator= (const OS_CLockable&);
};
// ............................................................................
// CLASS NAME: OS_CAutoLock
//
// RESPONSIBILITIES:
// Provides automatic locking and unlocking of an OS_CLockable object
// within a code scope.
//
// NOTES:
// ............................................................................
class OS_CAutoLock
{
public:
// ## Constructors
OS_CAutoLock(OS_CLockable &lockable) : m_lockable(lockable)
{lockable.Acquire(); };
// ## Destructor
~OS_CAutoLock() { m_lockable.Release();};
private:
// The controlled lockable object
OS_CLockable &m_lockable;
// ## declarations with no implementations (prevent accidental use)
OS_CAutoLock ();
OS_CAutoLock (const OS_CAutoLock&);
OS_CAutoLock& operator= (const OS_CAutoLock&);
};
// ----------------------------------------------------------------------------
// OS_CLockable METHOD DEFINITIONS
// ----------------------------------------------------------------------------
// ...........................................................................
//
// NAME: OS_CLockable::OS_CLockable (Constructor)
//
// IMPLEMENTATION NOTES:
// ...........................................................................
OS_CLockable::OS_CLockable()
{
OSVERSIONINFO osvi = {0};
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
dwWindowsMajorVersion = osvi.dwMajorVersion;
dwWindowsMinorVersion = osvi.dwMinorVersion;
if(dwWindowsMajorVersion < 6)
::InitializeCriticalSection(&m_critSec);
return;
}
// ...........................................................................
//
// NAME: OS_CLockable::~OS_CLockable (Destructor)
//
// IMPLEMENTATION NOTES:
// ...........................................................................
OS_CLockable::~OS_CLockable()
{
if(dwWindowsMajorVersion < 6)
::DeleteCriticalSection(&m_critSec);
return;
}
// ...........................................................................
//
// NAME: OS_CLockable::Acquire
//
// IMPLEMENTATION NOTES:
// ...........................................................................
void OS_CLockable::Acquire()
{
if(dwWindowsMajorVersion < 6)
::EnterCriticalSection(&m_critSec);
}
// ...........................................................................
//
// NAME: OS_CLockable::Release
//
// IMPLEMENTATION NOTES:
// ...........................................................................
void OS_CLockable::Release()
{
if(dwWindowsMajorVersion < 6)
::LeaveCriticalSection(&m_critSec);
}
#endif
#define MAX_LOG_FILE_SIZE static_cast<int>(1024*1024/2)
#ifdef _DEBUG
int g_iDebugLevel = DBG_VERBOSE;
#else
int g_iDebugLevel = DBG_ERROR;
#endif
bool g_bSaveLogFile = false;
TCHAR g_bLogSavePath[MAX_PATH] = _T("C:\\log");
static TCHAR s_szLogFile[MAX_PATH] = {0};
static bool s_bLogPathInit = false;
#if defined(DBG_THREADSAFE)
static OS_CLockable s_mtxEntry;
#endif
BOOL DbgPrint(__in LPCTSTR lpszFormatString, ...)
{
BOOL bResult = TRUE;
va_list VAList;
va_start(VAList, lpszFormatString);
if (g_bSaveLogFile)
{
#if defined(DBG_THREADSAFE)
OS_CAutoLock lock(s_mtxEntry);
#endif
if (!s_bLogPathInit)
{
SYSTEMTIME stime = {0};
GetLocalTime(&stime);
#if defined(UNDER_CE)
_stprintf(s_szLogFile, _T("%s\\log_%04d%02d%02d_%d.log"), g_bLogSavePath, stime.wYear, stime.wMonth, stime.wDay, GetTickCount());
#else
_stprintf_s(s_szLogFile, MAX_PATH, _T("%s\\log_%04d%02d%02d_%d.log"), g_bLogSavePath, stime.wYear, stime.wMonth, stime.wDay, GetTickCount());
#endif
s_bLogPathInit = true;
}
FILE* pFile = _tfopen(s_szLogFile, _T("a"));
if (pFile != NULL)
{
fseek(pFile,SEEK_END,0);
long cbSize = ftell(pFile);
if (cbSize > MAX_LOG_FILE_SIZE)
{
fclose(pFile);
{
SYSTEMTIME stime = {0};
GetLocalTime(&stime);
#if defined(UNDER_CE)
_stprintf(s_szLogFile, _T("%s\\log_%04d%02d%02d_%d.log"), g_bLogSavePath, stime.wYear, stime.wMonth, stime.wDay, GetTickCount());
#else
_stprintf_s(s_szLogFile, MAX_PATH, _T("%s\\log_%04d%02d%02d_%d.log"), g_bLogSavePath, stime.wYear, stime.wMonth, stime.wDay, GetTickCount());
#endif
s_bLogPathInit = true;
}
pFile = _tfopen(s_szLogFile, _T("a"));
if (pFile == NULL)
{
return FALSE;
}
}
_vftprintf(pFile, lpszFormatString, VAList);
fflush(pFile);
fclose(pFile);
pFile = NULL;
}
}
else
{
#if defined(UNDER_CE)
_vtprintf(lpszFormatString, VAList);
#else
TCHAR szBuf[MAX_PATH * 2] = {0};
_vstprintf_s(szBuf, MAX_PATH, lpszFormatString, VAList);
OutputDebugString(szBuf);
#endif
}
va_end(VAList);
return bResult;
}