2 // DEBUG.CPP: Debugging support
\r
3 // by James L. Hammons
\r
4 // (C) 2002 Underground Software
\r
6 // JLH = James Hammons <jlhamm@acm.org>
\r
9 // --- ---------- ------------------------------------------------------------
\r
10 // JLH 07/31/2002 Created this file
\r
11 // JLH 07/31/2002 Added debug log functions & system error logging functions
\r
12 // JLH 08/16/2002 Added debug log function for SQL error reporting, made
\r
13 // WriteLogMsg thread safe
\r
14 // JLH 12/10/2002 Added code to have a background message window
\r
15 // JLH 05/14/2004 Converted code to C++ (sans ODBC logging)
\r
16 // JLH 05/15/2005 Converted code to generic C++
\r
19 //#include <windows.h>
\r
27 //#define USDB_WRITEMESSAGE WM_USER + 1 // Display a message on the debug window
\r
29 // Function prototypes
\r
31 //void CreateDebugWin(void);
\r
32 //LRESULT CALLBACK DebugWinProc(HWND, UINT, WPARAM, LPARAM);
\r
36 const char logFilename[] = "debug.log";
\r
40 FILE * logFile = NULL;
\r
42 // UNINITIALIZED DATA
\r
44 //CRITICAL_SECTION csLock; // Critical section lock
\r
47 // Open the debugging log file
\r
49 void OpenDebugLog(void)
\r
51 // hLogFile = CreateFile(logFilename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
\r
52 // NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH | FILE_ATTRIBUTE_NORMAL, NULL);
\r
53 // InitializeCriticalSection(&csLock);
\r
54 logFile = fopen(logFilename, "wb");
\r
61 // Close the debugging log file
\r
63 void CloseDebugLog(void)
\r
65 // CloseHandle(hLogFile);
\r
67 // DeleteCriticalSection(&csLock);
\r
70 // DestroyWindow(hDebugWnd);
\r
74 // Write a message to the log file
\r
76 /*void WriteLogMsg(char * msg)
\r
78 if (!msg) // Check for valid pointer
\r
81 EnterCriticalSection(&csLock);
\r
84 WriteFile(hLogFile, msg, lstrlen(msg), &wfBytesWritten, NULL);
\r
87 SendMessage(hDebugWnd, USDB_WRITEMESSAGE, FALSE, (LPARAM)msg);
\r
89 LeaveCriticalSection(&csLock);
\r
93 // This logger is used mainly to ensure that text gets written to the log file
\r
94 // even if the program crashes. The performance hit is acceptable in this case!
\r
96 void WriteLogMsg(const char * msg, ...)
\r
98 if (!msg) // Check for valid pointer
\r
101 // EnterCriticalSection(&csLock);
\r
105 va_start(arg, msg);
\r
106 // wvsprintf(str, msg, arg);
\r
109 vfprintf(logFile, msg, arg);
\r
116 // WriteFile(hLogFile, str, lstrlen(msg), &wfBytesWritten, NULL);
\r
119 // SendMessage(hDebugWnd, USDB_WRITEMESSAGE, FALSE, (LPARAM)str);
\r
121 // LeaveCriticalSection(&csLock);
\r
126 // Display a system error message on the screen
\r
128 /*void DisplaySysError(HWND hWnd)
\r
130 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
\r
131 GetLastError(), 1024, pBuf, 0, NULL);
\r
132 MessageBox(hWnd, pBuf, errTitle, MB_ICONERROR);
\r
137 // Create "live log" debug window
\r
139 void CreateDebugWin(void)
\r
143 RtlZeroMemory(&wc, sizeof(wc));
\r
144 wc.lpfnWndProc = DebugWinProc;
\r
145 wc.hInstance = GetModuleHandle(NULL);
\r
146 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
\r
147 wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
\r
148 wc.lpszClassName = CNDebug;
\r
150 if (!RegisterClass(&wc))
\r
153 hDebugWnd = CreateWindowEx(NULL, CNDebug, debugWin,
\r
154 WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_SYSMENU,
\r
155 0, 0, 400, 400, NULL, NULL, NULL, NULL);
\r
159 // Debug "live log" window procedure
\r
161 LRESULT CALLBACK DebugWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
165 // *****************
\r
166 // *** WM_CREATE ***
\r
167 // *****************
\r
170 hEdit1 = CreateWindowEx(NULL, CNEdit, NULL,
\r
171 WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_READONLY | ES_MULTILINE | ES_AUTOVSCROLL,
\r
172 0, 0, 1, 1, hWnd, NULL, NULL, NULL);
\r
175 // ******************
\r
176 // *** WM_DESTROY ***
\r
177 // ******************
\r
180 hDebugWnd = NULL; // Just in case user closes it himself...
\r
188 SetWindowPos(hEdit1, NULL, 0, 0, lParam & 0xFFFF, lParam >> 16, SWP_NOMOVE | SWP_NOZORDER);
\r
191 // *************************
\r
192 // *** USDB_WRITEMESSAGE ***
\r
193 // *************************
\r
195 case USDB_WRITEMESSAGE:
\r
196 SendMessage(hEdit1, EM_SETSEL, -2, -2);
\r
197 SendMessage(hEdit1, EM_REPLACESEL, wParam, lParam);
\r
201 return DefWindowProc(hWnd, uMsg, wParam, lParam);
\r