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