1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/log.cpp
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/arrstr.h"
35 #include "wx/string.h"
39 #include "wx/apptrait.h"
40 #include "wx/datetime.h"
42 #include "wx/msgout.h"
43 #include "wx/textfile.h"
44 #include "wx/thread.h"
47 // other standard headers
58 #include "wx/msw/wince/time.h"
60 #endif /* ! __WXPALMOS5__ */
62 #if defined(__WINDOWS__)
63 #include "wx/msw/private.h" // includes windows.h
66 // ----------------------------------------------------------------------------
67 // non member functions
68 // ----------------------------------------------------------------------------
70 // define this to enable wrapping of log messages
71 //#define LOG_PRETTY_WRAP
73 #ifdef LOG_PRETTY_WRAP
74 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
82 // implementation of Log functions
84 // NB: unfortunately we need all these distinct functions, we can't make them
85 // macros and not all compilers inline vararg functions.
86 // ----------------------------------------------------------------------------
88 // generic log function
89 void wxVLogGeneric(wxLogLevel level
, const wxString
& format
, va_list argptr
)
91 if ( wxLog::IsEnabled() ) {
92 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), time(NULL
));
96 #if !wxUSE_UTF8_LOCALE_ONLY
97 void wxDoLogGenericWchar(wxLogLevel level
, const wxChar
*format
, ...)
100 va_start(argptr
, format
);
101 wxVLogGeneric(level
, format
, argptr
);
104 #endif // wxUSE_UTF8_LOCALE_ONLY
106 #if wxUSE_UNICODE_UTF8
107 void wxDoLogGenericUtf8(wxLogLevel level
, const char *format
, ...)
110 va_start(argptr
, format
);
111 wxVLogGeneric(level
, format
, argptr
);
114 #endif // wxUSE_UNICODE_UTF8
116 #if !wxUSE_UTF8_LOCALE_ONLY
117 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
118 void wxDoLog##level##Wchar(const wxChar *format, ...) \
121 va_start(argptr, format); \
122 wxVLog##level(format, argptr); \
126 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level)
129 #if wxUSE_UNICODE_UTF8
130 #define IMPLEMENT_LOG_FUNCTION_UTF8(level) \
131 void wxDoLog##level##Utf8(const char *format, ...) \
134 va_start(argptr, format); \
135 wxVLog##level(format, argptr); \
139 #define IMPLEMENT_LOG_FUNCTION_UTF8(level)
142 #define IMPLEMENT_LOG_FUNCTION(level) \
143 void wxVLog##level(const wxString& format, va_list argptr) \
145 if ( wxLog::IsEnabled() ) { \
146 wxLog::OnLog(wxLOG_##level, \
147 wxString::FormatV(format, argptr), time(NULL)); \
150 IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
151 IMPLEMENT_LOG_FUNCTION_UTF8(level)
153 IMPLEMENT_LOG_FUNCTION(Error
)
154 IMPLEMENT_LOG_FUNCTION(Warning
)
155 IMPLEMENT_LOG_FUNCTION(Message
)
156 IMPLEMENT_LOG_FUNCTION(Info
)
157 IMPLEMENT_LOG_FUNCTION(Status
)
159 void wxSafeShowMessage(const wxString
& title
, const wxString
& text
)
162 ::MessageBox(NULL
, text
.wx_str(), title
.wx_str(), MB_OK
| MB_ICONSTOP
);
164 wxFprintf(stderr
, wxS("%s: %s\n"), title
.c_str(), text
.c_str());
169 // fatal errors can't be suppressed nor handled by the custom log target and
170 // always terminate the program
171 void wxVLogFatalError(const wxString
& format
, va_list argptr
)
173 wxSafeShowMessage(wxS("Fatal Error"), wxString::FormatV(format
, argptr
));
182 #if !wxUSE_UTF8_LOCALE_ONLY
183 void wxDoLogFatalErrorWchar(const wxChar
*format
, ...)
186 va_start(argptr
, format
);
187 wxVLogFatalError(format
, argptr
);
189 // some compilers warn about unreachable code and it shouldn't matter
190 // for the others anyhow...
193 #endif // wxUSE_UTF8_LOCALE_ONLY
195 #if wxUSE_UNICODE_UTF8
196 void wxDoLogFatalErrorUtf8(const char *format
, ...)
199 va_start(argptr
, format
);
200 wxVLogFatalError(format
, argptr
);
202 // some compilers warn about unreachable code and it shouldn't matter
203 // for the others anyhow...
206 #endif // wxUSE_UNICODE_UTF8
208 // same as info, but only if 'verbose' mode is on
209 void wxVLogVerbose(const wxString
& format
, va_list argptr
)
211 if ( wxLog::IsEnabled() ) {
212 if ( wxLog::GetActiveTarget() != NULL
&& wxLog::GetVerbose() ) {
213 wxLog::OnLog(wxLOG_Info
,
214 wxString::FormatV(format
, argptr
), time(NULL
));
219 #if !wxUSE_UTF8_LOCALE_ONLY
220 void wxDoLogVerboseWchar(const wxChar
*format
, ...)
223 va_start(argptr
, format
);
224 wxVLogVerbose(format
, argptr
);
227 #endif // !wxUSE_UTF8_LOCALE_ONLY
229 #if wxUSE_UNICODE_UTF8
230 void wxDoLogVerboseUtf8(const char *format
, ...)
233 va_start(argptr
, format
);
234 wxVLogVerbose(format
, argptr
);
237 #endif // wxUSE_UNICODE_UTF8
242 #if !wxUSE_UTF8_LOCALE_ONLY
243 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
244 void wxDoLog##level##Wchar(const wxChar *format, ...) \
247 va_start(argptr, format); \
248 wxVLog##level(format, argptr); \
252 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level)
255 #if wxUSE_UNICODE_UTF8
256 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level) \
257 void wxDoLog##level##Utf8(const char *format, ...) \
260 va_start(argptr, format); \
261 wxVLog##level(format, argptr); \
265 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
268 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
269 void wxVLog##level(const wxString& format, va_list argptr) \
271 if ( wxLog::IsEnabled() ) { \
272 wxLog::OnLog(wxLOG_##level, \
273 wxString::FormatV(format, argptr), time(NULL)); \
276 IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
277 IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
280 void wxVLogTrace(const wxString
& mask
, const wxString
& format
, va_list argptr
)
282 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask
) ) {
284 msg
<< wxS("(") << mask
<< wxS(") ") << wxString::FormatV(format
, argptr
);
286 wxLog::OnLog(wxLOG_Trace
, msg
, time(NULL
));
290 #if !wxUSE_UTF8_LOCALE_ONLY
291 void wxDoLogTraceWchar(const wxString
& mask
, const wxChar
*format
, ...)
294 va_start(argptr
, format
);
295 wxVLogTrace(mask
, format
, argptr
);
298 #endif // !wxUSE_UTF8_LOCALE_ONLY
300 #if wxUSE_UNICODE_UTF8
301 void wxDoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
304 va_start(argptr
, format
);
305 wxVLogTrace(mask
, format
, argptr
);
308 #endif // wxUSE_UNICODE_UTF8
310 void wxVLogTrace(wxTraceMask mask
, const wxString
& format
, va_list argptr
)
312 // we check that all of mask bits are set in the current mask, so
313 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
314 // if both bits are set.
315 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask
) == mask
) ) {
316 wxLog::OnLog(wxLOG_Trace
, wxString::FormatV(format
, argptr
), time(NULL
));
320 #if !wxUSE_UTF8_LOCALE_ONLY
321 void wxDoLogTraceWchar(wxTraceMask mask
, const wxChar
*format
, ...)
324 va_start(argptr
, format
);
325 wxVLogTrace(mask
, format
, argptr
);
328 #endif // !wxUSE_UTF8_LOCALE_ONLY
330 #if wxUSE_UNICODE_UTF8
331 void wxDoLogTraceUtf8(wxTraceMask mask
, const char *format
, ...)
334 va_start(argptr
, format
);
335 wxVLogTrace(mask
, format
, argptr
);
338 #endif // wxUSE_UNICODE_UTF8
341 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
342 void wxDoLogTraceWchar(int mask
, const wxChar
*format
, ...)
345 va_start(argptr
, format
);
346 wxVLogTrace(mask
, format
, argptr
);
350 void wxDoLogTraceWchar(const char *mask
, const wxChar
*format
, ...)
353 va_start(argptr
, format
);
354 wxVLogTrace(mask
, format
, argptr
);
358 void wxDoLogTraceWchar(const wchar_t *mask
, const wxChar
*format
, ...)
361 va_start(argptr
, format
);
362 wxVLogTrace(mask
, format
, argptr
);
366 void wxVLogTrace(int mask
, const wxString
& format
, va_list argptr
)
367 { wxVLogTrace((wxTraceMask
)mask
, format
, argptr
); }
368 void wxVLogTrace(const char *mask
, const wxString
& format
, va_list argptr
)
369 { wxVLogTrace(wxString(mask
), format
, argptr
); }
370 void wxVLogTrace(const wchar_t *mask
, const wxString
& format
, va_list argptr
)
371 { wxVLogTrace(wxString(mask
), format
, argptr
); }
372 #endif // __WATCOMC__
375 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
378 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug
)
379 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace
)
381 // wxLogSysError: one uses the last error code, for other you must give it
384 // return the system error message description
385 static inline wxString
wxLogSysErrorHelper(long err
)
387 return wxString::Format(_(" (error %ld: %s)"), err
, wxSysErrorMsg(err
));
390 void WXDLLIMPEXP_BASE
wxVLogSysError(const wxString
& format
, va_list argptr
)
392 wxVLogSysError(wxSysErrorCode(), format
, argptr
);
395 #if !wxUSE_UTF8_LOCALE_ONLY
396 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(const wxChar
*format
, ...)
399 va_start(argptr
, format
);
400 wxVLogSysError(format
, argptr
);
403 #endif // !wxUSE_UTF8_LOCALE_ONLY
405 #if wxUSE_UNICODE_UTF8
406 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(const char *format
, ...)
409 va_start(argptr
, format
);
410 wxVLogSysError(format
, argptr
);
413 #endif // wxUSE_UNICODE_UTF8
415 void WXDLLIMPEXP_BASE
wxVLogSysError(long err
, const wxString
& format
, va_list argptr
)
417 if ( wxLog::IsEnabled() ) {
418 wxLog::OnLog(wxLOG_Error
,
419 wxString::FormatV(format
, argptr
) + wxLogSysErrorHelper(err
),
424 #if !wxUSE_UTF8_LOCALE_ONLY
425 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(long lErrCode
, const wxChar
*format
, ...)
428 va_start(argptr
, format
);
429 wxVLogSysError(lErrCode
, format
, argptr
);
432 #endif // !wxUSE_UTF8_LOCALE_ONLY
434 #if wxUSE_UNICODE_UTF8
435 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(long lErrCode
, const char *format
, ...)
438 va_start(argptr
, format
);
439 wxVLogSysError(lErrCode
, format
, argptr
);
442 #endif // wxUSE_UNICODE_UTF8
445 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
446 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(unsigned long lErrCode
, const wxChar
*format
, ...)
449 va_start(argptr
, format
);
450 wxVLogSysError(lErrCode
, format
, argptr
);
454 void WXDLLIMPEXP_BASE
wxVLogSysError(unsigned long err
, const wxString
& format
, va_list argptr
)
455 { wxVLogSysError((long)err
, format
, argptr
); }
456 #endif // __WATCOMC__
458 // ----------------------------------------------------------------------------
459 // wxLog class implementation
460 // ----------------------------------------------------------------------------
462 unsigned wxLog::LogLastRepeatIfNeeded()
464 wxCRIT_SECT_LOCKER(lock
, ms_prevCS
);
466 return LogLastRepeatIfNeededUnlocked();
469 unsigned wxLog::LogLastRepeatIfNeededUnlocked()
471 const unsigned count
= ms_prevCounter
;
473 if ( ms_prevCounter
)
477 msg
.Printf(wxPLURAL("The previous message repeated once.",
478 "The previous message repeated %lu times.",
482 msg
.Printf(wxS("The previous message was repeated %lu times."),
486 ms_prevString
.clear();
487 DoLog(ms_prevLevel
, msg
, ms_prevTimeStamp
);
495 // Flush() must be called before destroying the object as otherwise some
496 // messages could be lost
497 if ( ms_prevCounter
)
499 wxMessageOutputDebug().Printf
501 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
509 void wxLog::OnLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
511 if ( IsEnabled() && ms_logLevel
>= level
)
513 wxLog
*pLogger
= GetActiveTarget();
516 if ( GetRepetitionCounting() )
518 wxCRIT_SECT_LOCKER(lock
, ms_prevCS
);
520 if ( szString
== ms_prevString
)
524 // nothing else to do, in particular, don't log the
529 pLogger
->LogLastRepeatIfNeededUnlocked();
531 // reset repetition counter for a new message
532 ms_prevString
= szString
;
533 ms_prevLevel
= level
;
534 ms_prevTimeStamp
= t
;
537 pLogger
->DoLog(level
, szString
, t
);
542 // deprecated function
543 #if WXWIN_COMPATIBILITY_2_6
545 wxChar
*wxLog::SetLogBuffer(wxChar
* WXUNUSED(buf
), size_t WXUNUSED(size
))
550 #endif // WXWIN_COMPATIBILITY_2_6
552 #if WXWIN_COMPATIBILITY_2_8
554 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
),
555 const char *WXUNUSED(szString
),
560 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
),
561 const wchar_t *WXUNUSED(wzString
),
566 #endif // WXWIN_COMPATIBILITY_2_8
568 wxLog
*wxLog::GetActiveTarget()
570 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
571 // prevent infinite recursion if someone calls wxLogXXX() from
572 // wxApp::CreateLogTarget()
573 static bool s_bInGetActiveTarget
= false;
574 if ( !s_bInGetActiveTarget
) {
575 s_bInGetActiveTarget
= true;
577 // ask the application to create a log target for us
578 if ( wxTheApp
!= NULL
)
579 ms_pLogger
= wxTheApp
->GetTraits()->CreateLogTarget();
581 ms_pLogger
= new wxLogStderr
;
583 s_bInGetActiveTarget
= false;
585 // do nothing if it fails - what can we do?
592 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
594 if ( ms_pLogger
!= NULL
) {
595 // flush the old messages before changing because otherwise they might
596 // get lost later if this target is not restored
600 wxLog
*pOldLogger
= ms_pLogger
;
601 ms_pLogger
= pLogger
;
606 void wxLog::DontCreateOnDemand()
608 ms_bAutoCreate
= false;
610 // this is usually called at the end of the program and we assume that it
611 // is *always* called at the end - so we free memory here to avoid false
612 // memory leak reports from wxWin memory tracking code
616 void wxLog::DoCreateOnDemand()
618 ms_bAutoCreate
= true;
621 void wxLog::RemoveTraceMask(const wxString
& str
)
623 int index
= ms_aTraceMasks
.Index(str
);
624 if ( index
!= wxNOT_FOUND
)
625 ms_aTraceMasks
.RemoveAt((size_t)index
);
628 void wxLog::ClearTraceMasks()
630 ms_aTraceMasks
.Clear();
633 void wxLog::TimeStamp(wxString
*str
)
636 if ( !ms_timestamp
.empty() )
640 (void)time(&timeNow
);
643 wxStrftime(buf
, WXSIZEOF(buf
),
644 ms_timestamp
, wxLocaltime_r(&timeNow
, &tm
));
647 *str
<< buf
<< wxS(": ");
649 #endif // wxUSE_DATETIME
652 void wxLog::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
654 #if WXWIN_COMPATIBILITY_2_8
655 // DoLog() signature changed since 2.8, so we call the old versions here
656 // so that existing custom log classes still work:
657 DoLog(level
, (const char*)szString
.mb_str(), t
);
658 DoLog(level
, (const wchar_t*)szString
.wc_str(), t
);
662 case wxLOG_FatalError
:
663 LogString(_("Fatal error: ") + szString
, t
);
664 LogString(_("Program aborted."), t
);
674 LogString(_("Error: ") + szString
, t
);
678 LogString(_("Warning: ") + szString
, t
);
685 default: // log unknown log levels too
686 LogString(szString
, t
);
693 wxString msg
= level
== wxLOG_Trace
? wxS("Trace: ")
703 void wxLog::DoLogString(const wxString
& szString
, time_t t
)
705 #if WXWIN_COMPATIBILITY_2_8
706 // DoLogString() signature changed since 2.8, so we call the old versions
707 // here so that existing custom log classes still work; unfortunately this
708 // also means that we can't have the wxFAIL_MSG below in compat mode
709 DoLogString((const char*)szString
.mb_str(), t
);
710 DoLogString((const wchar_t*)szString
.wc_str(), t
);
712 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
713 wxUnusedVar(szString
);
720 LogLastRepeatIfNeeded();
723 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
725 for ( wxArrayString::iterator it
= ms_aTraceMasks
.begin(),
726 en
= ms_aTraceMasks
.end();
733 // ----------------------------------------------------------------------------
734 // wxLogBuffer implementation
735 // ----------------------------------------------------------------------------
737 void wxLogBuffer::Flush()
739 if ( !m_str
.empty() )
741 wxMessageOutputBest out
;
742 out
.Printf(wxS("%s"), m_str
.c_str());
747 void wxLogBuffer::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
754 // don't put debug messages in the buffer, we don't want to show
755 // them to the user in a msg box, log them immediately
761 wxMessageOutputDebug dbgout
;
762 dbgout
.Printf(wxS("%s\n"), str
.c_str());
764 #endif // __WXDEBUG__
768 wxLog::DoLog(level
, szString
, t
);
772 void wxLogBuffer::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
774 m_str
<< szString
<< wxS("\n");
777 // ----------------------------------------------------------------------------
778 // wxLogStderr class implementation
779 // ----------------------------------------------------------------------------
781 wxLogStderr::wxLogStderr(FILE *fp
)
789 void wxLogStderr::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
796 wxFputc(wxS('\n'), m_fp
);
799 // under GUI systems such as Windows or Mac, programs usually don't have
800 // stderr at all, so show the messages also somewhere else, typically in
801 // the debugger window so that they go at least somewhere instead of being
803 if ( m_fp
== stderr
)
805 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
806 if ( traits
&& !traits
->HasStderr() )
808 wxMessageOutputDebug dbgout
;
809 dbgout
.Printf(wxS("%s\n"), str
.c_str());
814 // ----------------------------------------------------------------------------
815 // wxLogStream implementation
816 // ----------------------------------------------------------------------------
818 #if wxUSE_STD_IOSTREAM
819 #include "wx/ioswrap.h"
820 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
823 m_ostr
= &wxSTD cerr
;
828 void wxLogStream::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
832 (*m_ostr
) << stamp
<< szString
<< wxSTD endl
;
834 #endif // wxUSE_STD_IOSTREAM
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 wxLogChain::wxLogChain(wxLog
*logger
)
842 m_bPassMessages
= true;
845 m_logOld
= wxLog::SetActiveTarget(this);
848 wxLogChain::~wxLogChain()
852 if ( m_logNew
!= this )
856 void wxLogChain::SetLog(wxLog
*logger
)
858 if ( m_logNew
!= this )
864 void wxLogChain::Flush()
869 // be careful to avoid infinite recursion
870 if ( m_logNew
&& m_logNew
!= this )
874 void wxLogChain::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
876 // let the previous logger show it
877 if ( m_logOld
&& IsPassingMessages() )
879 // bogus cast just to access protected DoLog
880 ((wxLogChain
*)m_logOld
)->DoLog(level
, szString
, t
);
883 if ( m_logNew
&& m_logNew
!= this )
886 ((wxLogChain
*)m_logNew
)->DoLog(level
, szString
, t
);
891 // "'this' : used in base member initializer list" - so what?
892 #pragma warning(disable:4355)
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 wxLogInterposer::wxLogInterposer()
904 // ----------------------------------------------------------------------------
905 // wxLogInterposerTemp
906 // ----------------------------------------------------------------------------
908 wxLogInterposerTemp::wxLogInterposerTemp()
915 #pragma warning(default:4355)
918 // ============================================================================
919 // Global functions/variables
920 // ============================================================================
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
927 wxCriticalSection
wxLog::ms_prevCS
;
928 #endif // wxUSE_THREADS
929 bool wxLog::ms_bRepetCounting
= false;
930 wxString
wxLog::ms_prevString
;
931 unsigned int wxLog::ms_prevCounter
= 0;
932 time_t wxLog::ms_prevTimeStamp
= 0;
933 wxLogLevel
wxLog::ms_prevLevel
;
935 wxLog
*wxLog::ms_pLogger
= (wxLog
*)NULL
;
936 bool wxLog::ms_doLog
= true;
937 bool wxLog::ms_bAutoCreate
= true;
938 bool wxLog::ms_bVerbose
= false;
940 wxLogLevel
wxLog::ms_logLevel
= wxLOG_Max
; // log everything by default
942 size_t wxLog::ms_suspendCount
= 0;
944 wxString
wxLog::ms_timestamp(wxS("%X")); // time only, no date
946 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
947 wxArrayString
wxLog::ms_aTraceMasks
;
949 // ----------------------------------------------------------------------------
950 // stdout error logging helper
951 // ----------------------------------------------------------------------------
953 // helper function: wraps the message and justifies it under given position
954 // (looks more pretty on the terminal). Also adds newline at the end.
956 // TODO this is now disabled until I find a portable way of determining the
957 // terminal window size (ok, I found it but does anybody really cares?)
958 #ifdef LOG_PRETTY_WRAP
959 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
961 size_t nMax
= 80; // FIXME
962 size_t nStart
= strlen(pszPrefix
);
966 while ( *psz
!= '\0' ) {
967 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
971 if ( *psz
!= '\0' ) {
973 for ( n
= 0; n
< nStart
; n
++ )
976 // as we wrapped, squeeze all white space
977 while ( isspace(*psz
) )
984 #endif //LOG_PRETTY_WRAP
986 // ----------------------------------------------------------------------------
987 // error code/error message retrieval functions
988 // ----------------------------------------------------------------------------
990 // get error code from syste
991 unsigned long wxSysErrorCode()
993 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
994 return ::GetLastError();
1000 // get error message from system
1001 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1003 if ( nErrCode
== 0 )
1004 nErrCode
= wxSysErrorCode();
1006 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1007 static wxChar s_szBuf
[1024];
1009 // get error message from system
1011 if ( ::FormatMessage
1013 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1016 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1022 // if this happens, something is seriously wrong, so don't use _() here
1024 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1029 // copy it to our buffer and free memory
1030 // Crashes on SmartPhone (FIXME)
1031 #if !defined(__SMARTPHONE__) /* of WinCE */
1034 wxStrncpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
) - 1);
1035 s_szBuf
[WXSIZEOF(s_szBuf
) - 1] = wxS('\0');
1037 LocalFree(lpMsgBuf
);
1039 // returned string is capitalized and ended with '\r\n' - bad
1040 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1041 size_t len
= wxStrlen(s_szBuf
);
1044 if ( s_szBuf
[len
- 2] == wxS('\r') )
1045 s_szBuf
[len
- 2] = wxS('\0');
1049 #endif // !__SMARTPHONE__
1051 s_szBuf
[0] = wxS('\0');
1057 static wchar_t s_wzBuf
[1024];
1058 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1059 WXSIZEOF(s_wzBuf
) - 1);
1062 return strerror((int)nErrCode
);
1064 #endif // __WXMSW__/!__WXMSW__