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