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