1+ //
2+ // debug.cpp
3+ // ~~~~~~~~~
4+ //
5+ // Copyright (c) 2011 achellies (achellies at 163 dot com)
6+ //
7+ // This code may be used in compiled form in any way you desire. This
8+ // source file may be redistributed by any means PROVIDING it is
9+ // not sold for profit without the authors written consent, and
10+ // providing that this notice and the authors name is included.
11+ //
12+ // This file is provided "as is" with no expressed or implied warranty.
13+ // The author accepts no liability if it causes any damage to you or your
14+ // computer whatsoever. It's free, so don't hassle me about it.
15+ //
16+ // Beware of bugs.
17+ //
18+
19+ #include " stdafx.h"
20+ #include " debug.hpp"
21+ #include < winbase.h>
22+
23+ #if defined(DBG_THREADSAFE)
24+
25+ class OS_CLockable
26+ {
27+ public:
28+ // ## Constructor / Destructor
29+ OS_CLockable ();
30+ ~OS_CLockable ();
31+
32+ // ....................................................................
33+ // METHOD NAME: Acquire
34+ // DESCRIPTION:
35+ // Acquires a lock, blocking.
36+ // PARAMETERS:
37+ // none
38+ // RETURN VALUE:
39+ // none
40+ // ....................................................................
41+ void Acquire ();
42+
43+ // ....................................................................
44+ // METHOD NAME: Release
45+ // DESCRIPTION:
46+ // Releases the lock.
47+ // object.
48+ // PARAMETERS:
49+ // none
50+ // RETURN VALUE:
51+ // none
52+ // ....................................................................
53+ void Release ();
54+
55+ private:
56+ DWORD dwVersion;
57+ DWORD dwWindowsMajorVersion;
58+ DWORD dwWindowsMinorVersion;
59+ // ## Instance Variables
60+
61+ CRITICAL_SECTION m_critSec;
62+
63+ // ## declarations with no implementations (prevent accidental use)
64+
65+ OS_CLockable (const OS_CLockable&);
66+ OS_CLockable& operator = (const OS_CLockable&);
67+ };
68+
69+ // ............................................................................
70+ // CLASS NAME: OS_CAutoLock
71+ //
72+ // RESPONSIBILITIES:
73+ // Provides automatic locking and unlocking of an OS_CLockable object
74+ // within a code scope.
75+ //
76+ // NOTES:
77+ // ............................................................................
78+ class OS_CAutoLock
79+ {
80+ public:
81+
82+ // ## Constructors
83+ OS_CAutoLock (OS_CLockable &lockable) : m_lockable(lockable)
84+ {lockable.Acquire (); };
85+
86+ // ## Destructor
87+ ~OS_CAutoLock () { m_lockable.Release ();};
88+
89+ private:
90+
91+ // The controlled lockable object
92+ OS_CLockable &m_lockable;
93+
94+ // ## declarations with no implementations (prevent accidental use)
95+
96+ OS_CAutoLock ();
97+ OS_CAutoLock (const OS_CAutoLock&);
98+ OS_CAutoLock& operator = (const OS_CAutoLock&);
99+ };
100+
101+ // ----------------------------------------------------------------------------
102+ // OS_CLockable METHOD DEFINITIONS
103+ // ----------------------------------------------------------------------------
104+
105+ // ...........................................................................
106+ //
107+ // NAME: OS_CLockable::OS_CLockable (Constructor)
108+ //
109+ // IMPLEMENTATION NOTES:
110+ // ...........................................................................
111+ OS_CLockable::OS_CLockable ()
112+ {
113+ OSVERSIONINFO osvi = {0 };
114+ osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
115+ GetVersionEx (&osvi);
116+ dwWindowsMajorVersion = osvi.dwMajorVersion ;
117+ dwWindowsMinorVersion = osvi.dwMinorVersion ;
118+ if (dwWindowsMajorVersion < 6 )
119+ ::InitializeCriticalSection (&m_critSec);
120+
121+ return ;
122+ }
123+
124+ // ...........................................................................
125+ //
126+ // NAME: OS_CLockable::~OS_CLockable (Destructor)
127+ //
128+ // IMPLEMENTATION NOTES:
129+ // ...........................................................................
130+ OS_CLockable::~OS_CLockable ()
131+ {
132+ if (dwWindowsMajorVersion < 6 )
133+ ::DeleteCriticalSection (&m_critSec);
134+
135+ return ;
136+ }
137+
138+ // ...........................................................................
139+ //
140+ // NAME: OS_CLockable::Acquire
141+ //
142+ // IMPLEMENTATION NOTES:
143+ // ...........................................................................
144+ void OS_CLockable::Acquire ()
145+ {
146+ if (dwWindowsMajorVersion < 6 )
147+ ::EnterCriticalSection (&m_critSec);
148+ }
149+
150+ // ...........................................................................
151+ //
152+ // NAME: OS_CLockable::Release
153+ //
154+ // IMPLEMENTATION NOTES:
155+ // ...........................................................................
156+ void OS_CLockable::Release ()
157+ {
158+ if (dwWindowsMajorVersion < 6 )
159+ ::LeaveCriticalSection (&m_critSec);
160+ }
161+ #endif
162+
163+ #define MAX_LOG_FILE_SIZE static_cast <int >(1024 *1024 /2 )
164+
165+ #ifdef _DEBUG
166+ int g_iDebugLevel = DBG_VERBOSE;
167+ #else
168+ int g_iDebugLevel = DBG_ERROR;
169+ #endif
170+
171+ bool g_bSaveLogFile = false ;
172+
173+ TCHAR g_bLogSavePath[MAX_PATH] = _T(" \\ storage card\\ hwsys" );
174+
175+ static TCHAR s_szLogFile[MAX_PATH] = {0 };
176+ static bool s_bLogPathInit = false ;
177+
178+ #if defined(DBG_THREADSAFE)
179+ static OS_CLockable s_mtxEntry;
180+ #endif
181+
182+ BOOL DbgPrint (__in LPCTSTR lpszFormatString, ...)
183+ {
184+ BOOL bResult = TRUE ;
185+
186+ va_list VAList;
187+ va_start (VAList, lpszFormatString);
188+
189+ if (g_bSaveLogFile)
190+ {
191+ #if defined(DBG_THREADSAFE)
192+ OS_CAutoLock lock (s_mtxEntry);
193+ #endif
194+ if (!s_bLogPathInit)
195+ {
196+ SYSTEMTIME stime = {0 };
197+ GetLocalTime (&stime);
198+ #if defined(UNDER_CE)
199+ _stprintf (s_szLogFile, _T (" %s\\ log_%04d%02d%02d_%d.log" ), g_bLogSavePath, stime.wYear , stime.wMonth , stime.wDay , GetTickCount ());
200+ #else
201+ _stprintf_s (s_szLogFile, MAX_PATH, _T (" %s\\ log_%04d%02d%02d_%d.log" ), g_bLogSavePath, stime.wYear , stime.wMonth , stime.wDay , GetTickCount ());
202+ #endif
203+ s_bLogPathInit = true ;
204+ }
205+
206+ FILE* pFile = _tfopen (s_szLogFile, _T (" a" ));
207+ if (pFile != NULL )
208+ {
209+ fseek (pFile,SEEK_END,0 );
210+ long cbSize = ftell (pFile);
211+ if (cbSize > MAX_LOG_FILE_SIZE)
212+ {
213+ fclose (pFile);
214+ {
215+ SYSTEMTIME stime = {0 };
216+ GetLocalTime (&stime);
217+ #if defined(UNDER_CE)
218+ _stprintf (s_szLogFile, _T (" %s\\ log_%04d%02d%02d_%d.log" ), g_bLogSavePath, stime.wYear , stime.wMonth , stime.wDay , GetTickCount ());
219+ #else
220+ _stprintf_s (s_szLogFile, MAX_PATH, _T (" %s\\ log_%04d%02d%02d_%d.log" ), g_bLogSavePath, stime.wYear , stime.wMonth , stime.wDay , GetTickCount ());
221+ #endif
222+ s_bLogPathInit = true ;
223+ }
224+ pFile = _tfopen (s_szLogFile, _T (" a" ));
225+ if (pFile == NULL )
226+ {
227+ return FALSE ;
228+ }
229+ }
230+ _vftprintf (pFile, lpszFormatString, VAList);
231+ fflush (pFile);
232+ fclose (pFile);
233+ pFile = NULL ;
234+ }
235+ }
236+ else
237+ {
238+ #if defined(UNDER_CE)
239+ _vtprintf (lpszFormatString, VAList);
240+ #else
241+ TCHAR szBuf[MAX_PATH * 2 ] = {0 };
242+ _vstprintf_s (szBuf, MAX_PATH, lpszFormatString, VAList);
243+ OutputDebugString (szBuf);
244+ #endif
245+ }
246+ va_end (VAList);
247+
248+ return bResult;
249+ }
0 commit comments