]> git.saurik.com Git - wxWidgets.git/blob - include/wx/log.h
6bde1a61d0104d3145b114773927f252f54b88a7
[wxWidgets.git] / include / wx / log.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/log.h
3 // Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_LOG_H_
13 #define _WX_LOG_H_
14
15 #include "wx/defs.h"
16
17 // ----------------------------------------------------------------------------
18 // types
19 // ----------------------------------------------------------------------------
20
21 // NB: this is needed even if wxUSE_LOG == 0
22 typedef unsigned long wxLogLevel;
23
24 // the trace masks have been superseded by symbolic trace constants, they're
25 // for compatibility only and will be removed soon - do NOT use them
26 #if WXWIN_COMPATIBILITY_2_8
27 #define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
28 #define wxTraceMessages 0x0002 // trace window messages/X callbacks
29 #define wxTraceResAlloc 0x0004 // trace GDI resource allocation
30 #define wxTraceRefCount 0x0008 // trace various ref counting operations
31
32 #ifdef __WXMSW__
33 #define wxTraceOleCalls 0x0100 // OLE interface calls
34 #endif
35
36 typedef unsigned long wxTraceMask;
37 #endif // WXWIN_COMPATIBILITY_2_8
38
39 // ----------------------------------------------------------------------------
40 // headers
41 // ----------------------------------------------------------------------------
42
43 #include "wx/string.h"
44 #include "wx/strvararg.h"
45
46 #if wxUSE_LOG
47
48 #include "wx/arrstr.h"
49
50 #ifndef __WXPALMOS5__
51 #ifndef __WXWINCE__
52 #include <time.h> // for time_t
53 #endif
54 #endif // ! __WXPALMOS5__
55
56 #include "wx/dynarray.h"
57
58 #if wxUSE_THREADS
59 #include "wx/thread.h"
60 #endif // wxUSE_THREADS
61
62 // wxUSE_LOG_DEBUG enables the debug log messages
63 #ifndef wxUSE_LOG_DEBUG
64 #if wxDEBUG_LEVEL
65 #define wxUSE_LOG_DEBUG 1
66 #else // !wxDEBUG_LEVEL
67 #define wxUSE_LOG_DEBUG 0
68 #endif
69 #endif
70
71 // wxUSE_LOG_TRACE enables the trace messages, they are disabled by default
72 #ifndef wxUSE_LOG_TRACE
73 #if wxDEBUG_LEVEL
74 #define wxUSE_LOG_TRACE 1
75 #else // !wxDEBUG_LEVEL
76 #define wxUSE_LOG_TRACE 0
77 #endif
78 #endif // wxUSE_LOG_TRACE
79
80 // ----------------------------------------------------------------------------
81 // forward declarations
82 // ----------------------------------------------------------------------------
83
84 #if wxUSE_GUI
85 class WXDLLIMPEXP_FWD_CORE wxFrame;
86 #endif // wxUSE_GUI
87
88 // ----------------------------------------------------------------------------
89 // constants
90 // ----------------------------------------------------------------------------
91
92 // different standard log levels (you may also define your own)
93 enum wxLogLevelValues
94 {
95 wxLOG_FatalError, // program can't continue, abort immediately
96 wxLOG_Error, // a serious error, user must be informed about it
97 wxLOG_Warning, // user is normally informed about it but may be ignored
98 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
99 wxLOG_Status, // informational: might go to the status line of GUI app
100 wxLOG_Info, // informational message (a.k.a. 'Verbose')
101 wxLOG_Debug, // never shown to the user, disabled in release mode
102 wxLOG_Trace, // trace messages are also only enabled in debug mode
103 wxLOG_Progress, // used for progress indicator (not yet)
104 wxLOG_User = 100, // user defined levels start here
105 wxLOG_Max = 10000
106 };
107
108 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
109 // discarded unless the string "foo" has been added to the list of allowed
110 // ones with AddTraceMask()
111
112 #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
113 #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
114 #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
115 #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
116
117 #ifdef __WXMSW__
118 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
119 #endif
120
121 #include "wx/iosfwrap.h"
122
123 // ----------------------------------------------------------------------------
124 // information about a log record, i.e. unit of log output
125 // ----------------------------------------------------------------------------
126
127 struct wxLogRecordInfo
128 {
129 wxLogRecordInfo()
130 {
131 timestamp = 0;
132
133 #if wxUSE_THREADS
134 threadId = 0;
135 #endif // wxUSE_THREADS
136 }
137
138 // default copy ctor, assignment operator and dtor are ok
139
140
141 // time of record generation
142 time_t timestamp;
143
144 #if wxUSE_THREADS
145 // id of the thread which logged this record
146 wxThreadIdType threadId;
147 #endif // wxUSE_THREADS
148 };
149
150 // ----------------------------------------------------------------------------
151 // derive from this class to redirect (or suppress, or ...) log messages
152 // normally, only a single instance of this class exists but it's not enforced
153 // ----------------------------------------------------------------------------
154
155 class WXDLLIMPEXP_BASE wxLog
156 {
157 public:
158 // ctor
159 wxLog() { }
160
161 // make dtor virtual for all derived classes
162 virtual ~wxLog();
163
164
165 // these functions allow to completely disable all log messages
166
167 // is logging enabled at all now?
168 static bool IsEnabled() { return ms_doLog; }
169
170 // is logging at this level enabled?
171 static bool IsLevelEnabled(wxLogLevel level)
172 { return IsEnabled() && level <= ms_logLevel; }
173
174 // change the flag state, return the previous one
175 static bool EnableLogging(bool doIt = true)
176 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
177
178 // message buffering
179
180 // flush shows all messages if they're not logged immediately (FILE
181 // and iostream logs don't need it, but wxGuiLog does to avoid showing
182 // 17 modal dialogs one after another)
183 virtual void Flush();
184
185 // flush the active target if any
186 static void FlushActive()
187 {
188 if ( !ms_suspendCount )
189 {
190 wxLog *log = GetActiveTarget();
191 if ( log )
192 log->Flush();
193 }
194 }
195
196 // only one sink is active at each moment
197 // get current log target, will call wxApp::CreateLogTarget() to
198 // create one if none exists
199 static wxLog *GetActiveTarget();
200
201 // change log target, pLogger may be NULL
202 static wxLog *SetActiveTarget(wxLog *pLogger);
203
204 // suspend the message flushing of the main target until the next call
205 // to Resume() - this is mainly for internal use (to prevent wxYield()
206 // from flashing the messages)
207 static void Suspend() { ms_suspendCount++; }
208
209 // must be called for each Suspend()!
210 static void Resume() { ms_suspendCount--; }
211
212 // functions controlling the default wxLog behaviour
213 // verbose mode is activated by standard command-line '--verbose'
214 // option
215 static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; }
216
217 // Set log level. Log messages with level > logLevel will not be logged.
218 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
219
220 // should GetActiveTarget() try to create a new log object if the
221 // current is NULL?
222 static void DontCreateOnDemand();
223
224 // Make GetActiveTarget() create a new log object again.
225 static void DoCreateOnDemand();
226
227 // log the count of repeating messages instead of logging the messages
228 // multiple times
229 static void SetRepetitionCounting(bool bRepetCounting = true)
230 { ms_bRepetCounting = bRepetCounting; }
231
232 // gets duplicate counting status
233 static bool GetRepetitionCounting() { return ms_bRepetCounting; }
234
235 // add string trace mask
236 static void AddTraceMask(const wxString& str);
237
238 // add string trace mask
239 static void RemoveTraceMask(const wxString& str);
240
241 // remove all string trace masks
242 static void ClearTraceMasks();
243
244 // get string trace masks: note that this is MT-unsafe if other threads can
245 // call AddTraceMask() concurrently
246 static const wxArrayString& GetTraceMasks() { return ms_aTraceMasks; }
247
248 // sets the time stamp string format: this is used as strftime() format
249 // string for the log targets which add time stamps to the messages; set
250 // it to empty string to disable time stamping completely.
251 static void SetTimestamp(const wxString& ts) { ms_timestamp = ts; }
252
253 // disable time stamping of log messages
254 static void DisableTimestamp() { SetTimestamp(wxEmptyString); }
255
256
257 // accessors
258
259 // gets the verbose status
260 static bool GetVerbose() { return ms_bVerbose; }
261
262 // is this trace mask in the list?
263 static bool IsAllowedTraceMask(const wxString& mask);
264
265 // return the current loglevel limit
266 static wxLogLevel GetLogLevel() { return ms_logLevel; }
267
268 // get the current timestamp format string (maybe empty)
269 static const wxString& GetTimestamp() { return ms_timestamp; }
270
271
272
273 // helpers: all functions in this section are mostly for internal use only,
274 // don't call them from your code even if they are not formally deprecated
275
276 // put the time stamp into the string if ms_timestamp != NULL (don't
277 // change it otherwise)
278 static void TimeStamp(wxString *str);
279
280 // these methods should only be called from derived classes DoLogRecord(),
281 // DoLogTextAtLevel() and DoLogText() implementations respectively and
282 // shouldn't be called directly, use logging functions instead
283 void LogRecord(wxLogLevel level,
284 const wxString& msg,
285 const wxLogRecordInfo& info)
286 {
287 DoLogRecord(level, msg, info);
288 }
289
290 void LogTextAtLevel(wxLogLevel level, const wxString& msg)
291 {
292 DoLogTextAtLevel(level, msg);
293 }
294
295 void LogText(const wxString& msg)
296 {
297 DoLogText(msg);
298 }
299
300 // this is a helper used by wxLogXXX() functions, don't call it directly
301 // and see DoLog() for function to overload in the derived classes
302 static void OnLog(wxLogLevel level,
303 const wxString& msg,
304 const wxLogRecordInfo& info);
305
306 // version called when no information about the location of the log record
307 // generation is available (but the time stamp is), it mainly exists for
308 // backwards compatibility, don't use it in new code
309 static void OnLog(wxLogLevel level, const wxString& msg, time_t t);
310
311 // a helper calling the above overload with current time
312 static void OnLog(wxLogLevel level, const wxString& msg)
313 {
314 OnLog(level, msg, time(NULL));
315 }
316
317
318 // this method exists for backwards compatibility only, don't use
319 bool HasPendingMessages() const { return true; }
320
321 #if WXWIN_COMPATIBILITY_2_6
322 // this function doesn't do anything any more, don't call it
323 wxDEPRECATED_INLINE(
324 static wxChar *SetLogBuffer(wxChar *, size_t = 0), return NULL;
325 );
326 #endif // WXWIN_COMPATIBILITY_2_6
327
328 // don't use integer masks any more, use string trace masks instead
329 #if WXWIN_COMPATIBILITY_2_8
330 wxDEPRECATED_INLINE( static void SetTraceMask(wxTraceMask ulMask),
331 ms_ulTraceMask = ulMask; )
332 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE( static wxTraceMask GetTraceMask(),
333 return ms_ulTraceMask; )
334 #endif // WXWIN_COMPATIBILITY_2_8
335
336 protected:
337 // the logging functions that can be overridden: DoLogRecord() is called
338 // for every "record", i.e. a unit of log output, to be logged and by
339 // default formats the message and passes it to DoLogTextAtLevel() which in
340 // turn passes it to DoLogText() by default
341
342 // override this method if you want to change message formatting or do
343 // dynamic filtering
344 virtual void DoLogRecord(wxLogLevel level,
345 const wxString& msg,
346 const wxLogRecordInfo& info);
347
348 // override this method to redirect output to different channels depending
349 // on its level only; if even the level doesn't matter, override
350 // DoLogText() instead
351 virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
352
353 // this function is not pure virtual as it might not be needed if you do
354 // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly
355 // but if you do not override them in your derived class you must override
356 // this one as the default implementation of it simply asserts
357 virtual void DoLogText(const wxString& msg);
358
359
360 // the rest of the functions are for backwards compatibility only, don't
361 // use them in new code; if you're updating your existing code you need to
362 // switch to overriding DoLogRecord/Text() above (although as long as these
363 // functions exist, log classes using them will continue to work)
364 #if WXWIN_COMPATIBILITY_2_8
365 wxDEPRECATED_BUT_USED_INTERNALLY(
366 virtual void DoLog(wxLogLevel level, const char *szString, time_t t)
367 );
368
369 wxDEPRECATED_BUT_USED_INTERNALLY(
370 virtual void DoLog(wxLogLevel level, const wchar_t *wzString, time_t t)
371 );
372
373 // these shouldn't be used by new code
374 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
375 virtual void DoLogString(const char *WXUNUSED(szString),
376 time_t WXUNUSED(t)),
377 )
378
379 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
380 virtual void DoLogString(const wchar_t *WXUNUSED(wzString),
381 time_t WXUNUSED(t)),
382 )
383 #endif // WXWIN_COMPATIBILITY_2_8
384
385
386 // log a message indicating the number of times the previous message was
387 // repeated if previous repetition counter is strictly positive, does
388 // nothing otherwise; return the old value of repetition counter
389 unsigned LogLastRepeatIfNeeded();
390
391 private:
392 // implement of LogLastRepeatIfNeeded(): it assumes that the
393 // caller had already locked GetPreviousLogCS()
394 unsigned LogLastRepeatIfNeededUnlocked();
395
396 // static variables
397 // ----------------
398
399 // if true, don't log the same message multiple times, only log it once
400 // with the number of times it was repeated
401 static bool ms_bRepetCounting;
402
403 static wxLog *ms_pLogger; // currently active log sink
404 static bool ms_doLog; // false => all logging disabled
405 static bool ms_bAutoCreate; // create new log targets on demand?
406 static bool ms_bVerbose; // false => ignore LogInfo messages
407
408 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
409
410 static size_t ms_suspendCount; // if positive, logs are not flushed
411
412 // format string for strftime(), if empty, time stamping log messages is
413 // disabled
414 static wxString ms_timestamp;
415
416 #if WXWIN_COMPATIBILITY_2_8
417 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
418 #endif // WXWIN_COMPATIBILITY_2_8
419
420 // currently enabled trace masks
421 static wxArrayString ms_aTraceMasks;
422 };
423
424 // ----------------------------------------------------------------------------
425 // "trivial" derivations of wxLog
426 // ----------------------------------------------------------------------------
427
428 // log everything except for the debug/trace messages (which are passed to
429 // wxMessageOutputDebug) to a buffer
430 class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog
431 {
432 public:
433 wxLogBuffer() { }
434
435 // get the string contents with all messages logged
436 const wxString& GetBuffer() const { return m_str; }
437
438 // show the buffer contents to the user in the best possible way (this uses
439 // wxMessageOutputMessageBox) and clear it
440 virtual void Flush();
441
442 protected:
443 virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
444
445 private:
446 wxString m_str;
447
448 wxDECLARE_NO_COPY_CLASS(wxLogBuffer);
449 };
450
451
452 // log everything to a "FILE *", stderr by default
453 class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
454 {
455 public:
456 // redirect log output to a FILE
457 wxLogStderr(FILE *fp = NULL);
458
459 protected:
460 // implement sink function
461 virtual void DoLogText(const wxString& msg);
462
463 FILE *m_fp;
464
465 wxDECLARE_NO_COPY_CLASS(wxLogStderr);
466 };
467
468 #if wxUSE_STD_IOSTREAM
469
470 // log everything to an "ostream", cerr by default
471 class WXDLLIMPEXP_BASE wxLogStream : public wxLog
472 {
473 public:
474 // redirect log output to an ostream
475 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
476
477 protected:
478 // implement sink function
479 virtual void DoLogText(const wxString& msg);
480
481 // using ptr here to avoid including <iostream.h> from this file
482 wxSTD ostream *m_ostr;
483 };
484
485 #endif // wxUSE_STD_IOSTREAM
486
487 // ----------------------------------------------------------------------------
488 // /dev/null log target: suppress logging until this object goes out of scope
489 // ----------------------------------------------------------------------------
490
491 // example of usage:
492 /*
493 void Foo()
494 {
495 wxFile file;
496
497 // wxFile.Open() normally complains if file can't be opened, we don't
498 // want it
499 wxLogNull logNo;
500
501 if ( !file.Open("bar") )
502 ... process error ourselves ...
503
504 // ~wxLogNull called, old log sink restored
505 }
506 */
507 class WXDLLIMPEXP_BASE wxLogNull
508 {
509 public:
510 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
511 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
512
513 private:
514 bool m_flagOld; // the previous value of the wxLog::ms_doLog
515 };
516
517 // ----------------------------------------------------------------------------
518 // chaining log target: installs itself as a log target and passes all
519 // messages to the real log target given to it in the ctor but also forwards
520 // them to the previously active one
521 //
522 // note that you don't have to call SetActiveTarget() with this class, it
523 // does it itself in its ctor
524 // ----------------------------------------------------------------------------
525
526 class WXDLLIMPEXP_BASE wxLogChain : public wxLog
527 {
528 public:
529 wxLogChain(wxLog *logger);
530 virtual ~wxLogChain();
531
532 // change the new log target
533 void SetLog(wxLog *logger);
534
535 // this can be used to temporarily disable (and then reenable) passing
536 // messages to the old logger (by default we do pass them)
537 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
538
539 // are we passing the messages to the previous log target?
540 bool IsPassingMessages() const { return m_bPassMessages; }
541
542 // return the previous log target (may be NULL)
543 wxLog *GetOldLog() const { return m_logOld; }
544
545 // override base class version to flush the old logger as well
546 virtual void Flush();
547
548 // call to avoid destroying the old log target
549 void DetachOldLog() { m_logOld = NULL; }
550
551 protected:
552 // pass the record to the old logger if needed
553 virtual void DoLogRecord(wxLogLevel level,
554 const wxString& msg,
555 const wxLogRecordInfo& info);
556
557 private:
558 // the current log target
559 wxLog *m_logNew;
560
561 // the previous log target
562 wxLog *m_logOld;
563
564 // do we pass the messages to the old logger?
565 bool m_bPassMessages;
566
567 wxDECLARE_NO_COPY_CLASS(wxLogChain);
568 };
569
570 // a chain log target which uses itself as the new logger
571
572 #define wxLogPassThrough wxLogInterposer
573
574 class WXDLLIMPEXP_BASE wxLogInterposer : public wxLogChain
575 {
576 public:
577 wxLogInterposer();
578
579 private:
580 wxDECLARE_NO_COPY_CLASS(wxLogInterposer);
581 };
582
583 // a temporary interposer which doesn't destroy the old log target
584 // (calls DetachOldLog)
585
586 class WXDLLIMPEXP_BASE wxLogInterposerTemp : public wxLogChain
587 {
588 public:
589 wxLogInterposerTemp();
590
591 private:
592 wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp);
593 };
594
595 #if wxUSE_GUI
596 // include GUI log targets:
597 #include "wx/generic/logg.h"
598 #endif // wxUSE_GUI
599
600 // ============================================================================
601 // global functions
602 // ============================================================================
603
604 // ----------------------------------------------------------------------------
605 // Log functions should be used by application instead of stdio, iostream &c
606 // for log messages for easy redirection
607 // ----------------------------------------------------------------------------
608
609 // ----------------------------------------------------------------------------
610 // get error code/error message from system in a portable way
611 // ----------------------------------------------------------------------------
612
613 // return the last system error code
614 WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
615
616 // return the error message for given (or last if 0) error code
617 WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
618
619 // ----------------------------------------------------------------------------
620 // define wxLog<level>
621 // ----------------------------------------------------------------------------
622
623 #define DECLARE_LOG_FUNCTION(level) \
624 extern void WXDLLIMPEXP_BASE \
625 wxDoLog##level##Wchar(const wxChar *format, ...); \
626 extern void WXDLLIMPEXP_BASE \
627 wxDoLog##level##Utf8(const char *format, ...); \
628 WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \
629 1, (const wxFormatString&), \
630 wxDoLog##level##Wchar, wxDoLog##level##Utf8) \
631 DECLARE_LOG_FUNCTION_WATCOM(level) \
632 extern void WXDLLIMPEXP_BASE wxVLog##level(const wxString& format, \
633 va_list argptr)
634
635 #ifdef __WATCOMC__
636 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351;
637 // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to
638 // something too big for Borland C++ to handle
639 #define DECLARE_LOG_FUNCTION_WATCOM(level) \
640 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
641 1, (const wxString&), \
642 (wxFormatString(f1))) \
643 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
644 1, (const wxCStrData&), \
645 (wxFormatString(f1))) \
646 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
647 1, (const char*), \
648 (wxFormatString(f1))) \
649 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
650 1, (const wchar_t*), \
651 (wxFormatString(f1)))
652 #else
653 #define DECLARE_LOG_FUNCTION_WATCOM(level)
654 #endif
655
656
657 #define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
658 extern void expdecl wxDoLog##level##Wchar(argclass arg, \
659 const wxChar *format, ...); \
660 extern void expdecl wxDoLog##level##Utf8(argclass arg, \
661 const char *format, ...); \
662 WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \
663 2, (argclass, const wxFormatString&), \
664 wxDoLog##level##Wchar, wxDoLog##level##Utf8) \
665 DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \
666 extern void expdecl wxVLog##level(argclass arg, \
667 const wxString& format, \
668 va_list argptr)
669
670 #ifdef __WATCOMC__
671 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351;
672 // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to
673 // something too big for Borland C++ to handle
674 #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \
675 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
676 2, (argclass, const wxString&), \
677 (f1, wxFormatString(f2))) \
678 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
679 2, (argclass, const wxCStrData&), \
680 (f1, wxFormatString(f2))) \
681 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
682 2, (argclass, const char*), \
683 (f1, wxFormatString(f2))) \
684 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
685 2, (argclass, const wchar_t*), \
686 (f1, wxFormatString(f2)))
687 #else
688 #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl)
689 #endif
690
691
692 #else // !wxUSE_LOG
693
694 #undef wxUSE_LOG_DEBUG
695 #define wxUSE_LOG_DEBUG 0
696
697 #undef wxUSE_LOG_TRACE
698 #define wxUSE_LOG_TRACE 0
699
700 #ifdef __WATCOMC__
701 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
702 #define WX_WATCOM_ONLY_CODE( x ) x
703 #else
704 #define WX_WATCOM_ONLY_CODE( x )
705 #endif
706
707 #if defined(__WATCOMC__) || defined(__MINGW32__)
708 // Mingw has similar problem with wxLogSysError:
709 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
710 #else
711 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
712 #endif
713
714 // log functions do nothing at all
715 #define DECLARE_LOG_FUNCTION(level) \
716 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
717 WX_WATCOM_ONLY_CODE( \
718 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
719 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
720 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
721 ) \
722 inline void wxVLog##level(const wxString& WXUNUSED(format), \
723 va_list WXUNUSED(argptr)) { } \
724
725 #define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
726 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
727 WX_WATCOM_OR_MINGW_ONLY_CODE( \
728 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
729 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
730 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
731 ) \
732 inline void wxVLog##level(argclass WXUNUSED(arg), \
733 const wxString& WXUNUSED(format), \
734 va_list WXUNUSED(argptr)) {}
735
736 // Empty Class to fake wxLogNull
737 class WXDLLIMPEXP_BASE wxLogNull
738 {
739 public:
740 wxLogNull() { }
741 };
742
743 // Dummy macros to replace some functions.
744 #define wxSysErrorCode() (unsigned long)0
745 #define wxSysErrorMsg( X ) (const wxChar*)NULL
746
747 // Fake symbolic trace masks... for those that are used frequently
748 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
749
750 #endif // wxUSE_LOG/!wxUSE_LOG
751
752 #define DECLARE_LOG_FUNCTION2(level, argclass, arg) \
753 DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, WXDLLIMPEXP_BASE)
754
755 // VC6 produces a warning if we a macro expanding to nothing to
756 // DECLARE_LOG_FUNCTION2:
757 #if defined(__VISUALC__) && __VISUALC__ < 1300
758 #pragma warning(push)
759 // "not enough actual parameters for macro 'DECLARE_LOG_FUNCTION2_EXP'"
760 #pragma warning(disable:4003)
761 #endif
762
763 // a generic function for all levels (level is passes as parameter)
764 DECLARE_LOG_FUNCTION2(Generic, wxLogLevel, level);
765
766 // one function per each level
767 DECLARE_LOG_FUNCTION(FatalError);
768 DECLARE_LOG_FUNCTION(Error);
769 DECLARE_LOG_FUNCTION(Warning);
770 DECLARE_LOG_FUNCTION(Message);
771 DECLARE_LOG_FUNCTION(Info);
772 DECLARE_LOG_FUNCTION(Verbose);
773
774 // this function sends the log message to the status line of the top level
775 // application frame, if any
776 DECLARE_LOG_FUNCTION(Status);
777
778 #if wxUSE_GUI
779 // this one is the same as previous except that it allows to explicitly
780 class WXDLLIMPEXP_FWD_CORE wxFrame;
781 // specify the frame to which the output should go
782 DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *, pFrame, WXDLLIMPEXP_CORE);
783 #endif // wxUSE_GUI
784
785 // additional one: as wxLogError, but also logs last system call error code
786 // and the corresponding error message if available
787 DECLARE_LOG_FUNCTION(SysError);
788
789 // and another one which also takes the error code (for those broken APIs
790 // that don't set the errno (like registry APIs in Win32))
791 DECLARE_LOG_FUNCTION2(SysError, long, lErrCode);
792 #ifdef __WATCOMC__
793 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
794 DECLARE_LOG_FUNCTION2(SysError, unsigned long, lErrCode);
795 #endif
796
797
798 // debug functions can be completely disabled in optimized builds
799
800 // if these log functions are disabled, we prefer to define them as (empty)
801 // variadic macros as this completely removes them and their argument
802 // evaluation from the object code but if this is not supported by compiler we
803 // use empty inline functions instead (defining them as nothing would result in
804 // compiler warnings)
805 //
806 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
807 // a bad idea as some compilers are stupid enough to not inline even empty
808 // functions if their parameters are complicated enough, but by defining them
809 // as an empty inline function we ensure that even dumbest compilers optimise
810 // them away
811 #ifdef __BORLANDC__
812 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
813 // to define it differently for it to avoid these warnings (same problem as
814 // with wxUnusedVar())
815 #define wxLogNop() { }
816 #else
817 inline void wxLogNop() { }
818 #endif
819
820 #if wxUSE_LOG_DEBUG
821 DECLARE_LOG_FUNCTION(Debug);
822 #else // !wxUSE_LOG_DEBUG
823 #define wxVLogDebug(fmt, valist) wxLogNop()
824
825 #ifdef HAVE_VARIADIC_MACROS
826 #define wxLogDebug(fmt, ...) wxLogNop()
827 #else // !HAVE_VARIADIC_MACROS
828 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug, 1, (const wxString&))
829 #endif
830 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
831
832 #if wxUSE_LOG_TRACE
833 // this version only logs the message if the mask had been added to the
834 // list of masks with AddTraceMask()
835 DECLARE_LOG_FUNCTION2(Trace, const wxString&, mask);
836 #ifdef __WATCOMC__
837 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
838 DECLARE_LOG_FUNCTION2(Trace, const char*, mask);
839 DECLARE_LOG_FUNCTION2(Trace, const wchar_t*, mask);
840 #endif
841
842 // and this one does nothing if all of level bits are not set in
843 // wxLog::GetActive()->GetTraceMask() -- it's deprecated in favour of
844 // string identifiers
845 #if WXWIN_COMPATIBILITY_2_8
846 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask, mask);
847 #ifdef __WATCOMC__
848 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
849 DECLARE_LOG_FUNCTION2(Trace, int, mask);
850 #endif
851 #endif // WXWIN_COMPATIBILITY_2_8
852
853 #else // !wxUSE_LOG_TRACE
854 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
855
856 #ifdef HAVE_VARIADIC_MACROS
857 #define wxLogTrace(mask, fmt, ...) wxLogNop()
858 #else // !HAVE_VARIADIC_MACROS
859 #if WXWIN_COMPATIBILITY_2_8
860 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (wxTraceMask, const wxString&))
861 #endif
862 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wxString&, const wxString&))
863 #ifdef __WATCOMC__
864 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
865 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const char*, const char*))
866 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wchar_t*, const wchar_t*))
867 #endif
868 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
869 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
870
871 #if defined(__VISUALC__) && __VISUALC__ < 1300
872 #pragma warning(pop)
873 #endif
874
875 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
876 // i.e. without using wxMessageBox() for example because it could crash
877 void WXDLLIMPEXP_BASE
878 wxSafeShowMessage(const wxString& title, const wxString& text);
879
880 // ----------------------------------------------------------------------------
881 // debug only logging functions: use them with API name and error code
882 // ----------------------------------------------------------------------------
883
884 #if wxUSE_LOG_DEBUG
885 // make life easier for people using VC++ IDE: clicking on the message
886 // will take us immediately to the place of the failed API
887 #ifdef __VISUALC__
888 #define wxLogApiError(api, rc) \
889 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
890 __FILE__, __LINE__, api, \
891 (long)rc, wxSysErrorMsg(rc))
892 #else // !VC++
893 #define wxLogApiError(api, rc) \
894 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
895 wxT("error 0x%08lx (%s)."), \
896 __FILE__, __LINE__, api, \
897 (long)rc, wxSysErrorMsg(rc))
898 #endif // VC++/!VC++
899
900 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
901
902 #else // !wxUSE_LOG_DEBUG
903 #define wxLogApiError(api, err) wxLogNop()
904 #define wxLogLastError(api) wxLogNop()
905 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
906
907 // wxCocoa has additiional trace masks
908 #if defined(__WXCOCOA__)
909 #include "wx/cocoa/log.h"
910 #endif
911
912 #ifdef WX_WATCOM_ONLY_CODE
913 #undef WX_WATCOM_ONLY_CODE
914 #endif
915
916 #endif // _WX_LOG_H_
917