1 /* trace_nt.c - Debugging routines
2 for Midnight Commander, under Win32
4 Written 951215 by Juan Grigera <grigera@isis.unlp.edu.ar>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 /* Global variables */
31 int __win32_tracing_enabled
= 1;
33 static int _win32_tracing_started
= 0;
34 static FILE *__win32_trace_f
= NULL
;
37 #define TRACE_FILE "mcTrace.out"
39 /* Prototypes - static funcs */
40 static void _win32InitTrace (void);
41 static void _win32EndTrace (void);
42 static const char* GetLastErrorText(void);
43 static char *visbuf(const char *buf
);
47 void _win32InitTrace()
48 This func will open file TRACE_FILE for output and add _win32EndTrace to onexit
51 static void _win32InitTrace()
53 if (!_win32_tracing_started
) {
54 _win32_tracing_started
= 1;
56 __win32_trace_f
= fopen(TRACE_FILE
, "wt");
57 if (__win32_trace_f
== NULL
) {
58 printf("Midnight Commander[DEBUG]: Can't open trace file '" TRACE_FILE
"': %s \n", strerror(errno
));
60 atexit (&_win32EndTrace
);
66 This func closes file TRACE_FILE if opened.
68 static void _win32EndTrace()
70 if (_win32_tracing_started
) {
71 _win32_tracing_started
= 0;
73 fclose (__win32_trace_f
);
79 void _win32Trace (char *fmt, ...)
80 Format and output debug strings. They are written to TRACE_FILE.
81 Debug Output is controlled by SetTrace (see below).
82 Win32: Output is sent to Debug Output also.
84 void _win32Trace (const char *fmt
, ...)
91 if (!_win32_tracing_started
)
95 vsprintf(buffer
, fmt
, ap
);
98 #ifdef _OS_NT /* Write Output to Debug monitor also */
99 OutputDebugString (vp
);
100 #if (_MSC_VER > 800) /* Don't write newline in MSVC++ 1.0, has a dammed bug in Debug Output screen */
101 OutputDebugString ("\n");
106 fprintf (__win32_trace_f
, "%s\n", vp
);
110 void SetTrace (int trace)
111 Control debug output. Turn it of or on.
112 trace: 0 = off, 1 = on.
114 void _win32SetTrace (int trace
)
116 /* Prototypes - interlan funcs */
117 __win32_tracing_enabled
= trace
;
119 void _win32TraceOn ()
121 __win32_tracing_enabled
= 1;
123 void _win32TraceOff()
125 __win32_tracing_enabled
= 0;
131 void DebugFailedWin32APICall (const char* name, int line, const char* file)
132 Report a System call failure.
133 name - text containing the source code that called the offending API func
134 line, file - place of "name" in code
136 See Also: definition of win32APICALL macro.
138 void _win32DebugFailedWin32APICall (const char* name
, int line
, const char* file
)
140 _win32Trace ("%s(%d): Call to Win32 API Failed. \"%s\".", file
, line
, name
);
141 _win32Trace (" System Error (%d): %s. ", GetLastError(), GetLastErrorText());
146 void DebugAssertionFailed (const char* name, int line, const char* file)
147 Report a logical condition failure. (e.g. a bad argument to a func)
148 name - text containing the logical condition
149 line, file - place of "name" in code
151 See Also: definition of ASSERT macro.
153 void _win32DebugAssertionFailed (const char* name
, int line
, const char* file
)
155 _win32Trace ("%s(%d): Assertion failed! \"%s\".", file
, line
, name
);
159 /* const char* GetLastErrorText()
160 Retrieves the text associated with the last system error.
162 Returns pointer to static buffer. Contents valid till next call
164 static const char* GetLastErrorText()
166 #define MAX_MSG_SIZE 256
167 static char szMsgBuf
[MAX_MSG_SIZE
];
168 DWORD dwError
, dwRes
;
170 dwError
= GetLastError ();
172 dwRes
= FormatMessage (
173 FORMAT_MESSAGE_FROM_SYSTEM
,
176 MAKELANGID (LANG_ENGLISH
, SUBLANG_ENGLISH_US
),
181 sprintf (szMsgBuf
, "FormatMessage failed with %d", GetLastError());
186 #endif /*HAVE_TRACE*/