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