]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/log.cpp
add wx/unix/private directory
[wxWidgets.git] / src / common / log.cpp
... / ...
CommitLineData
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/wxchar.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
87void 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
95void 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
105void 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
151IMPLEMENT_LOG_FUNCTION(Error)
152IMPLEMENT_LOG_FUNCTION(Warning)
153IMPLEMENT_LOG_FUNCTION(Message)
154IMPLEMENT_LOG_FUNCTION(Info)
155IMPLEMENT_LOG_FUNCTION(Status)
156
157void wxSafeShowMessage(const wxString& title, const wxString& text)
158{
159#ifdef __WINDOWS__
160 ::MessageBox(NULL, text, title, MB_OK | MB_ICONSTOP);
161#else
162 wxFprintf(stderr, _T("%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
169void wxVLogFatalError(const wxString& format, va_list argptr)
170{
171 wxSafeShowMessage(_T("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
181void 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
194void 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
207void 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
218void 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
228void 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 << _T("(") << mask << _T(") ") << 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
376IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
377IMPLEMENT_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
383static inline wxString wxLogSysErrorHelper(long err)
384{
385 return wxString::Format(_(" (error %ld: %s)"), err, wxSysErrorMsg(err));
386}
387
388void WXDLLEXPORT wxVLogSysError(const wxString& format, va_list argptr)
389{
390 wxVLogSysError(wxSysErrorCode(), format, argptr);
391}
392
393#if !wxUSE_UTF8_LOCALE_ONLY
394void WXDLLEXPORT 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
404void WXDLLEXPORT 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
413void WXDLLEXPORT 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
423void WXDLLEXPORT 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
433void WXDLLEXPORT 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
444void WXDLLEXPORT 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
452void WXDLLEXPORT 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/* static */
461unsigned wxLog::DoLogNumberOfRepeats()
462{
463 long retval = ms_prevCounter;
464 wxLog *pLogger = GetActiveTarget();
465 if ( pLogger && ms_prevCounter > 0 )
466 {
467 wxString msg;
468#if wxUSE_INTL
469 msg.Printf(wxPLURAL("The previous message repeated once.",
470 "The previous message repeated %lu times.",
471 ms_prevCounter),
472 ms_prevCounter);
473#else
474 msg.Printf(wxT("The previous message was repeated."));
475#endif
476 ms_prevCounter = 0;
477 ms_prevString.clear();
478 pLogger->DoLog(ms_prevLevel, msg, ms_prevTimeStamp);
479 }
480 return retval;
481}
482
483wxLog::~wxLog()
484{
485 if ( ms_prevCounter > 0 )
486 {
487 // looks like the repeat count has not been logged yet,
488 // so let's do it now
489 wxLog::DoLogNumberOfRepeats();
490 }
491}
492
493/* static */
494void wxLog::OnLog(wxLogLevel level, const wxString& szString, time_t t)
495{
496 if ( IsEnabled() && ms_logLevel >= level )
497 {
498 wxLog *pLogger = GetActiveTarget();
499 if ( pLogger )
500 {
501 if ( GetRepetitionCounting() && ms_prevString == szString )
502 {
503 ms_prevCounter++;
504 }
505 else
506 {
507 if ( GetRepetitionCounting() )
508 {
509 DoLogNumberOfRepeats();
510 }
511 ms_prevString = szString;
512 ms_prevLevel = level;
513 ms_prevTimeStamp = t;
514 pLogger->DoLog(level, szString, t);
515 }
516 }
517 }
518}
519
520// deprecated function
521#if WXWIN_COMPATIBILITY_2_6
522
523wxChar *wxLog::SetLogBuffer(wxChar * WXUNUSED(buf), size_t WXUNUSED(size))
524{
525 return NULL;
526}
527
528#endif // WXWIN_COMPATIBILITY_2_6
529
530wxLog *wxLog::GetActiveTarget()
531{
532 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
533 // prevent infinite recursion if someone calls wxLogXXX() from
534 // wxApp::CreateLogTarget()
535 static bool s_bInGetActiveTarget = false;
536 if ( !s_bInGetActiveTarget ) {
537 s_bInGetActiveTarget = true;
538
539 // ask the application to create a log target for us
540 if ( wxTheApp != NULL )
541 ms_pLogger = wxTheApp->GetTraits()->CreateLogTarget();
542 else
543 ms_pLogger = new wxLogStderr;
544
545 s_bInGetActiveTarget = false;
546
547 // do nothing if it fails - what can we do?
548 }
549 }
550
551 return ms_pLogger;
552}
553
554wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
555{
556 if ( ms_pLogger != NULL ) {
557 // flush the old messages before changing because otherwise they might
558 // get lost later if this target is not restored
559 ms_pLogger->Flush();
560 }
561
562 wxLog *pOldLogger = ms_pLogger;
563 ms_pLogger = pLogger;
564
565 return pOldLogger;
566}
567
568void wxLog::DontCreateOnDemand()
569{
570 ms_bAutoCreate = false;
571
572 // this is usually called at the end of the program and we assume that it
573 // is *always* called at the end - so we free memory here to avoid false
574 // memory leak reports from wxWin memory tracking code
575 ClearTraceMasks();
576}
577
578void wxLog::DoCreateOnDemand()
579{
580 ms_bAutoCreate = true;
581}
582
583void wxLog::RemoveTraceMask(const wxString& str)
584{
585 int index = ms_aTraceMasks.Index(str);
586 if ( index != wxNOT_FOUND )
587 ms_aTraceMasks.RemoveAt((size_t)index);
588}
589
590void wxLog::ClearTraceMasks()
591{
592 ms_aTraceMasks.Clear();
593}
594
595void wxLog::TimeStamp(wxString *str)
596{
597#if wxUSE_DATETIME
598 if ( ms_timestamp )
599 {
600 wxChar buf[256];
601 time_t timeNow;
602 (void)time(&timeNow);
603
604 struct tm tm;
605 wxStrftime(buf, WXSIZEOF(buf),
606 ms_timestamp, wxLocaltime_r(&timeNow, &tm));
607
608 str->Empty();
609 *str << buf << wxT(": ");
610 }
611#endif // wxUSE_DATETIME
612}
613
614void wxLog::DoLog(wxLogLevel level, const wxString& szString, time_t t)
615{
616#if WXWIN_COMPATIBILITY_2_8
617 // DoLog() signature changed since 2.8, so we call the old versions here
618 // so that existing custom log classes still work:
619 DoLog(level, (const char*)szString.mb_str(), t);
620 DoLog(level, (const wchar_t*)szString.wc_str(), t);
621#endif
622
623 switch ( level ) {
624 case wxLOG_FatalError:
625 LogString(_("Fatal error: ") + szString, t);
626 LogString(_("Program aborted."), t);
627 Flush();
628#ifdef __WXWINCE__
629 ExitThread(3);
630#else
631 abort();
632#endif
633 break;
634
635 case wxLOG_Error:
636 LogString(_("Error: ") + szString, t);
637 break;
638
639 case wxLOG_Warning:
640 LogString(_("Warning: ") + szString, t);
641 break;
642
643 case wxLOG_Info:
644 if ( GetVerbose() )
645 case wxLOG_Message:
646 case wxLOG_Status:
647 default: // log unknown log levels too
648 LogString(szString, t);
649 break;
650
651 case wxLOG_Trace:
652 case wxLOG_Debug:
653#ifdef __WXDEBUG__
654 {
655 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
656 : wxT("Debug: ");
657 msg << szString;
658 LogString(msg, t);
659 }
660#endif // Debug
661 break;
662 }
663}
664
665void wxLog::DoLogString(const wxString& szString, time_t t)
666{
667#if WXWIN_COMPATIBILITY_2_8
668 // DoLogString() signature changed since 2.8, so we call the old versions
669 // here so that existing custom log classes still work; unfortunately this
670 // also means that we can't have the wxFAIL_MSG below in compat mode
671 DoLogString((const char*)szString.mb_str(), t);
672 DoLogString((const wchar_t*)szString.wc_str(), t);
673#else
674 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
675 wxUnusedVar(szString);
676 wxUnusedVar(t);
677#endif
678}
679
680void wxLog::Flush()
681{
682 // nothing to do here
683}
684
685/*static*/ bool wxLog::IsAllowedTraceMask(const wxString& mask)
686{
687 for ( wxArrayString::iterator it = ms_aTraceMasks.begin(),
688 en = ms_aTraceMasks.end();
689 it != en; ++it )
690 if ( *it == mask)
691 return true;
692 return false;
693}
694
695// ----------------------------------------------------------------------------
696// wxLogBuffer implementation
697// ----------------------------------------------------------------------------
698
699void wxLogBuffer::Flush()
700{
701 if ( !m_str.empty() )
702 {
703 wxMessageOutputBest out;
704 out.Printf(_T("%s"), m_str.c_str());
705 m_str.clear();
706 }
707}
708
709void wxLogBuffer::DoLog(wxLogLevel level, const wxString& szString, time_t t)
710{
711 switch ( level )
712 {
713 case wxLOG_Trace:
714 case wxLOG_Debug:
715#ifdef __WXDEBUG__
716 // don't put debug messages in the buffer, we don't want to show
717 // them to the user in a msg box, log them immediately
718 {
719 wxString str;
720 TimeStamp(&str);
721 str += szString;
722
723 wxMessageOutputDebug dbgout;
724 dbgout.Printf(_T("%s\n"), str.c_str());
725 }
726#endif // __WXDEBUG__
727 break;
728
729 default:
730 wxLog::DoLog(level, szString, t);
731 }
732}
733
734void wxLogBuffer::DoLogString(const wxString& szString, time_t WXUNUSED(t))
735{
736 m_str << szString << _T("\n");
737}
738
739// ----------------------------------------------------------------------------
740// wxLogStderr class implementation
741// ----------------------------------------------------------------------------
742
743wxLogStderr::wxLogStderr(FILE *fp)
744{
745 if ( fp == NULL )
746 m_fp = stderr;
747 else
748 m_fp = fp;
749}
750
751void wxLogStderr::DoLogString(const wxString& szString, time_t WXUNUSED(t))
752{
753 wxString str;
754 TimeStamp(&str);
755 str << szString;
756
757 wxFputs(str, m_fp);
758 wxFputc(_T('\n'), m_fp);
759 fflush(m_fp);
760
761 // under GUI systems such as Windows or Mac, programs usually don't have
762 // stderr at all, so show the messages also somewhere else, typically in
763 // the debugger window so that they go at least somewhere instead of being
764 // simply lost
765 if ( m_fp == stderr )
766 {
767 wxAppTraits *traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
768 if ( traits && !traits->HasStderr() )
769 {
770 wxMessageOutputDebug dbgout;
771 dbgout.Printf(_T("%s\n"), str.c_str());
772 }
773 }
774}
775
776// ----------------------------------------------------------------------------
777// wxLogStream implementation
778// ----------------------------------------------------------------------------
779
780#if wxUSE_STD_IOSTREAM
781#include "wx/ioswrap.h"
782wxLogStream::wxLogStream(wxSTD ostream *ostr)
783{
784 if ( ostr == NULL )
785 m_ostr = &wxSTD cerr;
786 else
787 m_ostr = ostr;
788}
789
790void wxLogStream::DoLogString(const wxString& szString, time_t WXUNUSED(t))
791{
792 wxString stamp;
793 TimeStamp(&stamp);
794 (*m_ostr) << stamp << szString << wxSTD endl;
795}
796#endif // wxUSE_STD_IOSTREAM
797
798// ----------------------------------------------------------------------------
799// wxLogChain
800// ----------------------------------------------------------------------------
801
802wxLogChain::wxLogChain(wxLog *logger)
803{
804 m_bPassMessages = true;
805
806 m_logNew = logger;
807 m_logOld = wxLog::SetActiveTarget(this);
808}
809
810wxLogChain::~wxLogChain()
811{
812 delete m_logOld;
813
814 if ( m_logNew != this )
815 delete m_logNew;
816}
817
818void wxLogChain::SetLog(wxLog *logger)
819{
820 if ( m_logNew != this )
821 delete m_logNew;
822
823 m_logNew = logger;
824}
825
826void wxLogChain::Flush()
827{
828 if ( m_logOld )
829 m_logOld->Flush();
830
831 // be careful to avoid infinite recursion
832 if ( m_logNew && m_logNew != this )
833 m_logNew->Flush();
834}
835
836void wxLogChain::DoLog(wxLogLevel level, const wxString& szString, time_t t)
837{
838 // let the previous logger show it
839 if ( m_logOld && IsPassingMessages() )
840 {
841 // bogus cast just to access protected DoLog
842 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
843 }
844
845 if ( m_logNew && m_logNew != this )
846 {
847 // as above...
848 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
849 }
850}
851
852// ----------------------------------------------------------------------------
853// wxLogPassThrough
854// ----------------------------------------------------------------------------
855
856#ifdef __VISUALC__
857 // "'this' : used in base member initializer list" - so what?
858 #pragma warning(disable:4355)
859#endif // VC++
860
861wxLogPassThrough::wxLogPassThrough()
862 : wxLogChain(this)
863{
864}
865
866#ifdef __VISUALC__
867 #pragma warning(default:4355)
868#endif // VC++
869
870// ============================================================================
871// Global functions/variables
872// ============================================================================
873
874// ----------------------------------------------------------------------------
875// static variables
876// ----------------------------------------------------------------------------
877
878bool wxLog::ms_bRepetCounting = false;
879wxString wxLog::ms_prevString;
880unsigned int wxLog::ms_prevCounter = 0;
881time_t wxLog::ms_prevTimeStamp= 0;
882wxLogLevel wxLog::ms_prevLevel;
883
884wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
885bool wxLog::ms_doLog = true;
886bool wxLog::ms_bAutoCreate = true;
887bool wxLog::ms_bVerbose = false;
888
889wxLogLevel wxLog::ms_logLevel = wxLOG_Max; // log everything by default
890
891size_t wxLog::ms_suspendCount = 0;
892
893const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
894
895wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
896wxArrayString wxLog::ms_aTraceMasks;
897
898// ----------------------------------------------------------------------------
899// stdout error logging helper
900// ----------------------------------------------------------------------------
901
902// helper function: wraps the message and justifies it under given position
903// (looks more pretty on the terminal). Also adds newline at the end.
904//
905// TODO this is now disabled until I find a portable way of determining the
906// terminal window size (ok, I found it but does anybody really cares?)
907#ifdef LOG_PRETTY_WRAP
908static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
909{
910 size_t nMax = 80; // FIXME
911 size_t nStart = strlen(pszPrefix);
912 fputs(pszPrefix, f);
913
914 size_t n;
915 while ( *psz != '\0' ) {
916 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
917 putc(*psz++, f);
918
919 // wrapped?
920 if ( *psz != '\0' ) {
921 /*putc('\n', f);*/
922 for ( n = 0; n < nStart; n++ )
923 putc(' ', f);
924
925 // as we wrapped, squeeze all white space
926 while ( isspace(*psz) )
927 psz++;
928 }
929 }
930
931 putc('\n', f);
932}
933#endif //LOG_PRETTY_WRAP
934
935// ----------------------------------------------------------------------------
936// error code/error message retrieval functions
937// ----------------------------------------------------------------------------
938
939// get error code from syste
940unsigned long wxSysErrorCode()
941{
942#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
943 return ::GetLastError();
944#else //Unix
945 return errno;
946#endif //Win/Unix
947}
948
949// get error message from system
950const wxChar *wxSysErrorMsg(unsigned long nErrCode)
951{
952 if ( nErrCode == 0 )
953 nErrCode = wxSysErrorCode();
954
955#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
956 static wxChar s_szBuf[1024];
957
958 // get error message from system
959 LPVOID lpMsgBuf;
960 if ( ::FormatMessage
961 (
962 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
963 NULL,
964 nErrCode,
965 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
966 (LPTSTR)&lpMsgBuf,
967 0,
968 NULL
969 ) == 0 )
970 {
971 // if this happens, something is seriously wrong, so don't use _() here
972 // for safety
973 wxSprintf(s_szBuf, _T("unknown error %lx"), nErrCode);
974 return s_szBuf;
975 }
976
977
978 // copy it to our buffer and free memory
979 // Crashes on SmartPhone (FIXME)
980#if !defined(__SMARTPHONE__) /* of WinCE */
981 if( lpMsgBuf != 0 )
982 {
983 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
984 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
985
986 LocalFree(lpMsgBuf);
987
988 // returned string is capitalized and ended with '\r\n' - bad
989 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
990 size_t len = wxStrlen(s_szBuf);
991 if ( len > 0 ) {
992 // truncate string
993 if ( s_szBuf[len - 2] == wxT('\r') )
994 s_szBuf[len - 2] = wxT('\0');
995 }
996 }
997 else
998#endif // !__SMARTPHONE__
999 {
1000 s_szBuf[0] = wxT('\0');
1001 }
1002
1003 return s_szBuf;
1004#else // !__WXMSW__
1005 #if wxUSE_UNICODE
1006 static wchar_t s_wzBuf[1024];
1007 wxConvCurrent->MB2WC(s_wzBuf, strerror((int)nErrCode),
1008 WXSIZEOF(s_wzBuf) - 1);
1009 return s_wzBuf;
1010 #else
1011 return strerror((int)nErrCode);
1012 #endif
1013#endif // __WXMSW__/!__WXMSW__
1014}
1015
1016#endif // wxUSE_LOG