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