--- np2/win9xc/trace.cpp 2003/10/16 17:59:19 1.1.1.1 +++ np2/win9xc/trace.cpp 2004/03/07 01:23:14 1.9 @@ -2,12 +2,8 @@ #include #include "strres.h" #include "dosio.h" - - -// #define WRITE_INI -#if defined(WRITE_INI) #include "ini.h" -#endif + #ifdef TRACE @@ -25,6 +21,12 @@ #define VIEW_SIZE 12 typedef struct { + BYTE en; + FILEH fh; + HWND hwnd; +} TRACEWIN; + +typedef struct { int posx; int posy; int width; @@ -34,27 +36,34 @@ typedef struct { extern HINSTANCE hInst; extern HINSTANCE hPrev; +enum { + IDM_TRACEEN = 3300, + IDM_TRACEFH, + IDM_TRACECL +}; + static const char ProgTitle[] = "console"; static const char ClassName[] = "TRACE-console"; static const char ClassEdit[] = "EDIT"; +static const char traceen[] = "Enable"; +static const char tracefh[] = "File out"; +static const char tracecl[] = "Clear"; static const char crlf[] = "\r\n"; -static HWND hWndConsole = NULL; +static TRACEWIN tracewin; static HWND hView = NULL; static HFONT hfView = NULL; static HBRUSH hBrush = NULL; static char szView[VIEW_BUFFERSIZE]; static TRACECFG tracecfg; -#if defined(WRITE_INI) +static const char np2trace[] = "np2trace.ini"; static const char inititle[] = "TRACE"; static const INITBL initbl[4] = { - {str_posx, INITYPE_SINT32, &tracecfg.posx, 0}, - {str_posy, INITYPE_SINT32, &tracecfg.posy, 0}, - {str_width, INITYPE_SINT32, &tracecfg.width, 0}, - {str_height, INITYPE_SINT32, &tracecfg.height, 0}}; -#endif - + {"posx", INITYPE_SINT32, &tracecfg.posx, 0}, + {"posy", INITYPE_SINT32, &tracecfg.posy, 0}, + {"width", INITYPE_SINT32, &tracecfg.width, 0}, + {"height", INITYPE_SINT32, &tracecfg.height, 0}}; static void View_ScrollToBottom(HWND hWnd) { @@ -65,7 +74,13 @@ static void View_ScrollToBottom(HWND hWn PostMessage(hWnd, EM_LINESCROLL, 0, MaxPos); } -static void View_AddString(char *lpszString) { +static void View_ClrString(void) { + + szView[0] = '\0'; + SetWindowText(hView, szView); +} + +static void View_AddString(const char *lpszString) { int len, vlen; char *p; @@ -94,9 +109,22 @@ static void View_AddString(char *lpszStr static LRESULT CALLBACK traceproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { RECT rc; + HMENU hmenu; switch (msg) { case WM_CREATE: + hmenu = GetSystemMenu(hWnd, FALSE); + InsertMenu(hmenu, 0, MF_BYPOSITION | MF_STRING, + IDM_TRACEEN, traceen); + InsertMenu(hmenu, 1, MF_BYPOSITION | MF_STRING, + IDM_TRACEFH, tracefh); + InsertMenu(hmenu, 2, MF_BYPOSITION | MF_STRING, + IDM_TRACECL, tracecl); + InsertMenu(hmenu, 3, MF_BYPOSITION | MF_SEPARATOR, 0, NULL); + + CheckMenuItem(hmenu, IDM_TRACEEN, + (tracewin.en & 1)?MF_CHECKED:MF_UNCHECKED); + GetClientRect(hWnd, &rc); hView = CreateWindowEx(WS_EX_CLIENTEDGE, ClassEdit, NULL, @@ -121,6 +149,38 @@ static LRESULT CALLBACK traceproc(HWND h SetFocus(hView); return(TRUE); + case WM_SYSCOMMAND: + switch(wp) { + case IDM_TRACEEN: + tracewin.en ^= 1; + hmenu = GetSystemMenu(hWnd, FALSE); + CheckMenuItem(hmenu, IDM_TRACEEN, + (tracewin.en & 1)?MF_CHECKED:MF_UNCHECKED); + break; + + case IDM_TRACEFH: + if (tracewin.fh != FILEH_INVALID) { + file_close(tracewin.fh); + tracewin.fh = FILEH_INVALID; + } + else { + tracewin.fh = file_create("traceout.txt"); + } + hmenu = GetSystemMenu(hWnd, FALSE); + CheckMenuItem(hmenu, IDM_TRACEFH, + (tracewin.fh != FILEH_INVALID)? + MF_CHECKED:MF_UNCHECKED); + break; + + case IDM_TRACECL: + View_ClrString(); + break; + + default: + return(DefWindowProc(hWnd, msg, wp, lp)); + } + break; + case WM_MOVE: if (!(GetWindowLong(hWnd, GWL_STYLE) & (WS_MAXIMIZE | WS_MINIMIZE))) { @@ -185,6 +245,8 @@ static LRESULT CALLBACK traceproc(HWND h void trace_init(void) { + HWND hwnd; + if (!hPrev) { WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; @@ -202,48 +264,94 @@ void trace_init(void) { } } +#if 1 + tracewin.en = 0; + tracewin.fh = FILEH_INVALID; +#else + tracewin.en = 0; + tracewin.fh = file_create_c("traces.txt"); +#endif + tracecfg.posx = CW_USEDEFAULT; tracecfg.posy = CW_USEDEFAULT; tracecfg.width = CW_USEDEFAULT; tracecfg.height = CW_USEDEFAULT; -#if defined(WRITE_INI) - ini_read(NULL, inititle, initbl, 4); -#endif + ini_read(file_getcd(np2trace), inititle, initbl, 4); - hWndConsole = CreateWindowEx(WS_EX_CONTROLPARENT, + hwnd = CreateWindowEx(WS_EX_CONTROLPARENT, ClassName, ProgTitle, WS_OVERLAPPEDWINDOW, tracecfg.posx, tracecfg.posy, tracecfg.width, tracecfg.height, NULL, NULL, hInst, NULL); - if (!hWndConsole) { + tracewin.hwnd = hwnd; + if (hwnd == NULL) { return; } - ShowWindow(hWndConsole, SW_SHOW); - UpdateWindow(hWndConsole); + ShowWindow(hwnd, SW_SHOW); + UpdateWindow(hwnd); } void trace_term(void) { - if (hWndConsole) { - DestroyWindow(hWndConsole); - hWndConsole = NULL; -#if defined(WRITE_INI) - ini_write(NULL, inititle, initbl, 4); -#endif + if (tracewin.fh != FILEH_INVALID) { + file_close(tracewin.fh); + tracewin.fh = FILEH_INVALID; + } + if (tracewin.hwnd) { + DestroyWindow(tracewin.hwnd); + tracewin.hwnd = NULL; + ini_write(file_getcd(np2trace), inititle, initbl, 4); } } void trace_fmt(const char *fmt, ...) { - char buf[0x1000]; + BOOL en; va_list ap; + char buf[0x1000]; + va_start(ap, fmt); + vsprintf(buf, fmt, ap); + va_end(ap); if (hView) { + View_AddString(buf); + } + if (tracewin.fh != FILEH_INVALID) { + file_write(tracewin.fh, buf, strlen(buf)); + file_write(tracewin.fh, crlf, strlen(crlf)); + } +} + +void trace_fmt2(const char *fmt, ...) { + + BOOL en; + va_list ap; + char buf[0x1000]; + + en = (tracewin.en & 1) || (tracewin.fh != FILEH_INVALID); + if (en) { va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); - View_AddString(buf); + if ((tracewin.en & 1) && (hView)) { + View_AddString(buf); + } + if (tracewin.fh != FILEH_INVALID) { + file_write(tracewin.fh, buf, strlen(buf)); + file_write(tracewin.fh, crlf, strlen(crlf)); + } + } +} + +void trace_fileout(const char *fname) { + + if (tracewin.fh != FILEH_INVALID) { + file_close(tracewin.fh); + tracewin.fh = FILEH_INVALID; + } + if (fname) { + tracewin.fh = file_create_c(fname); } }