]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
fix wxTimeSpan::Format() for negative spans with 0 hour component (#10055)
[wxWidgets.git] / src / common / log.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LOG
28
29 // wxWidgets
30 #ifndef WX_PRECOMP
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/arrstr.h"
34 #include "wx/intl.h"
35 #include "wx/string.h"
36 #include "wx/utils.h"
37 #endif //WX_PRECOMP
38
39 #include "wx/apptrait.h"
40 #include "wx/datetime.h"
41 #include "wx/file.h"
42 #include "wx/msgout.h"
43 #include "wx/textfile.h"
44 #include "wx/thread.h"
45 #include "wx/crt.h"
46
47 // other standard headers
48 #ifndef __WXWINCE__
49 #include <errno.h>
50 #endif
51
52 #include <stdlib.h>
53
54 #ifndef __WXPALMOS5__
55 #ifndef __WXWINCE__
56 #include <time.h>
57 #else
58 #include "wx/msw/wince/time.h"
59 #endif
60 #endif /* ! __WXPALMOS5__ */
61
62 #if defined(__WINDOWS__)
63 #include "wx/msw/private.h" // includes windows.h
64 #endif
65
66 #if wxUSE_THREADS
67
68 // define static functions providing access to the critical sections we use
69 // instead of just using static critical section variables as log functions may
70 // be used during static initialization and while this is certainly not
71 // advisable it's still better to not crash (as we'd do if we used a yet
72 // uninitialized critical section) if it happens
73
74 static inline wxCriticalSection& GetTraceMaskCS()
75 {
76 static wxCriticalSection s_csTrace;
77
78 return s_csTrace;
79 }
80
81 static inline wxCriticalSection& GetPreviousLogCS()
82 {
83 static wxCriticalSection s_csPrev;
84
85 return s_csPrev;
86 }
87
88 #endif // wxUSE_THREADS
89
90 // ----------------------------------------------------------------------------
91 // non member functions
92 // ----------------------------------------------------------------------------
93
94 // define this to enable wrapping of log messages
95 //#define LOG_PRETTY_WRAP
96
97 #ifdef LOG_PRETTY_WRAP
98 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
99 #endif
100
101 // ============================================================================
102 // implementation
103 // ============================================================================
104
105 // ----------------------------------------------------------------------------
106 // implementation of Log functions
107 //
108 // NB: unfortunately we need all these distinct functions, we can't make them
109 // macros and not all compilers inline vararg functions.
110 // ----------------------------------------------------------------------------
111
112 // generic log function
113 void wxVLogGeneric(wxLogLevel level, const wxString& format, va_list argptr)
114 {
115 if ( wxLog::IsEnabled() ) {
116 wxLog::OnLog(level, wxString::FormatV(format, argptr), time(NULL));
117 }
118 }
119
120 #if !wxUSE_UTF8_LOCALE_ONLY
121 void wxDoLogGenericWchar(wxLogLevel level, const wxChar *format, ...)
122 {
123 va_list argptr;
124 va_start(argptr, format);
125 wxVLogGeneric(level, format, argptr);
126 va_end(argptr);
127 }
128 #endif // wxUSE_UTF8_LOCALE_ONLY
129
130 #if wxUSE_UNICODE_UTF8
131 void wxDoLogGenericUtf8(wxLogLevel level, const char *format, ...)
132 {
133 va_list argptr;
134 va_start(argptr, format);
135 wxVLogGeneric(level, format, argptr);
136 va_end(argptr);
137 }
138 #endif // wxUSE_UNICODE_UTF8
139
140 #if !wxUSE_UTF8_LOCALE_ONLY
141 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
142 void wxDoLog##level##Wchar(const wxChar *format, ...) \
143 { \
144 va_list argptr; \
145 va_start(argptr, format); \
146 wxVLog##level(format, argptr); \
147 va_end(argptr); \
148 }
149 #else
150 #define IMPLEMENT_LOG_FUNCTION_WCHAR(level)
151 #endif
152
153 #if wxUSE_UNICODE_UTF8
154 #define IMPLEMENT_LOG_FUNCTION_UTF8(level) \
155 void wxDoLog##level##Utf8(const char *format, ...) \
156 { \
157 va_list argptr; \
158 va_start(argptr, format); \
159 wxVLog##level(format, argptr); \
160 va_end(argptr); \
161 }
162 #else
163 #define IMPLEMENT_LOG_FUNCTION_UTF8(level)
164 #endif
165
166 #define IMPLEMENT_LOG_FUNCTION(level) \
167 void wxVLog##level(const wxString& format, va_list argptr) \
168 { \
169 if ( wxLog::IsEnabled() ) { \
170 wxLog::OnLog(wxLOG_##level, \
171 wxString::FormatV(format, argptr), time(NULL)); \
172 } \
173 } \
174 IMPLEMENT_LOG_FUNCTION_WCHAR(level) \
175 IMPLEMENT_LOG_FUNCTION_UTF8(level)
176
177 IMPLEMENT_LOG_FUNCTION(Error)
178 IMPLEMENT_LOG_FUNCTION(Warning)
179 IMPLEMENT_LOG_FUNCTION(Message)
180 IMPLEMENT_LOG_FUNCTION(Info)
181 IMPLEMENT_LOG_FUNCTION(Status)
182
183 void wxSafeShowMessage(const wxString& title, const wxString& text)
184 {
185 #ifdef __WINDOWS__
186 ::MessageBox(NULL, text.t_str(), title.t_str(), MB_OK | MB_ICONSTOP);
187 #else
188 wxFprintf(stderr, wxS("%s: %s\n"), title.c_str(), text.c_str());
189 fflush(stderr);
190 #endif
191 }
192
193 // fatal errors can't be suppressed nor handled by the custom log target and
194 // always terminate the program
195 void wxVLogFatalError(const wxString& format, va_list argptr)
196 {
197 wxSafeShowMessage(wxS("Fatal Error"), wxString::FormatV(format, argptr));
198
199 #ifdef __WXWINCE__
200 ExitThread(3);
201 #else
202 abort();
203 #endif
204 }
205
206 #if !wxUSE_UTF8_LOCALE_ONLY
207 void wxDoLogFatalErrorWchar(const wxChar *format, ...)
208 {
209 va_list argptr;
210 va_start(argptr, format);
211 wxVLogFatalError(format, argptr);
212
213 // some compilers warn about unreachable code and it shouldn't matter
214 // for the others anyhow...
215 //va_end(argptr);
216 }
217 #endif // wxUSE_UTF8_LOCALE_ONLY
218
219 #if wxUSE_UNICODE_UTF8
220 void wxDoLogFatalErrorUtf8(const char *format, ...)
221 {
222 va_list argptr;
223 va_start(argptr, format);
224 wxVLogFatalError(format, argptr);
225
226 // some compilers warn about unreachable code and it shouldn't matter
227 // for the others anyhow...
228 //va_end(argptr);
229 }
230 #endif // wxUSE_UNICODE_UTF8
231
232 // same as info, but only if 'verbose' mode is on
233 void wxVLogVerbose(const wxString& format, va_list argptr)
234 {
235 if ( wxLog::IsEnabled() ) {
236 if ( wxLog::GetActiveTarget() != NULL && wxLog::GetVerbose() ) {
237 wxLog::OnLog(wxLOG_Info,
238 wxString::FormatV(format, argptr), time(NULL));
239 }
240 }
241 }
242
243 #if !wxUSE_UTF8_LOCALE_ONLY
244 void wxDoLogVerboseWchar(const wxChar *format, ...)
245 {
246 va_list argptr;
247 va_start(argptr, format);
248 wxVLogVerbose(format, argptr);
249 va_end(argptr);
250 }
251 #endif // !wxUSE_UTF8_LOCALE_ONLY
252
253 #if wxUSE_UNICODE_UTF8
254 void wxDoLogVerboseUtf8(const char *format, ...)
255 {
256 va_list argptr;
257 va_start(argptr, format);
258 wxVLogVerbose(format, argptr);
259 va_end(argptr);
260 }
261 #endif // wxUSE_UNICODE_UTF8
262
263 // debug functions
264 #ifdef __WXDEBUG__
265
266 #if !wxUSE_UTF8_LOCALE_ONLY
267 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
268 void wxDoLog##level##Wchar(const wxChar *format, ...) \
269 { \
270 va_list argptr; \
271 va_start(argptr, format); \
272 wxVLog##level(format, argptr); \
273 va_end(argptr); \
274 }
275 #else
276 #define IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level)
277 #endif
278
279 #if wxUSE_UNICODE_UTF8
280 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level) \
281 void wxDoLog##level##Utf8(const char *format, ...) \
282 { \
283 va_list argptr; \
284 va_start(argptr, format); \
285 wxVLog##level(format, argptr); \
286 va_end(argptr); \
287 }
288 #else
289 #define IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
290 #endif
291
292 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
293 void wxVLog##level(const wxString& format, va_list argptr) \
294 { \
295 if ( wxLog::IsEnabled() ) { \
296 wxLog::OnLog(wxLOG_##level, \
297 wxString::FormatV(format, argptr), time(NULL)); \
298 } \
299 } \
300 IMPLEMENT_LOG_DEBUG_FUNCTION_WCHAR(level) \
301 IMPLEMENT_LOG_DEBUG_FUNCTION_UTF8(level)
302
303
304 void wxVLogTrace(const wxString& mask, const wxString& format, va_list argptr)
305 {
306 if ( wxLog::IsEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
307 wxString msg;
308 msg << wxS("(") << mask << wxS(") ") << wxString::FormatV(format, argptr);
309
310 wxLog::OnLog(wxLOG_Trace, msg, time(NULL));
311 }
312 }
313
314 #if !wxUSE_UTF8_LOCALE_ONLY
315 void wxDoLogTraceWchar(const wxString& mask, const wxChar *format, ...)
316 {
317 va_list argptr;
318 va_start(argptr, format);
319 wxVLogTrace(mask, format, argptr);
320 va_end(argptr);
321 }
322 #endif // !wxUSE_UTF8_LOCALE_ONLY
323
324 #if wxUSE_UNICODE_UTF8
325 void wxDoLogTraceUtf8(const wxString& mask, const char *format, ...)
326 {
327 va_list argptr;
328 va_start(argptr, format);
329 wxVLogTrace(mask, format, argptr);
330 va_end(argptr);
331 }
332 #endif // wxUSE_UNICODE_UTF8
333
334 void wxVLogTrace(wxTraceMask mask, const wxString& format, va_list argptr)
335 {
336 // we check that all of mask bits are set in the current mask, so
337 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
338 // if both bits are set.
339 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
340 wxLog::OnLog(wxLOG_Trace, wxString::FormatV(format, argptr), time(NULL));
341 }
342 }
343
344 #if !wxUSE_UTF8_LOCALE_ONLY
345 void wxDoLogTraceWchar(wxTraceMask mask, const wxChar *format, ...)
346 {
347 va_list argptr;
348 va_start(argptr, format);
349 wxVLogTrace(mask, format, argptr);
350 va_end(argptr);
351 }
352 #endif // !wxUSE_UTF8_LOCALE_ONLY
353
354 #if wxUSE_UNICODE_UTF8
355 void wxDoLogTraceUtf8(wxTraceMask mask, const char *format, ...)
356 {
357 va_list argptr;
358 va_start(argptr, format);
359 wxVLogTrace(mask, format, argptr);
360 va_end(argptr);
361 }
362 #endif // wxUSE_UNICODE_UTF8
363
364 #ifdef __WATCOMC__
365 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
366 void wxDoLogTraceWchar(int mask, const wxChar *format, ...)
367 {
368 va_list argptr;
369 va_start(argptr, format);
370 wxVLogTrace(mask, format, argptr);
371 va_end(argptr);
372 }
373
374 void wxDoLogTraceWchar(const char *mask, const wxChar *format, ...)
375 {
376 va_list argptr;
377 va_start(argptr, format);
378 wxVLogTrace(mask, format, argptr);
379 va_end(argptr);
380 }
381
382 void wxDoLogTraceWchar(const wchar_t *mask, const wxChar *format, ...)
383 {
384 va_list argptr;
385 va_start(argptr, format);
386 wxVLogTrace(mask, format, argptr);
387 va_end(argptr);
388 }
389
390 void wxVLogTrace(int mask, const wxString& format, va_list argptr)
391 { wxVLogTrace((wxTraceMask)mask, format, argptr); }
392 void wxVLogTrace(const char *mask, const wxString& format, va_list argptr)
393 { wxVLogTrace(wxString(mask), format, argptr); }
394 void wxVLogTrace(const wchar_t *mask, const wxString& format, va_list argptr)
395 { wxVLogTrace(wxString(mask), format, argptr); }
396 #endif // __WATCOMC__
397
398 #else // release
399 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
400 #endif
401
402 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
403 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
404
405 // wxLogSysError: one uses the last error code, for other you must give it
406 // explicitly
407
408 // return the system error message description
409 static inline wxString wxLogSysErrorHelper(long err)
410 {
411 return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
412 }
413
414 void WXDLLIMPEXP_BASE wxVLogSysError(const wxString& format, va_list argptr)
415 {
416 wxVLogSysError(wxSysErrorCode(), format, argptr);
417 }
418
419 #if !wxUSE_UTF8_LOCALE_ONLY
420 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(const wxChar *format, ...)
421 {
422 va_list argptr;
423 va_start(argptr, format);
424 wxVLogSysError(format, argptr);
425 va_end(argptr);
426 }
427 #endif // !wxUSE_UTF8_LOCALE_ONLY
428
429 #if wxUSE_UNICODE_UTF8
430 void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(const char *format, ...)
431 {
432 va_list argptr;
433 va_start(argptr, format);
434 wxVLogSysError(format, argptr);
435 va_end(argptr);
436 }
437 #endif // wxUSE_UNICODE_UTF8
438
439 void WXDLLIMPEXP_BASE wxVLogSysError(long err, const wxString& format, va_list argptr)
440 {
441 if ( wxLog::IsEnabled() ) {
442 wxLog::OnLog(wxLOG_Error,
443 wxString::FormatV(format, argptr) + wxLogSysErrorHelper(err),
444 time(NULL));
445 }
446 }
447
448 #if !wxUSE_UTF8_LOCALE_ONLY
449 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(long lErrCode, const wxChar *format, ...)
450 {
451 va_list argptr;
452 va_start(argptr, format);
453 wxVLogSysError(lErrCode, format, argptr);
454 va_end(argptr);
455 }
456 #endif // !wxUSE_UTF8_LOCALE_ONLY
457
458 #if wxUSE_UNICODE_UTF8
459 void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(long lErrCode, const char *format, ...)
460 {
461 va_list argptr;
462 va_start(argptr, format);
463 wxVLogSysError(lErrCode, format, argptr);
464 va_end(argptr);
465 }
466 #endif // wxUSE_UNICODE_UTF8
467
468 #ifdef __WATCOMC__
469 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
470 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(unsigned long lErrCode, const wxChar *format, ...)
471 {
472 va_list argptr;
473 va_start(argptr, format);
474 wxVLogSysError(lErrCode, format, argptr);
475 va_end(argptr);
476 }
477
478 void WXDLLIMPEXP_BASE wxVLogSysError(unsigned long err, const wxString& format, va_list argptr)
479 { wxVLogSysError((long)err, format, argptr); }
480 #endif // __WATCOMC__
481
482 // ----------------------------------------------------------------------------
483 // wxLog class implementation
484 // ----------------------------------------------------------------------------
485
486 unsigned wxLog::LogLastRepeatIfNeeded()
487 {
488 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
489
490 return LogLastRepeatIfNeededUnlocked();
491 }
492
493 unsigned wxLog::LogLastRepeatIfNeededUnlocked()
494 {
495 const unsigned count = ms_prevCounter;
496
497 if ( ms_prevCounter )
498 {
499 wxString msg;
500 #if wxUSE_INTL
501 msg.Printf(wxPLURAL("The previous message repeated once.",
502 "The previous message repeated %lu times.",
503 ms_prevCounter),
504 ms_prevCounter);
505 #else
506 msg.Printf(wxS("The previous message was repeated %lu times."),
507 ms_prevCounter);
508 #endif
509 ms_prevCounter = 0;
510 ms_prevString.clear();
511 DoLog(ms_prevLevel, msg, ms_prevTimeStamp);
512 }
513
514 return count;
515 }
516
517 wxLog::~wxLog()
518 {
519 // Flush() must be called before destroying the object as otherwise some
520 // messages could be lost
521 if ( ms_prevCounter )
522 {
523 wxMessageOutputDebug().Printf
524 (
525 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
526 ms_prevString,
527 ms_prevCounter
528 );
529 }
530 }
531
532 /* static */
533 void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
534 {
535 if ( IsEnabled() && ms_logLevel >= level )
536 {
537 wxLog *pLogger = GetActiveTarget();
538 if ( pLogger )
539 {
540 if ( GetRepetitionCounting() )
541 {
542 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
543
544 if ( szString == ms_prevString )
545 {
546 ms_prevCounter++;
547
548 // nothing else to do, in particular, don't log the
549 // repeated message
550 return;
551 }
552
553 pLogger->LogLastRepeatIfNeededUnlocked();
554
555 // reset repetition counter for a new message
556 ms_prevString = szString;
557 ms_prevLevel = level;
558 ms_prevTimeStamp = t;
559 }
560
561 pLogger->DoLog(level, szString, t);
562 }
563 }
564 }
565
566 // deprecated function
567 #if WXWIN_COMPATIBILITY_2_6
568
569 wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
570 {
571 return NULL;
572 }
573
574 #endif // WXWIN_COMPATIBILITY_2_6
575
576 #if WXWIN_COMPATIBILITY_2_8
577
578 void wxLog::DoLog(wxLogLevel WXUNUSED(level),
579 const char *WXUNUSED(szString),
580 time_t WXUNUSED(t))
581 {
582 }
583
584 void wxLog::DoLog(wxLogLevel WXUNUSED(level),
585 const wchar_t *WXUNUSED(wzString),
586 time_t WXUNUSED(t))
587 {
588 }
589
590 #endif // WXWIN_COMPATIBILITY_2_8
591
592 wxLog *wxLog::GetActiveTarget()
593 {
594 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
595 // prevent infinite recursion if someone calls wxLogXXX() from
596 // wxApp::CreateLogTarget()
597 static bool s_bInGetActiveTarget = false;
598 if ( !s_bInGetActiveTarget ) {
599 s_bInGetActiveTarget = true;
600
601 // ask the application to create a log target for us
602 if ( wxTheApp != NULL )
603 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
604 else
605 ms_pLogger = new wxLogStderr;
606
607 s_bInGetActiveTarget = false;
608
609 // do nothing if it fails - what can we do?
610 }
611 }
612
613 return ms_pLogger;
614 }
615
616 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
617 {
618 if ( ms_pLogger != NULL ) {
619 // flush the old messages before changing because otherwise they might
620 // get lost later if this target is not restored
621 ms_pLogger->Flush();
622 }
623
624 wxLog *pOldLogger = ms_pLogger;
625 ms_pLogger = pLogger;
626
627 return pOldLogger;
628 }
629
630 void wxLog::DontCreateOnDemand()
631 {
632 ms_bAutoCreate = false;
633
634 // this is usually called at the end of the program and we assume that it
635 // is *always* called at the end - so we free memory here to avoid false
636 // memory leak reports from wxWin memory tracking code
637 ClearTraceMasks();
638 }
639
640 void wxLog::DoCreateOnDemand()
641 {
642 ms_bAutoCreate = true;
643 }
644
645 void wxLog::AddTraceMask(const wxString& str)
646 {
647 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
648
649 ms_aTraceMasks.push_back(str);
650 }
651
652 void wxLog::RemoveTraceMask(const wxString& str)
653 {
654 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
655
656 int index = ms_aTraceMasks.Index(str);
657 if ( index != wxNOT_FOUND )
658 ms_aTraceMasks.RemoveAt((size_t)index);
659 }
660
661 void wxLog::ClearTraceMasks()
662 {
663 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
664
665 ms_aTraceMasks.Clear();
666 }
667
668 void wxLog::TimeStamp(wxString *str)
669 {
670 #if wxUSE_DATETIME
671 if ( !ms_timestamp.empty() )
672 {
673 wxChar buf[256];
674 time_t timeNow;
675 (void)time(&timeNow);
676
677 struct tm tm;
678 wxStrftime(buf, WXSIZEOF(buf),
679 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
680
681 str->Empty();
682 *str << buf << wxS(": ");
683 }
684 #endif // wxUSE_DATETIME
685 }
686
687 void wxLog::DoLog(wxLogLevel level, const wxString& szString, time_t t)
688 {
689 #if WXWIN_COMPATIBILITY_2_8
690 // DoLog() signature changed since 2.8, so we call the old versions here
691 // so that existing custom log classes still work:
692 DoLog(level, (const char*)szString.mb_str(), t);
693 DoLog(level, (const wchar_t*)szString.wc_str(), t);
694 #endif
695
696 switch ( level ) {
697 case wxLOG_FatalError:
698 LogString(_("Fatal error: ") + szString, t);
699 LogString(_("Program aborted."), t);
700 Flush();
701 #ifdef __WXWINCE__
702 ExitThread(3);
703 #else
704 abort();
705 #endif
706 break;
707
708 case wxLOG_Error:
709 LogString(_("Error: ") + szString, t);
710 break;
711
712 case wxLOG_Warning:
713 LogString(_("Warning: ") + szString, t);
714 break;
715
716 case wxLOG_Info:
717 if ( GetVerbose() )
718 case wxLOG_Message:
719 case wxLOG_Status:
720 default: // log unknown log levels too
721 LogString(szString, t);
722 break;
723
724 case wxLOG_Trace:
725 case wxLOG_Debug:
726 #ifdef __WXDEBUG__
727 {
728 wxString msg = level == wxLOG_Trace ? wxS("Trace: ")
729 : wxS("Debug: ");
730 msg << szString;
731 LogString(msg, t);
732 }
733 #endif // Debug
734 break;
735 }
736 }
737
738 void wxLog::DoLogString(const wxString& szString, time_t t)
739 {
740 #if WXWIN_COMPATIBILITY_2_8
741 // DoLogString() signature changed since 2.8, so we call the old versions
742 // here so that existing custom log classes still work; unfortunately this
743 // also means that we can't have the wxFAIL_MSG below in compat mode
744 DoLogString((const char*)szString.mb_str(), t);
745 DoLogString((const wchar_t*)szString.wc_str(), t);
746 #else
747 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
748 wxUnusedVar(szString);
749 wxUnusedVar(t);
750 #endif
751 }
752
753 void wxLog::Flush()
754 {
755 LogLastRepeatIfNeeded();
756 }
757
758 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
759 {
760 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
761
762 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
763 en = ms_aTraceMasks.end();
764 it != en; ++it )
765 {
766 if ( *it == mask)
767 return true;
768 }
769
770 return false;
771 }
772
773 // ----------------------------------------------------------------------------
774 // wxLogBuffer implementation
775 // ----------------------------------------------------------------------------
776
777 void wxLogBuffer::Flush()
778 {
779 if ( !m_str.empty() )
780 {
781 wxMessageOutputBest out;
782 out.Printf(wxS("%s"), m_str.c_str());
783 m_str.clear();
784 }
785 }
786
787 void wxLogBuffer::DoLog(wxLogLevel level, const wxString& szString, time_t t)
788 {
789 switch ( level )
790 {
791 case wxLOG_Trace:
792 case wxLOG_Debug:
793 #ifdef __WXDEBUG__
794 // don't put debug messages in the buffer, we don't want to show
795 // them to the user in a msg box, log them immediately
796 {
797 wxString str;
798 TimeStamp(&str);
799 str += szString;
800
801 wxMessageOutputDebug dbgout;
802 dbgout.Printf(wxS("%s\n"), str.c_str());
803 }
804 #endif // __WXDEBUG__
805 break;
806
807 default:
808 wxLog::DoLog(level, szString, t);
809 }
810 }
811
812 void wxLogBuffer::DoLogString(const wxString& szString, time_t WXUNUSED(t))
813 {
814 m_str << szString << wxS("\n");
815 }
816
817 // ----------------------------------------------------------------------------
818 // wxLogStderr class implementation
819 // ----------------------------------------------------------------------------
820
821 wxLogStderr::wxLogStderr(FILE *fp)
822 {
823 if ( fp == NULL )
824 m_fp = stderr;
825 else
826 m_fp = fp;
827 }
828
829 void wxLogStderr::DoLogString(const wxString& szString, time_t WXUNUSED(t))
830 {
831 wxString str;
832 TimeStamp(&str);
833 str << szString;
834
835 wxFputs(str, m_fp);
836 wxFputc(wxS('\n'), m_fp);
837 fflush(m_fp);
838
839 // under GUI systems such as Windows or Mac, programs usually don't have
840 // stderr at all, so show the messages also somewhere else, typically in
841 // the debugger window so that they go at least somewhere instead of being
842 // simply lost
843 if ( m_fp == stderr )
844 {
845 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
846 if ( traits && !traits->HasStderr() )
847 {
848 wxMessageOutputDebug dbgout;
849 dbgout.Printf(wxS("%s\n"), str.c_str());
850 }
851 }
852 }
853
854 // ----------------------------------------------------------------------------
855 // wxLogStream implementation
856 // ----------------------------------------------------------------------------
857
858 #if wxUSE_STD_IOSTREAM
859 #include "wx/ioswrap.h"
860 wxLogStream::wxLogStream(wxSTD ostream *ostr)
861 {
862 if ( ostr == NULL )
863 m_ostr = &wxSTD cerr;
864 else
865 m_ostr = ostr;
866 }
867
868 void wxLogStream::DoLogString(const wxString& szString, time_t WXUNUSED(t))
869 {
870 wxString stamp;
871 TimeStamp(&stamp);
872 (*m_ostr) << stamp << szString << wxSTD endl;
873 }
874 #endif // wxUSE_STD_IOSTREAM
875
876 // ----------------------------------------------------------------------------
877 // wxLogChain
878 // ----------------------------------------------------------------------------
879
880 wxLogChain::wxLogChain(wxLog *logger)
881 {
882 m_bPassMessages = true;
883
884 m_logNew = logger;
885 m_logOld = wxLog::SetActiveTarget(this);
886 }
887
888 wxLogChain::~wxLogChain()
889 {
890 delete m_logOld;
891
892 if ( m_logNew != this )
893 delete m_logNew;
894 }
895
896 void wxLogChain::SetLog(wxLog *logger)
897 {
898 if ( m_logNew != this )
899 delete m_logNew;
900
901 m_logNew = logger;
902 }
903
904 void wxLogChain::Flush()
905 {
906 if ( m_logOld )
907 m_logOld->Flush();
908
909 // be careful to avoid infinite recursion
910 if ( m_logNew && m_logNew != this )
911 m_logNew->Flush();
912 }
913
914 void wxLogChain::DoLog(wxLogLevel level, const wxString& szString, time_t t)
915 {
916 // let the previous logger show it
917 if ( m_logOld && IsPassingMessages() )
918 {
919 // bogus cast just to access protected DoLog
920 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
921 }
922
923 if ( m_logNew && m_logNew != this )
924 {
925 // as above...
926 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
927 }
928 }
929
930 #ifdef __VISUALC__
931 // "'this' : used in base member initializer list" - so what?
932 #pragma warning(disable:4355)
933 #endif // VC++
934
935 // ----------------------------------------------------------------------------
936 // wxLogInterposer
937 // ----------------------------------------------------------------------------
938
939 wxLogInterposer::wxLogInterposer()
940 : wxLogChain(this)
941 {
942 }
943
944 // ----------------------------------------------------------------------------
945 // wxLogInterposerTemp
946 // ----------------------------------------------------------------------------
947
948 wxLogInterposerTemp::wxLogInterposerTemp()
949 : wxLogChain(this)
950 {
951 DetachOldLog();
952 }
953
954 #ifdef __VISUALC__
955 #pragma warning(default:4355)
956 #endif // VC++
957
958 // ============================================================================
959 // Global functions/variables
960 // ============================================================================
961
962 // ----------------------------------------------------------------------------
963 // static variables
964 // ----------------------------------------------------------------------------
965
966 bool wxLog::ms_bRepetCounting = false;
967 wxString wxLog::ms_prevString;
968 unsigned int wxLog::ms_prevCounter = 0;
969 time_t wxLog::ms_prevTimeStamp= 0;
970 wxLogLevel wxLog::ms_prevLevel;
971
972 wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
973 bool wxLog::ms_doLog = true;
974 bool wxLog::ms_bAutoCreate = true;
975 bool wxLog::ms_bVerbose = false;
976
977 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
978
979 size_t wxLog::ms_suspendCount = 0;
980
981 wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
982
983 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
984 wxArrayString wxLog::ms_aTraceMasks;
985
986 // ----------------------------------------------------------------------------
987 // stdout error logging helper
988 // ----------------------------------------------------------------------------
989
990 // helper function: wraps the message and justifies it under given position
991 // (looks more pretty on the terminal). Also adds newline at the end.
992 //
993 // TODO this is now disabled until I find a portable way of determining the
994 // terminal window size (ok, I found it but does anybody really cares?)
995 #ifdef LOG_PRETTY_WRAP
996 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
997 {
998 size_t nMax = 80; // FIXME
999 size_t nStart = strlen(pszPrefix);
1000 fputs(pszPrefix, f);
1001
1002 size_t n;
1003 while ( *psz != '\0' ) {
1004 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
1005 putc(*psz++, f);
1006
1007 // wrapped?
1008 if ( *psz != '\0' ) {
1009 /*putc('\n', f);*/
1010 for ( n = 0; n < nStart; n++ )
1011 putc(' ', f);
1012
1013 // as we wrapped, squeeze all white space
1014 while ( isspace(*psz) )
1015 psz++;
1016 }
1017 }
1018
1019 putc('\n', f);
1020 }
1021 #endif //LOG_PRETTY_WRAP
1022
1023 // ----------------------------------------------------------------------------
1024 // error code/error message retrieval functions
1025 // ----------------------------------------------------------------------------
1026
1027 // get error code from syste
1028 unsigned long wxSysErrorCode()
1029 {
1030 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1031 return ::GetLastError();
1032 #else //Unix
1033 return errno;
1034 #endif //Win/Unix
1035 }
1036
1037 // get error message from system
1038 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
1039 {
1040 if ( nErrCode == 0 )
1041 nErrCode = wxSysErrorCode();
1042
1043 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1044 static wxChar s_szBuf[1024];
1045
1046 // get error message from system
1047 LPVOID lpMsgBuf;
1048 if ( ::FormatMessage
1049 (
1050 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1051 NULL,
1052 nErrCode,
1053 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1054 (LPTSTR)&lpMsgBuf,
1055 0,
1056 NULL
1057 ) == 0 )
1058 {
1059 // if this happens, something is seriously wrong, so don't use _() here
1060 // for safety
1061 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
1062 return s_szBuf;
1063 }
1064
1065
1066 // copy it to our buffer and free memory
1067 // Crashes on SmartPhone (FIXME)
1068 #if !defined(__SMARTPHONE__) /* of WinCE */
1069 if( lpMsgBuf != 0 )
1070 {
1071 wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
1072
1073 LocalFree(lpMsgBuf);
1074
1075 // returned string is capitalized and ended with '\r\n' - bad
1076 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
1077 size_t len = wxStrlen(s_szBuf);
1078 if ( len > 0 ) {
1079 // truncate string
1080 if ( s_szBuf[len - 2] == wxS('\r') )
1081 s_szBuf[len - 2] = wxS('\0');
1082 }
1083 }
1084 else
1085 #endif // !__SMARTPHONE__
1086 {
1087 s_szBuf[0] = wxS('\0');
1088 }
1089
1090 return s_szBuf;
1091 #else // !__WXMSW__
1092 #if wxUSE_UNICODE
1093 static wchar_t s_wzBuf[1024];
1094 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1095 WXSIZEOF(s_wzBuf) - 1);
1096 return s_wzBuf;
1097 #else
1098 return strerror((int)nErrCode);
1099 #endif
1100 #endif // __WXMSW__/!__WXMSW__
1101 }
1102
1103 #endif // wxUSE_LOG