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 // ----------------------------------------------------------------------------
20 #pragma implementation "log.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
32 #include <wx/string.h>
34 #include <wx/generic/msgdlgg.h>
40 // other standard headers
48 // _WINDOWS_ is defined when windows.h is included,
49 // __WINDOWS__ is defined for MS Windows compilation
50 #if defined(__WINDOWS__) && !defined(_WINDOWS_)
54 // ----------------------------------------------------------------------------
55 // non member functions
56 // ----------------------------------------------------------------------------
58 // define this to enable wrapping of log messages
59 //#define LOG_PRETTY_WRAP
61 #ifdef LOG_PRETTY_WRAP
62 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
);
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // implementation of Log functions
72 // NB: unfortunately we need all these distinct functions, we can't make them
73 // macros and not all compilers inline vararg functions.
74 // ----------------------------------------------------------------------------
76 // log functions can't allocate memory (LogError("out of memory...") should
77 // work!), so we use a static buffer for all log messages
78 #define LOG_BUFFER_SIZE (4096)
80 // static buffer for error messages (@@@ MT-unsafe)
81 static char s_szBuf
[LOG_BUFFER_SIZE
];
83 // generic log function
84 void wxLogGeneric(wxLog::Level level
, wxTString strFormat
, ...)
86 if ( wxLog::GetActiveTarget() != NULL
) {
88 va_start(argptr
, strFormat
);
89 vsprintf(s_szBuf
, strFormat
, argptr
);
92 wxLog::OnLog(level
, s_szBuf
);
96 #define IMPLEMENT_LOG_FUNCTION(level) \
97 void wxLog##level(wxTString strFormat, ...) \
99 if ( wxLog::GetActiveTarget() != NULL ) { \
101 va_start(argptr, strFormat); \
102 vsprintf(s_szBuf, strFormat, argptr); \
105 wxLog::OnLog(wxLog::level, s_szBuf); \
109 IMPLEMENT_LOG_FUNCTION(FatalError
)
110 IMPLEMENT_LOG_FUNCTION(Error
)
111 IMPLEMENT_LOG_FUNCTION(Warning
)
112 IMPLEMENT_LOG_FUNCTION(Message
)
113 IMPLEMENT_LOG_FUNCTION(Info
)
114 IMPLEMENT_LOG_FUNCTION(Status
)
116 // debug functions don't use wxTString
117 #undef IMPLEMENT_LOG_FUNCTION
118 #define IMPLEMENT_LOG_FUNCTION(level) \
119 void wxLog##level(const char *szFormat, ...) \
121 if ( wxLog::GetActiveTarget() != NULL ) { \
123 va_start(argptr, szFormat); \
124 vsprintf(s_szBuf, szFormat, argptr); \
127 wxLog::OnLog(wxLog::level, s_szBuf); \
131 IMPLEMENT_LOG_FUNCTION(Debug
)
132 IMPLEMENT_LOG_FUNCTION(Trace
)
134 void wxLogVerbose(wxTString strFormat
, ...)
136 if ( wxLog::GetVerbose() && wxLog::GetActiveTarget() != NULL
) {
138 va_start(argptr
, strFormat
);
139 vsprintf(s_szBuf
, strFormat
, argptr
);
142 wxLog::OnLog(wxLog::Info
, s_szBuf
);
146 void wxLogSysError(wxTString str
, ...)
148 if ( wxLog::GetActiveTarget() != NULL
) {
150 va_start(argptr
, str
);
151 vsprintf(s_szBuf
, str
, argptr
);
154 char szErrMsg
[LOG_BUFFER_SIZE
/ 2];
155 sprintf(szErrMsg
, _(" (error %ld: %s)"), wxSysErrorCode(), wxSysErrorMsg());
156 strncat(s_szBuf
, szErrMsg
, WXSIZEOF(s_szBuf
) - strlen(s_szBuf
));
158 wxLog::OnLog(wxLog::Error
, s_szBuf
);
162 void WXDLLEXPORT
wxLogSysError(long lErrCode
, wxTString strFormat
, ...)
164 if ( wxLog::GetActiveTarget() != NULL
) {
166 va_start(argptr
, strFormat
);
167 vsprintf(s_szBuf
, strFormat
, argptr
);
170 char szErrMsg
[LOG_BUFFER_SIZE
/ 2];
171 sprintf(szErrMsg
, _(" (error %ld: %s)"), lErrCode
, wxSysErrorMsg(lErrCode
));
172 strncat(s_szBuf
, szErrMsg
, WXSIZEOF(s_szBuf
) - strlen(s_szBuf
));
174 wxLog::OnLog(wxLog::Error
, s_szBuf
);
178 // ----------------------------------------------------------------------------
179 // wxLog class implementation
180 // ----------------------------------------------------------------------------
184 m_bHasMessages
= FALSE
;
187 wxLog
*wxLog::GetActiveTarget()
189 if ( !ms_bInitialized
) {
190 // prevent infinite recursion if someone calls wxLogXXX() from wxApp
191 ms_bInitialized
= TRUE
;
193 // ask the application to create a log target for us if it exists
194 if ( wxTheApp
!= NULL
)
195 ms_pLogger
= wxTheApp
->CreateLogTarget();
197 ms_pLogger
= new wxLogStderr
;
199 // do nothing if it fails - what can we do?
205 wxLog
*wxLog::SetActiveTarget(wxLog
*pLogger
)
207 // flush the old messages before changing
208 if ( ms_pLogger
!= NULL
)
211 ms_bInitialized
= TRUE
;
213 wxLog
*pOldLogger
= ms_pLogger
;
214 ms_pLogger
= pLogger
;
218 void wxLog::DoLog(Level level
, const char *szString
)
225 ptmNow
= localtime(&timeNow
);
227 strftime(szBuf
, WXSIZEOF(szBuf
), ms_szTimeFormat
, ptmNow
);
228 wxString str
= szBuf
;
232 DoLogString(str
<< _("Fatal error: ") << szString
);
233 DoLogString(_("Program aborted."));
239 DoLogString(str
<< _("Error: ") << szString
);
243 DoLogString(str
<< _("Warning: ") << szString
);
249 DoLogString(str
+ szString
);
260 // in addition to normal logging, also send the string to debugger
261 // (don't prepend "Debug" here: it will go to debug window anyhow)
262 ::OutputDebugString(str
+ szString
+ "\n\r");
264 DoLogString(str
<< (level
== Trace
? _("Trace") : _("Debug"))
265 << ": " << szString
);
271 wxFAIL_MSG("unknown log level in wxLog::DoLog");
275 void wxLog::DoLogString(const char *WXUNUSED(szString
))
277 wxFAIL_MSG("DoLogString must be overrided if it's called.");
285 // ----------------------------------------------------------------------------
286 // wxLogStderr class implementation
287 // ----------------------------------------------------------------------------
289 wxLogStderr::wxLogStderr(FILE *fp
)
297 void wxLogStderr::DoLogString(const char *szString
)
299 fputs(szString
, m_fp
);
304 // ----------------------------------------------------------------------------
305 // wxLogStream implementation
306 // ----------------------------------------------------------------------------
308 wxLogStream::wxLogStream(ostream
*ostr
)
316 void wxLogStream::DoLogString(const char *szString
)
318 (*m_ostr
) << szString
<< endl
<< flush
;
321 // ----------------------------------------------------------------------------
322 // wxLogTextCtrl implementation
323 // ----------------------------------------------------------------------------
326 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
327 : wxLogStream(new ostream(pTextCtrl))
331 wxLogTextCtrl::~wxLogTextCtrl()
337 // ----------------------------------------------------------------------------
338 // wxLogGui implementation
339 // ----------------------------------------------------------------------------
341 #ifndef WX_TEST_MINIMAL
348 void wxLogGui::Flush()
350 if ( !m_bHasMessages
)
355 // concatenate all strings (but not too many to not overfill the msg box)
358 nMsgCount
= m_aMessages
.Count();
360 // start from the most recent message
361 for ( uint n
= nMsgCount
; n
> 0; n
-- ) {
362 // for Windows strings longer than this value are wrapped (NT 4.0)
363 const uint nMsgLineWidth
= 156;
365 nLines
+= (m_aMessages
[n
- 1].Len() + nMsgLineWidth
- 1) / nMsgLineWidth
;
367 if ( nLines
> 25 ) // don't put too many lines in message box
370 str
<< m_aMessages
[n
- 1] << "\n";
374 wxMessageBox(str
, _("Error"), wxOK
| wxICON_EXCLAMATION
);
377 wxMessageBox(str
, _("Information"), wxOK
| wxICON_INFORMATION
);
380 // no undisplayed messages whatsoever
386 // the default behaviour is to discard all informational messages if there
387 // are any errors/warnings.
388 void wxLogGui::DoLog(Level level
, const char *szString
)
395 m_aMessages
.Add(szString
);
396 m_bHasMessages
= TRUE
;
402 // find the top window and set it's status text if it has any
403 wxWindow
*pWin
= wxTheApp
->GetTopWindow();
404 if ( pWin
!= NULL
&& pWin
->IsKindOf(CLASSINFO(wxFrame
)) ) {
405 wxFrame
*pFrame
= (wxFrame
*)pWin
;
406 pFrame
->SetStatusText(szString
);
415 OutputDebugString(szString
);
416 OutputDebugString("\n\r");
418 // send them to stderr
419 printf(stderr
, level
== Trace
? "Trace: %s\n"
420 : "Debug: %s\n", szString
);
427 // show this one immediately
428 wxMessageBox(szString
, "Fatal error", wxICON_HAND
);
433 // discard earlier informational messages if this is the 1st error
436 m_bHasMessages
= TRUE
;
440 m_aMessages
.Add(szString
);
444 wxFAIL_MSG("unknown log level in wxLogGui::DoLog");
448 #endif //WX_TEST_MINIMAL
450 // ============================================================================
451 // Global functions/variables
452 // ============================================================================
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
457 wxLog
*wxLog::ms_pLogger
= NULL
;
458 bool wxLog::ms_bInitialized
= FALSE
;
459 bool wxLog::ms_bVerbose
= FALSE
;
460 const char *wxLog::ms_szTimeFormat
= "[%d/%b/%y %H:%M:%S] ";
462 // ----------------------------------------------------------------------------
463 // stdout error logging helper
464 // ----------------------------------------------------------------------------
466 // helper function: wraps the message and justifies it under given position
467 // (looks more pretty on the terminal). Also adds newline at the end.
469 // @@ this is now disabled until I find a portable way of determining the
470 // terminal window size
472 #ifdef LOG_PRETTY_WRAP
473 static void wxLogWrap(FILE *f
, const char *pszPrefix
, const char *psz
)
475 size_t nMax
= 80; // @@@@
476 size_t nStart
= strlen(pszPrefix
);
480 while ( *psz
!= '\0' ) {
481 for ( n
= nStart
; (n
< nMax
) && (*psz
!= '\0'); n
++ )
485 if ( *psz
!= '\0' ) {
487 for ( n
= 0; n
< nStart
; n
++ )
490 // as we wrapped, squeeze all white space
491 while ( isspace(*psz
) )
498 #endif //LOG_PRETTY_WRAP
500 // ----------------------------------------------------------------------------
501 // error code/error message retrieval functions
502 // ----------------------------------------------------------------------------
504 // get error code from syste
505 unsigned long wxSysErrorCode()
509 return ::GetLastError();
511 // @@@@ what to do on Windows 3.1?
519 // get error message from system
520 const char *wxSysErrorMsg(unsigned long nErrCode
)
523 nErrCode
= wxSysErrorCode();
527 static char s_szBuf
[LOG_BUFFER_SIZE
/ 2];
529 // get error message from system
531 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
533 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
537 // copy it to our buffer and free memory
538 strncpy(s_szBuf
, (const char *)lpMsgBuf
, SIZEOF(s_szBuf
) - 1);
539 s_szBuf
[SIZEOF(s_szBuf
) - 1] = '\0';
542 // returned string is capitalized and ended with '\r\n' - bad
543 s_szBuf
[0] = (char)tolower(s_szBuf
[0]);
544 size_t len
= strlen(s_szBuf
);
547 if ( s_szBuf
[len
- 2] == '\r' )
548 s_szBuf
[len
- 2] = '\0';
557 return strerror(nErrCode
);
561 // ----------------------------------------------------------------------------
563 // ----------------------------------------------------------------------------
567 // this function is called when an assert fails
568 void wxOnAssert(const char *szFile
, int nLine
, const char *szMsg
)
570 // this variable can be set to true to suppress "assert failure" messages
571 static s_bNoAsserts
= FALSE
;
573 char szBuf
[LOG_BUFFER_SIZE
];
574 sprintf(szBuf
, _("Assert failed in file %s at line %d"), szFile
, nLine
);
575 if ( szMsg
!= NULL
) {
577 strcat(szBuf
, szMsg
);
583 // send it to the normal log destination
587 if ( !s_bNoAsserts
) {
588 strcat(szBuf
, _("\nDo you want to stop the program?"
589 "\nYou can also choose [Cancel] to suppress "
590 "further warnings."));
592 switch ( ::MessageBox(NULL
, szBuf
, _("Debug"),
593 MB_YESNOCANCEL
| MB_ICONINFORMATION
) ) {
604 // @@@@ don't know how to start the debugger under generic Unix
605 s_bNoAsserts
= TRUE
; // suppress 'unused var' warning