]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
handle locales where AM/PM strings are empty
[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 m_logOld->Log(level, szString, t);
919
920 // and also send it to the new one
921 if ( m_logNew && m_logNew != this )
922 m_logNew->Log(level, szString, t);
923 }
924
925 #ifdef __VISUALC__
926 // "'this' : used in base member initializer list" - so what?
927 #pragma warning(disable:4355)
928 #endif // VC++
929
930 // ----------------------------------------------------------------------------
931 // wxLogInterposer
932 // ----------------------------------------------------------------------------
933
934 wxLogInterposer::wxLogInterposer()
935 : wxLogChain(this)
936 {
937 }
938
939 // ----------------------------------------------------------------------------
940 // wxLogInterposerTemp
941 // ----------------------------------------------------------------------------
942
943 wxLogInterposerTemp::wxLogInterposerTemp()
944 : wxLogChain(this)
945 {
946 DetachOldLog();
947 }
948
949 #ifdef __VISUALC__
950 #pragma warning(default:4355)
951 #endif // VC++
952
953 // ============================================================================
954 // Global functions/variables
955 // ============================================================================
956
957 // ----------------------------------------------------------------------------
958 // static variables
959 // ----------------------------------------------------------------------------
960
961 bool wxLog::ms_bRepetCounting = false;
962 wxString wxLog::ms_prevString;
963 unsigned int wxLog::ms_prevCounter = 0;
964 time_t wxLog::ms_prevTimeStamp= 0;
965 wxLogLevel wxLog::ms_prevLevel;
966
967 wxLog *wxLog::ms_pLogger = NULL;
968 bool wxLog::ms_doLog = true;
969 bool wxLog::ms_bAutoCreate = true;
970 bool wxLog::ms_bVerbose = false;
971
972 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
973
974 size_t wxLog::ms_suspendCount = 0;
975
976 wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
977
978 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
979 wxArrayString wxLog::ms_aTraceMasks;
980
981 // ----------------------------------------------------------------------------
982 // stdout error logging helper
983 // ----------------------------------------------------------------------------
984
985 // helper function: wraps the message and justifies it under given position
986 // (looks more pretty on the terminal). Also adds newline at the end.
987 //
988 // TODO this is now disabled until I find a portable way of determining the
989 // terminal window size (ok, I found it but does anybody really cares?)
990 #ifdef LOG_PRETTY_WRAP
991 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
992 {
993 size_t nMax = 80; // FIXME
994 size_t nStart = strlen(pszPrefix);
995 fputs(pszPrefix, f);
996
997 size_t n;
998 while ( *psz != '\0' ) {
999 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
1000 putc(*psz++, f);
1001
1002 // wrapped?
1003 if ( *psz != '\0' ) {
1004 /*putc('\n', f);*/
1005 for ( n = 0; n < nStart; n++ )
1006 putc(' ', f);
1007
1008 // as we wrapped, squeeze all white space
1009 while ( isspace(*psz) )
1010 psz++;
1011 }
1012 }
1013
1014 putc('\n', f);
1015 }
1016 #endif //LOG_PRETTY_WRAP
1017
1018 // ----------------------------------------------------------------------------
1019 // error code/error message retrieval functions
1020 // ----------------------------------------------------------------------------
1021
1022 // get error code from syste
1023 unsigned long wxSysErrorCode()
1024 {
1025 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1026 return ::GetLastError();
1027 #else //Unix
1028 return errno;
1029 #endif //Win/Unix
1030 }
1031
1032 // get error message from system
1033 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
1034 {
1035 if ( nErrCode == 0 )
1036 nErrCode = wxSysErrorCode();
1037
1038 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1039 static wxChar s_szBuf[1024];
1040
1041 // get error message from system
1042 LPVOID lpMsgBuf;
1043 if ( ::FormatMessage
1044 (
1045 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1046 NULL,
1047 nErrCode,
1048 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1049 (LPTSTR)&lpMsgBuf,
1050 0,
1051 NULL
1052 ) == 0 )
1053 {
1054 // if this happens, something is seriously wrong, so don't use _() here
1055 // for safety
1056 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
1057 return s_szBuf;
1058 }
1059
1060
1061 // copy it to our buffer and free memory
1062 // Crashes on SmartPhone (FIXME)
1063 #if !defined(__SMARTPHONE__) /* of WinCE */
1064 if( lpMsgBuf != 0 )
1065 {
1066 wxStrlcpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf));
1067
1068 LocalFree(lpMsgBuf);
1069
1070 // returned string is capitalized and ended with '\r\n' - bad
1071 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
1072 size_t len = wxStrlen(s_szBuf);
1073 if ( len > 0 ) {
1074 // truncate string
1075 if ( s_szBuf[len - 2] == wxS('\r') )
1076 s_szBuf[len - 2] = wxS('\0');
1077 }
1078 }
1079 else
1080 #endif // !__SMARTPHONE__
1081 {
1082 s_szBuf[0] = wxS('\0');
1083 }
1084
1085 return s_szBuf;
1086 #else // !__WXMSW__
1087 #if wxUSE_UNICODE
1088 static wchar_t s_wzBuf[1024];
1089 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1090 WXSIZEOF(s_wzBuf) - 1);
1091 return s_wzBuf;
1092 #else
1093 return strerror((int)nErrCode);
1094 #endif
1095 #endif // __WXMSW__/!__WXMSW__
1096 }
1097
1098 #endif // wxUSE_LOG