]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
[ 1522987 ] wxComboCtrl crashes if TAB is pressed while popup is open
[wxWidgets.git] / src / common / log.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LOG
28
29 // wxWidgets
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/arrstr.h"
34 #include "wx/intl.h"
35 #include "wx/string.h"
36 #include "wx/utils.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/apptrait.h"
40 #include "wx/file.h"
41 #include "wx/msgout.h"
42 #include "wx/textfile.h"
43 #include "wx/thread.h"
44 #include "wx/wxchar.h"
45
46 // other standard headers
47 #ifndef __WXWINCE__
48 #include <errno.h>
49 #endif
50
51 #include <stdlib.h>
52
53 #ifndef __WXWINCE__
54 #include <time.h>
55 #else
56 #include "wx/msw/wince/time.h"
57 #endif
58
59 #if defined(__WINDOWS__)
60 #include "wx/msw/private.h" // includes windows.h
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // non member functions
65 // ----------------------------------------------------------------------------
66
67 // define this to enable wrapping of log messages
68 //#define LOG_PRETTY_WRAP
69
70 #ifdef LOG_PRETTY_WRAP
71 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
72 #endif
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // globals
80 // ----------------------------------------------------------------------------
81
82 // log functions can't allocate memory (LogError("out of memory...") should
83 // work!), so we use a static buffer for all log messages
84 #define LOG_BUFFER_SIZE (4096)
85
86 // static buffer for error messages
87 static wxChar s_szBufStatic[LOG_BUFFER_SIZE];
88
89 static wxChar *s_szBuf = s_szBufStatic;
90 static size_t s_szBufSize = WXSIZEOF( s_szBufStatic );
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 // ----------------------------------------------------------------------------
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
106 // wrapper for wxVsnprintf(s_szBuf) which always NULL-terminates it
107 static inline void PrintfInLogBuf(const wxChar *szFormat, va_list argptr)
108 {
109 if ( wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr) < 0 )
110 {
111 // must NUL-terminate it manually
112 s_szBuf[s_szBufSize - 1] = _T('\0');
113 }
114 //else: NUL-terminated by vsnprintf()
115 }
116
117 // generic log function
118 void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr)
119 {
120 if ( wxLog::IsEnabled() ) {
121 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
122
123 PrintfInLogBuf(szFormat, argptr);
124
125 wxLog::OnLog(level, s_szBuf, time(NULL));
126 }
127 }
128
129 void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
130 {
131 va_list argptr;
132 va_start(argptr, szFormat);
133 wxVLogGeneric(level, szFormat, argptr);
134 va_end(argptr);
135 }
136
137 #define IMPLEMENT_LOG_FUNCTION(level) \
138 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
139 { \
140 if ( wxLog::IsEnabled() ) { \
141 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
142 \
143 PrintfInLogBuf(szFormat, argptr); \
144 \
145 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
146 } \
147 } \
148 \
149 void wxLog##level(const wxChar *szFormat, ...) \
150 { \
151 va_list argptr; \
152 va_start(argptr, szFormat); \
153 wxVLog##level(szFormat, argptr); \
154 va_end(argptr); \
155 }
156
157 IMPLEMENT_LOG_FUNCTION(Error)
158 IMPLEMENT_LOG_FUNCTION(Warning)
159 IMPLEMENT_LOG_FUNCTION(Message)
160 IMPLEMENT_LOG_FUNCTION(Info)
161 IMPLEMENT_LOG_FUNCTION(Status)
162
163 void wxSafeShowMessage(const wxString& title, const wxString& text)
164 {
165 #ifdef __WINDOWS__
166 ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP);
167 #else
168 wxFprintf(stderr, _T("%s: %s\n"), title.c_str(), text.c_str());
169 fflush(stderr);
170 #endif
171 }
172
173 // fatal errors can't be suppressed nor handled by the custom log target and
174 // always terminate the program
175 void wxVLogFatalError(const wxChar *szFormat, va_list argptr)
176 {
177 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
178
179 wxSafeShowMessage(_T("Fatal Error"), s_szBuf);
180
181 #ifdef __WXWINCE__
182 ExitThread(3);
183 #else
184 abort();
185 #endif
186 }
187
188 void wxLogFatalError(const wxChar *szFormat, ...)
189 {
190 va_list argptr;
191 va_start(argptr, szFormat);
192 wxVLogFatalError(szFormat, argptr);
193
194 // some compilers warn about unreachable code and it shouldn't matter
195 // for the others anyhow...
196 //va_end(argptr);
197 }
198
199 // same as info, but only if 'verbose' mode is on
200 void wxVLogVerbose(const wxChar *szFormat, va_list argptr)
201 {
202 if ( wxLog::IsEnabled() ) {
203 if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) {
204 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
205
206 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
207
208 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
209 }
210 }
211 }
212
213 void wxLogVerbose(const wxChar *szFormat, ...)
214 {
215 va_list argptr;
216 va_start(argptr, szFormat);
217 wxVLogVerbose(szFormat, argptr);
218 va_end(argptr);
219 }
220
221 // debug functions
222 #ifdef __WXDEBUG__
223 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
224 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
225 { \
226 if ( wxLog::IsEnabled() ) { \
227 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
228 \
229 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr); \
230 \
231 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
232 } \
233 } \
234 void wxLog##level(const wxChar *szFormat, ...) \
235 { \
236 va_list argptr; \
237 va_start(argptr, szFormat); \
238 wxVLog##level(szFormat, argptr); \
239 va_end(argptr); \
240 }
241
242 void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
243 {
244 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
245 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
246
247 wxChar *p = s_szBuf;
248 size_t len = s_szBufSize;
249 wxStrncpy(s_szBuf, _T("("), len);
250 len -= 1; // strlen("(")
251 p += 1;
252 wxStrncat(p, mask, len);
253 size_t lenMask = wxStrlen(mask);
254 len -= lenMask;
255 p += lenMask;
256
257 wxStrncat(p, _T(") "), len);
258 len -= 2;
259 p += 2;
260
261 wxVsnprintf(p, len, szFormat, argptr);
262
263 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
264 }
265 }
266
267 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
268 {
269 va_list argptr;
270 va_start(argptr, szFormat);
271 wxVLogTrace(mask, szFormat, argptr);
272 va_end(argptr);
273 }
274
275 void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
276 {
277 // we check that all of mask bits are set in the current mask, so
278 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
279 // if both bits are set.
280 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
281 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
282
283 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
284
285 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
286 }
287 }
288
289 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
290 {
291 va_list argptr;
292 va_start(argptr, szFormat);
293 wxVLogTrace(mask, szFormat, argptr);
294 va_end(argptr);
295 }
296
297 #else // release
298 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
299 #endif
300
301 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
302 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
303
304 // wxLogSysError: one uses the last error code, for other you must give it
305 // explicitly
306
307 // common part of both wxLogSysError
308 void wxLogSysErrorHelper(long lErrCode)
309 {
310 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
311 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
312 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
313 wxStrncat(s_szBuf, szErrMsg, s_szBufSize - wxStrlen(s_szBuf));
314
315 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
316 }
317
318 void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr)
319 {
320 if ( wxLog::IsEnabled() ) {
321 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
322
323 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
324
325 wxLogSysErrorHelper(wxSysErrorCode());
326 }
327 }
328
329 void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
330 {
331 va_list argptr;
332 va_start(argptr, szFormat);
333 wxVLogSysError(szFormat, argptr);
334 va_end(argptr);
335 }
336
337 void WXDLLEXPORT wxVLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr)
338 {
339 if ( wxLog::IsEnabled() ) {
340 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
341
342 wxVsnprintf(s_szBuf, s_szBufSize, szFormat, argptr);
343
344 wxLogSysErrorHelper(lErrCode);
345 }
346 }
347
348 void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
349 {
350 va_list argptr;
351 va_start(argptr, szFormat);
352 wxVLogSysError(lErrCode, szFormat, argptr);
353 va_end(argptr);
354 }
355
356 // ----------------------------------------------------------------------------
357 // wxLog class implementation
358 // ----------------------------------------------------------------------------
359
360 wxChar *wxLog::SetLogBuffer( wxChar *buf, size_t size)
361 {
362 wxChar *oldbuf = s_szBuf;
363
364 if( buf == 0 )
365 {
366 s_szBuf = s_szBufStatic;
367 s_szBufSize = WXSIZEOF( s_szBufStatic );
368 }
369 else
370 {
371 s_szBuf = buf;
372 s_szBufSize = size;
373 }
374
375 return (oldbuf == s_szBufStatic ) ? 0 : oldbuf;
376 }
377
378 wxLog *wxLog::GetActiveTarget()
379 {
380 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
381 // prevent infinite recursion if someone calls wxLogXXX() from
382 // wxApp::CreateLogTarget()
383 static bool s_bInGetActiveTarget = false;
384 if ( !s_bInGetActiveTarget ) {
385 s_bInGetActiveTarget = true;
386
387 // ask the application to create a log target for us
388 if ( wxTheApp != NULL )
389 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
390 else
391 ms_pLogger = new wxLogStderr;
392
393 s_bInGetActiveTarget = false;
394
395 // do nothing if it fails - what can we do?
396 }
397 }
398
399 return ms_pLogger;
400 }
401
402 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
403 {
404 if ( ms_pLogger != NULL ) {
405 // flush the old messages before changing because otherwise they might
406 // get lost later if this target is not restored
407 ms_pLogger->Flush();
408 }
409
410 wxLog *pOldLogger = ms_pLogger;
411 ms_pLogger = pLogger;
412
413 return pOldLogger;
414 }
415
416 void wxLog::DontCreateOnDemand()
417 {
418 ms_bAutoCreate = false;
419
420 // this is usually called at the end of the program and we assume that it
421 // is *always* called at the end - so we free memory here to avoid false
422 // memory leak reports from wxWin memory tracking code
423 ClearTraceMasks();
424 }
425
426 void wxLog::RemoveTraceMask(const wxString& str)
427 {
428 int index = ms_aTraceMasks.Index(str);
429 if ( index != wxNOT_FOUND )
430 ms_aTraceMasks.RemoveAt((size_t)index);
431 }
432
433 void wxLog::ClearTraceMasks()
434 {
435 ms_aTraceMasks.Clear();
436 }
437
438 void wxLog::TimeStamp(wxString *str)
439 {
440 if ( ms_timestamp )
441 {
442 wxChar buf[256];
443 time_t timeNow;
444 (void)time(&timeNow);
445 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
446
447 str->Empty();
448 *str << buf << wxT(": ");
449 }
450 }
451
452 void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
453 {
454 switch ( level ) {
455 case wxLOG_FatalError:
456 DoLogString(wxString(_("Fatal error: ")) + szString, t);
457 DoLogString(_("Program aborted."), t);
458 Flush();
459 #ifdef __WXWINCE__
460 ExitThread(3);
461 #else
462 abort();
463 #endif
464 break;
465
466 case wxLOG_Error:
467 DoLogString(wxString(_("Error: ")) + szString, t);
468 break;
469
470 case wxLOG_Warning:
471 DoLogString(wxString(_("Warning: ")) + szString, t);
472 break;
473
474 case wxLOG_Info:
475 if ( GetVerbose() )
476 case wxLOG_Message:
477 case wxLOG_Status:
478 default: // log unknown log levels too
479 DoLogString(szString, t);
480 break;
481
482 case wxLOG_Trace:
483 case wxLOG_Debug:
484 #ifdef __WXDEBUG__
485 {
486 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
487 : wxT("Debug: ");
488 msg << szString;
489 DoLogString(msg, t);
490 }
491 #endif // Debug
492 break;
493 }
494 }
495
496 void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
497 {
498 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
499 }
500
501 void wxLog::Flush()
502 {
503 // nothing to do here
504 }
505
506 /*static*/ bool wxLog::IsAllowedTraceMask(const wxChar *mask)
507 {
508 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
509 en = ms_aTraceMasks.end();
510 it != en; ++it )
511 if ( *it == mask)
512 return true;
513 return false;
514 }
515
516 // ----------------------------------------------------------------------------
517 // wxLogBuffer implementation
518 // ----------------------------------------------------------------------------
519
520 void wxLogBuffer::Flush()
521 {
522 if ( !m_str.empty() )
523 {
524 wxMessageOutputBest out;
525 out.Printf(_T("%s"), m_str.c_str());
526 m_str.clear();
527 }
528 }
529
530 void wxLogBuffer::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
531 {
532 switch ( level )
533 {
534 case wxLOG_Trace:
535 case wxLOG_Debug:
536 #ifdef __WXDEBUG__
537 // don't put debug messages in the buffer, we don't want to show
538 // them to the user in a msg box, log them immediately
539 {
540 wxString str;
541 TimeStamp(&str);
542 str += szString;
543
544 wxMessageOutputDebug dbgout;
545 dbgout.Printf(_T("%s\n"), str.c_str());
546 }
547 #endif // __WXDEBUG__
548 break;
549
550 default:
551 wxLog::DoLog(level, szString, t);
552 }
553 }
554
555 void wxLogBuffer::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
556 {
557 m_str << szString << _T("\n");
558 }
559
560 // ----------------------------------------------------------------------------
561 // wxLogStderr class implementation
562 // ----------------------------------------------------------------------------
563
564 wxLogStderr::wxLogStderr(FILE *fp)
565 {
566 if ( fp == NULL )
567 m_fp = stderr;
568 else
569 m_fp = fp;
570 }
571
572 void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
573 {
574 wxString str;
575 TimeStamp(&str);
576 str << szString;
577
578 fputs(str.mb_str(), m_fp);
579 fputc(_T('\n'), m_fp);
580 fflush(m_fp);
581
582 // under GUI systems such as Windows or Mac, programs usually don't have
583 // stderr at all, so show the messages also somewhere else, typically in
584 // the debugger window so that they go at least somewhere instead of being
585 // simply lost
586 if ( m_fp == stderr )
587 {
588 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
589 if ( traits && !traits->HasStderr() )
590 {
591 wxMessageOutputDebug dbgout;
592 dbgout.Printf(_T("%s\n"), str.c_str());
593 }
594 }
595 }
596
597 // ----------------------------------------------------------------------------
598 // wxLogStream implementation
599 // ----------------------------------------------------------------------------
600
601 #if wxUSE_STD_IOSTREAM
602 #include "wx/ioswrap.h"
603 wxLogStream::wxLogStream(wxSTD ostream *ostr)
604 {
605 if ( ostr == NULL )
606 m_ostr = &wxSTD cerr;
607 else
608 m_ostr = ostr;
609 }
610
611 void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
612 {
613 wxString str;
614 TimeStamp(&str);
615 (*m_ostr) << wxConvertWX2MB(str) << wxConvertWX2MB(szString) << wxSTD endl;
616 }
617 #endif // wxUSE_STD_IOSTREAM
618
619 // ----------------------------------------------------------------------------
620 // wxLogChain
621 // ----------------------------------------------------------------------------
622
623 wxLogChain::wxLogChain(wxLog *logger)
624 {
625 m_bPassMessages = true;
626
627 m_logNew = logger;
628 m_logOld = wxLog::SetActiveTarget(this);
629 }
630
631 wxLogChain::~wxLogChain()
632 {
633 delete m_logOld;
634
635 if ( m_logNew != this )
636 delete m_logNew;
637 }
638
639 void wxLogChain::SetLog(wxLog *logger)
640 {
641 if ( m_logNew != this )
642 delete m_logNew;
643
644 m_logNew = logger;
645 }
646
647 void wxLogChain::Flush()
648 {
649 if ( m_logOld )
650 m_logOld->Flush();
651
652 // be careful to avoid infinite recursion
653 if ( m_logNew && m_logNew != this )
654 m_logNew->Flush();
655 }
656
657 void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
658 {
659 // let the previous logger show it
660 if ( m_logOld && IsPassingMessages() )
661 {
662 // bogus cast just to access protected DoLog
663 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
664 }
665
666 if ( m_logNew && m_logNew != this )
667 {
668 // as above...
669 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
670 }
671 }
672
673 // ----------------------------------------------------------------------------
674 // wxLogPassThrough
675 // ----------------------------------------------------------------------------
676
677 #ifdef __VISUALC__
678 // "'this' : used in base member initializer list" - so what?
679 #pragma warning(disable:4355)
680 #endif // VC++
681
682 wxLogPassThrough::wxLogPassThrough()
683 : wxLogChain(this)
684 {
685 }
686
687 #ifdef __VISUALC__
688 #pragma warning(default:4355)
689 #endif // VC++
690
691 // ============================================================================
692 // Global functions/variables
693 // ============================================================================
694
695 // ----------------------------------------------------------------------------
696 // static variables
697 // ----------------------------------------------------------------------------
698
699 wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
700 bool wxLog::ms_doLog = true;
701 bool wxLog::ms_bAutoCreate = true;
702 bool wxLog::ms_bVerbose = false;
703
704 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
705
706 size_t wxLog::ms_suspendCount = 0;
707
708 const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
709
710 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
711 wxArrayString wxLog::ms_aTraceMasks;
712
713 // ----------------------------------------------------------------------------
714 // stdout error logging helper
715 // ----------------------------------------------------------------------------
716
717 // helper function: wraps the message and justifies it under given position
718 // (looks more pretty on the terminal). Also adds newline at the end.
719 //
720 // TODO this is now disabled until I find a portable way of determining the
721 // terminal window size (ok, I found it but does anybody really cares?)
722 #ifdef LOG_PRETTY_WRAP
723 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
724 {
725 size_t nMax = 80; // FIXME
726 size_t nStart = strlen(pszPrefix);
727 fputs(pszPrefix, f);
728
729 size_t n;
730 while ( *psz != '\0' ) {
731 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
732 putc(*psz++, f);
733
734 // wrapped?
735 if ( *psz != '\0' ) {
736 /*putc('\n', f);*/
737 for ( n = 0; n < nStart; n++ )
738 putc(' ', f);
739
740 // as we wrapped, squeeze all white space
741 while ( isspace(*psz) )
742 psz++;
743 }
744 }
745
746 putc('\n', f);
747 }
748 #endif //LOG_PRETTY_WRAP
749
750 // ----------------------------------------------------------------------------
751 // error code/error message retrieval functions
752 // ----------------------------------------------------------------------------
753
754 // get error code from syste
755 unsigned long wxSysErrorCode()
756 {
757 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
758 return ::GetLastError();
759 #else //Unix
760 return errno;
761 #endif //Win/Unix
762 }
763
764 // get error message from system
765 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
766 {
767 if ( nErrCode == 0 )
768 nErrCode = wxSysErrorCode();
769
770 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
771 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
772
773 // get error message from system
774 LPVOID lpMsgBuf;
775 if ( ::FormatMessage
776 (
777 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
778 NULL,
779 nErrCode,
780 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
781 (LPTSTR)&lpMsgBuf,
782 0,
783 NULL
784 ) == 0 )
785 {
786 // if this happens, something is seriously wrong, so don't use _() here
787 // for safety
788 wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode);
789 return s_szBuf;
790 }
791
792
793 // copy it to our buffer and free memory
794 // Crashes on SmartPhone (FIXME)
795 #if !defined(__SMARTPHONE__) /* of WinCE */
796 if( lpMsgBuf != 0 )
797 {
798 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
799 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
800
801 LocalFree(lpMsgBuf);
802
803 // returned string is capitalized and ended with '\r\n' - bad
804 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
805 size_t len = wxStrlen(s_szBuf);
806 if ( len > 0 ) {
807 // truncate string
808 if ( s_szBuf[len - 2] == wxT('\r') )
809 s_szBuf[len - 2] = wxT('\0');
810 }
811 }
812 else
813 #endif
814 {
815 s_szBuf[0] = wxT('\0');
816 }
817
818 return s_szBuf;
819 #else // Unix-WXMICROWIN
820 #if wxUSE_UNICODE
821 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
822 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
823 return s_szBuf;
824 #else
825 return strerror((int)nErrCode);
826 #endif
827 #endif // Win/Unix-WXMICROWIN
828 }
829
830 #endif // wxUSE_LOG