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