-
Notifications
You must be signed in to change notification settings - Fork 69
/
winDebugWindow.c
338 lines (267 loc) · 8.09 KB
/
winDebugWindow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "pharovm/pharo.h"
#include <Windows.h>
#include <shellapi.h>
#include <CommCtrl.h>
#include "pharovm/win/resources.h"
HWND debugWindowHWND;
long int logLength;
long int logPosition;
#define LOGBUFFER_SIZE 4195
#define LOG_LINES 29
#define APP_REFRESHLOG (WM_APP + 0)
char* logBuffer;
WCHAR* logBufferWide;
long int logBufferUsedSize;
extern int printCallStack();
extern int printAllStacks();
void updateLogText();
void updateFromScrollBar(int request);
void openLogFile(HWND hwnd){
char logFile[MAX_PATH + 1];
WCHAR logFileWide[MAX_PATH + 1];
getErrorLogNameInto(logFile, MAX_PATH + 1);
if(MultiByteToWideChar(CP_UTF8, 0, logFile, -1, logFileWide, MAX_PATH + 1) <= 0){
logErrorFromGetLastError("Converting logFile name");
return;
}
ShellExecuteW(hwnd, TEXT("open"), logFileWide, NULL, NULL, SW_SHOW);
}
INT_PTR DebugWindow_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case APP_REFRESHLOG: {
updateLogText();
break;
}
case WM_SIZE: {
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
logDebug("Resize %d, %d", width, height);
break;
}
case WM_CLOSE: {
EndDialog(hwnd,0);
debugWindowHWND = NULL;
free(logBuffer);
free(logBufferWide);
break;
}
case WM_VSCROLL:{
if((HWND)lParam == GetDlgItem(hwnd, IDC_LOGTEXT_SCROLL)){
updateFromScrollBar(LOWORD(wParam));
}
break;
}
case WM_COMMAND:{
switch(LOWORD(wParam)){
case IDC_CALLSTACK:
vm_printf("\nActive Stack trace:\n\n");
printCallStack();
updateLogText();
break;
case IDC_ALLSTACKS:
vm_printf("\nAll Stack traces (First is active one):\n\n");
printAllStacks();
updateLogText();
break;
case IDC_OPENLOG:
openLogFile(hwnd);
break;
case IDC_LOGLEVEL:
switch(HIWORD(wParam)){
case CBN_SELCHANGE: {
int newLevel = SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_GETCURSEL, (WPARAM)0, (LPARAM)0);
if(newLevel != CB_ERR && newLevel >= 0 && newLevel <= 5){
logDebug("Setting new logLevel to: %d", newLevel);
logLevel(newLevel);
}
break;
}
}
break;
}
break;
}
default:
return FALSE;
}
return TRUE;
}
void updateLogLevelComboBox(){
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("None"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("Error"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("Warning"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("Info"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("Debug"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_ADDSTRING, (WPARAM)0, (LPARAM)TEXT("Trace"));
SendDlgItemMessageW(debugWindowHWND, IDC_LOGLEVEL, CB_SETCURSEL, (WPARAM) getLogLevel(), (LPARAM)0);
}
void scrollToPosition(){
/*
* We move the position of the log to the logPosition variable.
* We copy the buffer to a newone transforming the line ending from \n to \r\n
*/
FILE *log = getErrorLogFile();
char logBuffer2[LOGBUFFER_SIZE * 2 + 1];
fflush(log);
fseek(log, logPosition, SEEK_SET);
logBufferUsedSize = fread(logBuffer, sizeof(char), LOGBUFFER_SIZE, log);
fseek(log, 0, SEEK_END);
if(logBufferUsedSize != -1){
logBuffer[logBufferUsedSize] = '\0';
long logLimit = strlen(logBuffer);
long logIndex = 0;
long logIndex2 = 0;
int linesCount = 0;
/*
* We want only LOG_LINES lines.
* We start from the end.
*
* only if the position is not 0.
* If it is 0 we start from the beginning.
*/
if(logPosition != 0){
logIndex = logLimit - 1;
while(logIndex > 0 && linesCount < LOG_LINES){
if(logBuffer[logIndex] == '\n'){
linesCount++;
}
logIndex --;
}
if(logIndex + 2 <= logLimit){
logIndex += 2;
}
}
/*
* If the position of the log is not the beginning, we look for the first line
* so we jump the first semi complete line.
* We are in the start of the buffer, and we are not sure if we have a complete line
*
* We walk until the next newline and add 1 to jump the newline
*/
if(logPosition != 0 && logIndex == 0){
while(logIndex < logLimit && logBuffer[logIndex] != '\n'){
logIndex++;
}
if(logIndex < logLimit){
logIndex++;
}
}
while(logIndex <= logLimit){
if(logBuffer[logIndex] == '\n'){
logBuffer2[logIndex2] = '\r';
logIndex2++;
}
logBuffer2[logIndex2] = logBuffer[logIndex];
logIndex ++;
logIndex2 ++;
}
MultiByteToWideChar(CP_UTF8, 0, logBuffer2, -1, logBufferWide, LOGBUFFER_SIZE + 1);
SetDlgItemTextW(debugWindowHWND, IDC_LOGTEXT, logBufferWide);
/*
* Scroll the Edit control to the end, just in case the edit control is showing more lines than expecting.
* We have the number of lines fixed by the size of the window.
* That is why the window has fixed size
*/
SendDlgItemMessageW(debugWindowHWND, IDC_LOGTEXT, EM_SETSEL, 0, -1); //Select all.
SendDlgItemMessageW(debugWindowHWND, IDC_LOGTEXT, EM_SETSEL, -1, -1);//Unselect and stay at the end pos
SendDlgItemMessageW(debugWindowHWND, IDC_LOGTEXT, EM_SCROLLCARET, 0, 0); //Set scrollcaret to the current Pos
}
}
void updateLogText(){
SCROLLINFO scrollInfo;
FILE *log = getErrorLogFile();
logLength = ftell(log);
logPosition = logLength - LOGBUFFER_SIZE;
if(logPosition < 0)
logPosition = 0;
scrollInfo.cbSize = sizeof(SCROLLINFO);
scrollInfo.fMask = SIF_RANGE | SIF_POS;
scrollInfo.nMin = 0;
scrollInfo.nMax = logLength;
scrollInfo.nPos = logPosition;
SetScrollInfo(GetDlgItem(debugWindowHWND, IDC_LOGTEXT_SCROLL), SB_CTL, &scrollInfo, TRUE);
scrollToPosition();
}
void updateFromScrollBar(int request){
SCROLLINFO scrollInfo;
scrollInfo.cbSize = sizeof(SCROLLINFO);
if(logLength == 0)
return;
switch(request){
case SB_TOP:
logPosition = 0;
break;
case SB_BOTTOM:
logPosition = logLength - 1;
break;
case SB_PAGEDOWN:
case SB_LINEDOWN:
logPosition = logPosition + 10;
break;
case SB_PAGEUP:
case SB_LINEUP:
logPosition = logPosition - 10 > 0 ? logPosition - 10 : 0;
break;
case SB_THUMBPOSITION:
case SB_THUMBTRACK:{
scrollInfo.fMask = SIF_TRACKPOS | SIF_POS;
GetScrollInfo(GetDlgItem(debugWindowHWND, IDC_LOGTEXT_SCROLL), SB_CTL, &scrollInfo);
if(logPosition != scrollInfo.nPos){
logPosition = scrollInfo.nPos;
}else{
logPosition = scrollInfo.nTrackPos;
}
break;
}
default:
return;
}
if(logPosition >= logLength)
logPosition = logLength - 1;
if(logPosition < 0)
logPosition = 0;
scrollInfo.fMask = SIF_POS;
scrollInfo.nPos = logPosition;
/*
* Accept the new position
*/
SetScrollInfo(GetDlgItem(debugWindowHWND, IDC_LOGTEXT_SCROLL), SB_CTL, &scrollInfo, TRUE);
scrollToPosition();
}
EXPORT(void) openDebugWindow(void* parentHwnd){
WCHAR windowTitle[255];
char logFile[MAX_PATH + 1];
WCHAR logFileWide[MAX_PATH + 1];
logLength = 0;
logPosition = 0;
logBufferUsedSize = 0;
logBuffer = (char*) malloc(sizeof(char) * (LOGBUFFER_SIZE + 1));
logBufferWide = (WCHAR*) malloc(sizeof(WCHAR) * (LOGBUFFER_SIZE + 1));
memset(logBuffer, 0, sizeof(char) * (LOGBUFFER_SIZE + 1));
memset(logBufferWide, 0, sizeof(WCHAR) * (LOGBUFFER_SIZE + 1));
if(debugWindowHWND != NULL)
return;
debugWindowHWND = CreateDialogParamW(
GetModuleHandle(NULL),
MAKEINTRESOURCEW(IDD_DEBUG),
(HWND)parentHwnd,
DebugWindow_WindowProc,
0);
if(debugWindowHWND == NULL){
logErrorFromGetLastError("Creating Window");
return;
}
_snwprintf(windowTitle, 255, TEXT("Pharo Debug - PID: %d"), GetCurrentProcessId());
SendMessageW(debugWindowHWND, WM_SETTEXT, (WPARAM) 0, (LPARAM)windowTitle);
getErrorLogNameInto(logFile, MAX_PATH + 1);
MultiByteToWideChar(CP_UTF8, 0, logFile, -1, logFileWide, MAX_PATH + 1);
SetDlgItemTextW(debugWindowHWND, IDC_LOGFILE, logFileWide);
updateLogLevelComboBox();
updateLogText();
ShowWindow(debugWindowHWND, SW_NORMAL | SW_SHOW);
}
EXPORT(void) notifyDebugWindow(){
if(debugWindowHWND){
SendMessageW(debugWindowHWND, APP_REFRESHLOG, (WPARAM)0, (LPARAM)0);
}
}