]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: log.cpp | |
3 | // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs) | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | #ifdef __GNUG__ | |
20 | #pragma implementation "log.h" | |
21 | #endif | |
22 | ||
23 | // For compilers that support precompilation, includes "wx.h". | |
24 | #include "wx/wxprec.h" | |
25 | ||
26 | #ifdef __BORLANDC__ | |
27 | #pragma hdrstop | |
28 | #endif | |
29 | ||
30 | // wxWindows | |
31 | #ifndef WX_PRECOMP | |
c801d85f | 32 | #include <wx/app.h> |
9ef3052c VZ |
33 | #include <wx/string.h> |
34 | #include <wx/intl.h> | |
cf4219e7 | 35 | #include <wx/menu.h> |
34138703 | 36 | #include <wx/frame.h> |
9ef3052c | 37 | |
c801d85f | 38 | #include <wx/generic/msgdlgg.h> |
9ef3052c VZ |
39 | #include <wx/filedlg.h> |
40 | #include <wx/textctrl.h> | |
41 | #endif //WX_PRECOMP | |
c801d85f | 42 | |
9ef3052c VZ |
43 | #include <wx/file.h> |
44 | #include <wx/textfile.h> | |
81d66cf3 | 45 | #include <wx/utils.h> |
c801d85f KB |
46 | #include <wx/log.h> |
47 | ||
48 | // other standard headers | |
49 | #include <errno.h> | |
50 | #include <stdlib.h> | |
51 | #include <time.h> | |
52 | ||
2049ba38 | 53 | #ifdef __WXMSW__ |
9ef3052c | 54 | #include <windows.h> |
3078c3a6 VZ |
55 | #else //Unix |
56 | #include <signal.h> | |
57 | #endif //Win/Unix | |
c801d85f KB |
58 | |
59 | // ---------------------------------------------------------------------------- | |
60 | // non member functions | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | // define this to enable wrapping of log messages | |
64 | //#define LOG_PRETTY_WRAP | |
65 | ||
9ef3052c | 66 | #ifdef LOG_PRETTY_WRAP |
c801d85f KB |
67 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); |
68 | #endif | |
69 | ||
470b7da3 VZ |
70 | // ---------------------------------------------------------------------------- |
71 | // global variables | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | // we use a global variable to store the frame pointer for wxLogStatus - bad, | |
75 | // but it's he easiest way | |
76 | static wxFrame *gs_pFrame; | |
77 | ||
c801d85f KB |
78 | // ============================================================================ |
79 | // implementation | |
80 | // ============================================================================ | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // implementation of Log functions | |
84 | // | |
85 | // NB: unfortunately we need all these distinct functions, we can't make them | |
86 | // macros and not all compilers inline vararg functions. | |
87 | // ---------------------------------------------------------------------------- | |
88 | ||
89 | // log functions can't allocate memory (LogError("out of memory...") should | |
90 | // work!), so we use a static buffer for all log messages | |
91 | #define LOG_BUFFER_SIZE (4096) | |
92 | ||
93 | // static buffer for error messages (@@@ MT-unsafe) | |
94 | static char s_szBuf[LOG_BUFFER_SIZE]; | |
95 | ||
96 | // generic log function | |
7502ba29 | 97 | void wxLogGeneric(wxLogLevel level, const char *szFormat, ...) |
c801d85f | 98 | { |
9ef3052c VZ |
99 | if ( wxLog::GetActiveTarget() != NULL ) { |
100 | va_list argptr; | |
7502ba29 VZ |
101 | va_start(argptr, szFormat); |
102 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
103 | va_end(argptr); |
104 | ||
105 | wxLog::OnLog(level, s_szBuf); | |
106 | } | |
c801d85f KB |
107 | } |
108 | ||
109 | #define IMPLEMENT_LOG_FUNCTION(level) \ | |
7502ba29 | 110 | void wxLog##level(const char *szFormat, ...) \ |
c801d85f KB |
111 | { \ |
112 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
113 | va_list argptr; \ | |
7502ba29 VZ |
114 | va_start(argptr, szFormat); \ |
115 | vsprintf(s_szBuf, szFormat, argptr); \ | |
c801d85f KB |
116 | va_end(argptr); \ |
117 | \ | |
9ef3052c | 118 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ |
c801d85f KB |
119 | } \ |
120 | } | |
121 | ||
122 | IMPLEMENT_LOG_FUNCTION(FatalError) | |
123 | IMPLEMENT_LOG_FUNCTION(Error) | |
124 | IMPLEMENT_LOG_FUNCTION(Warning) | |
125 | IMPLEMENT_LOG_FUNCTION(Message) | |
126 | IMPLEMENT_LOG_FUNCTION(Info) | |
127 | IMPLEMENT_LOG_FUNCTION(Status) | |
128 | ||
470b7da3 VZ |
129 | // accepts an additional argument which tells to which frame the output should |
130 | // be directed | |
131 | void wxLogStatus(wxFrame *pFrame, const char *szFormat, ...) | |
132 | { | |
133 | wxLog *pLog = wxLog::GetActiveTarget(); | |
134 | if ( pLog != NULL ) { | |
135 | va_list argptr; | |
136 | va_start(argptr, szFormat); | |
137 | vsprintf(s_szBuf, szFormat, argptr); | |
138 | va_end(argptr); | |
139 | ||
140 | wxASSERT( gs_pFrame == NULL ); // should be reset! | |
141 | gs_pFrame = pFrame; | |
142 | wxLog::OnLog(wxLOG_Status, s_szBuf); | |
c67daf87 | 143 | gs_pFrame = (wxFrame *) NULL; |
470b7da3 VZ |
144 | } |
145 | } | |
146 | ||
9ef3052c | 147 | // same as info, but only if 'verbose' mode is on |
7502ba29 | 148 | void wxLogVerbose(const char *szFormat, ...) |
9ef3052c VZ |
149 | { |
150 | wxLog *pLog = wxLog::GetActiveTarget(); | |
151 | if ( pLog != NULL && pLog->GetVerbose() ) { | |
152 | va_list argptr; | |
7502ba29 VZ |
153 | va_start(argptr, szFormat); |
154 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
155 | va_end(argptr); |
156 | ||
157 | wxLog::OnLog(wxLOG_Info, s_szBuf); | |
158 | } | |
159 | } | |
160 | ||
161 | // debug functions | |
b2aef89b | 162 | #ifdef __WXDEBUG__ |
9ef3052c | 163 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ |
c801d85f KB |
164 | void wxLog##level(const char *szFormat, ...) \ |
165 | { \ | |
166 | if ( wxLog::GetActiveTarget() != NULL ) { \ | |
167 | va_list argptr; \ | |
168 | va_start(argptr, szFormat); \ | |
169 | vsprintf(s_szBuf, szFormat, argptr); \ | |
170 | va_end(argptr); \ | |
171 | \ | |
9ef3052c | 172 | wxLog::OnLog(wxLOG_##level, s_szBuf); \ |
c801d85f KB |
173 | } \ |
174 | } | |
175 | ||
9ef3052c VZ |
176 | void wxLogTrace(wxTraceMask mask, const char *szFormat, ...) |
177 | { | |
178 | wxLog *pLog = wxLog::GetActiveTarget(); | |
c801d85f | 179 | |
9ef3052c VZ |
180 | // we check that all of mask bits are set in the current mask, so |
181 | // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something | |
182 | // if both bits are set. | |
d93f63db | 183 | if ( pLog != NULL && ((pLog->GetTraceMask() & mask) == mask) ) { |
9ef3052c VZ |
184 | va_list argptr; |
185 | va_start(argptr, szFormat); | |
186 | vsprintf(s_szBuf, szFormat, argptr); | |
187 | va_end(argptr); | |
188 | ||
189 | wxLog::OnLog(wxLOG_Trace, s_szBuf); | |
190 | } | |
191 | } | |
192 | ||
193 | #else // release | |
194 | #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) | |
195 | #endif | |
196 | ||
197 | IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) | |
198 | IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) | |
199 | ||
200 | // wxLogSysError: one uses the last error code, for other you must give it | |
201 | // explicitly | |
202 | ||
203 | // common part of both wxLogSysError | |
204 | void wxLogSysErrorHelper(long lErrCode) | |
c801d85f | 205 | { |
9ef3052c VZ |
206 | char szErrMsg[LOG_BUFFER_SIZE / 2]; |
207 | sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); | |
208 | strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf)); | |
c801d85f | 209 | |
9ef3052c VZ |
210 | wxLog::OnLog(wxLOG_Error, s_szBuf); |
211 | } | |
c801d85f | 212 | |
7502ba29 | 213 | void WXDLLEXPORT wxLogSysError(const char *szFormat, ...) |
9ef3052c VZ |
214 | { |
215 | va_list argptr; | |
7502ba29 VZ |
216 | va_start(argptr, szFormat); |
217 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c VZ |
218 | va_end(argptr); |
219 | ||
220 | wxLogSysErrorHelper(wxSysErrorCode()); | |
c801d85f KB |
221 | } |
222 | ||
7502ba29 | 223 | void WXDLLEXPORT wxLogSysError(long lErrCode, const char *szFormat, ...) |
c801d85f | 224 | { |
9ef3052c | 225 | va_list argptr; |
7502ba29 VZ |
226 | va_start(argptr, szFormat); |
227 | vsprintf(s_szBuf, szFormat, argptr); | |
9ef3052c | 228 | va_end(argptr); |
c801d85f | 229 | |
9ef3052c | 230 | wxLogSysErrorHelper(lErrCode); |
c801d85f KB |
231 | } |
232 | ||
233 | // ---------------------------------------------------------------------------- | |
234 | // wxLog class implementation | |
235 | // ---------------------------------------------------------------------------- | |
236 | ||
237 | wxLog::wxLog() | |
238 | { | |
239 | m_bHasMessages = FALSE; | |
9ef3052c VZ |
240 | m_bVerbose = FALSE; |
241 | m_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; | |
c801d85f KB |
242 | } |
243 | ||
9ec05cc9 VZ |
244 | wxLog *wxLog::GetActiveTarget() |
245 | { | |
275bf4c1 | 246 | if ( ms_bAutoCreate && ms_pLogger == NULL ) { |
9ef3052c VZ |
247 | // prevent infinite recursion if someone calls wxLogXXX() from |
248 | // wxApp::CreateLogTarget() | |
275bf4c1 VZ |
249 | static bool s_bInGetActiveTarget = FALSE; |
250 | if ( !s_bInGetActiveTarget ) { | |
251 | s_bInGetActiveTarget = TRUE; | |
252 | ||
27fc802d | 253 | #ifdef wxUSE_NOGUI |
275bf4c1 VZ |
254 | ms_pLogger = new wxLogStderr; |
255 | #else | |
256 | // ask the application to create a log target for us | |
a7489b36 VZ |
257 | if ( wxTheApp != NULL ) |
258 | ms_pLogger = wxTheApp->CreateLogTarget(); | |
275bf4c1 | 259 | #endif |
c801d85f | 260 | |
c085e333 VZ |
261 | s_bInGetActiveTarget = FALSE; |
262 | ||
275bf4c1 VZ |
263 | // do nothing if it fails - what can we do? |
264 | } | |
c801d85f KB |
265 | } |
266 | ||
9ec05cc9 | 267 | return ms_pLogger; |
c801d85f KB |
268 | } |
269 | ||
c085e333 | 270 | wxLog *wxLog::SetActiveTarget(wxLog *pLogger) |
9ec05cc9 | 271 | { |
c085e333 VZ |
272 | if ( ms_pLogger != NULL ) { |
273 | // flush the old messages before changing because otherwise they might | |
274 | // get lost later if this target is not restored | |
c801d85f | 275 | ms_pLogger->Flush(); |
1c4a764c | 276 | } |
c801d85f | 277 | |
9ec05cc9 VZ |
278 | wxLog *pOldLogger = ms_pLogger; |
279 | ms_pLogger = pLogger; | |
c085e333 | 280 | |
9ec05cc9 | 281 | return pOldLogger; |
c801d85f KB |
282 | } |
283 | ||
fe7b1156 | 284 | wxString wxLog::TimeStamp() const |
c801d85f | 285 | { |
9ef3052c | 286 | wxString str; |
c801d85f | 287 | |
9ef3052c VZ |
288 | if ( !IsEmpty(m_szTimeFormat) ) { |
289 | char szBuf[128]; | |
290 | time_t timeNow; | |
291 | struct tm *ptmNow; | |
c801d85f | 292 | |
9ef3052c VZ |
293 | time(&timeNow); |
294 | ptmNow = localtime(&timeNow); | |
295 | ||
296 | strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow); | |
297 | str = szBuf; | |
298 | } | |
c801d85f | 299 | |
fe7b1156 VZ |
300 | return str; |
301 | } | |
302 | ||
303 | void wxLog::DoLog(wxLogLevel level, const char *szString) | |
304 | { | |
305 | // prepend a timestamp if not disabled | |
306 | wxString str = TimeStamp(); | |
307 | ||
c801d85f | 308 | switch ( level ) { |
9ef3052c | 309 | case wxLOG_FatalError: |
c801d85f KB |
310 | DoLogString(str << _("Fatal error: ") << szString); |
311 | DoLogString(_("Program aborted.")); | |
312 | Flush(); | |
313 | abort(); | |
314 | break; | |
315 | ||
9ef3052c | 316 | case wxLOG_Error: |
c801d85f KB |
317 | DoLogString(str << _("Error: ") << szString); |
318 | break; | |
319 | ||
9ef3052c | 320 | case wxLOG_Warning: |
c801d85f KB |
321 | DoLogString(str << _("Warning: ") << szString); |
322 | break; | |
323 | ||
9ef3052c | 324 | case wxLOG_Info: |
c801d85f | 325 | if ( GetVerbose() ) |
9ef3052c | 326 | case wxLOG_Message: |
c801d85f KB |
327 | DoLogString(str + szString); |
328 | // fall through | |
329 | ||
9ef3052c | 330 | case wxLOG_Status: |
c801d85f KB |
331 | // nothing to do |
332 | break; | |
333 | ||
9ef3052c VZ |
334 | case wxLOG_Trace: |
335 | case wxLOG_Debug: | |
b2aef89b | 336 | #ifdef __WXDEBUG__ |
7502ba29 | 337 | DoLogString(str << (level == wxLOG_Trace ? _("Trace") : _("Debug")) |
c801d85f KB |
338 | << ": " << szString); |
339 | #endif | |
9ec05cc9 | 340 | |
c801d85f KB |
341 | break; |
342 | ||
343 | default: | |
1a5a8367 | 344 | wxFAIL_MSG(_("unknown log level in wxLog::DoLog")); |
c801d85f KB |
345 | } |
346 | } | |
347 | ||
46dc76ba | 348 | void wxLog::DoLogString(const char *WXUNUSED(szString)) |
c801d85f | 349 | { |
1a5a8367 | 350 | wxFAIL_MSG(_("DoLogString must be overrided if it's called.")); |
c801d85f KB |
351 | } |
352 | ||
353 | void wxLog::Flush() | |
354 | { | |
355 | // do nothing | |
356 | } | |
357 | ||
358 | // ---------------------------------------------------------------------------- | |
359 | // wxLogStderr class implementation | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
362 | wxLogStderr::wxLogStderr(FILE *fp) | |
363 | { | |
364 | if ( fp == NULL ) | |
365 | m_fp = stderr; | |
366 | else | |
367 | m_fp = fp; | |
368 | } | |
369 | ||
370 | void wxLogStderr::DoLogString(const char *szString) | |
371 | { | |
372 | fputs(szString, m_fp); | |
373 | fputc('\n', m_fp); | |
374 | fflush(m_fp); | |
375 | } | |
376 | ||
377 | // ---------------------------------------------------------------------------- | |
378 | // wxLogStream implementation | |
379 | // ---------------------------------------------------------------------------- | |
380 | ||
381 | wxLogStream::wxLogStream(ostream *ostr) | |
382 | { | |
383 | if ( ostr == NULL ) | |
384 | m_ostr = &cerr; | |
385 | else | |
386 | m_ostr = ostr; | |
387 | } | |
388 | ||
389 | void wxLogStream::DoLogString(const char *szString) | |
390 | { | |
391 | (*m_ostr) << szString << endl << flush; | |
392 | } | |
393 | ||
03f38c58 | 394 | #ifndef wxUSE_NOGUI |
c801d85f KB |
395 | // ---------------------------------------------------------------------------- |
396 | // wxLogTextCtrl implementation | |
397 | // ---------------------------------------------------------------------------- | |
c801d85f | 398 | wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) |
9ef3052c | 399 | // @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf |
5de76427 JS |
400 | |
401 | // Also, in DLL mode in wxMSW, can't use it. | |
402 | #if defined(NO_TEXT_WINDOW_STREAM) | |
403 | #else | |
c801d85f | 404 | : wxLogStream(new ostream(pTextCtrl)) |
5de76427 | 405 | #endif |
c801d85f KB |
406 | { |
407 | } | |
408 | ||
409 | wxLogTextCtrl::~wxLogTextCtrl() | |
410 | { | |
c5c16a30 | 411 | delete m_ostr; |
c801d85f | 412 | } |
c801d85f KB |
413 | |
414 | // ---------------------------------------------------------------------------- | |
415 | // wxLogGui implementation | |
416 | // ---------------------------------------------------------------------------- | |
417 | ||
c801d85f KB |
418 | wxLogGui::wxLogGui() |
419 | { | |
420 | m_bErrors = FALSE; | |
421 | } | |
422 | ||
423 | void wxLogGui::Flush() | |
424 | { | |
425 | if ( !m_bHasMessages ) | |
426 | return; | |
427 | ||
d553992a VZ |
428 | // do it right now to block any new calls to Flush() while we're here |
429 | m_bHasMessages = FALSE; | |
c9dac664 | 430 | |
c801d85f | 431 | // @@@ ugly... |
9ec05cc9 | 432 | |
c801d85f KB |
433 | // concatenate all strings (but not too many to not overfill the msg box) |
434 | wxString str; | |
c86f1403 | 435 | size_t nLines = 0, |
c801d85f KB |
436 | nMsgCount = m_aMessages.Count(); |
437 | ||
438 | // start from the most recent message | |
c86f1403 | 439 | for ( size_t n = nMsgCount; n > 0; n-- ) { |
c801d85f | 440 | // for Windows strings longer than this value are wrapped (NT 4.0) |
c86f1403 | 441 | const size_t nMsgLineWidth = 156; |
c801d85f KB |
442 | |
443 | nLines += (m_aMessages[n - 1].Len() + nMsgLineWidth - 1) / nMsgLineWidth; | |
444 | ||
445 | if ( nLines > 25 ) // don't put too many lines in message box | |
446 | break; | |
447 | ||
448 | str << m_aMessages[n - 1] << "\n"; | |
449 | } | |
450 | ||
451 | if ( m_bErrors ) { | |
452 | wxMessageBox(str, _("Error"), wxOK | wxICON_EXCLAMATION); | |
453 | } | |
454 | else { | |
455 | wxMessageBox(str, _("Information"), wxOK | wxICON_INFORMATION); | |
456 | } | |
457 | ||
458 | // no undisplayed messages whatsoever | |
c801d85f KB |
459 | m_bErrors = FALSE; |
460 | m_aMessages.Empty(); | |
461 | } | |
462 | ||
463 | // the default behaviour is to discard all informational messages if there | |
464 | // are any errors/warnings. | |
9ef3052c | 465 | void wxLogGui::DoLog(wxLogLevel level, const char *szString) |
c801d85f KB |
466 | { |
467 | switch ( level ) { | |
9ef3052c | 468 | case wxLOG_Info: |
c801d85f | 469 | if ( GetVerbose() ) |
9ef3052c | 470 | case wxLOG_Message: |
c801d85f KB |
471 | if ( !m_bErrors ) { |
472 | m_aMessages.Add(szString); | |
473 | m_bHasMessages = TRUE; | |
474 | } | |
475 | break; | |
476 | ||
9ef3052c | 477 | case wxLOG_Status: |
c801d85f KB |
478 | { |
479 | // find the top window and set it's status text if it has any | |
470b7da3 VZ |
480 | wxFrame *pFrame = gs_pFrame; |
481 | if ( pFrame == NULL ) { | |
482 | wxWindow *pWin = wxTheApp->GetTopWindow(); | |
483 | if ( pWin != NULL && pWin->IsKindOf(CLASSINFO(wxFrame)) ) { | |
484 | pFrame = (wxFrame *)pWin; | |
485 | } | |
c801d85f | 486 | } |
470b7da3 VZ |
487 | |
488 | if ( pFrame != NULL ) | |
489 | pFrame->SetStatusText(szString); | |
c801d85f KB |
490 | } |
491 | break; | |
492 | ||
9ef3052c VZ |
493 | case wxLOG_Trace: |
494 | case wxLOG_Debug: | |
b2aef89b | 495 | #ifdef __WXDEBUG__ |
fe7b1156 VZ |
496 | { |
497 | wxString strTime = TimeStamp(); | |
498 | ||
34138703 | 499 | #if defined(__WIN32__) && !defined(__WXSTUBS__) |
fe7b1156 VZ |
500 | // don't prepend debug/trace here: it goes to the debug window |
501 | // anyhow, but do put a timestamp | |
502 | OutputDebugString(strTime + szString + "\n\r"); | |
503 | #else //!WIN32 | |
504 | // send them to stderr | |
505 | fprintf(stderr, "%s %s: %s\n", | |
506 | strTime.c_str(), | |
507 | level == wxLOG_Trace ? _("Trace") : _("Debug"), | |
508 | szString); | |
509 | fflush(stderr); | |
510 | #endif // WIN32 | |
511 | } | |
c801d85f KB |
512 | #endif |
513 | break; | |
514 | ||
9ef3052c | 515 | case wxLOG_FatalError: |
c801d85f | 516 | // show this one immediately |
7502ba29 | 517 | wxMessageBox(szString, _("Fatal error"), wxICON_HAND); |
c801d85f KB |
518 | break; |
519 | ||
9ef3052c VZ |
520 | case wxLOG_Error: |
521 | case wxLOG_Warning: | |
c801d85f KB |
522 | // discard earlier informational messages if this is the 1st error |
523 | if ( !m_bErrors ) { | |
524 | m_aMessages.Empty(); | |
525 | m_bHasMessages = TRUE; | |
526 | m_bErrors = TRUE; | |
527 | } | |
528 | ||
529 | m_aMessages.Add(szString); | |
530 | break; | |
9ec05cc9 | 531 | |
c801d85f | 532 | default: |
1a5a8367 | 533 | wxFAIL_MSG(_("unknown log level in wxLogGui::DoLog")); |
c801d85f KB |
534 | } |
535 | } | |
536 | ||
9ef3052c | 537 | // ---------------------------------------------------------------------------- |
fe7b1156 | 538 | // wxLogWindow and wxLogFrame implementation |
9ef3052c VZ |
539 | // ---------------------------------------------------------------------------- |
540 | ||
541 | // log frame class | |
fe7b1156 | 542 | // --------------- |
9ef3052c VZ |
543 | class wxLogFrame : public wxFrame |
544 | { | |
545 | public: | |
fe7b1156 | 546 | // ctor & dtor |
470b7da3 | 547 | wxLogFrame(wxFrame *pParent, wxLogWindow *log, const char *szTitle); |
fe7b1156 | 548 | virtual ~wxLogFrame(); |
9ef3052c VZ |
549 | |
550 | // menu callbacks | |
551 | void OnClose(wxCommandEvent& event); | |
5260b1c5 | 552 | void OnCloseWindow(wxCloseEvent& event); |
9ef3052c VZ |
553 | void OnSave (wxCommandEvent& event); |
554 | void OnClear(wxCommandEvent& event); | |
555 | ||
fe7b1156 VZ |
556 | void OnIdle(wxIdleEvent&); |
557 | ||
9ef3052c VZ |
558 | // accessors |
559 | wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } | |
560 | ||
561 | private: | |
562 | enum | |
563 | { | |
564 | Menu_Close = 100, | |
565 | Menu_Save, | |
566 | Menu_Clear | |
567 | }; | |
568 | ||
fe7b1156 VZ |
569 | // instead of closing just hide the window to be able to Show() it later |
570 | void DoClose() { Show(FALSE); } | |
571 | ||
572 | wxTextCtrl *m_pTextCtrl; | |
573 | wxLogWindow *m_log; | |
9ef3052c VZ |
574 | |
575 | DECLARE_EVENT_TABLE() | |
576 | }; | |
577 | ||
578 | BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) | |
579 | // wxLogWindow menu events | |
580 | EVT_MENU(Menu_Close, wxLogFrame::OnClose) | |
581 | EVT_MENU(Menu_Save, wxLogFrame::OnSave) | |
582 | EVT_MENU(Menu_Clear, wxLogFrame::OnClear) | |
583 | ||
5260b1c5 | 584 | EVT_CLOSE(wxLogFrame::OnCloseWindow) |
9ec05cc9 | 585 | END_EVENT_TABLE() |
9ef3052c | 586 | |
470b7da3 VZ |
587 | wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const char *szTitle) |
588 | : wxFrame(pParent, -1, szTitle) | |
9ef3052c | 589 | { |
fe7b1156 | 590 | m_log = log; |
f42d2625 | 591 | |
9ef3052c | 592 | // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating |
fe7b1156 | 593 | // a rich edit control instead of a normal one we want in wxMSW |
9ef3052c VZ |
594 | m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, |
595 | wxDefaultSize, | |
470b7da3 | 596 | //wxSIMPLE_BORDER | |
9ef3052c VZ |
597 | wxTE_MULTILINE | |
598 | wxHSCROLL | | |
599 | wxTE_READONLY); | |
9ef3052c VZ |
600 | |
601 | // create menu | |
602 | wxMenuBar *pMenuBar = new wxMenuBar; | |
603 | wxMenu *pMenu = new wxMenu; | |
fe7b1156 VZ |
604 | pMenu->Append(Menu_Save, _("&Save..."), _("Save log contents to file")); |
605 | pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents")); | |
9ef3052c | 606 | pMenu->AppendSeparator(); |
fe7b1156 | 607 | pMenu->Append(Menu_Close, _("&Close"), _("Close this window")); |
1a5a8367 | 608 | pMenuBar->Append(pMenu, _("&Log")); |
9ef3052c VZ |
609 | SetMenuBar(pMenuBar); |
610 | ||
fe7b1156 VZ |
611 | // status bar for menu prompts |
612 | CreateStatusBar(); | |
613 | ||
614 | m_log->OnFrameCreate(this); | |
9ef3052c VZ |
615 | } |
616 | ||
46dc76ba | 617 | void wxLogFrame::OnClose(wxCommandEvent& WXUNUSED(event)) |
9ef3052c | 618 | { |
fe7b1156 | 619 | DoClose(); |
9ef3052c VZ |
620 | } |
621 | ||
46dc76ba | 622 | void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
5260b1c5 | 623 | { |
fe7b1156 VZ |
624 | DoClose(); |
625 | } | |
626 | ||
46dc76ba | 627 | void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
628 | { |
629 | // get the file name | |
630 | // ----------------- | |
631 | const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt"); | |
632 | if ( szFileName == NULL ) { | |
633 | // cancelled | |
634 | return; | |
635 | } | |
636 | ||
637 | // open file | |
638 | // --------- | |
639 | wxFile file; | |
c9dac664 | 640 | bool bOk = FALSE; |
9ef3052c | 641 | if ( wxFile::Exists(szFileName) ) { |
46dc76ba | 642 | bool bAppend = FALSE; |
9ef3052c VZ |
643 | wxString strMsg; |
644 | strMsg.Printf(_("Append log to file '%s' " | |
645 | "(choosing [No] will overwrite it)?"), szFileName); | |
1a5a8367 | 646 | switch ( wxMessageBox(strMsg, _("Question"), wxYES_NO | wxCANCEL) ) { |
9ef3052c VZ |
647 | case wxYES: |
648 | bAppend = TRUE; | |
649 | break; | |
650 | ||
651 | case wxNO: | |
652 | bAppend = FALSE; | |
653 | break; | |
654 | ||
655 | case wxCANCEL: | |
656 | return; | |
657 | ||
658 | default: | |
1a5a8367 | 659 | wxFAIL_MSG(_("invalid message box return value")); |
9ef3052c VZ |
660 | } |
661 | ||
662 | if ( bAppend ) { | |
663 | bOk = file.Open(szFileName, wxFile::write_append); | |
664 | } | |
665 | else { | |
666 | bOk = file.Create(szFileName, TRUE /* overwrite */); | |
667 | } | |
668 | } | |
669 | else { | |
670 | bOk = file.Create(szFileName); | |
671 | } | |
672 | ||
673 | // retrieve text and save it | |
674 | // ------------------------- | |
2049ba38 | 675 | #ifdef __WXGTK__ |
9ef3052c | 676 | // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet |
1a5a8367 | 677 | wxLogError(_("Sorry, this function is not implemented under GTK")); |
f42d2625 | 678 | #else |
9ef3052c VZ |
679 | int nLines = m_pTextCtrl->GetNumberOfLines(); |
680 | for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { | |
681 | bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL()); | |
682 | } | |
683 | #endif //GTK | |
9ec05cc9 | 684 | |
9ef3052c VZ |
685 | if ( bOk ) |
686 | bOk = file.Close(); | |
687 | ||
688 | if ( !bOk ) { | |
7502ba29 | 689 | wxLogError(_("Can't save log contents to file.")); |
9ef3052c VZ |
690 | return; |
691 | } | |
692 | } | |
693 | ||
46dc76ba | 694 | void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event)) |
9ef3052c VZ |
695 | { |
696 | m_pTextCtrl->Clear(); | |
697 | } | |
698 | ||
fe7b1156 VZ |
699 | wxLogFrame::~wxLogFrame() |
700 | { | |
701 | m_log->OnFrameDelete(this); | |
702 | } | |
703 | ||
704 | // wxLogWindow | |
705 | // ----------- | |
470b7da3 VZ |
706 | wxLogWindow::wxLogWindow(wxFrame *pParent, |
707 | const char *szTitle, | |
708 | bool bShow, | |
709 | bool bDoPass) | |
9ef3052c | 710 | { |
a3622daa VZ |
711 | m_bPassMessages = bDoPass; |
712 | ||
470b7da3 | 713 | m_pLogFrame = new wxLogFrame(pParent, this, szTitle); |
a3622daa | 714 | m_pOldLog = wxLog::SetActiveTarget(this); |
9ec05cc9 | 715 | |
f42d2625 VZ |
716 | if ( bShow ) |
717 | m_pLogFrame->Show(TRUE); | |
9ef3052c VZ |
718 | } |
719 | ||
e51c4943 | 720 | void wxLogWindow::Show(bool bShow) |
9ef3052c VZ |
721 | { |
722 | m_pLogFrame->Show(bShow); | |
723 | } | |
724 | ||
fe7b1156 | 725 | void wxLogWindow::Flush() |
06db8ebd | 726 | { |
fe7b1156 VZ |
727 | if ( m_pOldLog != NULL ) |
728 | m_pOldLog->Flush(); | |
729 | ||
c9dac664 | 730 | m_bHasMessages = FALSE; |
06db8ebd VZ |
731 | } |
732 | ||
9ef3052c VZ |
733 | void wxLogWindow::DoLog(wxLogLevel level, const char *szString) |
734 | { | |
735 | // first let the previous logger show it | |
a3622daa | 736 | if ( m_pOldLog != NULL && m_bPassMessages ) { |
9ec05cc9 | 737 | // @@@ why can't we access protected wxLog method from here (we derive |
9ef3052c VZ |
738 | // from wxLog)? gcc gives "DoLog is protected in this context", what |
739 | // does this mean? Anyhow, the cast is harmless and let's us do what | |
740 | // we want. | |
741 | ((wxLogWindow *)m_pOldLog)->DoLog(level, szString); | |
742 | } | |
9ec05cc9 | 743 | |
1c4a764c VZ |
744 | if ( m_pLogFrame ) { |
745 | switch ( level ) { | |
746 | case wxLOG_Status: | |
747 | // by default, these messages are ignored by wxLog, so process | |
748 | // them ourselves | |
749 | { | |
750 | wxString str = TimeStamp(); | |
751 | str << _("Status: ") << szString; | |
752 | DoLogString(str); | |
753 | } | |
754 | break; | |
755 | ||
756 | // don't put trace messages in the text window for 2 reasons: | |
757 | // 1) there are too many of them | |
758 | // 2) they may provoke other trace messages thus sending a program | |
759 | // into an infinite loop | |
760 | case wxLOG_Trace: | |
761 | break; | |
762 | ||
763 | default: | |
764 | // and this will format it nicely and call our DoLogString() | |
765 | wxLog::DoLog(level, szString); | |
766 | } | |
3b926128 | 767 | } |
fe7b1156 VZ |
768 | |
769 | m_bHasMessages = TRUE; | |
9ef3052c VZ |
770 | } |
771 | ||
772 | void wxLogWindow::DoLogString(const char *szString) | |
773 | { | |
774 | // put the text into our window | |
775 | wxTextCtrl *pText = m_pLogFrame->TextCtrl(); | |
776 | ||
777 | // remove selection (WriteText is in fact ReplaceSelection) | |
2049ba38 | 778 | #ifdef __WXMSW__ |
f42d2625 VZ |
779 | long nLen = pText->GetLastPosition(); |
780 | pText->SetSelection(nLen, nLen); | |
781 | #endif // Windows | |
9ef3052c VZ |
782 | |
783 | pText->WriteText(szString); | |
784 | pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n") | |
785 | ||
c085e333 | 786 | // TODO ensure that the line can be seen |
9ef3052c VZ |
787 | } |
788 | ||
fe7b1156 VZ |
789 | wxFrame *wxLogWindow::GetFrame() const |
790 | { | |
791 | return m_pLogFrame; | |
792 | } | |
793 | ||
8fdca65c | 794 | void wxLogWindow::OnFrameCreate(wxFrame *WXUNUSED(frame)) |
fe7b1156 VZ |
795 | { |
796 | } | |
797 | ||
8fdca65c | 798 | void wxLogWindow::OnFrameDelete(wxFrame *WXUNUSED(frame)) |
fe7b1156 | 799 | { |
c085e333 | 800 | m_pLogFrame = (wxLogFrame *)NULL; |
fe7b1156 VZ |
801 | } |
802 | ||
9ef3052c VZ |
803 | wxLogWindow::~wxLogWindow() |
804 | { | |
c085e333 VZ |
805 | delete m_pOldLog; |
806 | ||
fe7b1156 VZ |
807 | // may be NULL if log frame already auto destroyed itself |
808 | delete m_pLogFrame; | |
9ef3052c VZ |
809 | } |
810 | ||
27fc802d | 811 | #endif //wxUSE_NOGUI |
c801d85f KB |
812 | |
813 | // ============================================================================ | |
814 | // Global functions/variables | |
815 | // ============================================================================ | |
816 | ||
817 | // ---------------------------------------------------------------------------- | |
818 | // static variables | |
819 | // ---------------------------------------------------------------------------- | |
c67daf87 | 820 | wxLog *wxLog::ms_pLogger = (wxLog *) NULL; |
c085e333 | 821 | bool wxLog::ms_doLog = TRUE; |
275bf4c1 | 822 | bool wxLog::ms_bAutoCreate = TRUE; |
88f2aa37 | 823 | wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0; |
c801d85f KB |
824 | |
825 | // ---------------------------------------------------------------------------- | |
826 | // stdout error logging helper | |
827 | // ---------------------------------------------------------------------------- | |
828 | ||
829 | // helper function: wraps the message and justifies it under given position | |
830 | // (looks more pretty on the terminal). Also adds newline at the end. | |
831 | // | |
832 | // @@ this is now disabled until I find a portable way of determining the | |
9ef3052c | 833 | // terminal window size (ok, I found it but does anybody really cares?) |
c801d85f KB |
834 | #ifdef LOG_PRETTY_WRAP |
835 | static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) | |
836 | { | |
837 | size_t nMax = 80; // @@@@ | |
838 | size_t nStart = strlen(pszPrefix); | |
839 | fputs(pszPrefix, f); | |
840 | ||
841 | size_t n; | |
842 | while ( *psz != '\0' ) { | |
843 | for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ ) | |
844 | putc(*psz++, f); | |
845 | ||
846 | // wrapped? | |
847 | if ( *psz != '\0' ) { | |
848 | /*putc('\n', f);*/ | |
849 | for ( n = 0; n < nStart; n++ ) | |
850 | putc(' ', f); | |
851 | ||
852 | // as we wrapped, squeeze all white space | |
853 | while ( isspace(*psz) ) | |
854 | psz++; | |
855 | } | |
856 | } | |
857 | ||
858 | putc('\n', f); | |
859 | } | |
860 | #endif //LOG_PRETTY_WRAP | |
861 | ||
862 | // ---------------------------------------------------------------------------- | |
863 | // error code/error message retrieval functions | |
864 | // ---------------------------------------------------------------------------- | |
865 | ||
866 | // get error code from syste | |
867 | unsigned long wxSysErrorCode() | |
868 | { | |
2049ba38 | 869 | #ifdef __WXMSW__ |
c801d85f KB |
870 | #ifdef __WIN32__ |
871 | return ::GetLastError(); | |
872 | #else //WIN16 | |
873 | // @@@@ what to do on Windows 3.1? | |
874 | return 0; | |
875 | #endif //WIN16/32 | |
876 | #else //Unix | |
877 | return errno; | |
878 | #endif //Win/Unix | |
879 | } | |
880 | ||
881 | // get error message from system | |
882 | const char *wxSysErrorMsg(unsigned long nErrCode) | |
883 | { | |
884 | if ( nErrCode == 0 ) | |
885 | nErrCode = wxSysErrorCode(); | |
886 | ||
2049ba38 | 887 | #ifdef __WXMSW__ |
9ef3052c VZ |
888 | #ifdef __WIN32__ |
889 | static char s_szBuf[LOG_BUFFER_SIZE / 2]; | |
890 | ||
891 | // get error message from system | |
892 | LPVOID lpMsgBuf; | |
893 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | |
9ec05cc9 | 894 | NULL, nErrCode, |
9ef3052c VZ |
895 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
896 | (LPTSTR)&lpMsgBuf, | |
897 | 0, NULL); | |
898 | ||
899 | // copy it to our buffer and free memory | |
900 | strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); | |
901 | s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; | |
902 | LocalFree(lpMsgBuf); | |
903 | ||
904 | // returned string is capitalized and ended with '\r\n' - bad | |
81d66cf3 | 905 | s_szBuf[0] = (char)wxToLower(s_szBuf[0]); |
9ef3052c VZ |
906 | size_t len = strlen(s_szBuf); |
907 | if ( len > 0 ) { | |
908 | // truncate string | |
909 | if ( s_szBuf[len - 2] == '\r' ) | |
910 | s_szBuf[len - 2] = '\0'; | |
911 | } | |
c801d85f | 912 | |
9ef3052c VZ |
913 | return s_szBuf; |
914 | #else //Win16 | |
915 | // TODO @@@@ | |
916 | return NULL; | |
917 | #endif // Win16/32 | |
918 | #else // Unix | |
919 | return strerror(nErrCode); | |
920 | #endif // Win/Unix | |
c801d85f KB |
921 | } |
922 | ||
923 | // ---------------------------------------------------------------------------- | |
924 | // debug helper | |
925 | // ---------------------------------------------------------------------------- | |
926 | ||
b2aef89b | 927 | #ifdef __WXDEBUG__ |
c801d85f | 928 | |
7502ba29 VZ |
929 | void Trap() |
930 | { | |
931 | #ifdef __WXMSW__ | |
932 | DebugBreak(); | |
34138703 JS |
933 | #elif defined(__WXSTUBS__) |
934 | // TODO | |
7502ba29 VZ |
935 | #else // Unix |
936 | raise(SIGTRAP); | |
937 | #endif // Win/Unix | |
938 | } | |
939 | ||
c801d85f KB |
940 | // this function is called when an assert fails |
941 | void wxOnAssert(const char *szFile, int nLine, const char *szMsg) | |
942 | { | |
943 | // this variable can be set to true to suppress "assert failure" messages | |
7502ba29 VZ |
944 | static bool s_bNoAsserts = FALSE; |
945 | static bool s_bInAssert = FALSE; | |
946 | ||
947 | if ( s_bInAssert ) { | |
948 | // He-e-e-e-elp!! we're trapped in endless loop | |
949 | Trap(); | |
c085e333 VZ |
950 | |
951 | return; | |
7502ba29 VZ |
952 | } |
953 | ||
954 | s_bInAssert = TRUE; | |
c801d85f KB |
955 | |
956 | char szBuf[LOG_BUFFER_SIZE]; | |
957 | sprintf(szBuf, _("Assert failed in file %s at line %d"), szFile, nLine); | |
958 | if ( szMsg != NULL ) { | |
959 | strcat(szBuf, ": "); | |
960 | strcat(szBuf, szMsg); | |
961 | } | |
962 | else { | |
963 | strcat(szBuf, "."); | |
964 | } | |
965 | ||
3078c3a6 | 966 | if ( !s_bNoAsserts ) { |
9ec05cc9 VZ |
967 | // send it to the normal log destination |
968 | wxLogDebug(szBuf); | |
969 | ||
03f38c58 VZ |
970 | #ifdef wxUSE_NOGUI |
971 | Trap(); | |
972 | #else | |
973 | strcat(szBuf, _("\nDo you want to stop the program?" | |
974 | "\nYou can also choose [Cancel] to suppress " | |
975 | "further warnings.")); | |
3078c3a6 | 976 | |
03f38c58 VZ |
977 | switch ( wxMessageBox(szBuf, _("Debug"), |
978 | wxYES_NO | wxCANCEL | wxICON_STOP ) ) { | |
979 | case wxYES: | |
980 | Trap(); | |
981 | break; | |
c801d85f | 982 | |
03f38c58 VZ |
983 | case wxCANCEL: |
984 | s_bNoAsserts = TRUE; | |
985 | break; | |
9ec05cc9 | 986 | |
03f38c58 VZ |
987 | //case wxNO: nothing to do |
988 | } | |
989 | #endif // USE_NOGUI | |
3078c3a6 | 990 | } |
7502ba29 VZ |
991 | |
992 | s_bInAssert = FALSE; | |
c801d85f KB |
993 | } |
994 | ||
b2aef89b | 995 | #endif //WXDEBUG |
c801d85f | 996 |