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