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