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 // ----------------------------------------------------------------------------
108 // this struct is used to store information about the previous log message used
109 // by OnLog() to (optionally) avoid logging multiple copies of the same message
110 struct PreviousLogInfo
118 // previous message itself
124 // other information about it
125 wxLogRecordInfo info
;
127 // the number of times it was already repeated
128 unsigned numRepeated
;
131 PreviousLogInfo gs_prevLog
;
133 } // anonymous namespace
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
140 // implementation of Log functions
142 // NB: unfortunately we need all these distinct functions, we can't make them
143 // macros and not all compilers inline vararg functions.
144 // ----------------------------------------------------------------------------
146 // generic log function
147 void wxVLogGeneric(wxLogLevel level
, const wxString
& format
, va_list argptr
)
149 if ( wxLog::IsEnabled() )
151 wxLog::OnLog(level
, wxString::FormatV(format
, argptr
));
155 #if !wxUSE_UTF8_LOCALE_ONLY
156 void wxDoLogGenericWchar(wxLogLevel level
, const wxChar
*format
, ...)
159 va_start(argptr
, format
);
160 wxVLogGeneric(level
, format
, argptr
);
163 #endif // wxUSE_UTF8_LOCALE_ONLY
165 #if wxUSE_UNICODE_UTF8
166 void wxDoLogGenericUtf8(wxLogLevel level
, const char *format
, ...)
169 va_start(argptr
, format
);
170 wxVLogGeneric(level
, format
, argptr
);
173 #endif // wxUSE_UNICODE_UTF8
175 #if !wxUSE_UTF8_LOCALE_ONLY
176 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
177 void wxDoLog##level##Wchar(const wxChar *format, ...) \
180 va_start(argptr, format); \
181 wxVLog##level(format, argptr); \
185 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level)
188 #if wxUSE_UNICODE_UTF8
189 #define IMPLEMENT_LOG_FUNCTION_UTF8(level) \
190 void wxDoLog##level##Utf8(const char *format, ...) \
193 va_start(argptr, format); \
194 wxVLog##level(format, argptr); \
198 #define IMPLEMENT_LOG_FUNCTION_UTF8(level)
201 #define IMPLEMENT_LOG_FUNCTION(level) \
202 void wxVLog##level(const wxString& format, va_list argptr) \
204 if ( wxLog::IsEnabled() ) \
205 wxLog::OnLog(wxLOG_##level, wxString::FormatV(format, argptr)); \
207 IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
208 IMPLEMENT_LOG_FUNCTION_UTF8(level)
210 IMPLEMENT_LOG_FUNCTION(Error
)
211 IMPLEMENT_LOG_FUNCTION(Warning
)
212 IMPLEMENT_LOG_FUNCTION(Message
)
213 IMPLEMENT_LOG_FUNCTION(Info
)
214 IMPLEMENT_LOG_FUNCTION(Status
)
216 void wxSafeShowMessage(const wxString
& title
, const wxString
& text
)
219 ::MessageBox(NULL
, text
.t_str(), title
.t_str(), MB_OK
| MB_ICONSTOP
);
221 wxFprintf(stderr
, wxS("%s: %s\n"), title
.c_str(), text
.c_str());
226 // fatal errors can't be suppressed nor handled by the custom log target and
227 // always terminate the program
228 void wxVLogFatalError(const wxString
& format
, va_list argptr
)
230 wxSafeShowMessage(wxS("Fatal Error"), wxString::FormatV(format
, argptr
));
239 #if !wxUSE_UTF8_LOCALE_ONLY
240 void wxDoLogFatalErrorWchar(const wxChar
*format
, ...)
243 va_start(argptr
, format
);
244 wxVLogFatalError(format
, argptr
);
246 // some compilers warn about unreachable code and it shouldn't matter
247 // for the others anyhow...
250 #endif // wxUSE_UTF8_LOCALE_ONLY
252 #if wxUSE_UNICODE_UTF8
253 void wxDoLogFatalErrorUtf8(const char *format
, ...)
256 va_start(argptr
, format
);
257 wxVLogFatalError(format
, argptr
);
259 // some compilers warn about unreachable code and it shouldn't matter
260 // for the others anyhow...
263 #endif // wxUSE_UNICODE_UTF8
265 // same as info, but only if 'verbose' mode is on
266 void wxVLogVerbose(const wxString
& format
, va_list argptr
)
268 if ( wxLog::IsEnabled() ) {
269 if ( wxLog::GetActiveTarget() != NULL
&& wxLog::GetVerbose() )
270 wxLog::OnLog(wxLOG_Info
, wxString::FormatV(format
, argptr
));
274 #if !wxUSE_UTF8_LOCALE_ONLY
275 void wxDoLogVerboseWchar(const wxChar
*format
, ...)
278 va_start(argptr
, format
);
279 wxVLogVerbose(format
, argptr
);
282 #endif // !wxUSE_UTF8_LOCALE_ONLY
284 #if wxUSE_UNICODE_UTF8
285 void wxDoLogVerboseUtf8(const char *format
, ...)
288 va_start(argptr
, format
);
289 wxVLogVerbose(format
, argptr
);
292 #endif // wxUSE_UNICODE_UTF8
294 // ----------------------------------------------------------------------------
295 // debug and trace functions
296 // ----------------------------------------------------------------------------
299 void wxVLogDebug(const wxString
& format
, va_list argptr
)
301 if ( wxLog::IsEnabled() )
303 wxLog::OnLog(wxLOG_Debug
, wxString::FormatV(format
, argptr
));
307 #if !wxUSE_UTF8_LOCALE_ONLY
308 void wxDoLogDebugWchar(const wxChar
*format
, ...)
311 va_start(argptr
, format
);
312 wxVLogDebug(format
, argptr
);
315 #endif // !wxUSE_UTF8_LOCALE_ONLY
317 #if wxUSE_UNICODE_UTF8
318 void wxDoLogDebugUtf8(const char *format
, ...)
321 va_start(argptr
, format
);
322 wxVLogDebug(format
, argptr
);
325 #endif // wxUSE_UNICODE_UTF8
326 #endif // wxUSE_LOG_DEBUG
329 void wxVLogTrace(const wxString
& mask
, const wxString
& format
, va_list argptr
)
331 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask
) ) {
333 msg
<< wxS("(") << mask
<< wxS(") ") << wxString::FormatV(format
, argptr
);
335 wxLog::OnLog(wxLOG_Trace
, msg
);
339 #if !wxUSE_UTF8_LOCALE_ONLY
340 void wxDoLogTraceWchar(const wxString
& mask
, const wxChar
*format
, ...)
343 va_start(argptr
, format
);
344 wxVLogTrace(mask
, format
, argptr
);
347 #endif // !wxUSE_UTF8_LOCALE_ONLY
349 #if wxUSE_UNICODE_UTF8
350 void wxDoLogTraceUtf8(const wxString
& mask
, const char *format
, ...)
353 va_start(argptr
, format
);
354 wxVLogTrace(mask
, format
, argptr
);
357 #endif // wxUSE_UNICODE_UTF8
359 // deprecated (but not declared as such because we don't want to complicate
360 // DECLARE_LOG_FUNCTION macros even more) overloads for wxTraceMask
361 #if WXWIN_COMPATIBILITY_2_8
362 void wxVLogTrace(wxTraceMask mask
, const wxString
& format
, va_list argptr
)
364 // we check that all of mask bits are set in the current mask, so
365 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
366 // if both bits are set.
367 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask
) == mask
) ) {
368 wxLog::OnLog(wxLOG_Trace
, wxString::FormatV(format
, argptr
));
372 #if !wxUSE_UTF8_LOCALE_ONLY
373 void wxDoLogTraceWchar(wxTraceMask mask
, const wxChar
*format
, ...)
376 va_start(argptr
, format
);
377 wxVLogTrace(mask
, format
, argptr
);
380 #endif // !wxUSE_UTF8_LOCALE_ONLY
382 #if wxUSE_UNICODE_UTF8
383 void wxDoLogTraceUtf8(wxTraceMask mask
, const char *format
, ...)
386 va_start(argptr
, format
);
387 wxVLogTrace(mask
, format
, argptr
);
390 #endif // wxUSE_UNICODE_UTF8
392 #endif // WXWIN_COMPATIBILITY_2_8
395 #if WXWIN_COMPATIBILITY_2_8
396 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
397 void wxDoLogTraceWchar(int mask
, const wxChar
*format
, ...)
400 va_start(argptr
, format
);
401 wxVLogTrace(mask
, format
, argptr
);
404 #endif // WXWIN_COMPATIBILITY_2_8
406 void wxDoLogTraceWchar(const char *mask
, const wxChar
*format
, ...)
409 va_start(argptr
, format
);
410 wxVLogTrace(mask
, format
, argptr
);
414 void wxDoLogTraceWchar(const wchar_t *mask
, const wxChar
*format
, ...)
417 va_start(argptr
, format
);
418 wxVLogTrace(mask
, format
, argptr
);
422 #if WXWIN_COMPATIBILITY_2_8
423 void wxVLogTrace(int mask
, const wxString
& format
, va_list argptr
)
424 { wxVLogTrace((wxTraceMask
)mask
, format
, argptr
); }
425 #endif // WXWIN_COMPATIBILITY_2_8
426 void wxVLogTrace(const char *mask
, const wxString
& format
, va_list argptr
)
427 { wxVLogTrace(wxString(mask
), format
, argptr
); }
428 void wxVLogTrace(const wchar_t *mask
, const wxString
& format
, va_list argptr
)
429 { wxVLogTrace(wxString(mask
), format
, argptr
); }
430 #endif // __WATCOMC__
431 #endif // wxUSE_LOG_TRACE
434 // wxLogSysError: one uses the last error code, for other you must give it
437 // return the system error message description
438 static inline wxString
wxLogSysErrorHelper(long err
)
440 return wxString::Format(_(" (error %ld: %s)"), err
, wxSysErrorMsg(err
));
443 void WXDLLIMPEXP_BASE
wxVLogSysError(const wxString
& format
, va_list argptr
)
445 wxVLogSysError(wxSysErrorCode(), format
, argptr
);
448 #if !wxUSE_UTF8_LOCALE_ONLY
449 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(const wxChar
*format
, ...)
452 va_start(argptr
, format
);
453 wxVLogSysError(format
, argptr
);
456 #endif // !wxUSE_UTF8_LOCALE_ONLY
458 #if wxUSE_UNICODE_UTF8
459 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(const char *format
, ...)
462 va_start(argptr
, format
);
463 wxVLogSysError(format
, argptr
);
466 #endif // wxUSE_UNICODE_UTF8
468 void WXDLLIMPEXP_BASE
wxVLogSysError(long err
, const wxString
& format
, va_list argptr
)
470 if ( wxLog::IsEnabled() )
472 wxLog::OnLog(wxLOG_Error
,
473 wxString::FormatV(format
, argptr
) + wxLogSysErrorHelper(err
));
477 #if !wxUSE_UTF8_LOCALE_ONLY
478 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(long lErrCode
, const wxChar
*format
, ...)
481 va_start(argptr
, format
);
482 wxVLogSysError(lErrCode
, format
, argptr
);
485 #endif // !wxUSE_UTF8_LOCALE_ONLY
487 #if wxUSE_UNICODE_UTF8
488 void WXDLLIMPEXP_BASE
wxDoLogSysErrorUtf8(long lErrCode
, const char *format
, ...)
491 va_start(argptr
, format
);
492 wxVLogSysError(lErrCode
, format
, argptr
);
495 #endif // wxUSE_UNICODE_UTF8
498 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
499 void WXDLLIMPEXP_BASE
wxDoLogSysErrorWchar(unsigned long lErrCode
, const wxChar
*format
, ...)
502 va_start(argptr
, format
);
503 wxVLogSysError(lErrCode
, format
, argptr
);
507 void WXDLLIMPEXP_BASE
wxVLogSysError(unsigned long err
, const wxString
& format
, va_list argptr
)
508 { wxVLogSysError((long)err
, format
, argptr
); }
509 #endif // __WATCOMC__
511 // ----------------------------------------------------------------------------
512 // wxLog class implementation
513 // ----------------------------------------------------------------------------
515 unsigned wxLog::LogLastRepeatIfNeeded()
517 wxCRIT_SECT_LOCKER(lock
, GetPreviousLogCS());
519 return LogLastRepeatIfNeededUnlocked();
522 unsigned wxLog::LogLastRepeatIfNeededUnlocked()
524 const unsigned count
= gs_prevLog
.numRepeated
;
526 if ( gs_prevLog
.numRepeated
)
530 msg
.Printf(wxPLURAL("The previous message repeated once.",
531 "The previous message repeated %lu times.",
532 gs_prevLog
.numRepeated
),
533 gs_prevLog
.numRepeated
);
535 msg
.Printf(wxS("The previous message was repeated %lu times."),
536 gs_prevLog
.numRepeated
);
538 gs_prevLog
.numRepeated
= 0;
539 gs_prevLog
.msg
.clear();
540 DoLogRecord(gs_prevLog
.level
, msg
, gs_prevLog
.info
);
548 // Flush() must be called before destroying the object as otherwise some
549 // messages could be lost
550 if ( gs_prevLog
.numRepeated
)
552 wxMessageOutputDebug().Printf
554 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
556 gs_prevLog
.numRepeated
561 // ----------------------------------------------------------------------------
562 // wxLog logging functions
563 // ----------------------------------------------------------------------------
567 wxLog::OnLog(wxLogLevel level
, const wxString
& msg
, time_t t
)
569 wxLogRecordInfo info
;
572 info
.threadId
= wxThread::GetCurrentId();
573 #endif // wxUSE_THREADS
575 OnLog(level
, msg
, info
);
580 wxLog::OnLog(wxLogLevel level
,
582 const wxLogRecordInfo
& info
)
584 if ( IsEnabled() && ms_logLevel
>= level
)
586 wxLog
*pLogger
= GetActiveTarget();
589 if ( GetRepetitionCounting() )
591 wxCRIT_SECT_LOCKER(lock
, GetPreviousLogCS());
593 if ( msg
== gs_prevLog
.msg
)
595 gs_prevLog
.numRepeated
++;
597 // nothing else to do, in particular, don't log the
602 pLogger
->LogLastRepeatIfNeededUnlocked();
604 // reset repetition counter for a new message
605 gs_prevLog
.msg
= msg
;
606 gs_prevLog
.level
= level
;
607 gs_prevLog
.info
= info
;
610 pLogger
->DoLogRecord(level
, msg
, info
);
615 void wxLog::DoLogRecord(wxLogLevel level
,
617 const wxLogRecordInfo
& info
)
619 #if WXWIN_COMPATIBILITY_2_8
620 // call the old DoLog() to ensure that existing custom log classes still
623 // as the user code could have defined it as either taking "const char *"
624 // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice
625 // but to call both of them
626 DoLog(level
, (const char*)msg
.mb_str(), info
.timestamp
);
627 DoLog(level
, (const wchar_t*)msg
.wc_str(), info
.timestamp
);
628 #endif // WXWIN_COMPATIBILITY_2_8
631 // TODO: it would be better to extract message formatting in a separate
632 // wxLogFormatter class but for now we hard code formatting here
636 // don't time stamp debug messages under MSW as debug viewers usually
637 // already have an option to do it
639 if ( level
!= wxLOG_Debug
&& level
!= wxLOG_Trace
)
643 // TODO: use the other wxLogRecordInfo fields
648 prefix
+= _("Error: ");
652 prefix
+= _("Warning: ");
655 // don't prepend "debug/trace" prefix under MSW as it goes to the debug
656 // window anyhow and so can't be confused with something else
659 // this prefix (as well as the one below) is intentionally not
660 // translated as nobody translates debug messages anyhow
670 DoLogTextAtLevel(level
, prefix
+ msg
);
673 void wxLog::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
675 // we know about debug messages (because using wxMessageOutputDebug is the
676 // right thing to do in 99% of all cases and also for compatibility) but
677 // anything else needs to be handled in the derived class
678 if ( level
== wxLOG_Debug
|| level
== wxLOG_Trace
)
680 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
688 void wxLog::DoLogText(const wxString
& WXUNUSED(msg
))
690 // in 2.8-compatible build the derived class might override DoLog() or
691 // DoLogString() instead so we can't have this assert there
692 #if !WXWIN_COMPATIBILITY_2_8
693 wxFAIL_MSG( "must be overridden if it is called" );
694 #endif // WXWIN_COMPATIBILITY_2_8
697 #if WXWIN_COMPATIBILITY_2_8
699 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const char *szString
, time_t t
)
701 DoLogString(szString
, t
);
704 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const wchar_t *wzString
, time_t t
)
706 DoLogString(wzString
, t
);
709 #endif // WXWIN_COMPATIBILITY_2_8
711 // ----------------------------------------------------------------------------
712 // wxLog active target management
713 // ----------------------------------------------------------------------------
715 wxLog
*wxLog::GetActiveTarget()
717 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
718 // prevent infinite recursion if someone calls wxLogXXX() from
719 // wxApp::CreateLogTarget()
720 static bool s_bInGetActiveTarget
= false;
721 if ( !s_bInGetActiveTarget
) {
722 s_bInGetActiveTarget
= true;
724 // ask the application to create a log target for us
725 if ( wxTheApp
!= NULL
)
726 ms_pLogger
= wxTheApp
->GetTraits()->CreateLogTarget();
728 ms_pLogger
= new wxLogStderr
;
730 s_bInGetActiveTarget
= false;
732 // do nothing if it fails - what can we do?
739 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
741 if ( ms_pLogger
!= NULL
) {
742 // flush the old messages before changing because otherwise they might
743 // get lost later if this target is not restored
747 wxLog
*pOldLogger
= ms_pLogger
;
748 ms_pLogger
= pLogger
;
753 void wxLog::DontCreateOnDemand()
755 ms_bAutoCreate
= false;
757 // this is usually called at the end of the program and we assume that it
758 // is *always* called at the end - so we free memory here to avoid false
759 // memory leak reports from wxWin memory tracking code
763 void wxLog::DoCreateOnDemand()
765 ms_bAutoCreate
= true;
768 void wxLog::AddTraceMask(const wxString
& str
)
770 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
772 ms_aTraceMasks
.push_back(str
);
775 void wxLog::RemoveTraceMask(const wxString
& str
)
777 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
779 int index
= ms_aTraceMasks
.Index(str
);
780 if ( index
!= wxNOT_FOUND
)
781 ms_aTraceMasks
.RemoveAt((size_t)index
);
784 void wxLog::ClearTraceMasks()
786 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
788 ms_aTraceMasks
.Clear();
791 void wxLog::TimeStamp(wxString
*str
)
794 if ( !ms_timestamp
.empty() )
798 (void)time(&timeNow
);
801 wxStrftime(buf
, WXSIZEOF(buf
),
802 ms_timestamp
, wxLocaltime_r(&timeNow
, &tm
));
805 *str
<< buf
<< wxS(": ");
807 #endif // wxUSE_DATETIME
812 LogLastRepeatIfNeeded();
815 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
817 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
819 for ( wxArrayString::iterator it
= ms_aTraceMasks
.begin(),
820 en
= ms_aTraceMasks
.end();
830 // ----------------------------------------------------------------------------
831 // wxLogBuffer implementation
832 // ----------------------------------------------------------------------------
834 void wxLogBuffer::Flush()
836 if ( !m_str
.empty() )
838 wxMessageOutputBest out
;
839 out
.Printf(wxS("%s"), m_str
.c_str());
844 void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
846 // don't put debug messages in the buffer, we don't want to show
847 // them to the user in a msg box, log them immediately
852 wxLog::DoLogTextAtLevel(level
, msg
);
856 m_str
<< msg
<< wxS("\n");
860 // ----------------------------------------------------------------------------
861 // wxLogStderr class implementation
862 // ----------------------------------------------------------------------------
864 wxLogStderr::wxLogStderr(FILE *fp
)
872 void wxLogStderr::DoLogText(const wxString
& msg
)
874 wxFputs(msg
+ '\n', m_fp
);
877 // under GUI systems such as Windows or Mac, programs usually don't have
878 // stderr at all, so show the messages also somewhere else, typically in
879 // the debugger window so that they go at least somewhere instead of being
881 if ( m_fp
== stderr
)
883 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
884 if ( traits
&& !traits
->HasStderr() )
886 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
891 // ----------------------------------------------------------------------------
892 // wxLogStream implementation
893 // ----------------------------------------------------------------------------
895 #if wxUSE_STD_IOSTREAM
896 #include "wx/ioswrap.h"
897 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
900 m_ostr
= &wxSTD cerr
;
905 void wxLogStream::DoLogText(const wxString
& msg
)
907 (*m_ostr
) << msg
<< wxSTD endl
;
909 #endif // wxUSE_STD_IOSTREAM
911 // ----------------------------------------------------------------------------
913 // ----------------------------------------------------------------------------
915 wxLogChain::wxLogChain(wxLog
*logger
)
917 m_bPassMessages
= true;
920 m_logOld
= wxLog::SetActiveTarget(this);
923 wxLogChain::~wxLogChain()
927 if ( m_logNew
!= this )
931 void wxLogChain::SetLog(wxLog
*logger
)
933 if ( m_logNew
!= this )
939 void wxLogChain::Flush()
944 // be careful to avoid infinite recursion
945 if ( m_logNew
&& m_logNew
!= this )
949 void wxLogChain::DoLogRecord(wxLogLevel level
,
951 const wxLogRecordInfo
& info
)
953 // let the previous logger show it
954 if ( m_logOld
&& IsPassingMessages() )
955 m_logOld
->LogRecord(level
, msg
, info
);
957 // and also send it to the new one
958 if ( m_logNew
&& m_logNew
!= this )
959 m_logNew
->LogRecord(level
, msg
, info
);
963 // "'this' : used in base member initializer list" - so what?
964 #pragma warning(disable:4355)
967 // ----------------------------------------------------------------------------
969 // ----------------------------------------------------------------------------
971 wxLogInterposer::wxLogInterposer()
976 // ----------------------------------------------------------------------------
977 // wxLogInterposerTemp
978 // ----------------------------------------------------------------------------
980 wxLogInterposerTemp::wxLogInterposerTemp()
987 #pragma warning(default:4355)
990 // ============================================================================
991 // Global functions/variables
992 // ============================================================================
994 // ----------------------------------------------------------------------------
996 // ----------------------------------------------------------------------------
998 bool wxLog::ms_bRepetCounting
= false;
1000 wxLog
*wxLog::ms_pLogger
= NULL
;
1001 bool wxLog::ms_doLog
= true;
1002 bool wxLog::ms_bAutoCreate
= true;
1003 bool wxLog::ms_bVerbose
= false;
1005 wxLogLevel
wxLog::ms_logLevel
= wxLOG_Max
; // log everything by default
1007 size_t wxLog::ms_suspendCount
= 0;
1009 wxString
wxLog::ms_timestamp(wxS("%X")); // time only, no date
1011 #if WXWIN_COMPATIBILITY_2_8
1012 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
1013 #endif // wxDEBUG_LEVEL
1015 wxArrayString
wxLog::ms_aTraceMasks
;
1017 // ----------------------------------------------------------------------------
1018 // stdout error logging helper
1019 // ----------------------------------------------------------------------------
1021 // helper function: wraps the message and justifies it under given position
1022 // (looks more pretty on the terminal). Also adds newline at the end.
1024 // TODO this is now disabled until I find a portable way of determining the
1025 // terminal window size (ok, I found it but does anybody really cares?)
1026 #ifdef LOG_PRETTY_WRAP
1027 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
1029 size_t nMax
= 80; // FIXME
1030 size_t nStart
= strlen(pszPrefix
);
1031 fputs(pszPrefix
, f
);
1034 while ( *psz
!= '\0' ) {
1035 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
1039 if ( *psz
!= '\0' ) {
1041 for ( n
= 0; n
< nStart
; n
++ )
1044 // as we wrapped, squeeze all white space
1045 while ( isspace(*psz
) )
1052 #endif //LOG_PRETTY_WRAP
1054 // ----------------------------------------------------------------------------
1055 // error code/error message retrieval functions
1056 // ----------------------------------------------------------------------------
1058 // get error code from syste
1059 unsigned long wxSysErrorCode()
1061 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1062 return ::GetLastError();
1068 // get error message from system
1069 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1071 if ( nErrCode
== 0 )
1072 nErrCode
= wxSysErrorCode();
1074 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1075 static wxChar s_szBuf
[1024];
1077 // get error message from system
1079 if ( ::FormatMessage
1081 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1084 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1090 // if this happens, something is seriously wrong, so don't use _() here
1092 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1097 // copy it to our buffer and free memory
1098 // Crashes on SmartPhone (FIXME)
1099 #if !defined(__SMARTPHONE__) /* of WinCE */
1102 wxStrlcpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
));
1104 LocalFree(lpMsgBuf
);
1106 // returned string is capitalized and ended with '\r\n' - bad
1107 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1108 size_t len
= wxStrlen(s_szBuf
);
1111 if ( s_szBuf
[len
- 2] == wxS('\r') )
1112 s_szBuf
[len
- 2] = wxS('\0');
1116 #endif // !__SMARTPHONE__
1118 s_szBuf
[0] = wxS('\0');
1124 static wchar_t s_wzBuf
[1024];
1125 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1126 WXSIZEOF(s_wzBuf
) - 1);
1129 return strerror((int)nErrCode
);
1131 #endif // __WXMSW__/!__WXMSW__