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()
90 wxTLS_TYPE(wxThreadSpecificInfo
) wxThreadInfoVar
;
95 // contains messages logged by the other threads and waiting to be shown until
96 // Flush() is called in the main one
97 typedef wxVector
<wxLogRecord
> wxLogRecords
;
98 wxLogRecords gs_bufferedLogRecords
;
100 #define WX_DEFINE_LOG_CS(name) WX_DEFINE_GLOBAL_VAR(wxCriticalSection, name##CS)
102 // this critical section is used for buffering the messages from threads other
103 // than main, i.e. it protects all accesses to gs_bufferedLogRecords above
104 WX_DEFINE_LOG_CS(BackgroundLog
);
106 // this one is used for protecting TraceMasks() from concurrent access
107 WX_DEFINE_LOG_CS(TraceMask
);
109 // and this one is used for GetComponentLevels()
110 WX_DEFINE_LOG_CS(Levels
);
112 } // anonymous namespace
114 #endif // wxUSE_THREADS
116 // ----------------------------------------------------------------------------
117 // non member functions
118 // ----------------------------------------------------------------------------
120 // define this to enable wrapping of log messages
121 //#define LOG_PRETTY_WRAP
123 #ifdef LOG_PRETTY_WRAP
124 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
134 // this struct is used to store information about the previous log message used
135 // by OnLog() to (optionally) avoid logging multiple copies of the same message
136 struct PreviousLogInfo
144 // previous message itself
150 // other information about it
151 wxLogRecordInfo info
;
153 // the number of times it was already repeated
154 unsigned numRepeated
;
157 PreviousLogInfo gs_prevLog
;
160 // map containing all components for which log level was explicitly set
162 // NB: all accesses to it must be protected by GetLevelsCS() critical section
163 WX_DEFINE_GLOBAL_VAR(wxStringToNumHashMap
, ComponentLevels
);
165 // ----------------------------------------------------------------------------
166 // wxLogOutputBest: wxLog wrapper around wxMessageOutputBest
167 // ----------------------------------------------------------------------------
169 class wxLogOutputBest
: public wxLog
172 wxLogOutputBest() { }
175 virtual void DoLogText(const wxString
& msg
)
177 wxMessageOutputBest().Output(msg
);
181 wxDECLARE_NO_COPY_CLASS(wxLogOutputBest
);
184 } // anonymous namespace
186 // ============================================================================
188 // ============================================================================
190 // ----------------------------------------------------------------------------
191 // helper global functions
192 // ----------------------------------------------------------------------------
194 void wxSafeShowMessage(const wxString
& title
, const wxString
& text
)
197 ::MessageBox(NULL
, text
.t_str(), title
.t_str(), MB_OK
| MB_ICONSTOP
);
199 wxFprintf(stderr
, wxS("%s: %s\n"), title
.c_str(), text
.c_str());
204 // ----------------------------------------------------------------------------
205 // wxLogFormatter class implementation
206 // ----------------------------------------------------------------------------
209 wxLogFormatter::Format(wxLogLevel level
,
211 const wxLogRecordInfo
& info
) const
215 // don't time stamp debug messages under MSW as debug viewers usually
216 // already have an option to do it
218 if ( level
!= wxLOG_Debug
&& level
!= wxLOG_Trace
)
219 #endif // __WINDOWS__
220 prefix
= FormatTime(info
.timestamp
);
225 prefix
+= _("Error: ");
229 prefix
+= _("Warning: ");
232 // don't prepend "debug/trace" prefix under MSW as it goes to the debug
233 // window anyhow and so can't be confused with something else
236 // this prefix (as well as the one below) is intentionally not
237 // translated as nobody translates debug messages anyhow
244 #endif // !__WINDOWS__
251 wxLogFormatter::FormatTime(time_t t
) const
254 wxLog::TimeStamp(&str
, t
);
260 // ----------------------------------------------------------------------------
261 // wxLog class implementation
262 // ----------------------------------------------------------------------------
264 unsigned wxLog::LogLastRepeatIfNeeded()
266 const unsigned count
= gs_prevLog
.numRepeated
;
268 if ( gs_prevLog
.numRepeated
)
272 if ( gs_prevLog
.numRepeated
== 1 )
274 // We use a separate message for this case as "repeated 1 time"
275 // looks somewhat strange.
276 msg
= _("The previous message repeated once.");
280 // Notice that we still use wxPLURAL() to ensure that multiple
281 // numbers of times are correctly formatted, even though we never
282 // actually use the singular string.
283 msg
.Printf(wxPLURAL("The previous message repeated %lu time.",
284 "The previous message repeated %lu times.",
285 gs_prevLog
.numRepeated
),
286 gs_prevLog
.numRepeated
);
289 msg
.Printf(wxS("The previous message was repeated %lu time(s)."),
290 gs_prevLog
.numRepeated
);
292 gs_prevLog
.numRepeated
= 0;
293 gs_prevLog
.msg
.clear();
294 DoLogRecord(gs_prevLog
.level
, msg
, gs_prevLog
.info
);
302 // Flush() must be called before destroying the object as otherwise some
303 // messages could be lost
304 if ( gs_prevLog
.numRepeated
)
306 wxMessageOutputDebug().Printf
311 "Last repeated message (\"%s\", %lu time) wasn't output",
312 "Last repeated message (\"%s\", %lu times) wasn't output",
313 gs_prevLog
.numRepeated
316 wxS("Last repeated message (\"%s\", %lu time(s)) wasn't output"),
319 gs_prevLog
.numRepeated
326 // ----------------------------------------------------------------------------
327 // wxLog logging functions
328 // ----------------------------------------------------------------------------
332 wxLog::OnLog(wxLogLevel level
, const wxString
& msg
, time_t t
)
334 wxLogRecordInfo info
;
337 info
.threadId
= wxThread::GetCurrentId();
338 #endif // wxUSE_THREADS
340 OnLog(level
, msg
, info
);
345 wxLog::OnLog(wxLogLevel level
,
347 const wxLogRecordInfo
& info
)
349 // fatal errors can't be suppressed nor handled by the custom log target
350 // and always terminate the program
351 if ( level
== wxLOG_FatalError
)
353 wxSafeShowMessage(wxS("Fatal Error"), msg
);
361 if ( !wxThread::IsMain() )
363 logger
= wxThreadInfo
.logger
;
368 // buffer the messages until they can be shown from the main
370 wxCriticalSectionLocker
lock(GetBackgroundLogCS());
372 gs_bufferedLogRecords
.push_back(wxLogRecord(level
, msg
, info
));
374 // ensure that our Flush() will be called soon
377 //else: we don't have any logger at all, there is no need to log
382 //else: we have a thread-specific logger, we can send messages to it
386 #endif // wxUSE_THREADS
388 logger
= GetMainThreadActiveTarget();
393 logger
->CallDoLogNow(level
, msg
, info
);
397 wxLog::CallDoLogNow(wxLogLevel level
,
399 const wxLogRecordInfo
& info
)
401 if ( GetRepetitionCounting() )
403 if ( msg
== gs_prevLog
.msg
)
405 gs_prevLog
.numRepeated
++;
407 // nothing else to do, in particular, don't log the
412 LogLastRepeatIfNeeded();
414 // reset repetition counter for a new message
415 gs_prevLog
.msg
= msg
;
416 gs_prevLog
.level
= level
;
417 gs_prevLog
.info
= info
;
420 // handle extra data which may be passed to us by wxLogXXX()
421 wxString prefix
, suffix
;
423 if ( info
.GetNumValue(wxLOG_KEY_SYS_ERROR_CODE
, &num
) )
425 const long err
= static_cast<long>(num
);
427 suffix
.Printf(_(" (error %ld: %s)"), err
, wxSysErrorMsg(err
));
432 if ( level
== wxLOG_Trace
&& info
.GetStrValue(wxLOG_KEY_TRACE_MASK
, &str
) )
434 prefix
= "(" + str
+ ") ";
436 #endif // wxUSE_LOG_TRACE
438 DoLogRecord(level
, prefix
+ msg
+ suffix
, info
);
441 void wxLog::DoLogRecord(wxLogLevel level
,
443 const wxLogRecordInfo
& info
)
445 #if WXWIN_COMPATIBILITY_2_8
446 // call the old DoLog() to ensure that existing custom log classes still
449 // as the user code could have defined it as either taking "const char *"
450 // (in ANSI build) or "const wxChar *" (in ANSI/Unicode), we have no choice
451 // but to call both of them
452 DoLog(level
, (const char*)msg
.mb_str(), info
.timestamp
);
453 DoLog(level
, (const wchar_t*)msg
.wc_str(), info
.timestamp
);
454 #else // !WXWIN_COMPATIBILITY_2_8
456 #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8
458 // Use wxLogFormatter to format the message
459 DoLogTextAtLevel(level
, m_formatter
->Format (level
, msg
, info
));
462 void wxLog::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
464 // we know about debug messages (because using wxMessageOutputDebug is the
465 // right thing to do in 99% of all cases and also for compatibility) but
466 // anything else needs to be handled in the derived class
467 if ( level
== wxLOG_Debug
|| level
== wxLOG_Trace
)
469 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
477 void wxLog::DoLogText(const wxString
& WXUNUSED(msg
))
479 // in 2.8-compatible build the derived class might override DoLog() or
480 // DoLogString() instead so we can't have this assert there
481 #if !WXWIN_COMPATIBILITY_2_8
482 wxFAIL_MSG( "must be overridden if it is called" );
483 #endif // WXWIN_COMPATIBILITY_2_8
486 #if WXWIN_COMPATIBILITY_2_8
488 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const char *szString
, time_t t
)
490 DoLogString(szString
, t
);
493 void wxLog::DoLog(wxLogLevel
WXUNUSED(level
), const wchar_t *wzString
, time_t t
)
495 DoLogString(wzString
, t
);
498 #endif // WXWIN_COMPATIBILITY_2_8
500 // ----------------------------------------------------------------------------
501 // wxLog active target management
502 // ----------------------------------------------------------------------------
504 wxLog
*wxLog::GetActiveTarget()
507 if ( !wxThread::IsMain() )
509 // check if we have a thread-specific log target
510 wxLog
* const logger
= wxThreadInfo
.logger
;
512 // the code below should be only executed for the main thread as
513 // CreateLogTarget() is not meant for auto-creating log targets for
514 // worker threads so skip it in any case
515 return logger
? logger
: ms_pLogger
;
517 #endif // wxUSE_THREADS
519 return GetMainThreadActiveTarget();
523 wxLog
*wxLog::GetMainThreadActiveTarget()
525 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
526 // prevent infinite recursion if someone calls wxLogXXX() from
527 // wxApp::CreateLogTarget()
528 static bool s_bInGetActiveTarget
= false;
529 if ( !s_bInGetActiveTarget
) {
530 s_bInGetActiveTarget
= true;
532 // ask the application to create a log target for us
533 if ( wxTheApp
!= NULL
)
534 ms_pLogger
= wxTheApp
->GetTraits()->CreateLogTarget();
536 ms_pLogger
= new wxLogOutputBest
;
538 s_bInGetActiveTarget
= false;
540 // do nothing if it fails - what can we do?
547 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
549 if ( ms_pLogger
!= NULL
) {
550 // flush the old messages before changing because otherwise they might
551 // get lost later if this target is not restored
555 wxLog
*pOldLogger
= ms_pLogger
;
556 ms_pLogger
= pLogger
;
563 wxLog
*wxLog::SetThreadActiveTarget(wxLog
*logger
)
565 wxASSERT_MSG( !wxThread::IsMain(), "use SetActiveTarget() for main thread" );
567 wxLog
* const oldLogger
= wxThreadInfo
.logger
;
571 wxThreadInfo
.logger
= logger
;
575 #endif // wxUSE_THREADS
577 void wxLog::DontCreateOnDemand()
579 ms_bAutoCreate
= false;
581 // this is usually called at the end of the program and we assume that it
582 // is *always* called at the end - so we free memory here to avoid false
583 // memory leak reports from wxWin memory tracking code
587 void wxLog::DoCreateOnDemand()
589 ms_bAutoCreate
= true;
592 // ----------------------------------------------------------------------------
593 // wxLog components levels
594 // ----------------------------------------------------------------------------
597 void wxLog::SetComponentLevel(const wxString
& component
, wxLogLevel level
)
599 if ( component
.empty() )
605 wxCRIT_SECT_LOCKER(lock
, GetLevelsCS());
607 GetComponentLevels()[component
] = level
;
612 wxLogLevel
wxLog::GetComponentLevel(wxString component
)
614 wxCRIT_SECT_LOCKER(lock
, GetLevelsCS());
616 const wxStringToNumHashMap
& componentLevels
= GetComponentLevels();
617 while ( !component
.empty() )
619 wxStringToNumHashMap::const_iterator
620 it
= componentLevels
.find(component
);
621 if ( it
!= componentLevels
.end() )
622 return static_cast<wxLogLevel
>(it
->second
);
624 component
= component
.BeforeLast('/');
627 return GetLogLevel();
630 // ----------------------------------------------------------------------------
632 // ----------------------------------------------------------------------------
637 // because IsAllowedTraceMask() may be called during static initialization
638 // (this is not recommended but it may still happen, see #11592) we can't use a
639 // simple static variable which might be not initialized itself just yet to
640 // store the trace masks, but need this accessor function which will ensure
641 // that the variable is always correctly initialized before being accessed
643 // notice that this doesn't make accessing it MT-safe, of course, you need to
644 // serialize accesses to it using GetTraceMaskCS() for this
645 wxArrayString
& TraceMasks()
647 static wxArrayString s_traceMasks
;
652 } // anonymous namespace
654 /* static */ const wxArrayString
& wxLog::GetTraceMasks()
656 // because of this function signature (it returns a reference, not the
657 // object), it is inherently MT-unsafe so there is no need to acquire the
663 void wxLog::AddTraceMask(const wxString
& str
)
665 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
667 TraceMasks().push_back(str
);
670 void wxLog::RemoveTraceMask(const wxString
& str
)
672 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
674 int index
= TraceMasks().Index(str
);
675 if ( index
!= wxNOT_FOUND
)
676 TraceMasks().RemoveAt((size_t)index
);
679 void wxLog::ClearTraceMasks()
681 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
683 TraceMasks().Clear();
686 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString
& mask
)
688 wxCRIT_SECT_LOCKER(lock
, GetTraceMaskCS());
690 const wxArrayString
& masks
= GetTraceMasks();
691 for ( wxArrayString::const_iterator it
= masks
.begin(),
703 // ----------------------------------------------------------------------------
704 // wxLog miscellaneous other methods
705 // ----------------------------------------------------------------------------
709 void wxLog::TimeStamp(wxString
*str
)
711 if ( !ms_timestamp
.empty() )
713 *str
= wxDateTime::UNow().Format(ms_timestamp
);
718 void wxLog::TimeStamp(wxString
*str
, time_t t
)
720 if ( !ms_timestamp
.empty() )
722 *str
= wxDateTime(t
).Format(ms_timestamp
);
727 #else // !wxUSE_DATETIME
729 void wxLog::TimeStamp(wxString
*)
733 void wxLog::TimeStamp(wxString
*, time_t)
737 #endif // wxUSE_DATETIME/!wxUSE_DATETIME
741 void wxLog::FlushThreadMessages()
743 // check if we have queued messages from other threads
744 wxLogRecords bufferedLogRecords
;
747 wxCriticalSectionLocker
lock(GetBackgroundLogCS());
748 bufferedLogRecords
.swap(gs_bufferedLogRecords
);
750 // release the lock now to not keep it while we are logging the
751 // messages below, allowing background threads to run
754 if ( !bufferedLogRecords
.empty() )
756 for ( wxLogRecords::const_iterator it
= bufferedLogRecords
.begin();
757 it
!= bufferedLogRecords
.end();
760 CallDoLogNow(it
->level
, it
->msg
, it
->info
);
766 bool wxLog::IsThreadLoggingEnabled()
768 return !wxThreadInfo
.loggingDisabled
;
772 bool wxLog::EnableThreadLogging(bool enable
)
774 const bool wasEnabled
= !wxThreadInfo
.loggingDisabled
;
775 wxThreadInfo
.loggingDisabled
= !enable
;
779 #endif // wxUSE_THREADS
781 wxLogFormatter
*wxLog::SetFormatter(wxLogFormatter
* formatter
)
783 wxLogFormatter
* formatterOld
= m_formatter
;
784 m_formatter
= formatter
? formatter
: new wxLogFormatter
;
791 LogLastRepeatIfNeeded();
795 void wxLog::FlushActive()
797 if ( ms_suspendCount
)
800 wxLog
* const log
= GetActiveTarget();
804 if ( wxThread::IsMain() )
805 log
->FlushThreadMessages();
806 #endif // wxUSE_THREADS
812 // ----------------------------------------------------------------------------
813 // wxLogBuffer implementation
814 // ----------------------------------------------------------------------------
816 void wxLogBuffer::Flush()
820 if ( !m_str
.empty() )
822 wxMessageOutputBest out
;
823 out
.Printf(wxS("%s"), m_str
.c_str());
828 void wxLogBuffer::DoLogTextAtLevel(wxLogLevel level
, const wxString
& msg
)
830 // don't put debug messages in the buffer, we don't want to show
831 // them to the user in a msg box, log them immediately
836 wxLog::DoLogTextAtLevel(level
, msg
);
840 m_str
<< msg
<< wxS("\n");
844 // ----------------------------------------------------------------------------
845 // wxLogStderr class implementation
846 // ----------------------------------------------------------------------------
848 wxLogStderr::wxLogStderr(FILE *fp
)
856 void wxLogStderr::DoLogText(const wxString
& msg
)
858 // First send it to stderr, even if we don't have it (e.g. in a Windows GUI
859 // application under) it's not a problem to try to use it and it's easier
860 // than determining whether we do have it or not.
861 wxMessageOutputStderr(m_fp
).Output(msg
);
863 // under GUI systems such as Windows or Mac, programs usually don't have
864 // stderr at all, so show the messages also somewhere else, typically in
865 // the debugger window so that they go at least somewhere instead of being
867 if ( m_fp
== stderr
)
869 wxAppTraits
*traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
870 if ( traits
&& !traits
->HasStderr() )
872 wxMessageOutputDebug().Output(msg
+ wxS('\n'));
877 // ----------------------------------------------------------------------------
878 // wxLogStream implementation
879 // ----------------------------------------------------------------------------
881 #if wxUSE_STD_IOSTREAM
882 #include "wx/ioswrap.h"
883 wxLogStream::wxLogStream(wxSTD ostream
*ostr
)
886 m_ostr
= &wxSTD cerr
;
891 void wxLogStream::DoLogText(const wxString
& msg
)
893 (*m_ostr
) << msg
<< wxSTD endl
;
895 #endif // wxUSE_STD_IOSTREAM
897 // ----------------------------------------------------------------------------
899 // ----------------------------------------------------------------------------
901 wxLogChain::wxLogChain(wxLog
*logger
)
903 m_bPassMessages
= true;
907 // Notice that we use GetActiveTarget() here instead of directly calling
908 // SetActiveTarget() to trigger wxLog auto-creation: if we're created as
909 // the first logger, we should still chain with the standard, implicit and
910 // possibly still not created standard logger instead of disabling normal
912 m_logOld
= wxLog::GetActiveTarget();
913 wxLog::SetActiveTarget(this);
916 wxLogChain::~wxLogChain()
918 wxLog::SetActiveTarget(m_logOld
);
920 if ( m_logNew
!= this )
924 void wxLogChain::SetLog(wxLog
*logger
)
926 if ( m_logNew
!= this )
932 void wxLogChain::Flush()
937 // be careful to avoid infinite recursion
938 if ( m_logNew
&& m_logNew
!= this )
942 void wxLogChain::DoLogRecord(wxLogLevel level
,
944 const wxLogRecordInfo
& info
)
946 // let the previous logger show it
947 if ( m_logOld
&& IsPassingMessages() )
948 m_logOld
->LogRecord(level
, msg
, info
);
950 // and also send it to the new one
953 // don't call m_logNew->LogRecord() to avoid infinite recursion when
954 // m_logNew is this object itself
955 if ( m_logNew
!= this )
956 m_logNew
->LogRecord(level
, msg
, info
);
958 wxLog::DoLogRecord(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 // ----------------------------------------------------------------------------
1016 // stdout error logging helper
1017 // ----------------------------------------------------------------------------
1019 // helper function: wraps the message and justifies it under given position
1020 // (looks more pretty on the terminal). Also adds newline at the end.
1022 // TODO this is now disabled until I find a portable way of determining the
1023 // terminal window size (ok, I found it but does anybody really cares?)
1024 #ifdef LOG_PRETTY_WRAP
1025 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
1027 size_t nMax
= 80; // FIXME
1028 size_t nStart
= strlen(pszPrefix
);
1029 fputs(pszPrefix
, f
);
1032 while ( *psz
!= '\0' ) {
1033 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
1037 if ( *psz
!= '\0' ) {
1039 for ( n
= 0; n
< nStart
; n
++ )
1042 // as we wrapped, squeeze all white space
1043 while ( isspace(*psz
) )
1050 #endif //LOG_PRETTY_WRAP
1052 // ----------------------------------------------------------------------------
1053 // error code/error message retrieval functions
1054 // ----------------------------------------------------------------------------
1056 // get error code from syste
1057 unsigned long wxSysErrorCode()
1059 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1060 return ::GetLastError();
1066 // get error message from system
1067 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
1069 if ( nErrCode
== 0 )
1070 nErrCode
= wxSysErrorCode();
1072 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1073 static wxChar s_szBuf
[1024];
1075 // get error message from system
1077 if ( ::FormatMessage
1079 FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
1082 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
1088 // if this happens, something is seriously wrong, so don't use _() here
1090 wxSprintf(s_szBuf
, wxS("unknown error %lx"), nErrCode
);
1095 // copy it to our buffer and free memory
1096 // Crashes on SmartPhone (FIXME)
1097 #if !defined(__SMARTPHONE__) /* of WinCE */
1100 wxStrlcpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
));
1102 LocalFree(lpMsgBuf
);
1104 // returned string is capitalized and ended with '\r\n' - bad
1105 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
1106 size_t len
= wxStrlen(s_szBuf
);
1109 if ( s_szBuf
[len
- 2] == wxS('\r') )
1110 s_szBuf
[len
- 2] = wxS('\0');
1114 #endif // !__SMARTPHONE__
1116 s_szBuf
[0] = wxS('\0');
1120 #else // !__WINDOWS__
1122 static wchar_t s_wzBuf
[1024];
1123 wxConvCurrent
->MB2WC(s_wzBuf
, strerror((int)nErrCode
),
1124 WXSIZEOF(s_wzBuf
) - 1);
1127 return strerror((int)nErrCode
);
1129 #endif // __WINDOWS__/!__WINDOWS__