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