1 /////////////////////////////////////////////////////////////////////////////
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 license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "log.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
38 #include "wx/window.h"
40 #include "wx/msw/private.h"
42 #include "wx/msgdlg.h"
47 #include "wx/textfile.h"
49 #include "wx/wxchar.h"
52 // other standard headers
59 // Redefines OutputDebugString if necessary
60 #include "wx/msw/private.h"
65 // ----------------------------------------------------------------------------
66 // non member functions
67 // ----------------------------------------------------------------------------
69 // define this to enable wrapping of log messages
70 //#define LOG_PRETTY_WRAP
72 #ifdef LOG_PRETTY_WRAP
73 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
81 // implementation of Log functions
83 // NB: unfortunately we need all these distinct functions, we can't make them
84 // macros and not all compilers inline vararg functions.
85 // ----------------------------------------------------------------------------
87 // log functions can't allocate memory (LogError("out of memory...") should
88 // work!), so we use a static buffer for all log messages
89 #define LOG_BUFFER_SIZE (4096)
91 // static buffer for error messages (FIXME MT-unsafe)
92 static wxChar s_szBuf
[LOG_BUFFER_SIZE
];
94 // generic log function
95 void wxLogGeneric(wxLogLevel level
, const wxChar
*szFormat
, ...)
97 if ( wxLog::GetActiveTarget() != NULL
) {
99 va_start(argptr
, szFormat
);
100 wxVsprintf(s_szBuf
, szFormat
, argptr
);
103 wxLog::OnLog(level
, s_szBuf
, time(NULL
));
107 #define IMPLEMENT_LOG_FUNCTION(level) \
108 void wxLog##level(const wxChar *szFormat, ...) \
110 if ( wxLog::GetActiveTarget() != NULL ) { \
112 va_start(argptr, szFormat); \
113 wxVsprintf(s_szBuf, szFormat, argptr); \
116 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
120 IMPLEMENT_LOG_FUNCTION(FatalError
)
121 IMPLEMENT_LOG_FUNCTION(Error
)
122 IMPLEMENT_LOG_FUNCTION(Warning
)
123 IMPLEMENT_LOG_FUNCTION(Message
)
124 IMPLEMENT_LOG_FUNCTION(Info
)
125 IMPLEMENT_LOG_FUNCTION(Status
)
127 // same as info, but only if 'verbose' mode is on
128 void wxLogVerbose(const wxChar
*szFormat
, ...)
130 wxLog
*pLog
= wxLog::GetActiveTarget();
131 if ( pLog
!= NULL
&& pLog
->GetVerbose() ) {
133 va_start(argptr
, szFormat
);
134 wxVsprintf(s_szBuf
, szFormat
, argptr
);
137 wxLog::OnLog(wxLOG_Info
, s_szBuf
, time(NULL
));
143 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
144 void wxLog##level(const wxChar *szFormat, ...) \
146 if ( wxLog::GetActiveTarget() != NULL ) { \
148 va_start(argptr, szFormat); \
149 wxVsprintf(s_szBuf, szFormat, argptr); \
152 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
156 void wxLogTrace(const wxChar
*mask
, const wxChar
*szFormat
, ...)
158 wxLog
*pLog
= wxLog::GetActiveTarget();
160 if ( pLog
!= NULL
&& wxLog::IsAllowedTraceMask(mask
) ) {
162 va_start(argptr
, szFormat
);
163 wxVsprintf(s_szBuf
, szFormat
, argptr
);
166 wxLog::OnLog(wxLOG_Trace
, s_szBuf
, time(NULL
));
170 void wxLogTrace(wxTraceMask mask
, const wxChar
*szFormat
, ...)
172 wxLog
*pLog
= wxLog::GetActiveTarget();
174 // we check that all of mask bits are set in the current mask, so
175 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
176 // if both bits are set.
177 if ( pLog
!= NULL
&& ((pLog
->GetTraceMask() & mask
) == mask
) ) {
179 va_start(argptr
, szFormat
);
180 wxVsprintf(s_szBuf
, szFormat
, argptr
);
183 wxLog::OnLog(wxLOG_Trace
, s_szBuf
, time(NULL
));
188 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
191 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug
)
192 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace
)
194 // wxLogSysError: one uses the last error code, for other you must give it
197 // common part of both wxLogSysError
198 void wxLogSysErrorHelper(long lErrCode
)
200 wxChar szErrMsg
[LOG_BUFFER_SIZE
/ 2];
201 wxSprintf(szErrMsg
, _(" (error %ld: %s)"), lErrCode
, wxSysErrorMsg(lErrCode
));
202 wxStrncat(s_szBuf
, szErrMsg
, WXSIZEOF(s_szBuf
) - wxStrlen(s_szBuf
));
204 wxLog::OnLog(wxLOG_Error
, s_szBuf
, time(NULL
));
207 void WXDLLEXPORT
wxLogSysError(const wxChar
*szFormat
, ...)
210 va_start(argptr
, szFormat
);
211 wxVsprintf(s_szBuf
, szFormat
, argptr
);
214 wxLogSysErrorHelper(wxSysErrorCode());
217 void WXDLLEXPORT
wxLogSysError(long lErrCode
, const wxChar
*szFormat
, ...)
220 va_start(argptr
, szFormat
);
221 wxVsprintf(s_szBuf
, szFormat
, argptr
);
224 wxLogSysErrorHelper(lErrCode
);
227 // ----------------------------------------------------------------------------
228 // wxLog class implementation
229 // ----------------------------------------------------------------------------
233 m_bHasMessages
= FALSE
;
235 // enable verbose messages by default in the debug builds
240 #endif // debug/release
243 wxLog
*wxLog::GetActiveTarget()
245 if ( ms_bAutoCreate
&& ms_pLogger
== NULL
) {
246 // prevent infinite recursion if someone calls wxLogXXX() from
247 // wxApp::CreateLogTarget()
248 static bool s_bInGetActiveTarget
= FALSE
;
249 if ( !s_bInGetActiveTarget
) {
250 s_bInGetActiveTarget
= TRUE
;
252 // ask the application to create a log target for us
253 if ( wxTheApp
!= NULL
)
254 ms_pLogger
= wxTheApp
->CreateLogTarget();
256 ms_pLogger
= new wxLogStderr
;
258 s_bInGetActiveTarget
= FALSE
;
260 // do nothing if it fails - what can we do?
267 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
269 if ( ms_pLogger
!= NULL
) {
270 // flush the old messages before changing because otherwise they might
271 // get lost later if this target is not restored
275 wxLog
*pOldLogger
= ms_pLogger
;
276 ms_pLogger
= pLogger
;
281 void wxLog::RemoveTraceMask(const wxString
& str
)
283 int index
= ms_aTraceMasks
.Index(str
);
284 if ( index
!= wxNOT_FOUND
)
285 ms_aTraceMasks
.Remove((size_t)index
);
288 void wxLog::TimeStamp(wxString
*str
)
294 (void)time(&timeNow
);
295 wxStrftime(buf
, WXSIZEOF(buf
), ms_timestamp
, localtime(&timeNow
));
298 *str
<< buf
<< T(": ");
302 void wxLog::DoLog(wxLogLevel level
, const wxChar
*szString
, time_t t
)
305 case wxLOG_FatalError
:
306 DoLogString(wxString(_("Fatal error: ")) + szString
, t
);
307 DoLogString(_("Program aborted."), t
);
313 DoLogString(wxString(_("Error: ")) + szString
, t
);
317 DoLogString(wxString(_("Warning: ")) + szString
, t
);
323 default: // log unknown log levels too
324 DoLogString(szString
, t
);
334 DoLogString(szString
, t
);
340 void wxLog::DoLogString(const wxChar
*WXUNUSED(szString
), time_t WXUNUSED(t
))
342 wxFAIL_MSG(T("DoLogString must be overriden if it's called."));
350 // ----------------------------------------------------------------------------
351 // wxLogStderr class implementation
352 // ----------------------------------------------------------------------------
354 wxLogStderr::wxLogStderr(FILE *fp
)
362 void wxLogStderr::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
366 str
<< szString
<< T('\n');
368 fputs(str
.mb_str(), m_fp
);
371 // under Windows, programs usually don't have stderr at all, so make show the
372 // messages also under debugger
374 OutputDebugString(str
+ T('\r'));
378 // ----------------------------------------------------------------------------
379 // wxLogStream implementation
380 // ----------------------------------------------------------------------------
382 #if wxUSE_STD_IOSTREAM
383 wxLogStream::wxLogStream(ostream
*ostr
)
391 void wxLogStream::DoLogString(const wxChar
*szString
, time_t WXUNUSED(t
))
393 (*m_ostr
) << wxConvertWX2MB(szString
) << endl
;
395 #endif // wxUSE_STD_IOSTREAM
397 // ============================================================================
398 // Global functions/variables
399 // ============================================================================
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 wxLog
*wxLog::ms_pLogger
= (wxLog
*)NULL
;
406 bool wxLog::ms_doLog
= TRUE
;
407 bool wxLog::ms_bAutoCreate
= TRUE
;
409 const wxChar
*wxLog::ms_timestamp
= T("%X"); // time only, no date
411 wxTraceMask
wxLog::ms_ulTraceMask
= (wxTraceMask
)0;
412 wxArrayString
wxLog::ms_aTraceMasks
;
414 // ----------------------------------------------------------------------------
415 // stdout error logging helper
416 // ----------------------------------------------------------------------------
418 // helper function: wraps the message and justifies it under given position
419 // (looks more pretty on the terminal). Also adds newline at the end.
421 // TODO this is now disabled until I find a portable way of determining the
422 // terminal window size (ok, I found it but does anybody really cares?)
423 #ifdef LOG_PRETTY_WRAP
424 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
426 size_t nMax
= 80; // FIXME
427 size_t nStart
= strlen(pszPrefix
);
431 while ( *psz
!= '\0' ) {
432 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
436 if ( *psz
!= '\0' ) {
438 for ( n
= 0; n
< nStart
; n
++ )
441 // as we wrapped, squeeze all white space
442 while ( isspace(*psz
) )
449 #endif //LOG_PRETTY_WRAP
451 // ----------------------------------------------------------------------------
452 // error code/error message retrieval functions
453 // ----------------------------------------------------------------------------
455 // get error code from syste
456 unsigned long wxSysErrorCode()
460 return ::GetLastError();
462 // TODO what to do on Windows 3.1?
470 // get error message from system
471 const wxChar
*wxSysErrorMsg(unsigned long nErrCode
)
474 nErrCode
= wxSysErrorCode();
478 static wxChar s_szBuf
[LOG_BUFFER_SIZE
/ 2];
480 // get error message from system
482 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
484 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
488 // copy it to our buffer and free memory
489 wxStrncpy(s_szBuf
, (const wxChar
*)lpMsgBuf
, WXSIZEOF(s_szBuf
) - 1);
490 s_szBuf
[WXSIZEOF(s_szBuf
) - 1] = T('\0');
493 // returned string is capitalized and ended with '\r\n' - bad
494 s_szBuf
[0] = (wxChar
)wxTolower(s_szBuf
[0]);
495 size_t len
= wxStrlen(s_szBuf
);
498 if ( s_szBuf
[len
- 2] == T('\r') )
499 s_szBuf
[len
- 2] = T('\0');
509 static wxChar s_szBuf
[LOG_BUFFER_SIZE
/ 2];
510 wxConvCurrent
->MB2WC(s_szBuf
, strerror(nErrCode
), WXSIZEOF(s_szBuf
) -1);
513 return strerror(nErrCode
);
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
524 // break into the debugger
529 #elif defined(__WXMAC__)
535 #elif defined(__UNIX__)
542 // this function is called when an assert fails
543 void wxOnAssert(const wxChar
*szFile
, int nLine
, const wxChar
*szMsg
)
545 // this variable can be set to true to suppress "assert failure" messages
546 static bool s_bNoAsserts
= FALSE
;
547 static bool s_bInAssert
= FALSE
; // FIXME MT-unsafe
550 // He-e-e-e-elp!! we're trapped in endless loop
560 wxChar szBuf
[LOG_BUFFER_SIZE
];
562 // make life easier for people using VC++ IDE: clicking on the message
563 // will take us immediately to the place of the failed assert
565 wxSprintf(szBuf
, T("%s(%d): assert failed"), szFile
, nLine
);
567 // make the error message more clear for all the others
568 wxSprintf(szBuf
, T("Assert failed in file %s at line %d"), szFile
, nLine
);
571 if ( szMsg
!= NULL
) {
572 wxStrcat(szBuf
, T(": "));
573 wxStrcat(szBuf
, szMsg
);
576 wxStrcat(szBuf
, T("."));
579 if ( !s_bNoAsserts
) {
580 // send it to the normal log destination
584 // this message is intentionally not translated - it is for
586 wxStrcat(szBuf
, T("\nDo you want to stop the program?"
587 "\nYou can also choose [Cancel] to suppress "
588 "further warnings."));
590 switch ( wxMessageBox(szBuf
, _("Debug"),
591 wxYES_NO
| wxCANCEL
| wxICON_STOP
) ) {
600 //case wxNO: nothing to do