deprecate integer trace masks and don't define the methods related to them if WXWIN_C...
[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 // deprecated (but not declared as such because we don't want to complicate
335 // DECLARE_LOG_FUNCTION macros even more) overloads for wxTraceMask
336 #if WXWIN_COMPATIBILITY_2_8
337 void wxVLogTrace(wxTraceMask mask, const wxString& format, va_list argptr)
338 {
339 // we check that all of mask bits are set in the current mask, so
340 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
341 // if both bits are set.
342 if ( wxLog::IsEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
343 wxLog::OnLog(wxLOG_Trace, wxString::FormatV(format, argptr), time(NULL));
344 }
345 }
346
347 #if !wxUSE_UTF8_LOCALE_ONLY
348 void wxDoLogTraceWchar(wxTraceMask mask, const wxChar *format, ...)
349 {
350 va_list argptr;
351 va_start(argptr, format);
352 wxVLogTrace(mask, format, argptr);
353 va_end(argptr);
354 }
355 #endif // !wxUSE_UTF8_LOCALE_ONLY
356
357 #if wxUSE_UNICODE_UTF8
358 void wxDoLogTraceUtf8(wxTraceMask mask, const char *format, ...)
359 {
360 va_list argptr;
361 va_start(argptr, format);
362 wxVLogTrace(mask, format, argptr);
363 va_end(argptr);
364 }
365 #endif // wxUSE_UNICODE_UTF8
366
367 #endif // WXWIN_COMPATIBILITY_2_8
368
369 #ifdef __WATCOMC__
370 #if WXWIN_COMPATIBILITY_2_8
371 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
372 void wxDoLogTraceWchar(int mask, const wxChar *format, ...)
373 {
374 va_list argptr;
375 va_start(argptr, format);
376 wxVLogTrace(mask, format, argptr);
377 va_end(argptr);
378 }
379 #endif // WXWIN_COMPATIBILITY_2_8
380
381 void wxDoLogTraceWchar(const char *mask, const wxChar *format, ...)
382 {
383 va_list argptr;
384 va_start(argptr, format);
385 wxVLogTrace(mask, format, argptr);
386 va_end(argptr);
387 }
388
389 void wxDoLogTraceWchar(const wchar_t *mask, const wxChar *format, ...)
390 {
391 va_list argptr;
392 va_start(argptr, format);
393 wxVLogTrace(mask, format, argptr);
394 va_end(argptr);
395 }
396
397 #if WXWIN_COMPATIBILITY_2_8
398 void wxVLogTrace(int mask, const wxString& format, va_list argptr)
399 { wxVLogTrace((wxTraceMask)mask, format, argptr); }
400 #endif // WXWIN_COMPATIBILITY_2_8
401 void wxVLogTrace(const char *mask, const wxString& format, va_list argptr)
402 { wxVLogTrace(wxString(mask), format, argptr); }
403 void wxVLogTrace(const wchar_t *mask, const wxString& format, va_list argptr)
404 { wxVLogTrace(wxString(mask), format, argptr); }
405 #endif // __WATCOMC__
406
407 #else // release
408 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
409 #endif
410
411 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
412 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
413
414 // wxLogSysError: one uses the last error code, for other you must give it
415 // explicitly
416
417 // return the system error message description
418 static inline wxString wxLogSysErrorHelper(long err)
419 {
420 return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
421 }
422
423 void WXDLLIMPEXP_BASE wxVLogSysError(const wxString& format, va_list argptr)
424 {
425 wxVLogSysError(wxSysErrorCode(), format, argptr);
426 }
427
428 #if !wxUSE_UTF8_LOCALE_ONLY
429 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(const wxChar *format, ...)
430 {
431 va_list argptr;
432 va_start(argptr, format);
433 wxVLogSysError(format, argptr);
434 va_end(argptr);
435 }
436 #endif // !wxUSE_UTF8_LOCALE_ONLY
437
438 #if wxUSE_UNICODE_UTF8
439 void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(const char *format, ...)
440 {
441 va_list argptr;
442 va_start(argptr, format);
443 wxVLogSysError(format, argptr);
444 va_end(argptr);
445 }
446 #endif // wxUSE_UNICODE_UTF8
447
448 void WXDLLIMPEXP_BASE wxVLogSysError(long err, const wxString& format, va_list argptr)
449 {
450 if ( wxLog::IsEnabled() ) {
451 wxLog::OnLog(wxLOG_Error,
452 wxString::FormatV(format, argptr) + wxLogSysErrorHelper(err),
453 time(NULL));
454 }
455 }
456
457 #if !wxUSE_UTF8_LOCALE_ONLY
458 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(long lErrCode, const wxChar *format, ...)
459 {
460 va_list argptr;
461 va_start(argptr, format);
462 wxVLogSysError(lErrCode, format, argptr);
463 va_end(argptr);
464 }
465 #endif // !wxUSE_UTF8_LOCALE_ONLY
466
467 #if wxUSE_UNICODE_UTF8
468 void WXDLLIMPEXP_BASE wxDoLogSysErrorUtf8(long lErrCode, const char *format, ...)
469 {
470 va_list argptr;
471 va_start(argptr, format);
472 wxVLogSysError(lErrCode, format, argptr);
473 va_end(argptr);
474 }
475 #endif // wxUSE_UNICODE_UTF8
476
477 #ifdef __WATCOMC__
478 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
479 void WXDLLIMPEXP_BASE wxDoLogSysErrorWchar(unsigned long lErrCode, const wxChar *format, ...)
480 {
481 va_list argptr;
482 va_start(argptr, format);
483 wxVLogSysError(lErrCode, format, argptr);
484 va_end(argptr);
485 }
486
487 void WXDLLIMPEXP_BASE wxVLogSysError(unsigned long err, const wxString& format, va_list argptr)
488 { wxVLogSysError((long)err, format, argptr); }
489 #endif // __WATCOMC__
490
491 // ----------------------------------------------------------------------------
492 // wxLog class implementation
493 // ----------------------------------------------------------------------------
494
495 unsigned wxLog::LogLastRepeatIfNeeded()
496 {
497 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
498
499 return LogLastRepeatIfNeededUnlocked();
500 }
501
502 unsigned wxLog::LogLastRepeatIfNeededUnlocked()
503 {
504 const unsigned count = ms_prevCounter;
505
506 if ( ms_prevCounter )
507 {
508 wxString msg;
509 #if wxUSE_INTL
510 msg.Printf(wxPLURAL("The previous message repeated once.",
511 "The previous message repeated %lu times.",
512 ms_prevCounter),
513 ms_prevCounter);
514 #else
515 msg.Printf(wxS("The previous message was repeated %lu times."),
516 ms_prevCounter);
517 #endif
518 ms_prevCounter = 0;
519 ms_prevString.clear();
520 DoLog(ms_prevLevel, msg, ms_prevTimeStamp);
521 }
522
523 return count;
524 }
525
526 wxLog::~wxLog()
527 {
528 // Flush() must be called before destroying the object as otherwise some
529 // messages could be lost
530 if ( ms_prevCounter )
531 {
532 wxMessageOutputDebug().Printf
533 (
534 wxS("Last repeated message (\"%s\", %lu times) wasn't output"),
535 ms_prevString,
536 ms_prevCounter
537 );
538 }
539 }
540
541 /* static */
542 void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
543 {
544 if ( IsEnabled() && ms_logLevel >= level )
545 {
546 wxLog *pLogger = GetActiveTarget();
547 if ( pLogger )
548 {
549 if ( GetRepetitionCounting() )
550 {
551 wxCRIT_SECT_LOCKER(lock, GetPreviousLogCS());
552
553 if ( szString == ms_prevString )
554 {
555 ms_prevCounter++;
556
557 // nothing else to do, in particular, don't log the
558 // repeated message
559 return;
560 }
561
562 pLogger->LogLastRepeatIfNeededUnlocked();
563
564 // reset repetition counter for a new message
565 ms_prevString = szString;
566 ms_prevLevel = level;
567 ms_prevTimeStamp = t;
568 }
569
570 pLogger->DoLog(level, szString, t);
571 }
572 }
573 }
574
575 // deprecated function
576 #if WXWIN_COMPATIBILITY_2_6
577
578 wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
579 {
580 return NULL;
581 }
582
583 #endif // WXWIN_COMPATIBILITY_2_6
584
585 #if WXWIN_COMPATIBILITY_2_8
586
587 void wxLog::DoLog(wxLogLevel WXUNUSED(level),
588 const char *WXUNUSED(szString),
589 time_t WXUNUSED(t))
590 {
591 }
592
593 void wxLog::DoLog(wxLogLevel WXUNUSED(level),
594 const wchar_t *WXUNUSED(wzString),
595 time_t WXUNUSED(t))
596 {
597 }
598
599 #endif // WXWIN_COMPATIBILITY_2_8
600
601 wxLog *wxLog::GetActiveTarget()
602 {
603 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
604 // prevent infinite recursion if someone calls wxLogXXX() from
605 // wxApp::CreateLogTarget()
606 static bool s_bInGetActiveTarget = false;
607 if ( !s_bInGetActiveTarget ) {
608 s_bInGetActiveTarget = true;
609
610 // ask the application to create a log target for us
611 if ( wxTheApp != NULL )
612 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
613 else
614 ms_pLogger = new wxLogStderr;
615
616 s_bInGetActiveTarget = false;
617
618 // do nothing if it fails - what can we do?
619 }
620 }
621
622 return ms_pLogger;
623 }
624
625 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
626 {
627 if ( ms_pLogger != NULL ) {
628 // flush the old messages before changing because otherwise they might
629 // get lost later if this target is not restored
630 ms_pLogger->Flush();
631 }
632
633 wxLog *pOldLogger = ms_pLogger;
634 ms_pLogger = pLogger;
635
636 return pOldLogger;
637 }
638
639 void wxLog::DontCreateOnDemand()
640 {
641 ms_bAutoCreate = false;
642
643 // this is usually called at the end of the program and we assume that it
644 // is *always* called at the end - so we free memory here to avoid false
645 // memory leak reports from wxWin memory tracking code
646 ClearTraceMasks();
647 }
648
649 void wxLog::DoCreateOnDemand()
650 {
651 ms_bAutoCreate = true;
652 }
653
654 void wxLog::AddTraceMask(const wxString& str)
655 {
656 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
657
658 ms_aTraceMasks.push_back(str);
659 }
660
661 void wxLog::RemoveTraceMask(const wxString& str)
662 {
663 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
664
665 int index = ms_aTraceMasks.Index(str);
666 if ( index != wxNOT_FOUND )
667 ms_aTraceMasks.RemoveAt((size_t)index);
668 }
669
670 void wxLog::ClearTraceMasks()
671 {
672 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
673
674 ms_aTraceMasks.Clear();
675 }
676
677 void wxLog::TimeStamp(wxString *str)
678 {
679 #if wxUSE_DATETIME
680 if ( !ms_timestamp.empty() )
681 {
682 wxChar buf[256];
683 time_t timeNow;
684 (void)time(&timeNow);
685
686 struct tm tm;
687 wxStrftime(buf, WXSIZEOF(buf),
688 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
689
690 str->Empty();
691 *str << buf << wxS(": ");
692 }
693 #endif // wxUSE_DATETIME
694 }
695
696 void wxLog::DoLog(wxLogLevel level, const wxString& szString, time_t t)
697 {
698 #if WXWIN_COMPATIBILITY_2_8
699 // DoLog() signature changed since 2.8, so we call the old versions here
700 // so that existing custom log classes still work:
701 DoLog(level, (const char*)szString.mb_str(), t);
702 DoLog(level, (const wchar_t*)szString.wc_str(), t);
703 #endif
704
705 switch ( level ) {
706 case wxLOG_FatalError:
707 LogString(_("Fatal error: ") + szString, t);
708 LogString(_("Program aborted."), t);
709 Flush();
710 #ifdef __WXWINCE__
711 ExitThread(3);
712 #else
713 abort();
714 #endif
715 break;
716
717 case wxLOG_Error:
718 LogString(_("Error: ") + szString, t);
719 break;
720
721 case wxLOG_Warning:
722 LogString(_("Warning: ") + szString, t);
723 break;
724
725 case wxLOG_Info:
726 if ( GetVerbose() )
727 case wxLOG_Message:
728 case wxLOG_Status:
729 default: // log unknown log levels too
730 LogString(szString, t);
731 break;
732
733 case wxLOG_Trace:
734 case wxLOG_Debug:
735 #ifdef __WXDEBUG__
736 {
737 wxString msg = level == wxLOG_Trace ? wxS("Trace: ")
738 : wxS("Debug: ");
739 msg << szString;
740 LogString(msg, t);
741 }
742 #endif // Debug
743 break;
744 }
745 }
746
747 void wxLog::DoLogString(const wxString& szString, time_t t)
748 {
749 #if WXWIN_COMPATIBILITY_2_8
750 // DoLogString() signature changed since 2.8, so we call the old versions
751 // here so that existing custom log classes still work; unfortunately this
752 // also means that we can't have the wxFAIL_MSG below in compat mode
753 DoLogString((const char*)szString.mb_str(), t);
754 DoLogString((const wchar_t*)szString.wc_str(), t);
755 #else
756 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
757 wxUnusedVar(szString);
758 wxUnusedVar(t);
759 #endif
760 }
761
762 void wxLog::Flush()
763 {
764 LogLastRepeatIfNeeded();
765 }
766
767 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
768 {
769 wxCRIT_SECT_LOCKER(lock, GetTraceMaskCS());
770
771 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
772 en = ms_aTraceMasks.end();
773 it != en; ++it )
774 {
775 if ( *it == mask)
776 return true;
777 }
778
779 return false;
780 }
781
782 // ----------------------------------------------------------------------------
783 // wxLogBuffer implementation
784 // ----------------------------------------------------------------------------
785
786 void wxLogBuffer::Flush()
787 {
788 if ( !m_str.empty() )
789 {
790 wxMessageOutputBest out;
791 out.Printf(wxS("%s"), m_str.c_str());
792 m_str.clear();
793 }
794 }
795
796 void wxLogBuffer::DoLog(wxLogLevel level, const wxString& szString, time_t t)
797 {
798 switch ( level )
799 {
800 case wxLOG_Trace:
801 case wxLOG_Debug:
802 #ifdef __WXDEBUG__
803 // don't put debug messages in the buffer, we don't want to show
804 // them to the user in a msg box, log them immediately
805 {
806 wxString str;
807 TimeStamp(&str);
808 str += szString;
809
810 wxMessageOutputDebug dbgout;
811 dbgout.Printf(wxS("%s\n"), str.c_str());
812 }
813 #endif // __WXDEBUG__
814 break;
815
816 default:
817 wxLog::DoLog(level, szString, t);
818 }
819 }
820
821 void wxLogBuffer::DoLogString(const wxString& szString, time_t WXUNUSED(t))
822 {
823 m_str << szString << wxS("\n");
824 }
825
826 // ----------------------------------------------------------------------------
827 // wxLogStderr class implementation
828 // ----------------------------------------------------------------------------
829
830 wxLogStderr::wxLogStderr(FILE *fp)
831 {
832 if ( fp == NULL )
833 m_fp = stderr;
834 else
835 m_fp = fp;
836 }
837
838 void wxLogStderr::DoLogString(const wxString& szString, time_t WXUNUSED(t))
839 {
840 wxString str;
841 TimeStamp(&str);
842 str << szString;
843
844 wxFputs(str, m_fp);
845 wxFputc(wxS('\n'), m_fp);
846 fflush(m_fp);
847
848 // under GUI systems such as Windows or Mac, programs usually don't have
849 // stderr at all, so show the messages also somewhere else, typically in
850 // the debugger window so that they go at least somewhere instead of being
851 // simply lost
852 if ( m_fp == stderr )
853 {
854 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
855 if ( traits && !traits->HasStderr() )
856 {
857 wxMessageOutputDebug dbgout;
858 dbgout.Printf(wxS("%s\n"), str.c_str());
859 }
860 }
861 }
862
863 // ----------------------------------------------------------------------------
864 // wxLogStream implementation
865 // ----------------------------------------------------------------------------
866
867 #if wxUSE_STD_IOSTREAM
868 #include "wx/ioswrap.h"
869 wxLogStream::wxLogStream(wxSTD ostream *ostr)
870 {
871 if ( ostr == NULL )
872 m_ostr = &wxSTD cerr;
873 else
874 m_ostr = ostr;
875 }
876
877 void wxLogStream::DoLogString(const wxString& szString, time_t WXUNUSED(t))
878 {
879 wxString stamp;
880 TimeStamp(&stamp);
881 (*m_ostr) << stamp << szString << wxSTD endl;
882 }
883 #endif // wxUSE_STD_IOSTREAM
884
885 // ----------------------------------------------------------------------------
886 // wxLogChain
887 // ----------------------------------------------------------------------------
888
889 wxLogChain::wxLogChain(wxLog *logger)
890 {
891 m_bPassMessages = true;
892
893 m_logNew = logger;
894 m_logOld = wxLog::SetActiveTarget(this);
895 }
896
897 wxLogChain::~wxLogChain()
898 {
899 delete m_logOld;
900
901 if ( m_logNew != this )
902 delete m_logNew;
903 }
904
905 void wxLogChain::SetLog(wxLog *logger)
906 {
907 if ( m_logNew != this )
908 delete m_logNew;
909
910 m_logNew = logger;
911 }
912
913 void wxLogChain::Flush()
914 {
915 if ( m_logOld )
916 m_logOld->Flush();
917
918 // be careful to avoid infinite recursion
919 if ( m_logNew && m_logNew != this )
920 m_logNew->Flush();
921 }
922
923 void wxLogChain::DoLog(wxLogLevel level, const wxString& szString, time_t t)
924 {
925 // let the previous logger show it
926 if ( m_logOld && IsPassingMessages() )
927 m_logOld->Log(level, szString, t);
928
929 // and also send it to the new one
930 if ( m_logNew && m_logNew != this )
931 m_logNew->Log(level, szString, t);
932 }
933
934 #ifdef __VISUALC__
935 // "'this' : used in base member initializer list" - so what?
936 #pragma warning(disable:4355)
937 #endif // VC++
938
939 // ----------------------------------------------------------------------------
940 // wxLogInterposer
941 // ----------------------------------------------------------------------------
942
943 wxLogInterposer::wxLogInterposer()
944 : wxLogChain(this)
945 {
946 }
947
948 // ----------------------------------------------------------------------------
949 // wxLogInterposerTemp
950 // ----------------------------------------------------------------------------
951
952 wxLogInterposerTemp::wxLogInterposerTemp()
953 : wxLogChain(this)
954 {
955 DetachOldLog();
956 }
957
958 #ifdef __VISUALC__
959 #pragma warning(default:4355)
960 #endif // VC++
961
962 // ============================================================================
963 // Global functions/variables
964 // ============================================================================
965
966 // ----------------------------------------------------------------------------
967 // static variables
968 // ----------------------------------------------------------------------------
969
970 bool wxLog::ms_bRepetCounting = false;
971 wxString wxLog::ms_prevString;
972 unsigned int wxLog::ms_prevCounter = 0;
973 time_t wxLog::ms_prevTimeStamp= 0;
974 wxLogLevel wxLog::ms_prevLevel;
975
976 wxLog *wxLog::ms_pLogger = NULL;
977 bool wxLog::ms_doLog = true;
978 bool wxLog::ms_bAutoCreate = true;
979 bool wxLog::ms_bVerbose = false;
980
981 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
982
983 size_t wxLog::ms_suspendCount = 0;
984
985 wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
986
987 #if WXWIN_COMPATIBILITY_2_8
988 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
989 #endif // wxDEBUG_LEVEL
990
991 wxArrayString wxLog::ms_aTraceMasks;
992
993 // ----------------------------------------------------------------------------
994 // stdout error logging helper
995 // ----------------------------------------------------------------------------
996
997 // helper function: wraps the message and justifies it under given position
998 // (looks more pretty on the terminal). Also adds newline at the end.
999 //
1000 // TODO this is now disabled until I find a portable way of determining the
1001 // terminal window size (ok, I found it but does anybody really cares?)
1002 #ifdef LOG_PRETTY_WRAP
1003 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
1004 {
1005 size_t nMax = 80; // FIXME
1006 size_t nStart = strlen(pszPrefix);
1007 fputs(pszPrefix, f);
1008
1009 size_t n;
1010 while ( *psz != '\0' ) {
1011 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
1012 putc(*psz++, f);
1013
1014 // wrapped?
1015 if ( *psz != '\0' ) {
1016 /*putc('\n', f);*/
1017 for ( n = 0; n < nStart; n++ )
1018 putc(' ', f);
1019
1020 // as we wrapped, squeeze all white space
1021 while ( isspace(*psz) )
1022 psz++;
1023 }
1024 }
1025
1026 putc('\n', f);
1027 }
1028 #endif //LOG_PRETTY_WRAP
1029
1030 // ----------------------------------------------------------------------------
1031 // error code/error message retrieval functions
1032 // ----------------------------------------------------------------------------
1033
1034 // get error code from syste
1035 unsigned long wxSysErrorCode()
1036 {
1037 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1038 return ::GetLastError();
1039 #else //Unix
1040 return errno;
1041 #endif //Win/Unix
1042 }
1043
1044 // get error message from system
1045 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
1046 {
1047 if ( nErrCode == 0 )
1048 nErrCode = wxSysErrorCode();
1049
1050 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1051 static wxChar s_szBuf[1024];
1052
1053 // get error message from system
1054 LPVOID lpMsgBuf;
1055 if ( ::FormatMessage
1056 (
1057 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1058 NULL,
1059 nErrCode,
1060 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1061 (LPTSTR)&lpMsgBuf,
1062 0,
1063 NULL
1064 ) == 0 )
1065 {
1066 // if this happens, something is seriously wrong, so don't use _() here
1067 // for safety
1068 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
1069 return s_szBuf;
1070 }
1071
1072
1073 // copy it to our buffer and free memory
1074 // Crashes on SmartPhone (FIXME)
1075 #if !defined(__SMARTPHONE__) /* of WinCE */
1076 if( lpMsgBuf != 0 )
1077 {
1078 wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
1079
1080 LocalFree(lpMsgBuf);
1081
1082 // returned string is capitalized and ended with '\r\n' - bad
1083 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
1084 size_t len = wxStrlen(s_szBuf);
1085 if ( len > 0 ) {
1086 // truncate string
1087 if ( s_szBuf[len - 2] == wxS('\r') )
1088 s_szBuf[len - 2] = wxS('\0');
1089 }
1090 }
1091 else
1092 #endif // !__SMARTPHONE__
1093 {
1094 s_szBuf[0] = wxS('\0');
1095 }
1096
1097 return s_szBuf;
1098 #else // !__WXMSW__
1099 #if wxUSE_UNICODE
1100 static wchar_t s_wzBuf[1024];
1101 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1102 WXSIZEOF(s_wzBuf) - 1);
1103 return s_wzBuf;
1104 #else
1105 return strerror((int)nErrCode);
1106 #endif
1107 #endif // __WXMSW__/!__WXMSW__
1108 }
1109
1110 #endif // wxUSE_LOG