]> git.saurik.com Git - wxWidgets.git/blob - src/common/log.cpp
ensure minimal mime support in wxHTML even without wxMimeTypesMananger
[wxWidgets.git] / src / common / log.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "log.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 // wxWindows
32 #ifndef WX_PRECOMP
33 #include "wx/string.h"
34 #include "wx/intl.h"
35 #include "wx/app.h"
36
37 #if wxUSE_GUI
38 #include "wx/window.h"
39 #ifdef __WXMSW__
40 #include "wx/msw/private.h"
41 #endif
42 #endif
43 #endif //WX_PRECOMP
44
45 #include "wx/file.h"
46 #include "wx/textfile.h"
47 #include "wx/utils.h"
48 #include "wx/wxchar.h"
49 #include "wx/log.h"
50 #include "wx/thread.h"
51
52 #if wxUSE_LOG
53
54 // other standard headers
55 #include <errno.h>
56 #include <stdlib.h>
57 #include <time.h>
58
59 #if defined(__WXMSW__)
60 #include "wx/msw/private.h" // includes windows.h for OutputDebugString
61 #endif
62
63 #if defined(__WXMAC__)
64 #include "wx/mac/private.h" // includes mac headers
65 #endif
66
67 // ----------------------------------------------------------------------------
68 // non member functions
69 // ----------------------------------------------------------------------------
70
71 // define this to enable wrapping of log messages
72 //#define LOG_PRETTY_WRAP
73
74 #ifdef LOG_PRETTY_WRAP
75 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
76 #endif
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // globals
84 // ----------------------------------------------------------------------------
85
86 // log functions can't allocate memory (LogError("out of memory...") should
87 // work!), so we use a static buffer for all log messages
88 #define LOG_BUFFER_SIZE (4096)
89
90 // static buffer for error messages
91 static wxChar s_szBuf[LOG_BUFFER_SIZE];
92
93 #if wxUSE_THREADS
94
95 // the critical section protecting the static buffer
96 static wxCriticalSection gs_csLogBuf;
97
98 #endif // wxUSE_THREADS
99
100 // return true if we have a non NULL non disabled log target
101 static inline bool IsLoggingEnabled()
102 {
103 return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL);
104 }
105
106 // ----------------------------------------------------------------------------
107 // implementation of Log functions
108 //
109 // NB: unfortunately we need all these distinct functions, we can't make them
110 // macros and not all compilers inline vararg functions.
111 // ----------------------------------------------------------------------------
112
113 // generic log function
114 void wxVLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr)
115 {
116 if ( IsLoggingEnabled() ) {
117 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
118
119 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
120
121 wxLog::OnLog(level, s_szBuf, time(NULL));
122 }
123 }
124
125 void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
126 {
127 va_list argptr;
128 va_start(argptr, szFormat);
129 wxVLogGeneric(level, szFormat, argptr);
130 va_end(argptr);
131 }
132
133 #define IMPLEMENT_LOG_FUNCTION(level) \
134 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
135 { \
136 if ( IsLoggingEnabled() ) { \
137 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
138 \
139 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
140 \
141 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
142 } \
143 } \
144 void wxLog##level(const wxChar *szFormat, ...) \
145 { \
146 va_list argptr; \
147 va_start(argptr, szFormat); \
148 wxVLog##level(szFormat, argptr); \
149 va_end(argptr); \
150 }
151
152 IMPLEMENT_LOG_FUNCTION(FatalError)
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 // same as info, but only if 'verbose' mode is on
160 void wxVLogVerbose(const wxChar *szFormat, va_list argptr)
161 {
162 if ( IsLoggingEnabled() ) {
163 wxLog *pLog = wxLog::GetActiveTarget();
164 if ( pLog != NULL && pLog->GetVerbose() ) {
165 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
166
167 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
168
169 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
170 }
171 }
172 }
173
174 void wxLogVerbose(const wxChar *szFormat, ...)
175 {
176 va_list argptr;
177 va_start(argptr, szFormat);
178 wxVLogVerbose(szFormat, argptr);
179 va_end(argptr);
180 }
181
182 // debug functions
183 #ifdef __WXDEBUG__
184 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
185 void wxVLog##level(const wxChar *szFormat, va_list argptr) \
186 { \
187 if ( IsLoggingEnabled() ) { \
188 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
189 \
190 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
191 \
192 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
193 } \
194 } \
195 void wxLog##level(const wxChar *szFormat, ...) \
196 { \
197 va_list argptr; \
198 va_start(argptr, szFormat); \
199 wxVLog##level(szFormat, argptr); \
200 va_end(argptr); \
201 }
202
203 void wxVLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
204 {
205 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
206 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
207
208 wxChar *p = s_szBuf;
209 size_t len = WXSIZEOF(s_szBuf);
210 wxStrncpy(s_szBuf, _T("("), len);
211 len -= 1; // strlen("(")
212 p += 1;
213 wxStrncat(p, mask, len);
214 size_t lenMask = wxStrlen(mask);
215 len -= lenMask;
216 p += lenMask;
217
218 wxStrncat(p, _T(") "), len);
219 len -= 2;
220 p += 2;
221
222 wxVsnprintf(p, len, szFormat, argptr);
223
224 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
225 }
226 }
227
228 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
229 {
230 va_list argptr;
231 va_start(argptr, szFormat);
232 wxVLogTrace(mask, szFormat, argptr);
233 va_end(argptr);
234 }
235
236 void wxVLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
237 {
238 // we check that all of mask bits are set in the current mask, so
239 // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
240 // if both bits are set.
241 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
242 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
243
244 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
245
246 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
247 }
248 }
249
250 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
251 {
252 va_list argptr;
253 va_start(argptr, szFormat);
254 wxVLogTrace(mask, szFormat, argptr);
255 va_end(argptr);
256 }
257
258 #else // release
259 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
260 #endif
261
262 IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
263 IMPLEMENT_LOG_DEBUG_FUNCTION(Trace)
264
265 // wxLogSysError: one uses the last error code, for other you must give it
266 // explicitly
267
268 // common part of both wxLogSysError
269 void wxLogSysErrorHelper(long lErrCode)
270 {
271 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
272 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
273 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
274 wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf));
275
276 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
277 }
278
279 void WXDLLEXPORT wxVLogSysError(const wxChar *szFormat, va_list argptr)
280 {
281 if ( IsLoggingEnabled() ) {
282 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
283
284 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
285
286 wxLogSysErrorHelper(wxSysErrorCode());
287 }
288 }
289
290 void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
291 {
292 va_list argptr;
293 va_start(argptr, szFormat);
294 wxVLogSysError(szFormat, argptr);
295 va_end(argptr);
296 }
297
298 void WXDLLEXPORT wxVLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr)
299 {
300 if ( IsLoggingEnabled() ) {
301 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
302
303 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
304
305 wxLogSysErrorHelper(lErrCode);
306 }
307 }
308
309 void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
310 {
311 va_list argptr;
312 va_start(argptr, szFormat);
313 wxVLogSysError(lErrCode, szFormat, argptr);
314 va_end(argptr);
315 }
316
317 // ----------------------------------------------------------------------------
318 // wxLog class implementation
319 // ----------------------------------------------------------------------------
320
321 wxLog::wxLog()
322 {
323 m_bHasMessages = FALSE;
324 }
325
326 wxLog *wxLog::GetActiveTarget()
327 {
328 if ( ms_bAutoCreate && ms_pLogger == NULL ) {
329 // prevent infinite recursion if someone calls wxLogXXX() from
330 // wxApp::CreateLogTarget()
331 static bool s_bInGetActiveTarget = FALSE;
332 if ( !s_bInGetActiveTarget ) {
333 s_bInGetActiveTarget = TRUE;
334
335 // ask the application to create a log target for us
336 if ( wxTheApp != NULL )
337 ms_pLogger = wxTheApp->CreateLogTarget();
338 else
339 ms_pLogger = new wxLogStderr;
340
341 s_bInGetActiveTarget = FALSE;
342
343 // do nothing if it fails - what can we do?
344 }
345 }
346
347 return ms_pLogger;
348 }
349
350 wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
351 {
352 if ( ms_pLogger != NULL ) {
353 // flush the old messages before changing because otherwise they might
354 // get lost later if this target is not restored
355 ms_pLogger->Flush();
356 }
357
358 wxLog *pOldLogger = ms_pLogger;
359 ms_pLogger = pLogger;
360
361 return pOldLogger;
362 }
363
364 void wxLog::DontCreateOnDemand()
365 {
366 ms_bAutoCreate = FALSE;
367
368 // this is usually called at the end of the program and we assume that it
369 // is *always* called at the end - so we free memory here to avoid false
370 // memory leak reports from wxWin memory tracking code
371 ClearTraceMasks();
372 }
373
374 void wxLog::RemoveTraceMask(const wxString& str)
375 {
376 int index = ms_aTraceMasks.Index(str);
377 if ( index != wxNOT_FOUND )
378 ms_aTraceMasks.Remove((size_t)index);
379 }
380
381 void wxLog::ClearTraceMasks()
382 {
383 ms_aTraceMasks.Clear();
384 }
385
386 void wxLog::TimeStamp(wxString *str)
387 {
388 if ( ms_timestamp )
389 {
390 wxChar buf[256];
391 time_t timeNow;
392 (void)time(&timeNow);
393 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
394
395 str->Empty();
396 *str << buf << wxT(": ");
397 }
398 }
399
400 void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
401 {
402 switch ( level ) {
403 case wxLOG_FatalError:
404 DoLogString(wxString(_("Fatal error: ")) + szString, t);
405 DoLogString(_("Program aborted."), t);
406 Flush();
407 abort();
408 break;
409
410 case wxLOG_Error:
411 DoLogString(wxString(_("Error: ")) + szString, t);
412 break;
413
414 case wxLOG_Warning:
415 DoLogString(wxString(_("Warning: ")) + szString, t);
416 break;
417
418 case wxLOG_Info:
419 if ( GetVerbose() )
420 case wxLOG_Message:
421 case wxLOG_Status:
422 default: // log unknown log levels too
423 DoLogString(szString, t);
424 break;
425
426 case wxLOG_Trace:
427 case wxLOG_Debug:
428 #ifdef __WXDEBUG__
429 {
430 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
431 : wxT("Debug: ");
432 msg << szString;
433 DoLogString(msg, t);
434 }
435 #endif // Debug
436 break;
437 }
438 }
439
440 void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
441 {
442 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
443 }
444
445 void wxLog::Flush()
446 {
447 // remember that we don't have any more messages to show
448 m_bHasMessages = FALSE;
449 }
450
451 // ----------------------------------------------------------------------------
452 // wxLogStderr class implementation
453 // ----------------------------------------------------------------------------
454
455 wxLogStderr::wxLogStderr(FILE *fp)
456 {
457 if ( fp == NULL )
458 m_fp = stderr;
459 else
460 m_fp = fp;
461 }
462
463 #if defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
464
465 #if !TARGET_API_MAC_CARBON
466 // MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
467
468 #ifndef __MetroNubUtils__
469 #include "MetroNubUtils.h"
470 #endif
471
472 #ifdef __cplusplus
473 extern "C" {
474 #endif
475
476 #ifndef __GESTALT__
477 #include <Gestalt.h>
478 #endif
479
480 #ifndef true
481 #define true 1
482 #endif
483
484 #ifndef false
485 #define false 0
486 #endif
487
488 #if TARGET_API_MAC_CARBON
489
490 #include <CodeFragments.h>
491
492 EXTERN_API_C( long )
493 CallUniversalProc(UniversalProcPtr theProcPtr, ProcInfoType procInfo, ...);
494
495 ProcPtr gCallUniversalProc_Proc = NULL;
496
497 #endif
498
499 static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
500
501 static long fRunOnce = false;
502
503 Boolean IsCompatibleVersion(short inVersion);
504
505 /* ---------------------------------------------------------------------------
506 IsCompatibleVersion
507 --------------------------------------------------------------------------- */
508
509 Boolean IsCompatibleVersion(short inVersion)
510 {
511 Boolean result = false;
512
513 if (fRunOnce)
514 {
515 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
516
517 result = (inVersion <= block->apiHiVersion);
518 }
519
520 return result;
521 }
522
523 /* ---------------------------------------------------------------------------
524 IsMetroNubInstalled
525 --------------------------------------------------------------------------- */
526
527 Boolean IsMetroNubInstalled()
528 {
529 if (!fRunOnce)
530 {
531 long result, value;
532
533 fRunOnce = true;
534 gMetroNubEntry = NULL;
535
536 if (Gestalt(gestaltSystemVersion, &value) == noErr && value < 0x1000)
537 {
538 /* look for MetroNub's Gestalt selector */
539 if (Gestalt(kMetroNubUserSignature, &result) == noErr)
540 {
541
542 #if TARGET_API_MAC_CARBON
543 if (gCallUniversalProc_Proc == NULL)
544 {
545 CFragConnectionID connectionID;
546 Ptr mainAddress;
547 Str255 errorString;
548 ProcPtr symbolAddress;
549 OSErr err;
550 CFragSymbolClass symbolClass;
551
552 symbolAddress = NULL;
553 err = GetSharedLibrary("\pInterfaceLib", kPowerPCCFragArch, kFindCFrag,
554 &connectionID, &mainAddress, errorString);
555
556 if (err != noErr)
557 {
558 gCallUniversalProc_Proc = NULL;
559 goto end;
560 }
561
562 err = FindSymbol(connectionID, "\pCallUniversalProc",
563 (Ptr *) &gCallUniversalProc_Proc, &symbolClass);
564
565 if (err != noErr)
566 {
567 gCallUniversalProc_Proc = NULL;
568 goto end;
569 }
570 }
571 #endif
572
573 {
574 MetroNubUserEntryBlock* block = (MetroNubUserEntryBlock *)result;
575
576 /* make sure the version of the API is compatible */
577 if (block->apiLowVersion <= kMetroNubUserAPIVersion &&
578 kMetroNubUserAPIVersion <= block->apiHiVersion)
579 gMetroNubEntry = block; /* success! */
580 }
581
582 }
583 }
584 }
585
586 end:
587
588 #if TARGET_API_MAC_CARBON
589 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
590 #else
591 return (gMetroNubEntry != NULL);
592 #endif
593 }
594
595 /* ---------------------------------------------------------------------------
596 IsMWDebuggerRunning [v1 API]
597 --------------------------------------------------------------------------- */
598
599 Boolean IsMWDebuggerRunning()
600 {
601 if (IsMetroNubInstalled())
602 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
603 else
604 return false;
605 }
606
607 /* ---------------------------------------------------------------------------
608 AmIBeingMWDebugged [v1 API]
609 --------------------------------------------------------------------------- */
610
611 Boolean AmIBeingMWDebugged()
612 {
613 if (IsMetroNubInstalled())
614 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
615 else
616 return false;
617 }
618
619 /* ---------------------------------------------------------------------------
620 UserSetWatchPoint [v2 API]
621 --------------------------------------------------------------------------- */
622
623 OSErr UserSetWatchPoint (Ptr address, long length, WatchPointIDT* watchPointID)
624 {
625 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
626 return CallUserSetWatchPointProc(gMetroNubEntry->userSetWatchPoint,
627 address, length, watchPointID);
628 else
629 return errProcessIsNotClient;
630 }
631
632 /* ---------------------------------------------------------------------------
633 ClearWatchPoint [v2 API]
634 --------------------------------------------------------------------------- */
635
636 OSErr ClearWatchPoint (WatchPointIDT watchPointID)
637 {
638 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
639 return CallClearWatchPointProc(gMetroNubEntry->clearWatchPoint,
640 watchPointID);
641 else
642 return errProcessIsNotClient;
643 }
644
645 #ifdef __cplusplus
646 }
647 #endif
648
649 #endif // !TARGET_API_MAC_CARBON
650
651 #endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
652
653 void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
654 {
655 wxString str;
656 TimeStamp(&str);
657 str << szString;
658
659 fputs(str.mb_str(), m_fp);
660 fputc(_T('\n'), m_fp);
661 fflush(m_fp);
662
663 // under Windows, programs usually don't have stderr at all, so show the
664 // messages also under debugger - unless it's a console program
665 #if defined(__WXMSW__) && wxUSE_GUI && !defined(__WXMICROWIN__)
666 str += wxT("\r\n") ;
667 OutputDebugString(str.c_str());
668 #endif // MSW
669 #if defined(__WXMAC__) && !defined(__DARWIN__) && wxUSE_GUI
670 Str255 pstr ;
671 strcpy( (char*) pstr , str.c_str() ) ;
672 strcat( (char*) pstr , ";g" ) ;
673 c2pstr( (char*) pstr ) ;
674
675 Boolean running = false ;
676
677 #if !TARGET_API_MAC_CARBON && (__MWERKS__ > 0x5300)
678
679 if ( IsMWDebuggerRunning() && AmIBeingMWDebugged() )
680 {
681 running = true ;
682 }
683
684 #endif
685
686 if (running)
687 {
688 #ifdef __powerc
689 DebugStr(pstr);
690 #else
691 SysBreakStr(pstr);
692 #endif
693 }
694 #endif // Mac
695 }
696
697 // ----------------------------------------------------------------------------
698 // wxLogStream implementation
699 // ----------------------------------------------------------------------------
700
701 #if wxUSE_STD_IOSTREAM
702 wxLogStream::wxLogStream(wxSTD ostream *ostr)
703 {
704 if ( ostr == NULL )
705 m_ostr = &wxSTD cerr;
706 else
707 m_ostr = ostr;
708 }
709
710 void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
711 {
712 wxString str;
713 TimeStamp(&str);
714 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
715 }
716 #endif // wxUSE_STD_IOSTREAM
717
718 // ----------------------------------------------------------------------------
719 // wxLogChain
720 // ----------------------------------------------------------------------------
721
722 wxLogChain::wxLogChain(wxLog *logger)
723 {
724 m_logNew = logger;
725 m_logOld = wxLog::SetActiveTarget(this);
726 }
727
728 void wxLogChain::SetLog(wxLog *logger)
729 {
730 if ( m_logNew != this )
731 delete m_logNew;
732
733 wxLog::SetActiveTarget(logger);
734
735 m_logNew = logger;
736 }
737
738 void wxLogChain::Flush()
739 {
740 if ( m_logOld )
741 m_logOld->Flush();
742
743 // be careful to avoid inifinite recursion
744 if ( m_logNew && m_logNew != this )
745 m_logNew->Flush();
746 }
747
748 void wxLogChain::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
749 {
750 // let the previous logger show it
751 if ( m_logOld && IsPassingMessages() )
752 {
753 // bogus cast just to access protected DoLog
754 ((wxLogChain *)m_logOld)->DoLog(level, szString, t);
755 }
756
757 if ( m_logNew && m_logNew != this )
758 {
759 // as above...
760 ((wxLogChain *)m_logNew)->DoLog(level, szString, t);
761 }
762 }
763
764 // ----------------------------------------------------------------------------
765 // wxLogPassThrough
766 // ----------------------------------------------------------------------------
767
768 #ifdef __VISUALC__
769 // "'this' : used in base member initializer list" - so what?
770 #pragma warning(disable:4355)
771 #endif // VC++
772
773 wxLogPassThrough::wxLogPassThrough()
774 : wxLogChain(this)
775 {
776 }
777
778 #ifdef __VISUALC__
779 #pragma warning(default:4355)
780 #endif // VC++
781
782 // ============================================================================
783 // Global functions/variables
784 // ============================================================================
785
786 // ----------------------------------------------------------------------------
787 // static variables
788 // ----------------------------------------------------------------------------
789
790 wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
791 bool wxLog::ms_doLog = TRUE;
792 bool wxLog::ms_bAutoCreate = TRUE;
793 bool wxLog::ms_bVerbose = FALSE;
794
795 size_t wxLog::ms_suspendCount = 0;
796
797 #if wxUSE_GUI
798 const wxChar *wxLog::ms_timestamp = wxT("%X"); // time only, no date
799 #else
800 const wxChar *wxLog::ms_timestamp = NULL; // save space
801 #endif
802
803 wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
804 wxArrayString wxLog::ms_aTraceMasks;
805
806 // ----------------------------------------------------------------------------
807 // stdout error logging helper
808 // ----------------------------------------------------------------------------
809
810 // helper function: wraps the message and justifies it under given position
811 // (looks more pretty on the terminal). Also adds newline at the end.
812 //
813 // TODO this is now disabled until I find a portable way of determining the
814 // terminal window size (ok, I found it but does anybody really cares?)
815 #ifdef LOG_PRETTY_WRAP
816 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
817 {
818 size_t nMax = 80; // FIXME
819 size_t nStart = strlen(pszPrefix);
820 fputs(pszPrefix, f);
821
822 size_t n;
823 while ( *psz != '\0' ) {
824 for ( n = nStart; (n < nMax) && (*psz != '\0'); n++ )
825 putc(*psz++, f);
826
827 // wrapped?
828 if ( *psz != '\0' ) {
829 /*putc('\n', f);*/
830 for ( n = 0; n < nStart; n++ )
831 putc(' ', f);
832
833 // as we wrapped, squeeze all white space
834 while ( isspace(*psz) )
835 psz++;
836 }
837 }
838
839 putc('\n', f);
840 }
841 #endif //LOG_PRETTY_WRAP
842
843 // ----------------------------------------------------------------------------
844 // error code/error message retrieval functions
845 // ----------------------------------------------------------------------------
846
847 // get error code from syste
848 unsigned long wxSysErrorCode()
849 {
850 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
851 #ifdef __WIN32__
852 return ::GetLastError();
853 #else //WIN16
854 // TODO what to do on Windows 3.1?
855 return 0;
856 #endif //WIN16/32
857 #else //Unix
858 return errno;
859 #endif //Win/Unix
860 }
861
862 // get error message from system
863 const wxChar *wxSysErrorMsg(unsigned long nErrCode)
864 {
865 if ( nErrCode == 0 )
866 nErrCode = wxSysErrorCode();
867
868 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
869 #ifdef __WIN32__
870 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
871
872 // get error message from system
873 LPVOID lpMsgBuf;
874 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
875 NULL, nErrCode,
876 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
877 (LPTSTR)&lpMsgBuf,
878 0, NULL);
879
880 // copy it to our buffer and free memory
881 if( lpMsgBuf != 0 ) {
882 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
883 s_szBuf[WXSIZEOF(s_szBuf) - 1] = wxT('\0');
884
885 LocalFree(lpMsgBuf);
886
887 // returned string is capitalized and ended with '\r\n' - bad
888 s_szBuf[0] = (wxChar)wxTolower(s_szBuf[0]);
889 size_t len = wxStrlen(s_szBuf);
890 if ( len > 0 ) {
891 // truncate string
892 if ( s_szBuf[len - 2] == wxT('\r') )
893 s_szBuf[len - 2] = wxT('\0');
894 }
895 }
896 else {
897 s_szBuf[0] = wxT('\0');
898 }
899
900 return s_szBuf;
901 #else //Win16
902 // TODO
903 return NULL;
904 #endif // Win16/32
905 #else // Unix
906 #if wxUSE_UNICODE
907 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
908 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
909 return s_szBuf;
910 #else
911 return strerror((int)nErrCode);
912 #endif
913 #endif // Win/Unix
914 }
915
916 #endif //wxUSE_LOG