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