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