]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: log.cpp | |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
dd85fc6b | 19 | |
c801d85f KB |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "log.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | // wxWindows | |
32 | #ifndef WX_PRECOMP | |
0c589ad0 | 33 | #include "wx/window.h" |
dd85fc6b VZ |
34 | #ifdef __WXMSW__ |
35 | #include "wx/msw/private.h" | |
36 | #endif | |
dbf3cd7a RR |
37 | #include "wx/event.h" |
38 | #include "wx/app.h" | |
39 | #include "wx/string.h" | |
40 | #include "wx/intl.h" | |
dd85fc6b VZ |
41 | #ifndef wxUSE_NOGUI |
42 | #include "wx/msgdlg.h" | |
43 | #endif | |
9ef3052c | 44 | #endif //WX_PRECOMP |
c801d85f | 45 | |
dbf3cd7a RR |
46 | #include "wx/file.h" |
47 | #include "wx/textfile.h" | |
48 | #include "wx/utils.h" | |
a324a7bc | 49 | #include "wx/wxchar.h" |
dbf3cd7a | 50 | #include "wx/log.h" |
c801d85f KB |
51 | |
52 | // other standard headers | |
53 | #include <errno.h> | |
54 | #include <stdlib.h> | |
55 | #include <time.h> | |
56 | ||
2049ba38 | 57 | #ifdef __WXMSW__ |
9ef3052c | 58 | #include <windows.h> |
a0a302dc JS |
59 | // Redefines OutputDebugString if necessary |
60 | #include "wx/msw/private.h" | |
3078c3a6 VZ |
61 | #else //Unix |
62 | #include <signal.h> | |
63 | #endif //Win/Unix | |
c801d85f KB |
64 | |
65 | // ---------------------------------------------------------------------------- | |
66 | // non member functions | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // define this to enable wrapping of log messages | |
70 | //#define LOG_PRETTY_WRAP | |
71 | ||
9ef3052c | 72 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
73 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
74 | #endif | |
75 | ||
76 | // ============================================================================ | |
77 | // implementation | |
78 | // ============================================================================ | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // implementation of Log functions | |
82 | // | |
83 | // NB: unfortunately we need all these distinct functions, we can't make them | |
84 | // macros and not all compilers inline vararg functions. | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | // log functions can't allocate memory (LogError("out of memory...") should | |
88 | // work!), so we use a static buffer for all log messages | |
89 | #define LOG_BUFFER_SIZE (4096) | |
90 | ||
0fb67cd1 | 91 | // static buffer for error messages (FIXME MT-unsafe) |
50920146 | 92 | static wxChar s_szBuf[LOG_BUFFER_SIZE]; |
c801d85f KB |
93 | |
94 | // generic log function | |
50920146 | 95 | void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...) |
c801d85f | 96 | { |
9ef3052c VZ |
97 | if ( wxLog::GetActiveTarget() != NULL ) { |
98 | va_list argptr; | |
7502ba29 | 99 | va_start(argptr, szFormat); |
50920146 | 100 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
101 | va_end(argptr); |
102 | ||
0fb67cd1 | 103 | wxLog::OnLog(level, s_szBuf, time(NULL)); |
9ef3052c | 104 | } |
c801d85f KB |
105 | } |
106 | ||
107 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
50920146 | 108 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
109 | { \ |
110 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
111 | va_list argptr; \ | |
7502ba29 | 112 | va_start(argptr, szFormat); \ |
50920146 | 113 | wxVsprintf(s_szBuf, szFormat, argptr); \ |
c801d85f KB |
114 | va_end(argptr); \ |
115 | \ | |
0fb67cd1 | 116 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
117 | } \ |
118 | } | |
119 | ||
120 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
121 | IMPLEMENT_LOG_FUNCTION(Error) | |
122 | IMPLEMENT_LOG_FUNCTION(Warning) | |
123 | IMPLEMENT_LOG_FUNCTION(Message) | |
124 | IMPLEMENT_LOG_FUNCTION(Info) | |
125 | IMPLEMENT_LOG_FUNCTION(Status) | |
126 | ||
9ef3052c | 127 | // same as info, but only if 'verbose' mode is on |
50920146 | 128 | void wxLogVerbose(const wxChar *szFormat, ...) |
9ef3052c VZ |
129 | { |
130 | wxLog *pLog = wxLog::GetActiveTarget(); | |
131 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
132 | va_list argptr; | |
7502ba29 | 133 | va_start(argptr, szFormat); |
50920146 | 134 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
135 | va_end(argptr); |
136 | ||
0fb67cd1 | 137 | wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL)); |
9ef3052c VZ |
138 | } |
139 | } | |
140 | ||
141 | // debug functions | |
b2aef89b | 142 | #ifdef __WXDEBUG__ |
9ef3052c | 143 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
50920146 | 144 | void wxLog##level(const wxChar *szFormat, ...) \ |
c801d85f KB |
145 | { \ |
146 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
147 | va_list argptr; \ | |
148 | va_start(argptr, szFormat); \ | |
50920146 | 149 | wxVsprintf(s_szBuf, szFormat, argptr); \ |
c801d85f KB |
150 | va_end(argptr); \ |
151 | \ | |
0fb67cd1 | 152 | wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \ |
c801d85f KB |
153 | } \ |
154 | } | |
155 | ||
50920146 | 156 | void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...) |
0fb67cd1 VZ |
157 | { |
158 | wxLog *pLog = wxLog::GetActiveTarget(); | |
159 | ||
160 | if ( pLog != NULL && wxLog::IsAllowedTraceMask(mask) ) { | |
161 | va_list argptr; | |
162 | va_start(argptr, szFormat); | |
50920146 | 163 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 VZ |
164 | va_end(argptr); |
165 | ||
166 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); | |
167 | } | |
168 | } | |
169 | ||
50920146 | 170 | void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...) |
9ef3052c VZ |
171 | { |
172 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 173 | |
9ef3052c VZ |
174 | // we check that all of mask bits are set in the current mask, so |
175 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
176 | // if both bits are set. | |
d93f63db | 177 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { |
9ef3052c VZ |
178 | va_list argptr; |
179 | va_start(argptr, szFormat); | |
50920146 | 180 | wxVsprintf(s_szBuf, szFormat, argptr); |
9ef3052c VZ |
181 | va_end(argptr); |
182 | ||
0fb67cd1 | 183 | wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL)); |
9ef3052c VZ |
184 | } |
185 | } | |
186 | ||
187 | #else // release | |
188 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
189 | #endif | |
190 | ||
191 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
192 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
193 | ||
194 | // wxLogSysError: one uses the last error code, for other you must give it | |
195 | // explicitly | |
196 | ||
197 | // common part of both wxLogSysError | |
198 | void wxLogSysErrorHelper(long lErrCode) | |
c801d85f | 199 | { |
50920146 OK |
200 | wxChar szErrMsg[LOG_BUFFER_SIZE / 2]; |
201 | wxSprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
202 | wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf)); | |
c801d85f | 203 | |
0fb67cd1 | 204 | wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL)); |
9ef3052c | 205 | } |
c801d85f | 206 | |
50920146 | 207 | void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...) |
9ef3052c | 208 | { |
0fb67cd1 VZ |
209 | va_list argptr; |
210 | va_start(argptr, szFormat); | |
50920146 | 211 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 | 212 | va_end(argptr); |
9ef3052c | 213 | |
0fb67cd1 | 214 | wxLogSysErrorHelper(wxSysErrorCode()); |
c801d85f KB |
215 | } |
216 | ||
50920146 | 217 | void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...) |
c801d85f | 218 | { |
0fb67cd1 VZ |
219 | va_list argptr; |
220 | va_start(argptr, szFormat); | |
50920146 | 221 | wxVsprintf(s_szBuf, szFormat, argptr); |
0fb67cd1 | 222 | va_end(argptr); |
c801d85f | 223 | |
0fb67cd1 | 224 | wxLogSysErrorHelper(lErrCode); |
c801d85f KB |
225 | } |
226 | ||
227 | // ---------------------------------------------------------------------------- | |
228 | // wxLog class implementation | |
229 | // ---------------------------------------------------------------------------- | |
230 | ||
231 | wxLog::wxLog() | |
232 | { | |
0fb67cd1 | 233 | m_bHasMessages = FALSE; |
a2ace6ff | 234 | |
0fb67cd1 | 235 | // enable verbose messages by default in the debug builds |
a2ace6ff | 236 | #ifdef __WXDEBUG__ |
0fb67cd1 | 237 | m_bVerbose = TRUE; |
a2ace6ff | 238 | #else // release |
0fb67cd1 | 239 | m_bVerbose = FALSE; |
a2ace6ff | 240 | #endif // debug/release |
c801d85f KB |
241 | } |
242 | ||
9ec05cc9 VZ |
243 | wxLog *wxLog::GetActiveTarget() |
244 | { | |
0fb67cd1 VZ |
245 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
246 | // prevent infinite recursion if someone calls wxLogXXX() from | |
247 | // wxApp::CreateLogTarget() | |
248 | static bool s_bInGetActiveTarget = FALSE; | |
249 | if ( !s_bInGetActiveTarget ) { | |
250 | s_bInGetActiveTarget = TRUE; | |
251 | ||
dd85fc6b | 252 | #ifdef wxUSE_NOGUI |
0fb67cd1 | 253 | ms_pLogger = new wxLogStderr; |
dd85fc6b | 254 | #else // GUI |
0fb67cd1 VZ |
255 | // ask the application to create a log target for us |
256 | if ( wxTheApp != NULL ) | |
257 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
258 | else | |
259 | ms_pLogger = new wxLogStderr; | |
dd85fc6b | 260 | #endif // !GUI/GUI |
0fb67cd1 VZ |
261 | |
262 | s_bInGetActiveTarget = FALSE; | |
263 | ||
264 | // do nothing if it fails - what can we do? | |
265 | } | |
275bf4c1 | 266 | } |
c801d85f | 267 | |
0fb67cd1 | 268 | return ms_pLogger; |
c801d85f KB |
269 | } |
270 | ||
c085e333 | 271 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 272 | { |
0fb67cd1 VZ |
273 | if ( ms_pLogger != NULL ) { |
274 | // flush the old messages before changing because otherwise they might | |
275 | // get lost later if this target is not restored | |
276 | ms_pLogger->Flush(); | |
277 | } | |
c801d85f | 278 | |
0fb67cd1 VZ |
279 | wxLog *pOldLogger = ms_pLogger; |
280 | ms_pLogger = pLogger; | |
c085e333 | 281 | |
0fb67cd1 | 282 | return pOldLogger; |
c801d85f KB |
283 | } |
284 | ||
0fb67cd1 | 285 | void wxLog::RemoveTraceMask(const wxString& str) |
c801d85f | 286 | { |
0fb67cd1 VZ |
287 | int index = ms_aTraceMasks.Index(str); |
288 | if ( index != wxNOT_FOUND ) | |
289 | ms_aTraceMasks.Remove((size_t)index); | |
290 | } | |
c801d85f | 291 | |
d2e1ef19 VZ |
292 | void wxLog::TimeStamp(wxString *str) |
293 | { | |
294 | if ( ms_timestamp ) | |
295 | { | |
296 | wxChar buf[256]; | |
297 | time_t timeNow; | |
298 | (void)time(&timeNow); | |
c49245f8 | 299 | wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow)); |
d2e1ef19 VZ |
300 | |
301 | str->Empty(); | |
302 | *str << buf << _T(": "); | |
303 | } | |
304 | } | |
305 | ||
50920146 | 306 | void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t) |
0fb67cd1 | 307 | { |
0fb67cd1 VZ |
308 | switch ( level ) { |
309 | case wxLOG_FatalError: | |
786855a1 | 310 | DoLogString(wxString(_("Fatal error: ")) + szString, t); |
0fb67cd1 VZ |
311 | DoLogString(_("Program aborted."), t); |
312 | Flush(); | |
313 | abort(); | |
314 | break; | |
315 | ||
316 | case wxLOG_Error: | |
786855a1 | 317 | DoLogString(wxString(_("Error: ")) + szString, t); |
0fb67cd1 VZ |
318 | break; |
319 | ||
320 | case wxLOG_Warning: | |
786855a1 | 321 | DoLogString(wxString(_("Warning: ")) + szString, t); |
0fb67cd1 VZ |
322 | break; |
323 | ||
324 | case wxLOG_Info: | |
0fb67cd1 | 325 | if ( GetVerbose() ) |
37278984 | 326 | case wxLOG_Message: |
786855a1 VZ |
327 | default: // log unknown log levels too |
328 | DoLogString(szString, t); | |
0fb67cd1 VZ |
329 | // fall through |
330 | ||
331 | case wxLOG_Status: | |
332 | // nothing to do | |
333 | break; | |
334 | ||
335 | case wxLOG_Trace: | |
336 | case wxLOG_Debug: | |
337 | #ifdef __WXDEBUG__ | |
338 | DoLogString(szString, t); | |
339 | #endif | |
0fb67cd1 | 340 | break; |
0fb67cd1 | 341 | } |
c801d85f KB |
342 | } |
343 | ||
74e3313b | 344 | void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t)) |
c801d85f | 345 | { |
50920146 | 346 | wxFAIL_MSG(_T("DoLogString must be overriden if it's called.")); |
c801d85f KB |
347 | } |
348 | ||
349 | void wxLog::Flush() | |
350 | { | |
0fb67cd1 | 351 | // do nothing |
c801d85f KB |
352 | } |
353 | ||
354 | // ---------------------------------------------------------------------------- | |
355 | // wxLogStderr class implementation | |
356 | // ---------------------------------------------------------------------------- | |
357 | ||
358 | wxLogStderr::wxLogStderr(FILE *fp) | |
359 | { | |
0fb67cd1 VZ |
360 | if ( fp == NULL ) |
361 | m_fp = stderr; | |
362 | else | |
363 | m_fp = fp; | |
c801d85f KB |
364 | } |
365 | ||
74e3313b | 366 | void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 367 | { |
d2e1ef19 VZ |
368 | wxString str; |
369 | TimeStamp(&str); | |
370 | str << szString << _T('\n'); | |
1e8a4bc2 | 371 | |
50920146 | 372 | fputs(str.mb_str(), m_fp); |
0fb67cd1 | 373 | fflush(m_fp); |
1e8a4bc2 | 374 | |
0fb67cd1 VZ |
375 | // under Windows, programs usually don't have stderr at all, so make show the |
376 | // messages also under debugger | |
1e8a4bc2 | 377 | #ifdef __WXMSW__ |
50920146 | 378 | OutputDebugString(str + _T('\r')); |
1e8a4bc2 | 379 | #endif // MSW |
c801d85f KB |
380 | } |
381 | ||
382 | // ---------------------------------------------------------------------------- | |
383 | // wxLogStream implementation | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
4bf78aae | 386 | #if wxUSE_STD_IOSTREAM |
c801d85f KB |
387 | wxLogStream::wxLogStream(ostream *ostr) |
388 | { | |
0fb67cd1 VZ |
389 | if ( ostr == NULL ) |
390 | m_ostr = &cerr; | |
391 | else | |
392 | m_ostr = ostr; | |
c801d85f KB |
393 | } |
394 | ||
74e3313b | 395 | void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
c801d85f | 396 | { |
dcf924a3 | 397 | (*m_ostr) << wxConvCurrent->cWX2MB(szString) << endl << flush; |
c801d85f | 398 | } |
0fb67cd1 | 399 | #endif // wxUSE_STD_IOSTREAM |
c801d85f | 400 | |
c801d85f KB |
401 | // ============================================================================ |
402 | // Global functions/variables | |
403 | // ============================================================================ | |
404 | ||
405 | // ---------------------------------------------------------------------------- | |
406 | // static variables | |
407 | // ---------------------------------------------------------------------------- | |
0fb67cd1 VZ |
408 | |
409 | wxLog *wxLog::ms_pLogger = (wxLog *)NULL; | |
410 | bool wxLog::ms_doLog = TRUE; | |
411 | bool wxLog::ms_bAutoCreate = TRUE; | |
d2e1ef19 | 412 | |
a324a7bc | 413 | const wxChar *wxLog::ms_timestamp = _T("%X"); // time only, no date |
d2e1ef19 | 414 | |
0fb67cd1 VZ |
415 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
416 | wxArrayString wxLog::ms_aTraceMasks; | |
c801d85f KB |
417 | |
418 | // ---------------------------------------------------------------------------- | |
419 | // stdout error logging helper | |
420 | // ---------------------------------------------------------------------------- | |
421 | ||
422 | // helper function: wraps the message and justifies it under given position | |
423 | // (looks more pretty on the terminal). Also adds newline at the end. | |
424 | // | |
0fb67cd1 VZ |
425 | // TODO this is now disabled until I find a portable way of determining the |
426 | // terminal window size (ok, I found it but does anybody really cares?) | |
427 | #ifdef LOG_PRETTY_WRAP | |
c801d85f KB |
428 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) |
429 | { | |
0fb67cd1 VZ |
430 | size_t nMax = 80; // FIXME |
431 | size_t nStart = strlen(pszPrefix); | |
432 | fputs(pszPrefix, f); | |
433 | ||
434 | size_t n; | |
435 | while ( *psz != '\0' ) { | |
436 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
437 | putc(*psz++, f); | |
438 | ||
439 | // wrapped? | |
440 | if ( *psz != '\0' ) { | |
441 | /*putc('\n', f);*/ | |
442 | for ( n = 0; n < nStart; n++ ) | |
443 | putc(' ', f); | |
444 | ||
445 | // as we wrapped, squeeze all white space | |
446 | while ( isspace(*psz) ) | |
447 | psz++; | |
448 | } | |
c801d85f | 449 | } |
c801d85f | 450 | |
0fb67cd1 | 451 | putc('\n', f); |
c801d85f KB |
452 | } |
453 | #endif //LOG_PRETTY_WRAP | |
454 | ||
455 | // ---------------------------------------------------------------------------- | |
456 | // error code/error message retrieval functions | |
457 | // ---------------------------------------------------------------------------- | |
458 | ||
459 | // get error code from syste | |
460 | unsigned long wxSysErrorCode() | |
461 | { | |
0fb67cd1 VZ |
462 | #ifdef __WXMSW__ |
463 | #ifdef __WIN32__ | |
464 | return ::GetLastError(); | |
465 | #else //WIN16 | |
466 | // TODO what to do on Windows 3.1? | |
467 | return 0; | |
468 | #endif //WIN16/32 | |
469 | #else //Unix | |
c801d85f | 470 | return errno; |
0fb67cd1 | 471 | #endif //Win/Unix |
c801d85f KB |
472 | } |
473 | ||
474 | // get error message from system | |
50920146 | 475 | const wxChar *wxSysErrorMsg(unsigned long nErrCode) |
c801d85f | 476 | { |
0fb67cd1 VZ |
477 | if ( nErrCode == 0 ) |
478 | nErrCode = wxSysErrorCode(); | |
479 | ||
480 | #ifdef __WXMSW__ | |
481 | #ifdef __WIN32__ | |
50920146 | 482 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; |
0fb67cd1 VZ |
483 | |
484 | // get error message from system | |
485 | LPVOID lpMsgBuf; | |
486 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
487 | NULL, nErrCode, | |
488 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | |
489 | (LPTSTR)&lpMsgBuf, | |
490 | 0, NULL); | |
491 | ||
492 | // copy it to our buffer and free memory | |
50920146 OK |
493 | wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); |
494 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = _T('\0'); | |
0fb67cd1 VZ |
495 | LocalFree(lpMsgBuf); |
496 | ||
497 | // returned string is capitalized and ended with '\r\n' - bad | |
50920146 OK |
498 | s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]); |
499 | size_t len = wxStrlen(s_szBuf); | |
0fb67cd1 | 500 | if ( len > 0 ) { |
9ef3052c | 501 | // truncate string |
50920146 OK |
502 | if ( s_szBuf[len - 2] == _T('\r') ) |
503 | s_szBuf[len - 2] = _T('\0'); | |
0fb67cd1 VZ |
504 | } |
505 | ||
506 | return s_szBuf; | |
507 | #else //Win16 | |
508 | // TODO | |
509 | return NULL; | |
510 | #endif // Win16/32 | |
511 | #else // Unix | |
50920146 OK |
512 | #if wxUSE_UNICODE |
513 | static wxChar s_szBuf[LOG_BUFFER_SIZE / 2]; | |
dcf924a3 | 514 | wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1); |
50920146 OK |
515 | return s_szBuf; |
516 | #else | |
9ef3052c | 517 | return strerror(nErrCode); |
50920146 | 518 | #endif |
0fb67cd1 | 519 | #endif // Win/Unix |
c801d85f KB |
520 | } |
521 | ||
522 | // ---------------------------------------------------------------------------- | |
523 | // debug helper | |
524 | // ---------------------------------------------------------------------------- | |
525 | ||
b2aef89b | 526 | #ifdef __WXDEBUG__ |
c801d85f | 527 | |
0fb67cd1 | 528 | // break into the debugger |
7502ba29 VZ |
529 | void Trap() |
530 | { | |
0fb67cd1 | 531 | #ifdef __WXMSW__ |
7502ba29 | 532 | DebugBreak(); |
0fb67cd1 VZ |
533 | #elif defined(__WXMAC__) |
534 | #if __powerc | |
17dff81c | 535 | Debugger(); |
0fb67cd1 | 536 | #else |
17dff81c | 537 | SysBreak(); |
0fb67cd1 VZ |
538 | #endif |
539 | #elif defined(__UNIX__) | |
7502ba29 | 540 | raise(SIGTRAP); |
0fb67cd1 VZ |
541 | #else |
542 | // TODO | |
543 | #endif // Win/Unix | |
7502ba29 VZ |
544 | } |
545 | ||
c801d85f | 546 | // this function is called when an assert fails |
6f9eb452 | 547 | void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg) |
c801d85f | 548 | { |
0fb67cd1 VZ |
549 | // this variable can be set to true to suppress "assert failure" messages |
550 | static bool s_bNoAsserts = FALSE; | |
551 | static bool s_bInAssert = FALSE; // FIXME MT-unsafe | |
7502ba29 | 552 | |
0fb67cd1 VZ |
553 | if ( s_bInAssert ) { |
554 | // He-e-e-e-elp!! we're trapped in endless loop | |
555 | Trap(); | |
c085e333 | 556 | |
0fb67cd1 | 557 | s_bInAssert = FALSE; |
1e8a4bc2 | 558 | |
0fb67cd1 VZ |
559 | return; |
560 | } | |
7502ba29 | 561 | |
0fb67cd1 | 562 | s_bInAssert = TRUE; |
c801d85f | 563 | |
50920146 | 564 | wxChar szBuf[LOG_BUFFER_SIZE]; |
c263077e | 565 | |
0fb67cd1 VZ |
566 | // make life easier for people using VC++ IDE: clicking on the message |
567 | // will take us immediately to the place of the failed assert | |
3f4a0c5b | 568 | #ifdef __VISUALC__ |
84fff0b3 | 569 | wxSprintf(szBuf, _T("%s(%d): assert failed"), szFile, nLine); |
c263077e | 570 | #else // !VC++ |
0fb67cd1 | 571 | // make the error message more clear for all the others |
50920146 | 572 | wxSprintf(szBuf, _T("Assert failed in file %s at line %d"), szFile, nLine); |
c263077e VZ |
573 | #endif // VC/!VC |
574 | ||
0fb67cd1 | 575 | if ( szMsg != NULL ) { |
50920146 OK |
576 | wxStrcat(szBuf, _T(": ")); |
577 | wxStrcat(szBuf, szMsg); | |
0fb67cd1 VZ |
578 | } |
579 | else { | |
50920146 | 580 | wxStrcat(szBuf, _T(".")); |
0fb67cd1 | 581 | } |
c801d85f | 582 | |
0fb67cd1 VZ |
583 | if ( !s_bNoAsserts ) { |
584 | // send it to the normal log destination | |
585 | wxLogDebug(szBuf); | |
7502ba29 | 586 | |
0fb67cd1 VZ |
587 | #if wxUSE_NOGUI |
588 | Trap(); | |
dd85fc6b | 589 | #else // GUI |
0fb67cd1 VZ |
590 | // this message is intentionally not translated - it is for |
591 | // developpers only | |
50920146 | 592 | wxStrcat(szBuf, _T("\nDo you want to stop the program?" |
0fb67cd1 | 593 | "\nYou can also choose [Cancel] to suppress " |
50920146 | 594 | "further warnings.")); |
0fb67cd1 VZ |
595 | |
596 | switch ( wxMessageBox(szBuf, _("Debug"), | |
597 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
598 | case wxYES: | |
599 | Trap(); | |
600 | break; | |
601 | ||
602 | case wxCANCEL: | |
603 | s_bNoAsserts = TRUE; | |
604 | break; | |
605 | ||
606 | //case wxNO: nothing to do | |
607 | } | |
dd85fc6b | 608 | #endif // !GUI/GUI |
0fb67cd1 VZ |
609 | } |
610 | ||
611 | s_bInAssert = FALSE; | |
c801d85f KB |
612 | } |
613 | ||
b2aef89b | 614 | #endif //WXDEBUG |
c801d85f | 615 |