]> git.saurik.com Git - wxWidgets.git/blame - src/common/log.cpp
added wxFont::IsFixedWidth(), documented it and implemented for wxGTK/Motif
[wxWidgets.git] / src / common / log.cpp
CommitLineData
c801d85f
KB
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// ----------------------------------------------------------------------------
dd85fc6b 19
c801d85f
KB
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
e90c1d2a
VZ
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
e90c1d2a 42 #endif
9ef3052c 43#endif //WX_PRECOMP
c801d85f 44
dbf3cd7a
RR
45#include "wx/file.h"
46#include "wx/textfile.h"
47#include "wx/utils.h"
a324a7bc 48#include "wx/wxchar.h"
dbf3cd7a 49#include "wx/log.h"
b568d04f 50#include "wx/thread.h"
c801d85f 51
f94dfb38
VS
52#if wxUSE_LOG
53
c801d85f
KB
54// other standard headers
55#include <errno.h>
56#include <stdlib.h>
57#include <time.h>
58
8cb172b4 59#if defined(__WXMSW__)
b568d04f 60 #include "wx/msw/private.h" // includes windows.h for OutputDebugString
8cb172b4
JS
61#endif
62
76a5e5d2
SC
63#if defined(__WXMAC__)
64 #include "wx/mac/private.h" // includes mac headers
65#endif
66
c801d85f
KB
67// ----------------------------------------------------------------------------
68// non member functions
69// ----------------------------------------------------------------------------
70
71// define this to enable wrapping of log messages
72//#define LOG_PRETTY_WRAP
73
9ef3052c 74#ifdef LOG_PRETTY_WRAP
c801d85f
KB
75 static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
76#endif
77
78// ============================================================================
79// implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
b568d04f 83// globals
c801d85f
KB
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
b568d04f 90// static buffer for error messages
50920146 91static wxChar s_szBuf[LOG_BUFFER_SIZE];
c801d85f 92
b568d04f
VZ
93#if wxUSE_THREADS
94
95// the critical section protecting the static buffer
96static wxCriticalSection gs_csLogBuf;
97
98#endif // wxUSE_THREADS
99
807a903e
VZ
100// return true if we have a non NULL non disabled log target
101static inline bool IsLoggingEnabled()
102{
103 return wxLog::IsEnabled() && (wxLog::GetActiveTarget() != NULL);
104}
105
b568d04f
VZ
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
c801d85f 113// generic log function
ea44a631 114void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, va_list argptr)
c801d85f 115{
807a903e
VZ
116 if ( IsLoggingEnabled() ) {
117 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
9ef3052c 118
807a903e 119 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
807a903e
VZ
120
121 wxLog::OnLog(level, s_szBuf, time(NULL));
122 }
c801d85f
KB
123}
124
ea44a631
GD
125void wxLogGeneric(wxLogLevel level, const wxChar *szFormat, ...)
126{
127 va_list argptr;
128 va_start(argptr, szFormat);
129 wxLogGeneric(level, szFormat, argptr);
130 va_end(argptr);
131}
132
807a903e 133#define IMPLEMENT_LOG_FUNCTION(level) \
ea44a631 134 void wxLog##level(const wxChar *szFormat, va_list argptr) \
807a903e
VZ
135 { \
136 if ( IsLoggingEnabled() ) { \
137 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
138 \
807a903e 139 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
807a903e
VZ
140 \
141 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
142 } \
ea44a631
GD
143 } \
144 void wxLog##level(const wxChar *szFormat, ...) \
145 { \
146 va_list argptr; \
147 va_start(argptr, szFormat); \
148 wxLog##level(szFormat, argptr); \
149 va_end(argptr); \
c801d85f
KB
150 }
151
152IMPLEMENT_LOG_FUNCTION(FatalError)
153IMPLEMENT_LOG_FUNCTION(Error)
154IMPLEMENT_LOG_FUNCTION(Warning)
155IMPLEMENT_LOG_FUNCTION(Message)
156IMPLEMENT_LOG_FUNCTION(Info)
157IMPLEMENT_LOG_FUNCTION(Status)
158
9ef3052c 159// same as info, but only if 'verbose' mode is on
ea44a631 160void wxLogVerbose(const wxChar *szFormat, va_list argptr)
9ef3052c 161{
807a903e
VZ
162 if ( IsLoggingEnabled() ) {
163 wxLog *pLog = wxLog::GetActiveTarget();
164 if ( pLog != NULL && pLog->GetVerbose() ) {
165 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
166
807a903e 167 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
807a903e
VZ
168
169 wxLog::OnLog(wxLOG_Info, s_szBuf, time(NULL));
170 }
171 }
9ef3052c
VZ
172}
173
ea44a631
GD
174void wxLogVerbose(const wxChar *szFormat, ...)
175{
176 va_list argptr;
177 va_start(argptr, szFormat);
178 wxLogVerbose(szFormat, argptr);
179 va_end(argptr);
180}
181
9ef3052c 182// debug functions
b2aef89b 183#ifdef __WXDEBUG__
807a903e 184#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \
ea44a631 185 void wxLog##level(const wxChar *szFormat, va_list argptr) \
807a903e
VZ
186 { \
187 if ( IsLoggingEnabled() ) { \
188 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf); \
189 \
807a903e 190 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr); \
807a903e
VZ
191 \
192 wxLog::OnLog(wxLOG_##level, s_szBuf, time(NULL)); \
193 } \
ea44a631
GD
194 } \
195 void wxLog##level(const wxChar *szFormat, ...) \
196 { \
197 va_list argptr; \
198 va_start(argptr, szFormat); \
199 wxLog##level(szFormat, argptr); \
200 va_end(argptr); \
c801d85f
KB
201 }
202
ea44a631 203 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, va_list argptr)
0fb67cd1 204 {
807a903e 205 if ( IsLoggingEnabled() && wxLog::IsAllowedTraceMask(mask) ) {
b568d04f
VZ
206 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
207
00c4e897
VZ
208 wxChar *p = s_szBuf;
209 size_t len = WXSIZEOF(s_szBuf);
f6bcfd97 210 wxStrncpy(s_szBuf, _T("("), len);
829f0541
VZ
211 len -= 1; // strlen("(")
212 p += 1;
f6bcfd97 213 wxStrncat(p, mask, len);
00c4e897
VZ
214 size_t lenMask = wxStrlen(mask);
215 len -= lenMask;
216 p += lenMask;
217
f6bcfd97 218 wxStrncat(p, _T(") "), len);
829f0541
VZ
219 len -= 2;
220 p += 2;
00c4e897 221
00c4e897 222 wxVsnprintf(p, len, szFormat, argptr);
ea44a631 223
0fb67cd1
VZ
224 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
225 }
226 }
227
ea44a631
GD
228 void wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...)
229 {
230 va_list argptr;
231 va_start(argptr, szFormat);
232 wxLogTrace(mask, szFormat, argptr);
233 va_end(argptr);
234 }
235
236 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, va_list argptr)
9ef3052c 237 {
9ef3052c
VZ
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.
807a903e 241 if ( IsLoggingEnabled() && ((wxLog::GetTraceMask() & mask) == mask) ) {
b568d04f
VZ
242 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
243
378b05f7 244 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
9ef3052c 245
0fb67cd1 246 wxLog::OnLog(wxLOG_Trace, s_szBuf, time(NULL));
9ef3052c
VZ
247 }
248 }
249
ea44a631
GD
250 void wxLogTrace(wxTraceMask mask, const wxChar *szFormat, ...)
251 {
252 va_list argptr;
253 va_start(argptr, szFormat);
254 wxLogTrace(mask, szFormat, argptr);
255 va_end(argptr);
256 }
257
9ef3052c
VZ
258#else // release
259 #define IMPLEMENT_LOG_DEBUG_FUNCTION(level)
260#endif
261
262IMPLEMENT_LOG_DEBUG_FUNCTION(Debug)
263IMPLEMENT_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
269void wxLogSysErrorHelper(long lErrCode)
c801d85f 270{
50920146 271 wxChar szErrMsg[LOG_BUFFER_SIZE / 2];
378b05f7
VZ
272 wxSnprintf(szErrMsg, WXSIZEOF(szErrMsg),
273 _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
50920146 274 wxStrncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - wxStrlen(s_szBuf));
c801d85f 275
0fb67cd1 276 wxLog::OnLog(wxLOG_Error, s_szBuf, time(NULL));
9ef3052c 277}
c801d85f 278
ea44a631 279void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, va_list argptr)
9ef3052c 280{
807a903e
VZ
281 if ( IsLoggingEnabled() ) {
282 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 283
807a903e 284 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
9ef3052c 285
807a903e
VZ
286 wxLogSysErrorHelper(wxSysErrorCode());
287 }
c801d85f
KB
288}
289
ea44a631
GD
290void WXDLLEXPORT wxLogSysError(const wxChar *szFormat, ...)
291{
292 va_list argptr;
293 va_start(argptr, szFormat);
294 wxLogSysError(szFormat, argptr);
295 va_end(argptr);
296}
297
298void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, va_list argptr)
c801d85f 299{
807a903e
VZ
300 if ( IsLoggingEnabled() ) {
301 wxCRIT_SECT_LOCKER(locker, gs_csLogBuf);
b568d04f 302
807a903e 303 wxVsnprintf(s_szBuf, WXSIZEOF(s_szBuf), szFormat, argptr);
c801d85f 304
807a903e
VZ
305 wxLogSysErrorHelper(lErrCode);
306 }
c801d85f
KB
307}
308
ea44a631
GD
309void WXDLLEXPORT wxLogSysError(long lErrCode, const wxChar *szFormat, ...)
310{
311 va_list argptr;
312 va_start(argptr, szFormat);
313 wxLogSysError(lErrCode, szFormat, argptr);
314 va_end(argptr);
315}
316
c801d85f
KB
317// ----------------------------------------------------------------------------
318// wxLog class implementation
319// ----------------------------------------------------------------------------
320
321wxLog::wxLog()
322{
0fb67cd1 323 m_bHasMessages = FALSE;
c801d85f
KB
324}
325
9ec05cc9
VZ
326wxLog *wxLog::GetActiveTarget()
327{
0fb67cd1
VZ
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
0fb67cd1
VZ
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;
0fb67cd1
VZ
340
341 s_bInGetActiveTarget = FALSE;
342
343 // do nothing if it fails - what can we do?
344 }
275bf4c1 345 }
c801d85f 346
0fb67cd1 347 return ms_pLogger;
c801d85f
KB
348}
349
c085e333 350wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
9ec05cc9 351{
0fb67cd1
VZ
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 }
c801d85f 357
0fb67cd1
VZ
358 wxLog *pOldLogger = ms_pLogger;
359 ms_pLogger = pLogger;
c085e333 360
0fb67cd1 361 return pOldLogger;
c801d85f
KB
362}
363
36bd6902
VZ
364void 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
0fb67cd1 374void wxLog::RemoveTraceMask(const wxString& str)
c801d85f 375{
0fb67cd1
VZ
376 int index = ms_aTraceMasks.Index(str);
377 if ( index != wxNOT_FOUND )
378 ms_aTraceMasks.Remove((size_t)index);
379}
c801d85f 380
36bd6902
VZ
381void wxLog::ClearTraceMasks()
382{
383 ms_aTraceMasks.Clear();
384}
385
d2e1ef19
VZ
386void wxLog::TimeStamp(wxString *str)
387{
388 if ( ms_timestamp )
389 {
390 wxChar buf[256];
391 time_t timeNow;
392 (void)time(&timeNow);
c49245f8 393 wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
d2e1ef19
VZ
394
395 str->Empty();
223d09f6 396 *str << buf << wxT(": ");
d2e1ef19
VZ
397 }
398}
399
50920146 400void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
0fb67cd1 401{
0fb67cd1
VZ
402 switch ( level ) {
403 case wxLOG_FatalError:
786855a1 404 DoLogString(wxString(_("Fatal error: ")) + szString, t);
0fb67cd1
VZ
405 DoLogString(_("Program aborted."), t);
406 Flush();
407 abort();
408 break;
409
410 case wxLOG_Error:
786855a1 411 DoLogString(wxString(_("Error: ")) + szString, t);
0fb67cd1
VZ
412 break;
413
414 case wxLOG_Warning:
786855a1 415 DoLogString(wxString(_("Warning: ")) + szString, t);
0fb67cd1
VZ
416 break;
417
418 case wxLOG_Info:
0fb67cd1 419 if ( GetVerbose() )
37278984 420 case wxLOG_Message:
87a1e308 421 case wxLOG_Status:
786855a1
VZ
422 default: // log unknown log levels too
423 DoLogString(szString, t);
0fb67cd1
VZ
424 break;
425
426 case wxLOG_Trace:
427 case wxLOG_Debug:
428#ifdef __WXDEBUG__
0131687b
VZ
429 {
430 wxString msg = level == wxLOG_Trace ? wxT("Trace: ")
54a8f42b 431 : wxT("Debug: ");
0131687b
VZ
432 msg << szString;
433 DoLogString(msg, t);
434 }
435#endif // Debug
0fb67cd1 436 break;
0fb67cd1 437 }
c801d85f
KB
438}
439
74e3313b 440void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
c801d85f 441{
223d09f6 442 wxFAIL_MSG(wxT("DoLogString must be overriden if it's called."));
c801d85f
KB
443}
444
445void wxLog::Flush()
446{
03147cd0
VZ
447 // remember that we don't have any more messages to show
448 m_bHasMessages = FALSE;
c801d85f
KB
449}
450
451// ----------------------------------------------------------------------------
452// wxLogStderr class implementation
453// ----------------------------------------------------------------------------
454
455wxLogStderr::wxLogStderr(FILE *fp)
456{
0fb67cd1
VZ
457 if ( fp == NULL )
458 m_fp = stderr;
459 else
460 m_fp = fp;
c801d85f
KB
461}
462
5c922642 463#if defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
7d610b90 464
5c922642 465#if !TARGET_API_MAC_CARBON
925ee99f
GD
466// MetroNub stuff doesn't seem to work in CodeWarrior 5.3 Carbon builds...
467
0adad2d7
SC
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
499static MetroNubUserEntryBlock* gMetroNubEntry = NULL;
500
501static long fRunOnce = false;
502
503Boolean IsCompatibleVersion(short inVersion);
504
505/* ---------------------------------------------------------------------------
506 IsCompatibleVersion
507 --------------------------------------------------------------------------- */
508
509Boolean IsCompatibleVersion(short inVersion)
03147cd0 510{
0adad2d7
SC
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}
03147cd0 522
0adad2d7
SC
523/* ---------------------------------------------------------------------------
524 IsMetroNubInstalled
525 --------------------------------------------------------------------------- */
03147cd0 526
0adad2d7
SC
527Boolean 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
586end:
587
588#if TARGET_API_MAC_CARBON
589 return (gMetroNubEntry != NULL && gCallUniversalProc_Proc != NULL);
590#else
591 return (gMetroNubEntry != NULL);
592#endif
593}
03147cd0 594
0adad2d7
SC
595/* ---------------------------------------------------------------------------
596 IsMWDebuggerRunning [v1 API]
597 --------------------------------------------------------------------------- */
598
599Boolean IsMWDebuggerRunning()
600{
601 if (IsMetroNubInstalled())
602 return CallIsDebuggerRunningProc(gMetroNubEntry->isDebuggerRunning);
603 else
604 return false;
605}
606
607/* ---------------------------------------------------------------------------
608 AmIBeingMWDebugged [v1 API]
609 --------------------------------------------------------------------------- */
610
611Boolean AmIBeingMWDebugged()
612{
613 if (IsMetroNubInstalled())
614 return CallAmIBeingDebuggedProc(gMetroNubEntry->amIBeingDebugged);
615 else
616 return false;
7d610b90
SC
617}
618
0adad2d7
SC
619/* ---------------------------------------------------------------------------
620 UserSetWatchPoint [v2 API]
621 --------------------------------------------------------------------------- */
622
623OSErr UserSetWatchPoint (Ptr address, long length, WatchPointIDT* watchPointID)
7d610b90 624{
0adad2d7
SC
625 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
626 return CallUserSetWatchPointProc(gMetroNubEntry->userSetWatchPoint,
627 address, length, watchPointID);
628 else
629 return errProcessIsNotClient;
7d610b90
SC
630}
631
0adad2d7
SC
632/* ---------------------------------------------------------------------------
633 ClearWatchPoint [v2 API]
634 --------------------------------------------------------------------------- */
635
636OSErr ClearWatchPoint (WatchPointIDT watchPointID)
7d610b90 637{
0adad2d7
SC
638 if (IsMetroNubInstalled() && IsCompatibleVersion(kMetroNubUserAPIVersion))
639 return CallClearWatchPointProc(gMetroNubEntry->clearWatchPoint,
640 watchPointID);
641 else
642 return errProcessIsNotClient;
7d610b90 643}
0adad2d7
SC
644
645#ifdef __cplusplus
646 }
647#endif
648
5c922642 649#endif // !TARGET_API_MAC_CARBON
925ee99f 650
5c922642 651#endif // defined(__WXMAC__) && !defined(__DARWIN__) && (__MWERKS__ > 0x5300)
7d610b90 652
74e3313b 653void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 654{
d2e1ef19
VZ
655 wxString str;
656 TimeStamp(&str);
b568d04f 657 str << szString;
1e8a4bc2 658
50920146 659 fputs(str.mb_str(), m_fp);
b568d04f 660 fputc(_T('\n'), m_fp);
0fb67cd1 661 fflush(m_fp);
1e8a4bc2 662
378b05f7 663 // under Windows, programs usually don't have stderr at all, so show the
b568d04f 664 // messages also under debugger - unless it's a console program
8cb172b4 665#if defined(__WXMSW__) && wxUSE_GUI && !defined(__WXMICROWIN__)
f6bcfd97
BP
666 str += wxT("\r\n") ;
667 OutputDebugString(str.c_str());
1e8a4bc2 668#endif // MSW
0cbb9297 669#if defined(__WXMAC__) && !defined(__DARWIN__) && wxUSE_GUI
03147cd0
VZ
670 Str255 pstr ;
671 strcpy( (char*) pstr , str.c_str() ) ;
672 strcat( (char*) pstr , ";g" ) ;
673 c2pstr( (char*) pstr ) ;
0adad2d7 674
03147cd0
VZ
675 Boolean running = false ;
676
5c922642 677#if !TARGET_API_MAC_CARBON && (__MWERKS__ > 0x5300)
925ee99f 678
0adad2d7 679 if ( IsMWDebuggerRunning() && AmIBeingMWDebugged() )
03147cd0 680 {
0adad2d7 681 running = true ;
03147cd0 682 }
0adad2d7 683
925ee99f
GD
684#endif
685
03147cd0
VZ
686 if (running)
687 {
688#ifdef __powerc
689 DebugStr(pstr);
690#else
691 SysBreakStr(pstr);
7d610b90 692#endif
03147cd0 693 }
03e11df5 694#endif // Mac
c801d85f
KB
695}
696
697// ----------------------------------------------------------------------------
698// wxLogStream implementation
699// ----------------------------------------------------------------------------
700
4bf78aae 701#if wxUSE_STD_IOSTREAM
dd107c50 702wxLogStream::wxLogStream(wxSTD ostream *ostr)
c801d85f 703{
0fb67cd1 704 if ( ostr == NULL )
dd107c50 705 m_ostr = &wxSTD cerr;
0fb67cd1
VZ
706 else
707 m_ostr = ostr;
c801d85f
KB
708}
709
74e3313b 710void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
c801d85f 711{
29fd317b
VZ
712 wxString str;
713 TimeStamp(&str);
dd107c50 714 (*m_ostr) << str << wxConvertWX2MB(szString) << wxSTD endl;
c801d85f 715}
0fb67cd1 716#endif // wxUSE_STD_IOSTREAM
c801d85f 717
03147cd0
VZ
718// ----------------------------------------------------------------------------
719// wxLogChain
720// ----------------------------------------------------------------------------
721
722wxLogChain::wxLogChain(wxLog *logger)
723{
724 m_logNew = logger;
725 m_logOld = wxLog::SetActiveTarget(this);
726}
727
728void 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
738void 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
748void 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
93d4c1d0
VZ
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
773wxLogPassThrough::wxLogPassThrough()
774 : wxLogChain(this)
775{
776}
777
778#ifdef __VISUALC__
779 #pragma warning(default:4355)
780#endif // VC++
781
c801d85f
KB
782// ============================================================================
783// Global functions/variables
784// ============================================================================
785
786// ----------------------------------------------------------------------------
787// static variables
788// ----------------------------------------------------------------------------
0fb67cd1
VZ
789
790wxLog *wxLog::ms_pLogger = (wxLog *)NULL;
791bool wxLog::ms_doLog = TRUE;
792bool wxLog::ms_bAutoCreate = TRUE;
fd7718b2 793bool wxLog::ms_bVerbose = FALSE;
d2e1ef19 794
2ed3265e
VZ
795size_t wxLog::ms_suspendCount = 0;
796
9f83044f
VZ
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
d2e1ef19 802
0fb67cd1
VZ
803wxTraceMask wxLog::ms_ulTraceMask = (wxTraceMask)0;
804wxArrayString wxLog::ms_aTraceMasks;
c801d85f
KB
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//
0fb67cd1
VZ
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
c801d85f
KB
816static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
817{
0fb67cd1
VZ
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 }
c801d85f 837 }
c801d85f 838
0fb67cd1 839 putc('\n', f);
c801d85f
KB
840}
841#endif //LOG_PRETTY_WRAP
842
843// ----------------------------------------------------------------------------
844// error code/error message retrieval functions
845// ----------------------------------------------------------------------------
846
847// get error code from syste
848unsigned long wxSysErrorCode()
849{
8cb172b4 850#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1
VZ
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
c801d85f 858 return errno;
0fb67cd1 859#endif //Win/Unix
c801d85f
KB
860}
861
862// get error message from system
50920146 863const wxChar *wxSysErrorMsg(unsigned long nErrCode)
c801d85f 864{
0fb67cd1
VZ
865 if ( nErrCode == 0 )
866 nErrCode = wxSysErrorCode();
867
8cb172b4 868#if defined(__WXMSW__) && !defined(__WXMICROWIN__)
0fb67cd1 869#ifdef __WIN32__
50920146 870 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
0fb67cd1
VZ
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
251244a0 881 if( lpMsgBuf != 0 ) {
8c5b1f0f 882 wxStrncpy(s_szBuf, (const wxChar *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
251244a0
VZ
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 {
8c5b1f0f 897 s_szBuf[0] = wxT('\0');
0fb67cd1
VZ
898 }
899
900 return s_szBuf;
901#else //Win16
902 // TODO
903 return NULL;
904#endif // Win16/32
905#else // Unix
50920146
OK
906#if wxUSE_UNICODE
907 static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
dcf924a3 908 wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
50920146
OK
909 return s_szBuf;
910#else
13111b2a 911 return strerror((int)nErrCode);
50920146 912#endif
0fb67cd1 913#endif // Win/Unix
c801d85f
KB
914}
915
f94dfb38 916#endif //wxUSE_LOG