]> Shamusworld >> Repos - ttedit/blob - src/debug.cpp
b165165e505f341d40be42a8289601cf4305758b
[ttedit] / src / debug.cpp
1 //\r
2 // DEBUG.CPP: Debugging support\r
3 // by James L. Hammons\r
4 // (C) 2002 Underground Software\r
5 //\r
6 //   JLH = James Hammons <jlhamm@acm.org>\r
7 //\r
8 // Who  When        What\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
17 //\r
18 \r
19 //#include <windows.h>\r
20 //#include <odbc.h>\r
21 #include <stdarg.h>\r
22 #include <stdio.h>\r
23 #include "debug.h"\r
24 \r
25 // EQUATES\r
26 \r
27 //#define USDB_WRITEMESSAGE  WM_USER + 1                        // Display a message on the debug window\r
28 \r
29 // Function prototypes\r
30 \r
31 //void CreateDebugWin(void);\r
32 //LRESULT CALLBACK DebugWinProc(HWND, UINT, WPARAM, LPARAM);\r
33 \r
34 // CONSTANTS\r
35 \r
36 const char logFilename[] = "debug.log";\r
37 \r
38 // DATA\r
39 \r
40 FILE * logFile = NULL;\r
41 \r
42 // UNINITIALIZED DATA\r
43 \r
44 //CRITICAL_SECTION csLock;                                              // Critical section lock\r
45 \r
46 //\r
47 // Open the debugging log file\r
48 //\r
49 void OpenDebugLog(void)\r
50 {\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
55 #ifdef DEBUGWIN\r
56         CreateDebugWin();\r
57 #endif\r
58 }\r
59 \r
60 //\r
61 // Close the debugging log file\r
62 //\r
63 void CloseDebugLog(void)\r
64 {\r
65 //      CloseHandle(hLogFile);\r
66         fclose(logFile);\r
67 //      DeleteCriticalSection(&csLock);\r
68 \r
69 //      if (hDebugWnd)\r
70 //              DestroyWindow(hDebugWnd);\r
71 }\r
72 \r
73 //\r
74 // Write a message to the log file\r
75 //\r
76 /*void WriteLogMsg(char * msg)\r
77 {\r
78         if (!msg)                                                                       // Check for valid pointer\r
79                 return;\r
80 \r
81         EnterCriticalSection(&csLock);\r
82 \r
83         if (hLogFile)\r
84                 WriteFile(hLogFile, msg, lstrlen(msg), &wfBytesWritten, NULL);\r
85 \r
86         if (hDebugWnd)\r
87                 SendMessage(hDebugWnd, USDB_WRITEMESSAGE, FALSE, (LPARAM)msg);\r
88 \r
89         LeaveCriticalSection(&csLock);\r
90 }//*/\r
91 \r
92 //\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
95 //\r
96 void WriteLogMsg(const char * msg, ...)\r
97 {\r
98         if (!msg)                                                                       // Check for valid pointer\r
99                 return;\r
100 \r
101 //      EnterCriticalSection(&csLock);\r
102 \r
103         va_list arg;\r
104 \r
105         va_start(arg, msg);\r
106 //      wvsprintf(str, msg, arg);\r
107         if (logFile)\r
108         {\r
109                 vfprintf(logFile, msg, arg);\r
110                 fflush(logFile);\r
111         }\r
112 \r
113         va_end(arg);\r
114 \r
115 //      if (hLogFile)\r
116 //              WriteFile(hLogFile, str, lstrlen(msg), &wfBytesWritten, NULL);\r
117 \r
118 //      if (hDebugWnd)\r
119 //              SendMessage(hDebugWnd, USDB_WRITEMESSAGE, FALSE, (LPARAM)str);\r
120 \r
121 //      LeaveCriticalSection(&csLock);\r
122 }\r
123 \r
124 \r
125 //\r
126 // Display a system error message on the screen\r
127 //\r
128 /*void DisplaySysError(HWND hWnd)\r
129 {\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
133         LocalFree(pBuf);\r
134 }\r
135 \r
136 //\r
137 // Create "live log" debug window\r
138 //\r
139 void CreateDebugWin(void)\r
140 {\r
141         WNDCLASS wc;\r
142 \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
149 \r
150         if (!RegisterClass(&wc))\r
151                 return;\r
152 \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
156 }\r
157 \r
158 //\r
159 // Debug "live log" window procedure\r
160 //\r
161 LRESULT CALLBACK DebugWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)\r
162 {\r
163         switch (uMsg)\r
164         {\r
165         // *****************\r
166         // *** WM_CREATE ***\r
167         // *****************\r
168 \r
169         case WM_CREATE:\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
173                 break;\r
174 \r
175         // ******************\r
176         // *** WM_DESTROY ***\r
177         // ******************\r
178 \r
179         case WM_DESTROY:\r
180                 hDebugWnd = NULL;                                               // Just in case user closes it himself...\r
181                 break;\r
182 \r
183         // ***************\r
184         // *** WM_SIZE ***\r
185         // ***************\r
186 \r
187         case WM_SIZE:\r
188                 SetWindowPos(hEdit1, NULL, 0, 0, lParam & 0xFFFF, lParam >> 16, SWP_NOMOVE | SWP_NOZORDER);\r
189                 break;\r
190 \r
191         // *************************\r
192         // *** USDB_WRITEMESSAGE ***\r
193         // *************************\r
194 \r
195         case USDB_WRITEMESSAGE:\r
196                 SendMessage(hEdit1, EM_SETSEL, -2, -2);\r
197                 SendMessage(hEdit1, EM_REPLACESEL, wParam, lParam);\r
198                 break;\r
199 \r
200         default:\r
201                 return DefWindowProc(hWnd, uMsg, wParam, lParam);\r
202         }\r
203 \r
204         return 0;\r
205 }\r
206 //*/\r