]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
applied patch 430835 (missing wxSTD breaking compilation for VC++ 5)
[wxWidgets.git] / src / common / log.cpp
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 // ----------------------------------------------------------------------------
19
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
33 #include "wx/string.h"
34 #include "wx/intl.h"
35 #include "wx/app.h"
36
37 #if wxUSE_GUI
38 #include "wx/window.h"
39 #ifdef __WXMSW__
40 #include "wx/msw/private.h"
41 #endif
42 #include "wx/msgdlg.h"
43 #endif
44 #endif //WX_PRECOMP
45
46 #include "wx/file.h"
47 #include "wx/textfile.h"
48 #include "wx/utils.h"
49 #include "wx/wxchar.h"
50 #include "wx/log.h"
51 #include "wx/thread.h"
52
53 #if wxUSE_LOG
54
55 // other standard headers
56 #include <errno.h>
57 #include <stdlib.h>
58 #include <time.h>
59
60 #ifdef __WXMSW__
61 #include "wx/msw/private.h" // includes windows.h for OutputDebugString
62 #else //Unix
63 #include <signal.h>
64 #endif //Win/Unix
65
66 // ----------------------------------------------------------------------------
67 // non member functions
68 // ----------------------------------------------------------------------------
69
70 // define this to enable wrapping of log messages
71 //#define LOG_PRETTY_WRAP
72
73 #ifdef LOG_PRETTY_WRAP
74 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
75 #endif
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // globals
83 // ----------------------------------------------------------------------------
84
85 // log functions can't allocate memory (LogError("out of memory...") should
86 // work!), so we use a static buffer for all log messages
87 #define LOG_BUFFER_SIZE (4096)
88
89 // static buffer for error messages
90 static wxChar s_szBuf[LOG_BUFFER_SIZE];
91
92 #if wxUSE_THREADS
93
94 // the critical section protecting the static buffer
95 static wxCriticalSection gs_csLogBuf;
96
97 #endif // wxUSE_THREADS
98
99 // return true if we have a non NULL non disabled log target
100 static inline bool IsLoggingEnabled()
101 {
102 return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL);
103 }
104
105 // ----------------------------------------------------------------------------
106 // implementation of Log functions
107 //
108 // NB: unfortunately we need all these distinct functions, we can't make them
109 // macros and not all compilers inline vararg functions.
110 // ----------------------------------------------------------------------------
111
112 // generic log function
113 void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
114 {
115 if ( IsLoggingEnabled() ) {
116 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
117
118 va_list argptr;
119 va_start(argptr, szFormat);
120 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
121 va_end(argptr);
122
123 wxLog::OnLog(level, s_szBuf, time(NULL));
124 }
125 }
126
127 #define IMPLEMENT_LOG_FUNCTION(level) \
128 void wxLog##level(const wxChar *szFormat, ...) \
129 { \
130 if ( IsLoggingEnabled() ) { \
131 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
132 \
133 va_list argptr; \
134 va_start(argptr, szFormat); \
135 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
136 va_end(argptr); \
137 \
138 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
139 } \
140 }
141
142 IMPLEMENT_LOG_FUNCTION(FatalError)
143 IMPLEMENT_LOG_FUNCTION(Error)
144 IMPLEMENT_LOG_FUNCTION(Warning)
145 IMPLEMENT_LOG_FUNCTION(Message)
146 IMPLEMENT_LOG_FUNCTION(Info)
147 IMPLEMENT_LOG_FUNCTION(Status)
148
149 // same as info, but only if 'verbose' mode is on
150 void wxLogVerbose(const wxChar *szFormat, ...)
151 {
152 if ( IsLoggingEnabled() ) {
153 wxLog *pLog = wxLog::GetActiveTarget();
154 if ( pLog != NULL && pLog->GetVerbose() ) {
155 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
156
157 va_list argptr;
158 va_start(argptr, szFormat);
159 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
160 va_end(argptr);
161
162 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
163 }
164 }
165 }
166
167 // debug functions
168 #ifdef __WXDEBUG__
169 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
170 void wxLog##level(const wxChar *szFormat, ...) \
171 { \
172 if ( IsLoggingEnabled() ) { \
173 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
174 \
175 va_list argptr; \
176 va_start(argptr, szFormat); \
177 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
178 va_end(argptr); \
179 \
180 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
181 } \
182 }
183
184 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
185 {
186 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
187 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
188
189 wxChar *p = s_szBuf;
190 size_t len = WXSIZEOF(s_szBuf);
191 wxStrncpy(s_szBuf, _T("("), len);
192 len -= 1; // strlen("(")
193 p += 1;
194 wxStrncat(p, mask, len);
195 size_t lenMask = wxStrlen(mask);
196 len -= lenMask;
197 p += lenMask;
198
199 wxStrncat(p, _T(") "), len);
200 len -= 2;
201 p += 2;
202
203 va_list argptr;
204 va_start(argptr, szFormat);
205 wxVsnprintf(p, len, szFormat, argptr);
206 va_end(argptr);
207
208 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
209 }
210 }
211
212 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
213 {
214 // we check that all of mask bits are set in the current mask, so
215 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
216 // if both bits are set.
217 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
218 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
219
220 va_list argptr;
221 va_start(argptr, szFormat);
222 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
223 va_end(argptr);
224
225 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
226 }
227 }
228
229 #else // release
230 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
231 #endif
232
233 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
234 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
235
236 // wxLogSysError: one uses the last error code, for other you must give it
237 // explicitly
238
239 // common part of both wxLogSysError
240 void wxLogSysErrorHelper(long lErrCode)
241 {
242 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
243 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
244 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
245 wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf));
246
247 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
248 }
249
250 void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
251 {
252 if ( IsLoggingEnabled() ) {
253 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
254
255 va_list argptr;
256 va_start(argptr, szFormat);
257 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
258 va_end(argptr);
259
260 wxLogSysErrorHelper(wxSysErrorCode());
261 }
262 }
263
264 void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
265 {
266 if ( IsLoggingEnabled() ) {
267 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
268
269 va_list argptr;
270 va_start(argptr, szFormat);
271 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
272 va_end(argptr);
273
274 wxLogSysErrorHelper(lErrCode);
275 }
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxLog class implementation
280 // ----------------------------------------------------------------------------
281
282 wxLog::wxLog()
283 {
284 m_bHasMessages = FALSE;
285 m_bVerbose = FALSE;
286 }
287
288 wxLog *wxLog::GetActiveTarget()
289 {
290 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
291 // prevent infinite recursion if someone calls wxLogXXX() from
292 // wxApp::CreateLogTarget()
293 static bool s_bInGetActiveTarget = FALSE;
294 if ( !s_bInGetActiveTarget ) {
295 s_bInGetActiveTarget = TRUE;
296
297 // ask the application to create a log target for us
298 if ( wxTheApp != NULL )
299 ms_pLogger = wxTheApp->CreateLogTarget();
300 else
301 ms_pLogger = new wxLogStderr;
302
303 s_bInGetActiveTarget = FALSE;
304
305 // do nothing if it fails - what can we do?
306 }
307 }
308
309 return ms_pLogger;
310 }
311
312 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
313 {
314 if ( ms_pLogger != NULL ) {
315 // flush the old messages before changing because otherwise they might
316 // get lost later if this target is not restored
317 ms_pLogger->Flush();
318 }
319
320 wxLog *pOldLogger = ms_pLogger;
321 ms_pLogger = pLogger;
322
323 return pOldLogger;
324 }
325
326 void wxLog::DontCreateOnDemand()
327 {
328 ms_bAutoCreate = FALSE;
329
330 // this is usually called at the end of the program and we assume that it
331 // is *always* called at the end - so we free memory here to avoid false
332 // memory leak reports from wxWin memory tracking code
333 ClearTraceMasks();
334 }
335
336 void wxLog::RemoveTraceMask(const wxString& str)
337 {
338 int index = ms_aTraceMasks.Index(str);
339 if ( index != wxNOT_FOUND )
340 ms_aTraceMasks.Remove((size_t)index);
341 }
342
343 void wxLog::ClearTraceMasks()
344 {
345 ms_aTraceMasks.Clear();
346 }
347
348 void wxLog::TimeStamp(wxString *str)
349 {
350 if ( ms_timestamp )
351 {
352 wxChar buf[256];
353 time_t timeNow;
354 (void)time(&timeNow);
355 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
356
357 str->Empty();
358 *str << buf << wxT(": ");
359 }
360 }
361
362 void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
363 {
364 switch ( level ) {
365 case wxLOG_FatalError:
366 DoLogString(wxString(_("Fatal error: ")) + szString, t);
367 DoLogString(_("Program aborted."), t);
368 Flush();
369 abort();
370 break;
371
372 case wxLOG_Error:
373 DoLogString(wxString(_("Error: ")) + szString, t);
374 break;
375
376 case wxLOG_Warning:
377 DoLogString(wxString(_("Warning: ")) + szString, t);
378 break;
379
380 case wxLOG_Info:
381 if ( GetVerbose() )
382 case wxLOG_Message:
383 case wxLOG_Status:
384 default: // log unknown log levels too
385 DoLogString(szString, t);
386 break;
387
388 case wxLOG_Trace:
389 case wxLOG_Debug:
390 #ifdef __WXDEBUG__
391 {
392 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
393 : wxT("Debug: ");
394 msg << szString;
395 DoLogString(msg, t);
396 }
397 #endif // Debug
398 break;
399 }
400 }
401
402 void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
403 {
404 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
405 }
406
407 void wxLog::Flush()
408 {
409 // do nothing
410 }
411
412 // ----------------------------------------------------------------------------
413 // wxLogStderr class implementation
414 // ----------------------------------------------------------------------------
415
416 wxLogStderr::wxLogStderr(FILE *fp)
417 {
418 if ( fp == NULL )
419 m_fp = stderr;
420 else
421 m_fp = fp;
422 }
423
424 #if defined(__WXMAC__) && !defined(__UNIX__)
425 #define kDebuggerSignature 'MWDB'
426
427 static Boolean FindProcessBySignature(OSType signature, ProcessInfoRec* info)
428 {
429 OSErr err;
430 ProcessSerialNumber psn;
431 Boolean found = false;
432 psn.highLongOfPSN = 0;
433 psn.lowLongOfPSN = kNoProcess;
434
435 if (!info) return false;
436
437 info->processInfoLength = sizeof(ProcessInfoRec);
438 info->processName = NULL;
439 info->processAppSpec = NULL;
440
441 err = noErr;
442 while (!found && err == noErr)
443 {
444 err = GetNextProcess(&psn);
445 if (err == noErr)
446 {
447 err = GetProcessInformation(&psn, info);
448 found = err == noErr && info->processSignature == signature;
449 }
450 }
451 return found;
452 }
453
454 pascal Boolean MWDebuggerIsRunning(void)
455 {
456 ProcessInfoRec info;
457 return FindProcessBySignature(kDebuggerSignature, &info);
458 }
459
460 pascal OSErr AmIBeingMWDebugged(Boolean* result)
461 {
462 OSErr err;
463 ProcessSerialNumber psn;
464 OSType sig = kDebuggerSignature;
465 AppleEvent theAE = {typeNull, NULL};
466 AppleEvent theReply = {typeNull, NULL};
467 AEAddressDesc addr = {typeNull, NULL};
468 DescType actualType;
469 Size actualSize;
470
471 if (!result) return paramErr;
472
473 err = AECreateDesc(typeApplSignature, &sig, sizeof(sig), &addr);
474 if (err != noErr) goto exit;
475
476 err = AECreateAppleEvent('MWDB', 'Dbg?', &addr,
477 kAutoGenerateReturnID, kAnyTransactionID, &theAE);
478 if (err != noErr) goto exit;
479
480 GetCurrentProcess(&psn);
481 err = AEPutParamPtr(&theAE, keyDirectObject, typeProcessSerialNumber,
482 &psn, sizeof(psn));
483 if (err != noErr) goto exit;
484
485 err = AESend(&theAE, &theReply, kAEWaitReply, kAENormalPriority,
486 kAEDefaultTimeout, NULL, NULL);
487 if (err != noErr) goto exit;
488
489 err = AEGetParamPtr(&theReply, keyAEResult, typeBoolean, &actualType, result,
490 sizeof(Boolean), &actualSize);
491
492 exit:
493 if (addr.dataHandle)
494 AEDisposeDesc(&addr);
495 if (theAE.dataHandle)
496 AEDisposeDesc(&theAE);
497 if (theReply.dataHandle)
498 AEDisposeDesc(&theReply);
499
500 return err;
501 }
502 #endif
503
504 void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
505 {
506 wxString str;
507 TimeStamp(&str);
508 str << szString;
509
510 fputs(str.mb_str(), m_fp);
511 fputc(_T('\n'), m_fp);
512 fflush(m_fp);
513
514 // under Windows, programs usually don't have stderr at all, so show the
515 // messages also under debugger - unless it's a console program
516 #if defined(__WXMSW__) && wxUSE_GUI
517 str += wxT("\r\n") ;
518 OutputDebugString(str.c_str());
519 #endif // MSW
520 #if defined(__WXMAC__) && !defined(__WXMAC_X__) && wxUSE_GUI
521 Str255 pstr ;
522 strcpy( (char*) pstr , str.c_str() ) ;
523 strcat( (char*) pstr , ";g" ) ;
524 c2pstr( (char*) pstr ) ;
525 #if __WXDEBUG__
526 Boolean running = false ;
527
528 /*
529 if ( MWDebuggerIsRunning() )
530 {
531 AmIBeingMWDebugged( &running ) ;
532 }
533 */
534 if (running)
535 {
536 #ifdef __powerc
537 DebugStr(pstr);
538 #else
539 SysBreakStr(pstr);
540 #endif
541 }
542 else
543 #endif
544 {
545 #ifdef __powerc
546 DebugStr(pstr);
547 #else
548 DebugStr(pstr);
549 #endif
550 }
551 #endif // Mac
552 }
553
554 // ----------------------------------------------------------------------------
555 // wxLogStream implementation
556 // ----------------------------------------------------------------------------
557
558 #if wxUSE_STD_IOSTREAM
559 wxLogStream::wxLogStream(wxSTD ostream *ostr)
560 {
561 if ( ostr == NULL )
562 m_ostr = &wxSTD cerr;
563 else
564 m_ostr = ostr;
565 }
566
567 void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
568 {
569 wxString str;
570 TimeStamp(&str);
571 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
572 }
573 #endif // wxUSE_STD_IOSTREAM
574
575 // ============================================================================
576 // Global functions/variables
577 // ============================================================================
578
579 // ----------------------------------------------------------------------------
580 // static variables
581 // ----------------------------------------------------------------------------
582
583 wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
584 bool wxLog::ms_doLog = TRUE;
585 bool wxLog::ms_bAutoCreate = TRUE;
586
587 size_t wxLog::ms_suspendCount = 0;
588
589 #if wxUSE_GUI
590 const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
591 #else
592 const wxChar *wxLog::ms_timestamp = NULL; // save space
593 #endif
594
595 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
596 wxArrayString wxLog::ms_aTraceMasks;
597
598 // ----------------------------------------------------------------------------
599 // stdout error logging helper
600 // ----------------------------------------------------------------------------
601
602 // helper function: wraps the message and justifies it under given position
603 // (looks more pretty on the terminal). Also adds newline at the end.
604 //
605 // TODO this is now disabled until I find a portable way of determining the
606 // terminal window size (ok, I found it but does anybody really cares?)
607 #ifdef LOG_PRETTY_WRAP
608 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
609 {
610 size_t nMax = 80; // FIXME
611 size_t nStart = strlen(pszPrefix);
612 fputs(pszPrefix, f);
613
614 size_t n;
615 while ( *psz != '\0' ) {
616 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
617 putc(*psz++, f);
618
619 // wrapped?
620 if ( *psz != '\0' ) {
621 /*putc('\n', f);*/
622 for ( n = 0; n < nStart; n++ )
623 putc(' ', f);
624
625 // as we wrapped, squeeze all white space
626 while ( isspace(*psz) )
627 psz++;
628 }
629 }
630
631 putc('\n', f);
632 }
633 #endif //LOG_PRETTY_WRAP
634
635 // ----------------------------------------------------------------------------
636 // error code/error message retrieval functions
637 // ----------------------------------------------------------------------------
638
639 // get error code from syste
640 unsigned long wxSysErrorCode()
641 {
642 #ifdef __WXMSW__
643 #ifdef __WIN32__
644 return ::GetLastError();
645 #else //WIN16
646 // TODO what to do on Windows 3.1?
647 return 0;
648 #endif //WIN16/32
649 #else //Unix
650 return errno;
651 #endif //Win/Unix
652 }
653
654 // get error message from system
655 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
656 {
657 if ( nErrCode == 0 )
658 nErrCode = wxSysErrorCode();
659
660 #ifdef __WXMSW__
661 #ifdef __WIN32__
662 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
663
664 // get error message from system
665 LPVOID lpMsgBuf;
666 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
667 NULL, nErrCode,
668 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
669 (LPTSTR)&lpMsgBuf,
670 0, NULL);
671
672 // copy it to our buffer and free memory
673 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
674 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
675 LocalFree(lpMsgBuf);
676
677 // returned string is capitalized and ended with '\r\n' - bad
678 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
679 size_t len = wxStrlen(s_szBuf);
680 if ( len > 0 ) {
681 // truncate string
682 if ( s_szBuf[len - 2] == wxT('\r') )
683 s_szBuf[len - 2] = wxT('\0');
684 }
685
686 return s_szBuf;
687 #else //Win16
688 // TODO
689 return NULL;
690 #endif // Win16/32
691 #else // Unix
692 #if wxUSE_UNICODE
693 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
694 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
695 return s_szBuf;
696 #else
697 return strerror((int)nErrCode);
698 #endif
699 #endif // Win/Unix
700 }
701
702 // ----------------------------------------------------------------------------
703 // debug helper
704 // ----------------------------------------------------------------------------
705
706 #ifdef __WXDEBUG__
707
708 // wxASSERT() helper
709 bool wxAssertIsEqual(int x, int y)
710 {
711 return x == y;
712 }
713
714 // break into the debugger
715 void wxTrap()
716 {
717 #ifdef __WXMSW__
718 DebugBreak();
719 #elif defined(__WXMAC__)
720 #if __powerc
721 Debugger();
722 #else
723 SysBreak();
724 #endif
725 #elif defined(__UNIX__)
726 raise(SIGTRAP);
727 #else
728 // TODO
729 #endif // Win/Unix
730 }
731
732 // this function is called when an assert fails
733 void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
734 {
735 // this variable can be set to true to suppress "assert failure" messages
736 static bool s_bNoAsserts = FALSE;
737 static bool s_bInAssert = FALSE; // FIXME MT-unsafe
738
739 if ( s_bInAssert ) {
740 // He-e-e-e-elp!! we're trapped in endless loop
741 wxTrap();
742
743 s_bInAssert = FALSE;
744
745 return;
746 }
747
748 s_bInAssert = TRUE;
749
750 wxChar szBuf[LOG_BUFFER_SIZE];
751
752 // make life easier for people using VC++ IDE: clicking on the message
753 // will take us immediately to the place of the failed assert
754 wxSnprintf(szBuf, WXSIZEOF(szBuf),
755 #ifdef __VISUALC__
756 wxT("%s(%d): assert failed"),
757 #else // !VC++
758 // make the error message more clear for all the others
759 wxT("Assert failed in file %s at line %d"),
760 #endif // VC/!VC
761 szFile, nLine);
762
763 if ( szMsg != NULL ) {
764 wxStrcat(szBuf, wxT(": "));
765 wxStrcat(szBuf, szMsg);
766 }
767 else {
768 wxStrcat(szBuf, wxT("."));
769 }
770
771 if ( !s_bNoAsserts ) {
772 // send it to the normal log destination
773 wxLogDebug(szBuf);
774
775 #if wxUSE_GUI || defined(__WXMSW__)
776 // this message is intentionally not translated - it is for
777 // developpers only
778 wxStrcat(szBuf, wxT("\nDo you want to stop the program?\nYou can also choose [Cancel] to suppress further warnings."));
779
780 // use the native message box if available: this is more robust than
781 // using our own
782 #ifdef __WXMSW__
783 switch ( ::MessageBox(NULL, szBuf, _T("Debug"),
784 MB_YESNOCANCEL | MB_ICONSTOP ) ) {
785 case IDYES:
786 wxTrap();
787 break;
788
789 case IDCANCEL:
790 s_bNoAsserts = TRUE;
791 break;
792
793 //case IDNO: nothing to do
794 }
795 #else // !MSW
796 switch ( wxMessageBox(szBuf, wxT("Debug"),
797 wxYES_NO | wxCANCEL | wxICON_STOP ) ) {
798 case wxYES:
799 wxTrap();
800 break;
801
802 case wxCANCEL:
803 s_bNoAsserts = TRUE;
804 break;
805
806 //case wxNO: nothing to do
807 }
808 #endif // GUI or MSW
809
810 #else // !GUI
811 wxTrap();
812 #endif // GUI/!GUI
813 }
814
815 s_bInAssert = FALSE;
816 }
817
818 #endif //WXDEBUG
819
820 #endif //wxUSE_LOG