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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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]: Cannot 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
);
99 #ifdef NATIVE_WIN32 /* Write Output to Debug monitor also */
100 OutputDebugString (vp
);
101 #if (_MSC_VER > 800) /* Don't write newline in MSVC++ 1.0, has a dammed bug in Debug Output screen */
102 OutputDebugString ("\n");
107 fprintf (__win32_trace_f
, "%s\n", vp
);
111 void SetTrace (int trace)
112 Control debug output. Turn it of or on.
113 trace: 0 = off, 1 = on.
115 void _win32SetTrace (int trace
)
117 /* Prototypes - interlan funcs */
118 __win32_tracing_enabled
= trace
;
120 void _win32TraceOn ()
122 __win32_tracing_enabled
= 1;
124 void _win32TraceOff()
126 __win32_tracing_enabled
= 0;
132 void DebugFailedWin32APICall (const char* name, int line, const char* file)
133 Report a System call failure.
134 name - text containing the source code that called the offending API func
135 line, file - place of "name" in code
137 See Also: definition of win32APICALL macro.
139 void _win32DebugFailedWin32APICall (const char* name
, int line
, const char* file
)
141 _win32Trace ("%s(%d): Call to Win32 API Failed. \"%s\".", file
, line
, name
);
142 _win32Trace (" System Error (%d): %s. ", GetLastError(), GetLastErrorText());
147 void DebugAssertionFailed (const char* name, int line, const char* file)
148 Report a logical condition failure. (e.g. a bad argument to a func)
149 name - text containing the logical condition
150 line, file - place of "name" in code
152 See Also: definition of ASSERT macro.
154 void _win32DebugAssertionFailed (const char* name
, int line
, const char* file
)
156 _win32Trace ("%s(%d): Assertion failed! \"%s\".", file
, line
, name
);
160 /* const char* GetLastErrorText()
161 Retrieves the text associated with the last system error.
163 Returns pointer to static buffer. Contents valid till next call
165 static const char* GetLastErrorText()
167 #define MAX_MSG_SIZE 256
168 static char szMsgBuf
[MAX_MSG_SIZE
];
169 DWORD dwError
, dwRes
;
171 dwError
= GetLastError ();
173 dwRes
= FormatMessage (
174 FORMAT_MESSAGE_FROM_SYSTEM
,
177 MAKELANGID (LANG_ENGLISH
, SUBLANG_ENGLISH_US
),
182 sprintf (szMsgBuf
, "FormatMessage failed with %d", GetLastError());
187 #endif /*HAVE_TRACE*/