]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
use quotes around SRCROOT to handle the case when it contains spaces (patch 1912354)
[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::AddTraceMask(const wxString& str)
622 {
623 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
624
625 ms_aTraceMasks.push_back(str);
626 }
627
628 void wxLog::RemoveTraceMask(const wxString& str)
629 {
630 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
631
632 int index = ms_aTraceMasks.Index(str);
633 if ( index != wxNOT_FOUND )
634 ms_aTraceMasks.RemoveAt((size_t)index);
635 }
636
637 void wxLog::ClearTraceMasks()
638 {
639 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
640
641 ms_aTraceMasks.Clear();
642 }
643
644 void wxLog::TimeStamp(wxString *str)
645 {
646 #if wxUSE_DATETIME
647 if ( !ms_timestamp.empty() )
648 {
649 wxChar buf[256];
650 time_t timeNow;
651 (void)time(&timeNow);
652
653 struct tm tm;
654 wxStrftime(buf, WXSIZEOF(buf),
655 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
656
657 str->Empty();
658 *str << buf << wxS(": ");
659 }
660 #endif // wxUSE_DATETIME
661 }
662
663 void wxLog::DoLog(wxLogLevel level, const wxString& szString, time_t t)
664 {
665 #if WXWIN_COMPATIBILITY_2_8
666 // DoLog() signature changed since 2.8, so we call the old versions here
667 // so that existing custom log classes still work:
668 DoLog(level, (const char*)szString.mb_str(), t);
669 DoLog(level, (const wchar_t*)szString.wc_str(), t);
670 #endif
671
672 switch ( level ) {
673 case wxLOG_FatalError:
674 LogString(_("Fatal error: ") + szString, t);
675 LogString(_("Program aborted."), t);
676 Flush();
677 #ifdef __WXWINCE__
678 ExitThread(3);
679 #else
680 abort();
681 #endif
682 break;
683
684 case wxLOG_Error:
685 LogString(_("Error: ") + szString, t);
686 break;
687
688 case wxLOG_Warning:
689 LogString(_("Warning: ") + szString, t);
690 break;
691
692 case wxLOG_Info:
693 if ( GetVerbose() )
694 case wxLOG_Message:
695 case wxLOG_Status:
696 default: // log unknown log levels too
697 LogString(szString, t);
698 break;
699
700 case wxLOG_Trace:
701 case wxLOG_Debug:
702 #ifdef __WXDEBUG__
703 {
704 wxString msg = level == wxLOG_Trace ? wxS("Trace: ")
705 : wxS("Debug: ");
706 msg << szString;
707 LogString(msg, t);
708 }
709 #endif // Debug
710 break;
711 }
712 }
713
714 void wxLog::DoLogString(const wxString& szString, time_t t)
715 {
716 #if WXWIN_COMPATIBILITY_2_8
717 // DoLogString() signature changed since 2.8, so we call the old versions
718 // here so that existing custom log classes still work; unfortunately this
719 // also means that we can't have the wxFAIL_MSG below in compat mode
720 DoLogString((const char*)szString.mb_str(), t);
721 DoLogString((const wchar_t*)szString.wc_str(), t);
722 #else
723 wxFAIL_MSG(wxS("DoLogString must be overriden if it's called."));
724 wxUnusedVar(szString);
725 wxUnusedVar(t);
726 #endif
727 }
728
729 void wxLog::Flush()
730 {
731 LogLastRepeatIfNeeded();
732 }
733
734 /*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
735 {
736 wxCRIT_SECT_LOCKER(lock, ms_traceCS);
737
738 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
739 en = ms_aTraceMasks.end();
740 it != en; ++it )
741 {
742 if ( *it == mask)
743 return true;
744 }
745
746 return false;
747 }
748
749 // ----------------------------------------------------------------------------
750 // wxLogBuffer implementation
751 // ----------------------------------------------------------------------------
752
753 void wxLogBuffer::Flush()
754 {
755 if ( !m_str.empty() )
756 {
757 wxMessageOutputBest out;
758 out.Printf(wxS("%s"), m_str.c_str());
759 m_str.clear();
760 }
761 }
762
763 void wxLogBuffer::DoLog(wxLogLevel level, const wxString& szString, time_t t)
764 {
765 switch ( level )
766 {
767 case wxLOG_Trace:
768 case wxLOG_Debug:
769 #ifdef __WXDEBUG__
770 // don't put debug messages in the buffer, we don't want to show
771 // them to the user in a msg box, log them immediately
772 {
773 wxString str;
774 TimeStamp(&str);
775 str += szString;
776
777 wxMessageOutputDebug dbgout;
778 dbgout.Printf(wxS("%s\n"), str.c_str());
779 }
780 #endif // __WXDEBUG__
781 break;
782
783 default:
784 wxLog::DoLog(level, szString, t);
785 }
786 }
787
788 void wxLogBuffer::DoLogString(const wxString& szString, time_t WXUNUSED(t))
789 {
790 m_str << szString << wxS("\n");
791 }
792
793 // ----------------------------------------------------------------------------
794 // wxLogStderr class implementation
795 // ----------------------------------------------------------------------------
796
797 wxLogStderr::wxLogStderr(FILE *fp)
798 {
799 if ( fp == NULL )
800 m_fp = stderr;
801 else
802 m_fp = fp;
803 }
804
805 void wxLogStderr::DoLogString(const wxString& szString, time_t WXUNUSED(t))
806 {
807 wxString str;
808 TimeStamp(&str);
809 str << szString;
810
811 wxFputs(str, m_fp);
812 wxFputc(wxS('\n'), m_fp);
813 fflush(m_fp);
814
815 // under GUI systems such as Windows or Mac, programs usually don't have
816 // stderr at all, so show the messages also somewhere else, typically in
817 // the debugger window so that they go at least somewhere instead of being
818 // simply lost
819 if ( m_fp == stderr )
820 {
821 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
822 if ( traits && !traits->HasStderr() )
823 {
824 wxMessageOutputDebug dbgout;
825 dbgout.Printf(wxS("%s\n"), str.c_str());
826 }
827 }
828 }
829
830 // ----------------------------------------------------------------------------
831 // wxLogStream implementation
832 // ----------------------------------------------------------------------------
833
834 #if wxUSE_STD_IOSTREAM
835 #include "wx/ioswrap.h"
836 wxLogStream::wxLogStream(wxSTD ostream *ostr)
837 {
838 if ( ostr == NULL )
839 m_ostr = &wxSTD cerr;
840 else
841 m_ostr = ostr;
842 }
843
844 void wxLogStream::DoLogString(const wxString& szString, time_t WXUNUSED(t))
845 {
846 wxString stamp;
847 TimeStamp(&stamp);
848 (*m_ostr) << stamp << szString << wxSTD endl;
849 }
850 #endif // wxUSE_STD_IOSTREAM
851
852 // ----------------------------------------------------------------------------
853 // wxLogChain
854 // ----------------------------------------------------------------------------
855
856 wxLogChain::wxLogChain(wxLog *logger)
857 {
858 m_bPassMessages = true;
859
860 m_logNew = logger;
861 m_logOld = wxLog::SetActiveTarget(this);
862 }
863
864 wxLogChain::~wxLogChain()
865 {
866 delete m_logOld;
867
868 if ( m_logNew != this )
869 delete m_logNew;
870 }
871
872 void wxLogChain::SetLog(wxLog *logger)
873 {
874 if ( m_logNew != this )
875 delete m_logNew;
876
877 m_logNew = logger;
878 }
879
880 void wxLogChain::Flush()
881 {
882 if ( m_logOld )
883 m_logOld->Flush();
884
885 // be careful to avoid infinite recursion
886 if ( m_logNew && m_logNew != this )
887 m_logNew->Flush();
888 }
889
890 void wxLogChain::DoLog(wxLogLevel level, const wxString& szString, time_t t)
891 {
892 // let the previous logger show it
893 if ( m_logOld && IsPassingMessages() )
894 {
895 // bogus cast just to access protected DoLog
896 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
897 }
898
899 if ( m_logNew && m_logNew != this )
900 {
901 // as above...
902 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
903 }
904 }
905
906 #ifdef __VISUALC__
907 // "'this' : used in base member initializer list" - so what?
908 #pragma warning(disable:4355)
909 #endif // VC++
910
911 // ----------------------------------------------------------------------------
912 // wxLogInterposer
913 // ----------------------------------------------------------------------------
914
915 wxLogInterposer::wxLogInterposer()
916 : wxLogChain(this)
917 {
918 }
919
920 // ----------------------------------------------------------------------------
921 // wxLogInterposerTemp
922 // ----------------------------------------------------------------------------
923
924 wxLogInterposerTemp::wxLogInterposerTemp()
925 : wxLogChain(this)
926 {
927 DetachOldLog();
928 }
929
930 #ifdef __VISUALC__
931 #pragma warning(default:4355)
932 #endif // VC++
933
934 // ============================================================================
935 // Global functions/variables
936 // ============================================================================
937
938 // ----------------------------------------------------------------------------
939 // static variables
940 // ----------------------------------------------------------------------------
941
942 #if wxUSE_THREADS
943 wxCriticalSection wxLog::ms_prevCS,
944 wxLog::ms_traceCS;
945 #endif // wxUSE_THREADS
946 bool wxLog::ms_bRepetCounting = false;
947 wxString wxLog::ms_prevString;
948 unsigned int wxLog::ms_prevCounter = 0;
949 time_t wxLog::ms_prevTimeStamp= 0;
950 wxLogLevel wxLog::ms_prevLevel;
951
952 wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
953 bool wxLog::ms_doLog = true;
954 bool wxLog::ms_bAutoCreate = true;
955 bool wxLog::ms_bVerbose = false;
956
957 wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
958
959 size_t wxLog::ms_suspendCount = 0;
960
961 wxString wxLog::ms_timestamp(wxS("%X")); // time only, no date
962
963 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
964 wxArrayString wxLog::ms_aTraceMasks;
965
966 // ----------------------------------------------------------------------------
967 // stdout error logging helper
968 // ----------------------------------------------------------------------------
969
970 // helper function: wraps the message and justifies it under given position
971 // (looks more pretty on the terminal). Also adds newline at the end.
972 //
973 // TODO this is now disabled until I find a portable way of determining the
974 // terminal window size (ok, I found it but does anybody really cares?)
975 #ifdef LOG_PRETTY_WRAP
976 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
977 {
978 size_t nMax = 80; // FIXME
979 size_t nStart = strlen(pszPrefix);
980 fputs(pszPrefix, f);
981
982 size_t n;
983 while ( *psz != '\0' ) {
984 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
985 putc(*psz++, f);
986
987 // wrapped?
988 if ( *psz != '\0' ) {
989 /*putc('\n', f);*/
990 for ( n = 0; n < nStart; n++ )
991 putc(' ', f);
992
993 // as we wrapped, squeeze all white space
994 while ( isspace(*psz) )
995 psz++;
996 }
997 }
998
999 putc('\n', f);
1000 }
1001 #endif //LOG_PRETTY_WRAP
1002
1003 // ----------------------------------------------------------------------------
1004 // error code/error message retrieval functions
1005 // ----------------------------------------------------------------------------
1006
1007 // get error code from syste
1008 unsigned long wxSysErrorCode()
1009 {
1010 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1011 return ::GetLastError();
1012 #else //Unix
1013 return errno;
1014 #endif //Win/Unix
1015 }
1016
1017 // get error message from system
1018 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
1019 {
1020 if ( nErrCode == 0 )
1021 nErrCode = wxSysErrorCode();
1022
1023 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
1024 static wxChar s_szBuf[1024];
1025
1026 // get error message from system
1027 LPVOID lpMsgBuf;
1028 if ( ::FormatMessage
1029 (
1030 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
1031 NULL,
1032 nErrCode,
1033 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1034 (LPTSTR)&lpMsgBuf,
1035 0,
1036 NULL
1037 ) == 0 )
1038 {
1039 // if this happens, something is seriously wrong, so don't use _() here
1040 // for safety
1041 wxSprintf(s_szBuf, wxS("unknown error %lx"), nErrCode);
1042 return s_szBuf;
1043 }
1044
1045
1046 // copy it to our buffer and free memory
1047 // Crashes on SmartPhone (FIXME)
1048 #if !defined(__SMARTPHONE__) /* of WinCE */
1049 if( lpMsgBuf != 0 )
1050 {
1051 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
1052 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxS('\0');
1053
1054 LocalFree(lpMsgBuf);
1055
1056 // returned string is capitalized and ended with '\r\n' - bad
1057 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
1058 size_t len = wxStrlen(s_szBuf);
1059 if ( len > 0 ) {
1060 // truncate string
1061 if ( s_szBuf[len - 2] == wxS('\r') )
1062 s_szBuf[len - 2] = wxS('\0');
1063 }
1064 }
1065 else
1066 #endif // !__SMARTPHONE__
1067 {
1068 s_szBuf[0] = wxS('\0');
1069 }
1070
1071 return s_szBuf;
1072 #else // !__WXMSW__
1073 #if wxUSE_UNICODE
1074 static wchar_t s_wzBuf[1024];
1075 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1076 WXSIZEOF(s_wzBuf) - 1);
1077 return s_wzBuf;
1078 #else
1079 return strerror((int)nErrCode);
1080 #endif
1081 #endif // __WXMSW__/!__WXMSW__
1082 }
1083
1084 #endif // wxUSE_LOG