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