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::AddTraceMask(const wxString
& str
)
623 wxCRIT_SECT_LOCKER(lock
, ms_traceCS
);
625 ms_aTraceMasks
.push_back(str
);
628 void wxLog::RemoveTraceMask(const wxString
& str
)
630 wxCRIT_SECT_LOCKER(lock
, ms_traceCS
);
632 int index
= ms_aTraceMasks
.Index(str
);
633 if ( index
!= wxNOT_FOUND
)
634 ms_aTraceMasks
.RemoveAt((size_t)index
);
637 void wxLog::ClearTraceMasks()
639 wxCRIT_SECT_LOCKER(lock
, ms_traceCS
);
641 ms_aTraceMasks
.Clear();
644 void wxLog::TimeStamp(wxString
*str
)
647 if ( !ms_timestamp
.empty() )
651 (void)time(&timeNow
);
654 wxStrftime(buf
, WXSIZEOF(buf
),
655 ms_timestamp
, wxLocaltime_r(&timeNow
, &tm
));
658 *str
<< buf
<< wxS(": ");
660 #endif // wxUSE_DATETIME
663 void wxLog::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
665 #if WXWIN_COMPATIBILITY_2_8
666 // DoLog() signature changed since 2.8, so we call the old versions here
667 // so that existing custom log classes still work:
668 DoLog(level
, (const char*)szString
.mb_str(), t
);
669 DoLog(level
, (const wchar_t*)szString
.wc_str(), t
);
673 case wxLOG_FatalError
:
674 LogString(_("Fatal error: ") + szString
, t
);
675 LogString(_("Program aborted."), t
);
685 LogString(_("Error: ") + szString
, t
);
689 LogString(_("Warning: ") + szString
, t
);
696 default: // log unknown log levels too
697 LogString(szString
, t
);
704 wxString msg
= level
== wxLOG_Trace
? wxS("Trace: ")
714 void wxLog::DoLogString(const wxString
& szString
, time_t t
)
716 #if WXWIN_COMPATIBILITY_2_8
717 // DoLogString() signature changed since 2.8, so we call the old versions
718 // here so that existing custom log classes still work; unfortunately this
719 // also means that we can't have the wxFAIL_MSG below in compat mode
720 DoLogString((const char*)szString
.mb_str(), t
);
721 DoLogString((const wchar_t*)szString
.wc_str(), t
);
723 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
724 wxUnusedVar(szString
);
731 LogLastRepeatIfNeeded();
734 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
736 wxCRIT_SECT_LOCKER(lock
, ms_traceCS
);
738 for ( wxArrayString::iterator it
= ms_aTraceMasks
.begin(),
739 en
= ms_aTraceMasks
.end();
749 // ----------------------------------------------------------------------------
750 // wxLogBuffer implementation
751 // ----------------------------------------------------------------------------
753 void wxLogBuffer::Flush()
755 if ( !m_str
.empty() )
757 wxMessageOutputBest out
;
758 out
.Printf(wxS("%s"), m_str
.c_str());
763 void wxLogBuffer::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
770 // don't put debug messages in the buffer, we don't want to show
771 // them to the user in a msg box, log them immediately
777 wxMessageOutputDebug dbgout
;
778 dbgout
.Printf(wxS("%s\n"), str
.c_str());
780 #endif // __WXDEBUG__
784 wxLog::DoLog(level
, szString
, t
);
788 void wxLogBuffer::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
790 m_str
<< szString
<< wxS("\n");
793 // ----------------------------------------------------------------------------
794 // wxLogStderr class implementation
795 // ----------------------------------------------------------------------------
797 wxLogStderr::wxLogStderr(FILE *fp
)
805 void wxLogStderr::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
812 wxFputc(wxS('\n'), m_fp
);
815 // under GUI systems such as Windows or Mac, programs usually don't have
816 // stderr at all, so show the messages also somewhere else, typically in
817 // the debugger window so that they go at least somewhere instead of being
819 if ( m_fp
== stderr
)
821 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
822 if ( traits
&& !traits
->HasStderr() )
824 wxMessageOutputDebug dbgout
;
825 dbgout
.Printf(wxS("%s\n"), str
.c_str());
830 // ----------------------------------------------------------------------------
831 // wxLogStream implementation
832 // ----------------------------------------------------------------------------
834 #if wxUSE_STD_IOSTREAM
835 #include "wx/ioswrap.h"
836 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
839 m_ostr
= &wxSTD cerr
;
844 void wxLogStream::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
848 (*m_ostr
) << stamp
<< szString
<< wxSTD endl
;
850 #endif // wxUSE_STD_IOSTREAM
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 wxLogChain::wxLogChain(wxLog
*logger
)
858 m_bPassMessages
= true;
861 m_logOld
= wxLog::SetActiveTarget(this);
864 wxLogChain::~wxLogChain()
868 if ( m_logNew
!= this )
872 void wxLogChain::SetLog(wxLog
*logger
)
874 if ( m_logNew
!= this )
880 void wxLogChain::Flush()
885 // be careful to avoid infinite recursion
886 if ( m_logNew
&& m_logNew
!= this )
890 void wxLogChain::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
892 // let the previous logger show it
893 if ( m_logOld
&& IsPassingMessages() )
895 // bogus cast just to access protected DoLog
896 ((wxLogChain
*)m_logOld
)->DoLog(level
, szString
, t
);
899 if ( m_logNew
&& m_logNew
!= this )
902 ((wxLogChain
*)m_logNew
)->DoLog(level
, szString
, t
);
907 // "'this' : used in base member initializer list" - so what?
908 #pragma warning(disable:4355)
911 // ----------------------------------------------------------------------------
913 // ----------------------------------------------------------------------------
915 wxLogInterposer::wxLogInterposer()
920 // ----------------------------------------------------------------------------
921 // wxLogInterposerTemp
922 // ----------------------------------------------------------------------------
924 wxLogInterposerTemp::wxLogInterposerTemp()
931 #pragma warning(default:4355)
934 // ============================================================================
935 // Global functions/variables
936 // ============================================================================
938 // ----------------------------------------------------------------------------
940 // ----------------------------------------------------------------------------
943 wxCriticalSection
wxLog::ms_prevCS
,
945 #endif // wxUSE_THREADS
946 bool wxLog::ms_bRepetCounting
= false;
947 wxString
wxLog::ms_prevString
;
948 unsigned int wxLog::ms_prevCounter
= 0;
949 time_t wxLog::ms_prevTimeStamp
= 0;
950 wxLogLevel
wxLog::ms_prevLevel
;
952 wxLog
*wxLog::ms_pLogger
= (wxLog
*)NULL
;
953 bool wxLog::ms_doLog
= true;
954 bool wxLog::ms_bAutoCreate
= true;
955 bool wxLog::ms_bVerbose
= false;
957 wxLogLevel
wxLog::ms_logLevel
= wxLOG_Max
; // log everything by default
959 size_t wxLog::ms_suspendCount
= 0;
961 wxString
wxLog::ms_timestamp(wxS("%X")); // time only, no date
963 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
964 wxArrayString
wxLog::ms_aTraceMasks
;
966 // ----------------------------------------------------------------------------
967 // stdout error logging helper
968 // ----------------------------------------------------------------------------
970 // helper function: wraps the message and justifies it under given position
971 // (looks more pretty on the terminal). Also adds newline at the end.
973 // TODO this is now disabled until I find a portable way of determining the
974 // terminal window size (ok, I found it but does anybody really cares?)
975 #ifdef LOG_PRETTY_WRAP
976 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
978 size_t nMax
= 80; // FIXME
979 size_t nStart
= strlen(pszPrefix
);
983 while ( *psz
!= '\0' ) {
984 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
988 if ( *psz
!= '\0' ) {
990 for ( n
= 0; n
< nStart
; n
++ )
993 // as we wrapped, squeeze all white space
994 while ( isspace(*psz
) )
1001 #endif //LOG_PRETTY_WRAP
1003 // ----------------------------------------------------------------------------
1004 // error code/error message retrieval functions
1005 // ----------------------------------------------------------------------------
1007 // get error code from syste
1008 unsigned long wxSysErrorCode()
1010 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1011 return ::GetLastError();
1017 // get error message from system
1018 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1020 if ( nErrCode
== 0 )
1021 nErrCode
= wxSysErrorCode();
1023 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1024 static wxChar s_szBuf
[1024];
1026 // get error message from system
1028 if ( ::FormatMessage
1030 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1033 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1039 // if this happens, something is seriously wrong, so don't use _() here
1041 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1046 // copy it to our buffer and free memory
1047 // Crashes on SmartPhone (FIXME)
1048 #if !defined(__SMARTPHONE__) /* of WinCE */
1051 wxStrncpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
) - 1);
1052 s_szBuf
[WXSIZEOF(s_szBuf
) - 1] = wxS('\0');
1054 LocalFree(lpMsgBuf
);
1056 // returned string is capitalized and ended with '\r\n' - bad
1057 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1058 size_t len
= wxStrlen(s_szBuf
);
1061 if ( s_szBuf
[len
- 2] == wxS('\r') )
1062 s_szBuf
[len
- 2] = wxS('\0');
1066 #endif // !__SMARTPHONE__
1068 s_szBuf
[0] = wxS('\0');
1074 static wchar_t s_wzBuf
[1024];
1075 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1076 WXSIZEOF(s_wzBuf
) - 1);
1079 return strerror((int)nErrCode
);
1081 #endif // __WXMSW__/!__WXMSW__