1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/log.cpp
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/arrstr.h"
34 #include "wx/string.h"
38 #include "wx/apptrait.h"
39 #include "wx/datetime.h"
41 #include "wx/msgout.h"
42 #include "wx/textfile.h"
43 #include "wx/thread.h"
44 #include "wx/private/threadinfo.h"
46 #include "wx/vector.h"
48 // other standard headers
58 #include "wx/msw/wince/time.h"
61 #if defined(__WINDOWS__)
62 #include "wx/msw/private.h" // includes windows.h
65 #undef wxLOG_COMPONENT
66 const char *wxLOG_COMPONENT
= "";
68 // this macro allows to define an object which will be initialized before any
69 // other function in this file is called: this is necessary to allow log
70 // functions to be used during static initialization (this is not advisable
71 // anyhow but we should at least try to not crash) and to also ensure that they
72 // are initialized by the time static initialization is done, i.e. before any
73 // threads are created hopefully
75 // the net effect of all this is that you can use Get##name() function to
76 // access the object without worrying about it being not initialized
78 // see also WX_DEFINE_GLOBAL_CONV2() in src/common/strconv.cpp
79 #define WX_DEFINE_GLOBAL_VAR(type, name) \
80 inline type& Get##name() \
82 static type s_##name; \
86 type *gs_##name##Ptr = &Get##name()
93 // contains messages logged by the other threads and waiting to be shown until
94 // Flush() is called in the main one
95 typedef wxVector
<wxLogRecord
> wxLogRecords
;
96 wxLogRecords gs_bufferedLogRecords
;
98 #define WX_DEFINE_LOG_CS(name) WX_DEFINE_GLOBAL_VAR(wxCriticalSection, name##CS)
100 // this critical section is used for buffering the messages from threads other
101 // than main, i.e. it protects all accesses to gs_bufferedLogRecords above
102 WX_DEFINE_LOG_CS(BackgroundLog
);
104 // this one is used for protecting TraceMasks() from concurrent access
105 WX_DEFINE_LOG_CS(TraceMask
);
107 // and this one is used for GetComponentLevels()
108 WX_DEFINE_LOG_CS(Levels
);
110 } // anonymous namespace
112 #endif // wxUSE_THREADS
114 // ----------------------------------------------------------------------------
115 // non member functions
116 // ----------------------------------------------------------------------------
118 // define this to enable wrapping of log messages
119 //#define LOG_PRETTY_WRAP
121 #ifdef LOG_PRETTY_WRAP
122 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
132 // this struct is used to store information about the previous log message used
133 // by OnLog() to (optionally) avoid logging multiple copies of the same message
134 struct PreviousLogInfo
142 // previous message itself
148 // other information about it
149 wxLogRecordInfo info
;
151 // the number of times it was already repeated
152 unsigned numRepeated
;
155 PreviousLogInfo gs_prevLog
;
158 // map containing all components for which log level was explicitly set
160 // NB: all accesses to it must be protected by GetLevelsCS() critical section
161 WX_DEFINE_GLOBAL_VAR(wxStringToNumHashMap
, ComponentLevels
);
163 // ----------------------------------------------------------------------------
164 // wxLogOutputBest: wxLog wrapper around wxMessageOutputBest
165 // ----------------------------------------------------------------------------
167 class wxLogOutputBest
: public wxLog
170 wxLogOutputBest() { }
173 virtual void DoLogText(const wxString
& msg
)
175 wxMessageOutputBest().Output(msg
);
179 wxDECLARE_NO_COPY_CLASS(wxLogOutputBest
);
182 } // anonymous namespace
184 // ============================================================================
186 // ============================================================================
188 // ----------------------------------------------------------------------------
189 // helper global functions
190 // ----------------------------------------------------------------------------
192 void wxSafeShowMessage(const wxString
& title
, const wxString
& text
)
195 ::MessageBox(NULL
, text
.t_str(), title
.t_str(), MB_OK
| MB_ICONSTOP
);
197 wxFprintf(stderr
, wxS("%s: %s\n"), title
.c_str(), text
.c_str());
202 // ----------------------------------------------------------------------------
203 // wxLogFormatter class implementation
204 // ----------------------------------------------------------------------------
207 wxLogFormatter::Format(wxLogLevel level
,
209 const wxLogRecordInfo
& info
) const
213 // don't time stamp debug messages under MSW as debug viewers usually
214 // already have an option to do it
216 if ( level
!= wxLOG_Debug
&& level
!= wxLOG_Trace
)
217 #endif // __WINDOWS__
218 prefix
= FormatTime(info
.timestamp
);
223 prefix
+= _("Error: ");
227 prefix
+= _("Warning: ");
230 // don't prepend "debug/trace" prefix under MSW as it goes to the debug
231 // window anyhow and so can't be confused with something else
234 // this prefix (as well as the one below) is intentionally not
235 // translated as nobody translates debug messages anyhow
242 #endif // !__WINDOWS__
249 wxLogFormatter::FormatTime(time_t t
) const
252 wxLog::TimeStamp(&str
, t
);
258 // ----------------------------------------------------------------------------
259 // wxLog class implementation
260 // ----------------------------------------------------------------------------
262 unsigned wxLog::LogLastRepeatIfNeeded()
264 const unsigned count
= gs_prevLog
.numRepeated
;
266 if ( gs_prevLog
.numRepeated
)
270 if ( gs_prevLog
.numRepeated
== 1 )
272 // We use a separate message for this case as "repeated 1 time"
273 // looks somewhat strange.
274 msg
= _("The previous message repeated once.");
278 // Notice that we still use wxPLURAL() to ensure that multiple
279 // numbers of times are correctly formatted, even though we never
280 // actually use the singular string.
281 msg
.Printf(wxPLURAL("The previous message repeated %lu time.",
282 "The previous message repeated %lu times.",
283 gs_prevLog
.numRepeated
),
284 gs_prevLog
.numRepeated
);
287 msg
.Printf(wxS("The previous message was repeated %lu time(s)."),
288 gs_prevLog
.numRepeated
);
290 gs_prevLog
.numRepeated
= 0;
291 gs_prevLog
.msg
.clear();
292 DoLogRecord(gs_prevLog
.level
, msg
, gs_prevLog
.info
);
300 // Flush() must be called before destroying the object as otherwise some
301 // messages could be lost
302 if ( gs_prevLog
.numRepeated
)
304 wxMessageOutputDebug().Printf
309 "Last repeated message (\"%s\", %lu time) wasn't output",
310 "Last repeated message (\"%s\", %lu times) wasn't output",
311 gs_prevLog
.numRepeated
314 wxS("Last repeated message (\"%s\", %lu time(s)) wasn't output"),
317 gs_prevLog
.numRepeated
324 // ----------------------------------------------------------------------------
325 // wxLog logging functions
326 // ----------------------------------------------------------------------------
330 wxLog::OnLog(wxLogLevel level
, const wxString
& msg
, time_t t
)
332 wxLogRecordInfo info
;
335 info
.threadId
= wxThread::GetCurrentId();
336 #endif // wxUSE_THREADS
338 OnLog(level
, msg
, info
);
343 wxLog::OnLog(wxLogLevel level
,
345 const wxLogRecordInfo
& info
)
347 // fatal errors can't be suppressed nor handled by the custom log target
348 // and always terminate the program
349 if ( level
== wxLOG_FatalError
)
351 wxSafeShowMessage(wxS("Fatal Error"), msg
);
359 if ( !wxThread::IsMain() )
361 logger
= wxThreadInfo
.logger
;
366 // buffer the messages until they can be shown from the main
368 wxCriticalSectionLocker
lock(GetBackgroundLogCS());
370 gs_bufferedLogRecords
.push_back(wxLogRecord(level
, msg
, info
));
372 // ensure that our Flush() will be called soon
375 //else: we don't have any logger at all, there is no need to log
380 //else: we have a thread-specific logger, we can send messages to it
384 #endif // wxUSE_THREADS
386 logger
= GetMainThreadActiveTarget();
391 logger
->CallDoLogNow(level
, msg
, info
);
395 wxLog::CallDoLogNow(wxLogLevel level
,
397 const wxLogRecordInfo
& info
)
399 if ( GetRepetitionCounting() )
401 if ( msg
== gs_prevLog
.msg
)
403 gs_prevLog
.numRepeated
++;
405 // nothing else to do, in particular, don't log the
410 LogLastRepeatIfNeeded();
412 // reset repetition counter for a new message
413 gs_prevLog
.msg
= msg
;
414 gs_prevLog
.level
= level
;
415 gs_prevLog
.info
= info
;
418 // handle extra data which may be passed to us by wxLogXXX()
419 wxString prefix
, suffix
;
421 if ( info
.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE
, &num
) )
423 const long err
= static_cast<long>(num
);
425 suffix
.Printf(_(" (error %ld: %s)"), err
, wxSysErrorMsg(err
));
430 if ( level
== wxLOG_Trace
&& info
.GetStrValue(wxLOG_KEY_TRACE_MASK
, &str
) )
432 prefix
= "(" + str
+ ") ";
434 #endif // wxUSE_LOG_TRACE
436 DoLogRecord(level
, prefix
+ msg
+ suffix
, info
);
439 void wxLog::DoLogRecord(wxLogLevel level
,
441 const wxLogRecordInfo
& info
)
443 #if WXWIN_COMPATIBILITY_2_8
444 // call the old DoLog() to ensure that existing custom log classes still
447 // as the user code could have defined it as either taking "const char *"
448 // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice
449 // but to call both of them
450 DoLog(level
, (const char*)msg
.mb_str(), info
.timestamp
);
451 DoLog(level
, (const wchar_t*)msg
.wc_str(), info
.timestamp
);
452 #else // !WXWIN_COMPATIBILITY_2_8
454 #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8
456 // Use wxLogFormatter to format the message
457 DoLogTextAtLevel(level
, m_formatter
->Format (level
, msg
, info
));
460 void wxLog::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
462 // we know about debug messages (because using wxMessageOutputDebug is the
463 // right thing to do in 99% of all cases and also for compatibility) but
464 // anything else needs to be handled in the derived class
465 if ( level
== wxLOG_Debug
|| level
== wxLOG_Trace
)
467 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
475 void wxLog::DoLogText(const wxString
& WXUNUSED(msg
))
477 // in 2.8-compatible build the derived class might override DoLog() or
478 // DoLogString() instead so we can't have this assert there
479 #if !WXWIN_COMPATIBILITY_2_8
480 wxFAIL_MSG( "must be overridden if it is called" );
481 #endif // WXWIN_COMPATIBILITY_2_8
484 #if WXWIN_COMPATIBILITY_2_8
486 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const char *szString
, time_t t
)
488 DoLogString(szString
, t
);
491 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const wchar_t *wzString
, time_t t
)
493 DoLogString(wzString
, t
);
496 #endif // WXWIN_COMPATIBILITY_2_8
498 // ----------------------------------------------------------------------------
499 // wxLog active target management
500 // ----------------------------------------------------------------------------
502 wxLog
*wxLog::GetActiveTarget()
505 if ( !wxThread::IsMain() )
507 // check if we have a thread-specific log target
508 wxLog
* const logger
= wxThreadInfo
.logger
;
510 // the code below should be only executed for the main thread as
511 // CreateLogTarget() is not meant for auto-creating log targets for
512 // worker threads so skip it in any case
513 return logger
? logger
: ms_pLogger
;
515 #endif // wxUSE_THREADS
517 return GetMainThreadActiveTarget();
521 wxLog
*wxLog::GetMainThreadActiveTarget()
523 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
524 // prevent infinite recursion if someone calls wxLogXXX() from
525 // wxApp::CreateLogTarget()
526 static bool s_bInGetActiveTarget
= false;
527 if ( !s_bInGetActiveTarget
) {
528 s_bInGetActiveTarget
= true;
530 // ask the application to create a log target for us
531 if ( wxTheApp
!= NULL
)
532 ms_pLogger
= wxTheApp
->GetTraits()->CreateLogTarget();
534 ms_pLogger
= new wxLogOutputBest
;
536 s_bInGetActiveTarget
= false;
538 // do nothing if it fails - what can we do?
545 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
547 if ( ms_pLogger
!= NULL
) {
548 // flush the old messages before changing because otherwise they might
549 // get lost later if this target is not restored
553 wxLog
*pOldLogger
= ms_pLogger
;
554 ms_pLogger
= pLogger
;
561 wxLog
*wxLog::SetThreadActiveTarget(wxLog
*logger
)
563 wxASSERT_MSG( !wxThread::IsMain(), "use SetActiveTarget() for main thread" );
565 wxLog
* const oldLogger
= wxThreadInfo
.logger
;
569 wxThreadInfo
.logger
= logger
;
573 #endif // wxUSE_THREADS
575 void wxLog::DontCreateOnDemand()
577 ms_bAutoCreate
= false;
579 // this is usually called at the end of the program and we assume that it
580 // is *always* called at the end - so we free memory here to avoid false
581 // memory leak reports from wxWin memory tracking code
585 void wxLog::DoCreateOnDemand()
587 ms_bAutoCreate
= true;
590 // ----------------------------------------------------------------------------
591 // wxLog components levels
592 // ----------------------------------------------------------------------------
595 void wxLog::SetComponentLevel(const wxString
& component
, wxLogLevel level
)
597 if ( component
.empty() )
603 wxCRIT_SECT_LOCKER(lock
, GetLevelsCS());
605 GetComponentLevels()[component
] = level
;
610 wxLogLevel
wxLog::GetComponentLevel(wxString component
)
612 wxCRIT_SECT_LOCKER(lock
, GetLevelsCS());
614 const wxStringToNumHashMap
& componentLevels
= GetComponentLevels();
615 while ( !component
.empty() )
617 wxStringToNumHashMap::const_iterator
618 it
= componentLevels
.find(component
);
619 if ( it
!= componentLevels
.end() )
620 return static_cast<wxLogLevel
>(it
->second
);
622 component
= component
.BeforeLast('/');
625 return GetLogLevel();
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
635 // because IsAllowedTraceMask() may be called during static initialization
636 // (this is not recommended but it may still happen, see #11592) we can't use a
637 // simple static variable which might be not initialized itself just yet to
638 // store the trace masks, but need this accessor function which will ensure
639 // that the variable is always correctly initialized before being accessed
641 // notice that this doesn't make accessing it MT-safe, of course, you need to
642 // serialize accesses to it using GetTraceMaskCS() for this
643 wxArrayString
& TraceMasks()
645 static wxArrayString s_traceMasks
;
650 } // anonymous namespace
652 /* static */ const wxArrayString
& wxLog::GetTraceMasks()
654 // because of this function signature (it returns a reference, not the
655 // object), it is inherently MT-unsafe so there is no need to acquire the
661 void wxLog::AddTraceMask(const wxString
& str
)
663 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
665 TraceMasks().push_back(str
);
668 void wxLog::RemoveTraceMask(const wxString
& str
)
670 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
672 int index
= TraceMasks().Index(str
);
673 if ( index
!= wxNOT_FOUND
)
674 TraceMasks().RemoveAt((size_t)index
);
677 void wxLog::ClearTraceMasks()
679 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
681 TraceMasks().Clear();
684 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
686 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
688 const wxArrayString
& masks
= GetTraceMasks();
689 for ( wxArrayString::const_iterator it
= masks
.begin(),
701 // ----------------------------------------------------------------------------
702 // wxLog miscellaneous other methods
703 // ----------------------------------------------------------------------------
707 void wxLog::TimeStamp(wxString
*str
)
709 if ( !ms_timestamp
.empty() )
711 *str
= wxDateTime::UNow().Format(ms_timestamp
);
716 void wxLog::TimeStamp(wxString
*str
, time_t t
)
718 if ( !ms_timestamp
.empty() )
720 *str
= wxDateTime(t
).Format(ms_timestamp
);
725 #else // !wxUSE_DATETIME
727 void wxLog::TimeStamp(wxString
*)
731 void wxLog::TimeStamp(wxString
*, time_t)
735 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
739 void wxLog::FlushThreadMessages()
741 // check if we have queued messages from other threads
742 wxLogRecords bufferedLogRecords
;
745 wxCriticalSectionLocker
lock(GetBackgroundLogCS());
746 bufferedLogRecords
.swap(gs_bufferedLogRecords
);
748 // release the lock now to not keep it while we are logging the
749 // messages below, allowing background threads to run
752 if ( !bufferedLogRecords
.empty() )
754 for ( wxLogRecords::const_iterator it
= bufferedLogRecords
.begin();
755 it
!= bufferedLogRecords
.end();
758 CallDoLogNow(it
->level
, it
->msg
, it
->info
);
764 bool wxLog::IsThreadLoggingEnabled()
766 return !wxThreadInfo
.loggingDisabled
;
770 bool wxLog::EnableThreadLogging(bool enable
)
772 const bool wasEnabled
= !wxThreadInfo
.loggingDisabled
;
773 wxThreadInfo
.loggingDisabled
= !enable
;
777 #endif // wxUSE_THREADS
779 wxLogFormatter
*wxLog::SetFormatter(wxLogFormatter
* formatter
)
781 wxLogFormatter
* formatterOld
= m_formatter
;
782 m_formatter
= formatter
? formatter
: new wxLogFormatter
;
789 LogLastRepeatIfNeeded();
793 void wxLog::FlushActive()
795 if ( ms_suspendCount
)
798 wxLog
* const log
= GetActiveTarget();
802 if ( wxThread::IsMain() )
803 log
->FlushThreadMessages();
804 #endif // wxUSE_THREADS
810 // ----------------------------------------------------------------------------
811 // wxLogBuffer implementation
812 // ----------------------------------------------------------------------------
814 void wxLogBuffer::Flush()
818 if ( !m_str
.empty() )
820 wxMessageOutputBest out
;
821 out
.Printf(wxS("%s"), m_str
.c_str());
826 void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
828 // don't put debug messages in the buffer, we don't want to show
829 // them to the user in a msg box, log them immediately
834 wxLog::DoLogTextAtLevel(level
, msg
);
838 m_str
<< msg
<< wxS("\n");
842 // ----------------------------------------------------------------------------
843 // wxLogStderr class implementation
844 // ----------------------------------------------------------------------------
846 wxLogStderr::wxLogStderr(FILE *fp
)
854 void wxLogStderr::DoLogText(const wxString
& msg
)
856 // First send it to stderr, even if we don't have it (e.g. in a Windows GUI
857 // application under) it's not a problem to try to use it and it's easier
858 // than determining whether we do have it or not.
859 wxMessageOutputStderr(m_fp
).Output(msg
);
861 // under GUI systems such as Windows or Mac, programs usually don't have
862 // stderr at all, so show the messages also somewhere else, typically in
863 // the debugger window so that they go at least somewhere instead of being
865 if ( m_fp
== stderr
)
867 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
868 if ( traits
&& !traits
->HasStderr() )
870 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
875 // ----------------------------------------------------------------------------
876 // wxLogStream implementation
877 // ----------------------------------------------------------------------------
879 #if wxUSE_STD_IOSTREAM
880 #include "wx/ioswrap.h"
881 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
884 m_ostr
= &wxSTD cerr
;
889 void wxLogStream::DoLogText(const wxString
& msg
)
891 (*m_ostr
) << msg
<< wxSTD endl
;
893 #endif // wxUSE_STD_IOSTREAM
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 wxLogChain::wxLogChain(wxLog
*logger
)
901 m_bPassMessages
= true;
905 // Notice that we use GetActiveTarget() here instead of directly calling
906 // SetActiveTarget() to trigger wxLog auto-creation: if we're created as
907 // the first logger, we should still chain with the standard, implicit and
908 // possibly still not created standard logger instead of disabling normal
910 m_logOld
= wxLog::GetActiveTarget();
911 wxLog::SetActiveTarget(this);
914 wxLogChain::~wxLogChain()
916 wxLog::SetActiveTarget(m_logOld
);
918 if ( m_logNew
!= this )
922 void wxLogChain::SetLog(wxLog
*logger
)
924 if ( m_logNew
!= this )
930 void wxLogChain::Flush()
935 // be careful to avoid infinite recursion
936 if ( m_logNew
&& m_logNew
!= this )
940 void wxLogChain::DoLogRecord(wxLogLevel level
,
942 const wxLogRecordInfo
& info
)
944 // let the previous logger show it
945 if ( m_logOld
&& IsPassingMessages() )
946 m_logOld
->LogRecord(level
, msg
, info
);
948 // and also send it to the new one
951 // don't call m_logNew->LogRecord() to avoid infinite recursion when
952 // m_logNew is this object itself
953 if ( m_logNew
!= this )
954 m_logNew
->LogRecord(level
, msg
, info
);
956 wxLog::DoLogRecord(level
, msg
, info
);
961 // "'this' : used in base member initializer list" - so what?
962 #pragma warning(disable:4355)
965 // ----------------------------------------------------------------------------
967 // ----------------------------------------------------------------------------
969 wxLogInterposer::wxLogInterposer()
974 // ----------------------------------------------------------------------------
975 // wxLogInterposerTemp
976 // ----------------------------------------------------------------------------
978 wxLogInterposerTemp::wxLogInterposerTemp()
985 #pragma warning(default:4355)
988 // ============================================================================
989 // Global functions/variables
990 // ============================================================================
992 // ----------------------------------------------------------------------------
994 // ----------------------------------------------------------------------------
996 bool wxLog::ms_bRepetCounting
= false;
998 wxLog
*wxLog::ms_pLogger
= NULL
;
999 bool wxLog::ms_doLog
= true;
1000 bool wxLog::ms_bAutoCreate
= true;
1001 bool wxLog::ms_bVerbose
= false;
1003 wxLogLevel
wxLog::ms_logLevel
= wxLOG_Max
; // log everything by default
1005 size_t wxLog::ms_suspendCount
= 0;
1007 wxString
wxLog::ms_timestamp(wxS("%X")); // time only, no date
1009 #if WXWIN_COMPATIBILITY_2_8
1010 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
1011 #endif // wxDEBUG_LEVEL
1013 // ----------------------------------------------------------------------------
1014 // stdout error logging helper
1015 // ----------------------------------------------------------------------------
1017 // helper function: wraps the message and justifies it under given position
1018 // (looks more pretty on the terminal). Also adds newline at the end.
1020 // TODO this is now disabled until I find a portable way of determining the
1021 // terminal window size (ok, I found it but does anybody really cares?)
1022 #ifdef LOG_PRETTY_WRAP
1023 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
1025 size_t nMax
= 80; // FIXME
1026 size_t nStart
= strlen(pszPrefix
);
1027 fputs(pszPrefix
, f
);
1030 while ( *psz
!= '\0' ) {
1031 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
1035 if ( *psz
!= '\0' ) {
1037 for ( n
= 0; n
< nStart
; n
++ )
1040 // as we wrapped, squeeze all white space
1041 while ( isspace(*psz
) )
1048 #endif //LOG_PRETTY_WRAP
1050 // ----------------------------------------------------------------------------
1051 // error code/error message retrieval functions
1052 // ----------------------------------------------------------------------------
1054 // get error code from syste
1055 unsigned long wxSysErrorCode()
1057 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1058 return ::GetLastError();
1064 // get error message from system
1065 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1067 if ( nErrCode
== 0 )
1068 nErrCode
= wxSysErrorCode();
1070 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1071 static wxChar s_szBuf
[1024];
1073 // get error message from system
1075 if ( ::FormatMessage
1077 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1080 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1086 // if this happens, something is seriously wrong, so don't use _() here
1088 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1093 // copy it to our buffer and free memory
1094 // Crashes on SmartPhone (FIXME)
1095 #if !defined(__SMARTPHONE__) /* of WinCE */
1098 wxStrlcpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
));
1100 LocalFree(lpMsgBuf
);
1102 // returned string is capitalized and ended with '\r\n' - bad
1103 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1104 size_t len
= wxStrlen(s_szBuf
);
1107 if ( s_szBuf
[len
- 2] == wxS('\r') )
1108 s_szBuf
[len
- 2] = wxS('\0');
1112 #endif // !__SMARTPHONE__
1114 s_szBuf
[0] = wxS('\0');
1118 #else // !__WINDOWS__
1120 static wchar_t s_wzBuf
[1024];
1121 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1122 WXSIZEOF(s_wzBuf
) - 1);
1125 return strerror((int)nErrCode
);
1127 #endif // __WINDOWS__/!__WINDOWS__