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