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
68 // define static functions providing access to the critical sections we use
69 // instead of just using static critical section variables as log functions may
70 // be used during static initialization and while this is certainly not
71 // advisable it's still better to not crash (as we'd do if we used a yet
72 // uninitialized critical section) if it happens
74 static inline wxCriticalSection
& GetTraceMaskCS()
76 static wxCriticalSection s_csTrace
;
81 static inline wxCriticalSection
& GetPreviousLogCS()
83 static wxCriticalSection s_csPrev
;
88 #endif // wxUSE_THREADS
90 // ----------------------------------------------------------------------------
91 // non member functions
92 // ----------------------------------------------------------------------------
94 // define this to enable wrapping of log messages
95 //#define LOG_PRETTY_WRAP
97 #ifdef LOG_PRETTY_WRAP
98 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
106 // implementation of Log functions
108 // NB: unfortunately we need all these distinct functions, we can't make them
109 // macros and not all compilers inline vararg functions.
110 // ----------------------------------------------------------------------------
112 // generic log function
113 void wxVLogGeneric(wxLogLevel level
, const wxString
& format
, va_list argptr
)
115 if ( wxLog::IsEnabled() ) {
116 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
), time(NULL
));
120 #if !wxUSE_UTF8_LOCALE_ONLY
121 void wxDoLogGenericWchar(wxLogLevel level
, const wxChar
*format
, ...)
124 va_start(argptr
, format
);
125 wxVLogGeneric(level
, format
, argptr
);
128 #endif // wxUSE_UTF8_LOCALE_ONLY
130 #if wxUSE_UNICODE_UTF8
131 void wxDoLogGenericUtf8(wxLogLevel level
, const char *format
, ...)
134 va_start(argptr
, format
);
135 wxVLogGeneric(level
, format
, argptr
);
138 #endif // wxUSE_UNICODE_UTF8
140 #if !wxUSE_UTF8_LOCALE_ONLY
141 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
142 void wxDoLog##level##Wchar(const wxChar *format, ...) \
145 va_start(argptr, format); \
146 wxVLog##level(format, argptr); \
150 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level)
153 #if wxUSE_UNICODE_UTF8
154 #define IMPLEMENT_LOG_FUNCTION_UTF8(level) \
155 void wxDoLog##level##Utf8(const char *format, ...) \
158 va_start(argptr, format); \
159 wxVLog##level(format, argptr); \
163 #define IMPLEMENT_LOG_FUNCTION_UTF8(level)
166 #define IMPLEMENT_LOG_FUNCTION(level) \
167 void wxVLog##level(const wxString& format, va_list argptr) \
169 if ( wxLog::IsEnabled() ) { \
170 wxLog::OnLog(wxLOG_##level, \
171 wxString::FormatV(format, argptr), time(NULL)); \
174 IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
175 IMPLEMENT_LOG_FUNCTION_UTF8(level)
177 IMPLEMENT_LOG_FUNCTION(Error
)
178 IMPLEMENT_LOG_FUNCTION(Warning
)
179 IMPLEMENT_LOG_FUNCTION(Message
)
180 IMPLEMENT_LOG_FUNCTION(Info
)
181 IMPLEMENT_LOG_FUNCTION(Status
)
183 void wxSafeShowMessage(const wxString
& title
, const wxString
& text
)
186 ::MessageBox(NULL
, text
.t_str(), title
.t_str(), MB_OK
| MB_ICONSTOP
);
188 wxFprintf(stderr
, wxS("%s: %s\n"), title
.c_str(), text
.c_str());
193 // fatal errors can't be suppressed nor handled by the custom log target and
194 // always terminate the program
195 void wxVLogFatalError(const wxString
& format
, va_list argptr
)
197 wxSafeShowMessage(wxS("Fatal Error"), wxString::FormatV(format
, argptr
));
206 #if !wxUSE_UTF8_LOCALE_ONLY
207 void wxDoLogFatalErrorWchar(const wxChar
*format
, ...)
210 va_start(argptr
, format
);
211 wxVLogFatalError(format
, argptr
);
213 // some compilers warn about unreachable code and it shouldn't matter
214 // for the others anyhow...
217 #endif // wxUSE_UTF8_LOCALE_ONLY
219 #if wxUSE_UNICODE_UTF8
220 void wxDoLogFatalErrorUtf8(const char *format
, ...)
223 va_start(argptr
, format
);
224 wxVLogFatalError(format
, argptr
);
226 // some compilers warn about unreachable code and it shouldn't matter
227 // for the others anyhow...
230 #endif // wxUSE_UNICODE_UTF8
232 // same as info, but only if 'verbose' mode is on
233 void wxVLogVerbose(const wxString
& format
, va_list argptr
)
235 if ( wxLog::IsEnabled() ) {
236 if ( wxLog::GetActiveTarget() != NULL
&& wxLog::GetVerbose() ) {
237 wxLog::OnLog(wxLOG_Info
,
238 wxString::FormatV(format
, argptr
), time(NULL
));
243 #if !wxUSE_UTF8_LOCALE_ONLY
244 void wxDoLogVerboseWchar(const wxChar
*format
, ...)
247 va_start(argptr
, format
);
248 wxVLogVerbose(format
, argptr
);
251 #endif // !wxUSE_UTF8_LOCALE_ONLY
253 #if wxUSE_UNICODE_UTF8
254 void wxDoLogVerboseUtf8(const char *format
, ...)
257 va_start(argptr
, format
);
258 wxVLogVerbose(format
, argptr
);
261 #endif // wxUSE_UNICODE_UTF8
266 #if !wxUSE_UTF8_LOCALE_ONLY
267 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
268 void wxDoLog##level##Wchar(const wxChar *format, ...) \
271 va_start(argptr, format); \
272 wxVLog##level(format, argptr); \
276 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level)
279 #if wxUSE_UNICODE_UTF8
280 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level) \
281 void wxDoLog##level##Utf8(const char *format, ...) \
284 va_start(argptr, format); \
285 wxVLog##level(format, argptr); \
289 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
292 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
293 void wxVLog##level(const wxString& format, va_list argptr) \
295 if ( wxLog::IsEnabled() ) { \
296 wxLog::OnLog(wxLOG_##level, \
297 wxString::FormatV(format, argptr), time(NULL)); \
300 IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
301 IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
304 void wxVLogTrace(const wxString
& mask
, const wxString
& format
, va_list argptr
)
306 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask
) ) {
308 msg
<< wxS("(") << mask
<< wxS(") ") << wxString::FormatV(format
, argptr
);
310 wxLog::OnLog(wxLOG_Trace
, msg
, time(NULL
));
314 #if !wxUSE_UTF8_LOCALE_ONLY
315 void wxDoLogTraceWchar(const wxString
& mask
, const wxChar
*format
, ...)
318 va_start(argptr
, format
);
319 wxVLogTrace(mask
, format
, argptr
);
322 #endif // !wxUSE_UTF8_LOCALE_ONLY
324 #if wxUSE_UNICODE_UTF8
325 void wxDoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
328 va_start(argptr
, format
);
329 wxVLogTrace(mask
, format
, argptr
);
332 #endif // wxUSE_UNICODE_UTF8
334 void wxVLogTrace(wxTraceMask mask
, const wxString
& format
, va_list argptr
)
336 // we check that all of mask bits are set in the current mask, so
337 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
338 // if both bits are set.
339 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask
) == mask
) ) {
340 wxLog::OnLog(wxLOG_Trace
, wxString::FormatV(format
, argptr
), time(NULL
));
344 #if !wxUSE_UTF8_LOCALE_ONLY
345 void wxDoLogTraceWchar(wxTraceMask mask
, const wxChar
*format
, ...)
348 va_start(argptr
, format
);
349 wxVLogTrace(mask
, format
, argptr
);
352 #endif // !wxUSE_UTF8_LOCALE_ONLY
354 #if wxUSE_UNICODE_UTF8
355 void wxDoLogTraceUtf8(wxTraceMask mask
, const char *format
, ...)
358 va_start(argptr
, format
);
359 wxVLogTrace(mask
, format
, argptr
);
362 #endif // wxUSE_UNICODE_UTF8
365 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
366 void wxDoLogTraceWchar(int mask
, const wxChar
*format
, ...)
369 va_start(argptr
, format
);
370 wxVLogTrace(mask
, format
, argptr
);
374 void wxDoLogTraceWchar(const char *mask
, const wxChar
*format
, ...)
377 va_start(argptr
, format
);
378 wxVLogTrace(mask
, format
, argptr
);
382 void wxDoLogTraceWchar(const wchar_t *mask
, const wxChar
*format
, ...)
385 va_start(argptr
, format
);
386 wxVLogTrace(mask
, format
, argptr
);
390 void wxVLogTrace(int mask
, const wxString
& format
, va_list argptr
)
391 { wxVLogTrace((wxTraceMask
)mask
, format
, argptr
); }
392 void wxVLogTrace(const char *mask
, const wxString
& format
, va_list argptr
)
393 { wxVLogTrace(wxString(mask
), format
, argptr
); }
394 void wxVLogTrace(const wchar_t *mask
, const wxString
& format
, va_list argptr
)
395 { wxVLogTrace(wxString(mask
), format
, argptr
); }
396 #endif // __WATCOMC__
399 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
402 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug
)
403 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace
)
405 // wxLogSysError: one uses the last error code, for other you must give it
408 // return the system error message description
409 static inline wxString
wxLogSysErrorHelper(long err
)
411 return wxString::Format(_(" (error %ld: %s)"), err
, wxSysErrorMsg(err
));
414 void WXDLLIMPEXP_BASE
wxVLogSysError(const wxString
& format
, va_list argptr
)
416 wxVLogSysError(wxSysErrorCode(), format
, argptr
);
419 #if !wxUSE_UTF8_LOCALE_ONLY
420 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(const wxChar
*format
, ...)
423 va_start(argptr
, format
);
424 wxVLogSysError(format
, argptr
);
427 #endif // !wxUSE_UTF8_LOCALE_ONLY
429 #if wxUSE_UNICODE_UTF8
430 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(const char *format
, ...)
433 va_start(argptr
, format
);
434 wxVLogSysError(format
, argptr
);
437 #endif // wxUSE_UNICODE_UTF8
439 void WXDLLIMPEXP_BASE
wxVLogSysError(long err
, const wxString
& format
, va_list argptr
)
441 if ( wxLog::IsEnabled() ) {
442 wxLog::OnLog(wxLOG_Error
,
443 wxString::FormatV(format
, argptr
) + wxLogSysErrorHelper(err
),
448 #if !wxUSE_UTF8_LOCALE_ONLY
449 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(long lErrCode
, const wxChar
*format
, ...)
452 va_start(argptr
, format
);
453 wxVLogSysError(lErrCode
, format
, argptr
);
456 #endif // !wxUSE_UTF8_LOCALE_ONLY
458 #if wxUSE_UNICODE_UTF8
459 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(long lErrCode
, const char *format
, ...)
462 va_start(argptr
, format
);
463 wxVLogSysError(lErrCode
, format
, argptr
);
466 #endif // wxUSE_UNICODE_UTF8
469 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
470 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(unsigned long lErrCode
, const wxChar
*format
, ...)
473 va_start(argptr
, format
);
474 wxVLogSysError(lErrCode
, format
, argptr
);
478 void WXDLLIMPEXP_BASE
wxVLogSysError(unsigned long err
, const wxString
& format
, va_list argptr
)
479 { wxVLogSysError((long)err
, format
, argptr
); }
480 #endif // __WATCOMC__
482 // ----------------------------------------------------------------------------
483 // wxLog class implementation
484 // ----------------------------------------------------------------------------
486 unsigned wxLog::LogLastRepeatIfNeeded()
488 wxCRIT_SECT_LOCKER(lock
, GetPreviousLogCS());
490 return LogLastRepeatIfNeededUnlocked();
493 unsigned wxLog::LogLastRepeatIfNeededUnlocked()
495 const unsigned count
= ms_prevCounter
;
497 if ( ms_prevCounter
)
501 msg
.Printf(wxPLURAL("The previous message repeated once.",
502 "The previous message repeated %lu times.",
506 msg
.Printf(wxS("The previous message was repeated %lu times."),
510 ms_prevString
.clear();
511 DoLog(ms_prevLevel
, msg
, ms_prevTimeStamp
);
519 // Flush() must be called before destroying the object as otherwise some
520 // messages could be lost
521 if ( ms_prevCounter
)
523 wxMessageOutputDebug().Printf
525 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
533 void wxLog::OnLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
535 if ( IsEnabled() && ms_logLevel
>= level
)
537 wxLog
*pLogger
= GetActiveTarget();
540 if ( GetRepetitionCounting() )
542 wxCRIT_SECT_LOCKER(lock
, GetPreviousLogCS());
544 if ( szString
== ms_prevString
)
548 // nothing else to do, in particular, don't log the
553 pLogger
->LogLastRepeatIfNeededUnlocked();
555 // reset repetition counter for a new message
556 ms_prevString
= szString
;
557 ms_prevLevel
= level
;
558 ms_prevTimeStamp
= t
;
561 pLogger
->DoLog(level
, szString
, t
);
566 // deprecated function
567 #if WXWIN_COMPATIBILITY_2_6
569 wxChar
*wxLog::SetLogBuffer(wxChar
* WXUNUSED(buf
), size_t WXUNUSED(size
))
574 #endif // WXWIN_COMPATIBILITY_2_6
576 #if WXWIN_COMPATIBILITY_2_8
578 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
),
579 const char *WXUNUSED(szString
),
584 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
),
585 const wchar_t *WXUNUSED(wzString
),
590 #endif // WXWIN_COMPATIBILITY_2_8
592 wxLog
*wxLog::GetActiveTarget()
594 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
595 // prevent infinite recursion if someone calls wxLogXXX() from
596 // wxApp::CreateLogTarget()
597 static bool s_bInGetActiveTarget
= false;
598 if ( !s_bInGetActiveTarget
) {
599 s_bInGetActiveTarget
= true;
601 // ask the application to create a log target for us
602 if ( wxTheApp
!= NULL
)
603 ms_pLogger
= wxTheApp
->GetTraits()->CreateLogTarget();
605 ms_pLogger
= new wxLogStderr
;
607 s_bInGetActiveTarget
= false;
609 // do nothing if it fails - what can we do?
616 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
618 if ( ms_pLogger
!= NULL
) {
619 // flush the old messages before changing because otherwise they might
620 // get lost later if this target is not restored
624 wxLog
*pOldLogger
= ms_pLogger
;
625 ms_pLogger
= pLogger
;
630 void wxLog::DontCreateOnDemand()
632 ms_bAutoCreate
= false;
634 // this is usually called at the end of the program and we assume that it
635 // is *always* called at the end - so we free memory here to avoid false
636 // memory leak reports from wxWin memory tracking code
640 void wxLog::DoCreateOnDemand()
642 ms_bAutoCreate
= true;
645 void wxLog::AddTraceMask(const wxString
& str
)
647 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
649 ms_aTraceMasks
.push_back(str
);
652 void wxLog::RemoveTraceMask(const wxString
& str
)
654 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
656 int index
= ms_aTraceMasks
.Index(str
);
657 if ( index
!= wxNOT_FOUND
)
658 ms_aTraceMasks
.RemoveAt((size_t)index
);
661 void wxLog::ClearTraceMasks()
663 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
665 ms_aTraceMasks
.Clear();
668 void wxLog::TimeStamp(wxString
*str
)
671 if ( !ms_timestamp
.empty() )
675 (void)time(&timeNow
);
678 wxStrftime(buf
, WXSIZEOF(buf
),
679 ms_timestamp
, wxLocaltime_r(&timeNow
, &tm
));
682 *str
<< buf
<< wxS(": ");
684 #endif // wxUSE_DATETIME
687 void wxLog::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
689 #if WXWIN_COMPATIBILITY_2_8
690 // DoLog() signature changed since 2.8, so we call the old versions here
691 // so that existing custom log classes still work:
692 DoLog(level
, (const char*)szString
.mb_str(), t
);
693 DoLog(level
, (const wchar_t*)szString
.wc_str(), t
);
697 case wxLOG_FatalError
:
698 LogString(_("Fatal error: ") + szString
, t
);
699 LogString(_("Program aborted."), t
);
709 LogString(_("Error: ") + szString
, t
);
713 LogString(_("Warning: ") + szString
, t
);
720 default: // log unknown log levels too
721 LogString(szString
, t
);
728 wxString msg
= level
== wxLOG_Trace
? wxS("Trace: ")
738 void wxLog::DoLogString(const wxString
& szString
, time_t t
)
740 #if WXWIN_COMPATIBILITY_2_8
741 // DoLogString() signature changed since 2.8, so we call the old versions
742 // here so that existing custom log classes still work; unfortunately this
743 // also means that we can't have the wxFAIL_MSG below in compat mode
744 DoLogString((const char*)szString
.mb_str(), t
);
745 DoLogString((const wchar_t*)szString
.wc_str(), t
);
747 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
748 wxUnusedVar(szString
);
755 LogLastRepeatIfNeeded();
758 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
760 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
762 for ( wxArrayString::iterator it
= ms_aTraceMasks
.begin(),
763 en
= ms_aTraceMasks
.end();
773 // ----------------------------------------------------------------------------
774 // wxLogBuffer implementation
775 // ----------------------------------------------------------------------------
777 void wxLogBuffer::Flush()
779 if ( !m_str
.empty() )
781 wxMessageOutputBest out
;
782 out
.Printf(wxS("%s"), m_str
.c_str());
787 void wxLogBuffer::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
794 // don't put debug messages in the buffer, we don't want to show
795 // them to the user in a msg box, log them immediately
801 wxMessageOutputDebug dbgout
;
802 dbgout
.Printf(wxS("%s\n"), str
.c_str());
804 #endif // __WXDEBUG__
808 wxLog::DoLog(level
, szString
, t
);
812 void wxLogBuffer::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
814 m_str
<< szString
<< wxS("\n");
817 // ----------------------------------------------------------------------------
818 // wxLogStderr class implementation
819 // ----------------------------------------------------------------------------
821 wxLogStderr::wxLogStderr(FILE *fp
)
829 void wxLogStderr::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
836 wxFputc(wxS('\n'), m_fp
);
839 // under GUI systems such as Windows or Mac, programs usually don't have
840 // stderr at all, so show the messages also somewhere else, typically in
841 // the debugger window so that they go at least somewhere instead of being
843 if ( m_fp
== stderr
)
845 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
846 if ( traits
&& !traits
->HasStderr() )
848 wxMessageOutputDebug dbgout
;
849 dbgout
.Printf(wxS("%s\n"), str
.c_str());
854 // ----------------------------------------------------------------------------
855 // wxLogStream implementation
856 // ----------------------------------------------------------------------------
858 #if wxUSE_STD_IOSTREAM
859 #include "wx/ioswrap.h"
860 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
863 m_ostr
= &wxSTD cerr
;
868 void wxLogStream::DoLogString(const wxString
& szString
, time_t WXUNUSED(t
))
872 (*m_ostr
) << stamp
<< szString
<< wxSTD endl
;
874 #endif // wxUSE_STD_IOSTREAM
876 // ----------------------------------------------------------------------------
878 // ----------------------------------------------------------------------------
880 wxLogChain::wxLogChain(wxLog
*logger
)
882 m_bPassMessages
= true;
885 m_logOld
= wxLog::SetActiveTarget(this);
888 wxLogChain::~wxLogChain()
892 if ( m_logNew
!= this )
896 void wxLogChain::SetLog(wxLog
*logger
)
898 if ( m_logNew
!= this )
904 void wxLogChain::Flush()
909 // be careful to avoid infinite recursion
910 if ( m_logNew
&& m_logNew
!= this )
914 void wxLogChain::DoLog(wxLogLevel level
, const wxString
& szString
, time_t t
)
916 // let the previous logger show it
917 if ( m_logOld
&& IsPassingMessages() )
918 m_logOld
->Log(level
, szString
, t
);
920 // and also send it to the new one
921 if ( m_logNew
&& m_logNew
!= this )
922 m_logNew
->Log(level
, szString
, t
);
926 // "'this' : used in base member initializer list" - so what?
927 #pragma warning(disable:4355)
930 // ----------------------------------------------------------------------------
932 // ----------------------------------------------------------------------------
934 wxLogInterposer::wxLogInterposer()
939 // ----------------------------------------------------------------------------
940 // wxLogInterposerTemp
941 // ----------------------------------------------------------------------------
943 wxLogInterposerTemp::wxLogInterposerTemp()
950 #pragma warning(default:4355)
953 // ============================================================================
954 // Global functions/variables
955 // ============================================================================
957 // ----------------------------------------------------------------------------
959 // ----------------------------------------------------------------------------
961 bool wxLog::ms_bRepetCounting
= false;
962 wxString
wxLog::ms_prevString
;
963 unsigned int wxLog::ms_prevCounter
= 0;
964 time_t wxLog::ms_prevTimeStamp
= 0;
965 wxLogLevel
wxLog::ms_prevLevel
;
967 wxLog
*wxLog::ms_pLogger
= NULL
;
968 bool wxLog::ms_doLog
= true;
969 bool wxLog::ms_bAutoCreate
= true;
970 bool wxLog::ms_bVerbose
= false;
972 wxLogLevel
wxLog::ms_logLevel
= wxLOG_Max
; // log everything by default
974 size_t wxLog::ms_suspendCount
= 0;
976 wxString
wxLog::ms_timestamp(wxS("%X")); // time only, no date
978 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
979 wxArrayString
wxLog::ms_aTraceMasks
;
981 // ----------------------------------------------------------------------------
982 // stdout error logging helper
983 // ----------------------------------------------------------------------------
985 // helper function: wraps the message and justifies it under given position
986 // (looks more pretty on the terminal). Also adds newline at the end.
988 // TODO this is now disabled until I find a portable way of determining the
989 // terminal window size (ok, I found it but does anybody really cares?)
990 #ifdef LOG_PRETTY_WRAP
991 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
993 size_t nMax
= 80; // FIXME
994 size_t nStart
= strlen(pszPrefix
);
998 while ( *psz
!= '\0' ) {
999 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
1003 if ( *psz
!= '\0' ) {
1005 for ( n
= 0; n
< nStart
; n
++ )
1008 // as we wrapped, squeeze all white space
1009 while ( isspace(*psz
) )
1016 #endif //LOG_PRETTY_WRAP
1018 // ----------------------------------------------------------------------------
1019 // error code/error message retrieval functions
1020 // ----------------------------------------------------------------------------
1022 // get error code from syste
1023 unsigned long wxSysErrorCode()
1025 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1026 return ::GetLastError();
1032 // get error message from system
1033 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1035 if ( nErrCode
== 0 )
1036 nErrCode
= wxSysErrorCode();
1038 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1039 static wxChar s_szBuf
[1024];
1041 // get error message from system
1043 if ( ::FormatMessage
1045 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1048 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1054 // if this happens, something is seriously wrong, so don't use _() here
1056 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1061 // copy it to our buffer and free memory
1062 // Crashes on SmartPhone (FIXME)
1063 #if !defined(__SMARTPHONE__) /* of WinCE */
1066 wxStrlcpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
));
1068 LocalFree(lpMsgBuf
);
1070 // returned string is capitalized and ended with '\r\n' - bad
1071 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1072 size_t len
= wxStrlen(s_szBuf
);
1075 if ( s_szBuf
[len
- 2] == wxS('\r') )
1076 s_szBuf
[len
- 2] = wxS('\0');
1080 #endif // !__SMARTPHONE__
1082 s_szBuf
[0] = wxS('\0');
1088 static wchar_t s_wzBuf
[1024];
1089 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1090 WXSIZEOF(s_wzBuf
) - 1);
1093 return strerror((int)nErrCode
);
1095 #endif // __WXMSW__/!__WXMSW__