Add component-level filtering to wxLog.
[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 #include "wx/hashmap.h"
58
59 #if wxUSE_THREADS
60 #include "wx/thread.h"
61 #endif // wxUSE_THREADS
62
63 // wxUSE_LOG_DEBUG enables the debug log messages
64 #ifndef wxUSE_LOG_DEBUG
65 #if wxDEBUG_LEVEL
66 #define wxUSE_LOG_DEBUG 1
67 #else // !wxDEBUG_LEVEL
68 #define wxUSE_LOG_DEBUG 0
69 #endif
70 #endif
71
72 // wxUSE_LOG_TRACE enables the trace messages, they are disabled by default
73 #ifndef wxUSE_LOG_TRACE
74 #if wxDEBUG_LEVEL
75 #define wxUSE_LOG_TRACE 1
76 #else // !wxDEBUG_LEVEL
77 #define wxUSE_LOG_TRACE 0
78 #endif
79 #endif // wxUSE_LOG_TRACE
80
81 // wxLOG_COMPONENT identifies the component which generated the log record and
82 // can be #define'd to a user-defined value when compiling the user code to use
83 // component-based filtering (see wxLog::SetComponentLevel())
84 #ifndef wxLOG_COMPONENT
85 // this is a variable and not a macro in order to allow the user code to
86 // just #define wxLOG_COMPONENT without #undef'ining it first
87 extern WXDLLIMPEXP_DATA_BASE(const char *) wxLOG_COMPONENT;
88
89 #ifdef WXBUILDING
90 #define wxLOG_COMPONENT "wx"
91 #endif
92 #endif
93
94 // ----------------------------------------------------------------------------
95 // forward declarations
96 // ----------------------------------------------------------------------------
97
98 class WXDLLIMPEXP_FWD_BASE wxObject;
99
100 #if wxUSE_GUI
101 class WXDLLIMPEXP_FWD_CORE wxFrame;
102 #endif // wxUSE_GUI
103
104 // ----------------------------------------------------------------------------
105 // constants
106 // ----------------------------------------------------------------------------
107
108 // different standard log levels (you may also define your own)
109 enum wxLogLevelValues
110 {
111 wxLOG_FatalError, // program can't continue, abort immediately
112 wxLOG_Error, // a serious error, user must be informed about it
113 wxLOG_Warning, // user is normally informed about it but may be ignored
114 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
115 wxLOG_Status, // informational: might go to the status line of GUI app
116 wxLOG_Info, // informational message (a.k.a. 'Verbose')
117 wxLOG_Debug, // never shown to the user, disabled in release mode
118 wxLOG_Trace, // trace messages are also only enabled in debug mode
119 wxLOG_Progress, // used for progress indicator (not yet)
120 wxLOG_User = 100, // user defined levels start here
121 wxLOG_Max = 10000
122 };
123
124 // symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
125 // discarded unless the string "foo" has been added to the list of allowed
126 // ones with AddTraceMask()
127
128 #define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
129 #define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
130 #define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
131 #define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
132
133 #ifdef __WXMSW__
134 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
135 #endif
136
137 #include "wx/iosfwrap.h"
138
139 // ----------------------------------------------------------------------------
140 // information about a log record, i.e. unit of log output
141 // ----------------------------------------------------------------------------
142
143 class wxLogRecordInfo
144 {
145 public:
146 // default ctor creates an uninitialized object
147 wxLogRecordInfo()
148 {
149 memset(this, 0, sizeof(*this));
150 }
151
152 // normal ctor, used by wxLogger specifies the location of the log
153 // statement; its time stamp and thread id are set up here
154 wxLogRecordInfo(const char *filename_,
155 int line_,
156 const char *func_,
157 const char *component_)
158 {
159 filename = filename_;
160 func = func_;
161 line = line_;
162 component = component_;
163
164 timestamp = time(NULL);
165
166 #if wxUSE_THREADS
167 threadId = wxThread::GetCurrentId();
168 #endif // wxUSE_THREADS
169
170 m_data = NULL;
171 }
172
173 // we need to define copy ctor and assignment operator because of m_data
174 wxLogRecordInfo(const wxLogRecordInfo& other)
175 {
176 Copy(other);
177 }
178
179 wxLogRecordInfo& operator=(const wxLogRecordInfo& other)
180 {
181 if ( &other != this )
182 {
183 delete m_data;
184 Copy(other);
185 }
186
187 return *this;
188 }
189
190 // dtor is non-virtual, this class is not meant to be derived from
191 ~wxLogRecordInfo()
192 {
193 delete m_data;
194 }
195
196
197 // the file name and line number of the file where the log record was
198 // generated, if available or NULL and 0 otherwise
199 const char *filename;
200 int line;
201
202 // the name of the function where the log record was generated (may be NULL
203 // if the compiler doesn't support __FUNCTION__)
204 const char *func;
205
206 // the name of the component which generated this message, may be NULL if
207 // not set (i.e. wxLOG_COMPONENT not defined)
208 const char *component;
209
210 // time of record generation
211 time_t timestamp;
212
213 #if wxUSE_THREADS
214 // id of the thread which logged this record
215 wxThreadIdType threadId;
216 #endif // wxUSE_THREADS
217
218
219 // store an arbitrary value in this record context
220 //
221 // wxWidgets always uses keys starting with "wx.", e.g. "wx.sys_error"
222 void StoreValue(const wxString& key, wxUIntPtr val)
223 {
224 if ( !m_data )
225 m_data = new ExtraData;
226
227 m_data->numValues[key] = val;
228 }
229
230 void StoreValue(const wxString& key, const wxString& val)
231 {
232 if ( !m_data )
233 m_data = new ExtraData;
234
235 m_data->strValues[key] = val;
236 }
237
238
239 // these functions retrieve the value of either numeric or string key,
240 // return false if not found
241 bool GetNumValue(const wxString& key, wxUIntPtr *val) const
242 {
243 if ( !m_data )
244 return false;
245
246 wxStringToNumHashMap::const_iterator it = m_data->numValues.find(key);
247 if ( it == m_data->numValues.end() )
248 return false;
249
250 *val = it->second;
251
252 return true;
253 }
254
255 bool GetStrValue(const wxString& key, wxString *val) const
256 {
257 if ( !m_data )
258 return false;
259
260 wxStringToStringHashMap::const_iterator it = m_data->strValues.find(key);
261 if ( it == m_data->strValues.end() )
262 return false;
263
264 *val = it->second;
265
266 return true;
267 }
268
269 private:
270 void Copy(const wxLogRecordInfo& other)
271 {
272 memcpy(this, &other, sizeof(*this));
273 if ( other.m_data )
274 m_data = new ExtraData(*other.m_data);
275 }
276
277 // extra data associated with the log record: this is completely optional
278 // and can be used to pass information from the log function to the log
279 // sink (e.g. wxLogSysError() uses this to pass the error code)
280 struct ExtraData
281 {
282 wxStringToNumHashMap numValues;
283 wxStringToStringHashMap strValues;
284 };
285
286 // NULL if not used
287 ExtraData *m_data;
288 };
289
290 #define wxLOG_KEY_TRACE_MASK "wx.trace_mask"
291
292 // ----------------------------------------------------------------------------
293 // derive from this class to redirect (or suppress, or ...) log messages
294 // normally, only a single instance of this class exists but it's not enforced
295 // ----------------------------------------------------------------------------
296
297 class WXDLLIMPEXP_BASE wxLog
298 {
299 public:
300 // ctor
301 wxLog() { }
302
303 // make dtor virtual for all derived classes
304 virtual ~wxLog();
305
306
307 // log messages selection
308 // ----------------------
309
310 // these functions allow to completely disable all log messages or disable
311 // log messages at level less important than specified
312
313 // is logging enabled at all now?
314 static bool IsEnabled() { return ms_doLog; }
315
316 // change the flag state, return the previous one
317 static bool EnableLogging(bool doIt = true)
318 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
319
320
321 // return the current global log level
322 static wxLogLevel GetLogLevel() { return ms_logLevel; }
323
324 // set global log level: messages with level > logLevel will not be logged
325 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
326
327 // set the log level for the given component
328 static void SetComponentLevel(const wxString& component, wxLogLevel level);
329
330 // return the effective log level for this component, falling back to
331 // parent component and to the default global log level if necessary
332 //
333 // NB: component argument is passed by value and not const reference in an
334 // attempt to encourage compiler to avoid an extra copy: as we modify
335 // the component internally, we'd create one anyhow and like this it
336 // can be avoided if the string is a temporary anyhow
337 static wxLogLevel GetComponentLevel(wxString component);
338
339
340 // is logging of messages from this component enabled at this level?
341 //
342 // usually always called with wxLOG_COMPONENT as second argument
343 static bool IsLevelEnabled(wxLogLevel level, wxString component)
344 {
345 return IsEnabled() && level <= GetComponentLevel(component);
346 }
347
348
349 // enable/disable messages at wxLOG_Verbose level (only relevant if the
350 // current log level is greater or equal to it)
351 //
352 // notice that verbose mode can be activated by the standard command-line
353 // '--verbose' option
354 static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; }
355
356 // check if verbose messages are enabled
357 static bool GetVerbose() { return ms_bVerbose; }
358
359
360 // message buffering
361 // -----------------
362
363 // flush shows all messages if they're not logged immediately (FILE
364 // and iostream logs don't need it, but wxGuiLog does to avoid showing
365 // 17 modal dialogs one after another)
366 virtual void Flush();
367
368 // flush the active target if any
369 static void FlushActive()
370 {
371 if ( !ms_suspendCount )
372 {
373 wxLog *log = GetActiveTarget();
374 if ( log )
375 log->Flush();
376 }
377 }
378
379 // only one sink is active at each moment
380 // get current log target, will call wxApp::CreateLogTarget() to
381 // create one if none exists
382 static wxLog *GetActiveTarget();
383
384 // change log target, pLogger may be NULL
385 static wxLog *SetActiveTarget(wxLog *pLogger);
386
387 // suspend the message flushing of the main target until the next call
388 // to Resume() - this is mainly for internal use (to prevent wxYield()
389 // from flashing the messages)
390 static void Suspend() { ms_suspendCount++; }
391
392 // must be called for each Suspend()!
393 static void Resume() { ms_suspendCount--; }
394
395 // should GetActiveTarget() try to create a new log object if the
396 // current is NULL?
397 static void DontCreateOnDemand();
398
399 // Make GetActiveTarget() create a new log object again.
400 static void DoCreateOnDemand();
401
402 // log the count of repeating messages instead of logging the messages
403 // multiple times
404 static void SetRepetitionCounting(bool bRepetCounting = true)
405 { ms_bRepetCounting = bRepetCounting; }
406
407 // gets duplicate counting status
408 static bool GetRepetitionCounting() { return ms_bRepetCounting; }
409
410 // add string trace mask
411 static void AddTraceMask(const wxString& str);
412
413 // add string trace mask
414 static void RemoveTraceMask(const wxString& str);
415
416 // remove all string trace masks
417 static void ClearTraceMasks();
418
419 // get string trace masks: note that this is MT-unsafe if other threads can
420 // call AddTraceMask() concurrently
421 static const wxArrayString& GetTraceMasks() { return ms_aTraceMasks; }
422
423 // sets the time stamp string format: this is used as strftime() format
424 // string for the log targets which add time stamps to the messages; set
425 // it to empty string to disable time stamping completely.
426 static void SetTimestamp(const wxString& ts) { ms_timestamp = ts; }
427
428 // disable time stamping of log messages
429 static void DisableTimestamp() { SetTimestamp(wxEmptyString); }
430
431
432 // is this trace mask in the list?
433 static bool IsAllowedTraceMask(const wxString& mask);
434
435 // get the current timestamp format string (maybe empty)
436 static const wxString& GetTimestamp() { return ms_timestamp; }
437
438
439
440 // helpers: all functions in this section are mostly for internal use only,
441 // don't call them from your code even if they are not formally deprecated
442
443 // put the time stamp into the string if ms_timestamp != NULL (don't
444 // change it otherwise)
445 static void TimeStamp(wxString *str);
446
447 // these methods should only be called from derived classes DoLogRecord(),
448 // DoLogTextAtLevel() and DoLogText() implementations respectively and
449 // shouldn't be called directly, use logging functions instead
450 void LogRecord(wxLogLevel level,
451 const wxString& msg,
452 const wxLogRecordInfo& info)
453 {
454 DoLogRecord(level, msg, info);
455 }
456
457 void LogTextAtLevel(wxLogLevel level, const wxString& msg)
458 {
459 DoLogTextAtLevel(level, msg);
460 }
461
462 void LogText(const wxString& msg)
463 {
464 DoLogText(msg);
465 }
466
467 // this is a helper used by wxLogXXX() functions, don't call it directly
468 // and see DoLog() for function to overload in the derived classes
469 static void OnLog(wxLogLevel level,
470 const wxString& msg,
471 const wxLogRecordInfo& info);
472
473 // version called when no information about the location of the log record
474 // generation is available (but the time stamp is), it mainly exists for
475 // backwards compatibility, don't use it in new code
476 static void OnLog(wxLogLevel level, const wxString& msg, time_t t);
477
478 // a helper calling the above overload with current time
479 static void OnLog(wxLogLevel level, const wxString& msg)
480 {
481 OnLog(level, msg, time(NULL));
482 }
483
484
485 // this method exists for backwards compatibility only, don't use
486 bool HasPendingMessages() const { return true; }
487
488 #if WXWIN_COMPATIBILITY_2_6
489 // this function doesn't do anything any more, don't call it
490 wxDEPRECATED_INLINE(
491 static wxChar *SetLogBuffer(wxChar *, size_t = 0), return NULL;
492 );
493 #endif // WXWIN_COMPATIBILITY_2_6
494
495 // don't use integer masks any more, use string trace masks instead
496 #if WXWIN_COMPATIBILITY_2_8
497 wxDEPRECATED_INLINE( static void SetTraceMask(wxTraceMask ulMask),
498 ms_ulTraceMask = ulMask; )
499
500 // this one can't be marked deprecated as it's used in our own wxLogger
501 // below but it still is deprecated and shouldn't be used
502 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
503 #endif // WXWIN_COMPATIBILITY_2_8
504
505 protected:
506 // the logging functions that can be overridden: DoLogRecord() is called
507 // for every "record", i.e. a unit of log output, to be logged and by
508 // default formats the message and passes it to DoLogTextAtLevel() which in
509 // turn passes it to DoLogText() by default
510
511 // override this method if you want to change message formatting or do
512 // dynamic filtering
513 virtual void DoLogRecord(wxLogLevel level,
514 const wxString& msg,
515 const wxLogRecordInfo& info);
516
517 // override this method to redirect output to different channels depending
518 // on its level only; if even the level doesn't matter, override
519 // DoLogText() instead
520 virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
521
522 // this function is not pure virtual as it might not be needed if you do
523 // the logging in overridden DoLogRecord() or DoLogTextAtLevel() directly
524 // but if you do not override them in your derived class you must override
525 // this one as the default implementation of it simply asserts
526 virtual void DoLogText(const wxString& msg);
527
528
529 // the rest of the functions are for backwards compatibility only, don't
530 // use them in new code; if you're updating your existing code you need to
531 // switch to overriding DoLogRecord/Text() above (although as long as these
532 // functions exist, log classes using them will continue to work)
533 #if WXWIN_COMPATIBILITY_2_8
534 wxDEPRECATED_BUT_USED_INTERNALLY(
535 virtual void DoLog(wxLogLevel level, const char *szString, time_t t)
536 );
537
538 wxDEPRECATED_BUT_USED_INTERNALLY(
539 virtual void DoLog(wxLogLevel level, const wchar_t *wzString, time_t t)
540 );
541
542 // these shouldn't be used by new code
543 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
544 virtual void DoLogString(const char *WXUNUSED(szString),
545 time_t WXUNUSED(t)),
546 wxEMPTY_PARAMETER_VALUE
547 )
548
549 wxDEPRECATED_BUT_USED_INTERNALLY_INLINE(
550 virtual void DoLogString(const wchar_t *WXUNUSED(wzString),
551 time_t WXUNUSED(t)),
552 wxEMPTY_PARAMETER_VALUE
553 )
554 #endif // WXWIN_COMPATIBILITY_2_8
555
556
557 // log a message indicating the number of times the previous message was
558 // repeated if previous repetition counter is strictly positive, does
559 // nothing otherwise; return the old value of repetition counter
560 unsigned LogLastRepeatIfNeeded();
561
562 private:
563 // implement of LogLastRepeatIfNeeded(): it assumes that the
564 // caller had already locked GetPreviousLogCS()
565 unsigned LogLastRepeatIfNeededUnlocked();
566
567 // static variables
568 // ----------------
569
570 // if true, don't log the same message multiple times, only log it once
571 // with the number of times it was repeated
572 static bool ms_bRepetCounting;
573
574 static wxLog *ms_pLogger; // currently active log sink
575 static bool ms_doLog; // false => all logging disabled
576 static bool ms_bAutoCreate; // create new log targets on demand?
577 static bool ms_bVerbose; // false => ignore LogInfo messages
578
579 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
580
581 static size_t ms_suspendCount; // if positive, logs are not flushed
582
583 // format string for strftime(), if empty, time stamping log messages is
584 // disabled
585 static wxString ms_timestamp;
586
587 #if WXWIN_COMPATIBILITY_2_8
588 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
589 #endif // WXWIN_COMPATIBILITY_2_8
590
591 // currently enabled trace masks
592 static wxArrayString ms_aTraceMasks;
593 };
594
595 // ----------------------------------------------------------------------------
596 // "trivial" derivations of wxLog
597 // ----------------------------------------------------------------------------
598
599 // log everything except for the debug/trace messages (which are passed to
600 // wxMessageOutputDebug) to a buffer
601 class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog
602 {
603 public:
604 wxLogBuffer() { }
605
606 // get the string contents with all messages logged
607 const wxString& GetBuffer() const { return m_str; }
608
609 // show the buffer contents to the user in the best possible way (this uses
610 // wxMessageOutputMessageBox) and clear it
611 virtual void Flush();
612
613 protected:
614 virtual void DoLogTextAtLevel(wxLogLevel level, const wxString& msg);
615
616 private:
617 wxString m_str;
618
619 wxDECLARE_NO_COPY_CLASS(wxLogBuffer);
620 };
621
622
623 // log everything to a "FILE *", stderr by default
624 class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
625 {
626 public:
627 // redirect log output to a FILE
628 wxLogStderr(FILE *fp = NULL);
629
630 protected:
631 // implement sink function
632 virtual void DoLogText(const wxString& msg);
633
634 FILE *m_fp;
635
636 wxDECLARE_NO_COPY_CLASS(wxLogStderr);
637 };
638
639 #if wxUSE_STD_IOSTREAM
640
641 // log everything to an "ostream", cerr by default
642 class WXDLLIMPEXP_BASE wxLogStream : public wxLog
643 {
644 public:
645 // redirect log output to an ostream
646 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
647
648 protected:
649 // implement sink function
650 virtual void DoLogText(const wxString& msg);
651
652 // using ptr here to avoid including <iostream.h> from this file
653 wxSTD ostream *m_ostr;
654 };
655
656 #endif // wxUSE_STD_IOSTREAM
657
658 // ----------------------------------------------------------------------------
659 // /dev/null log target: suppress logging until this object goes out of scope
660 // ----------------------------------------------------------------------------
661
662 // example of usage:
663 /*
664 void Foo()
665 {
666 wxFile file;
667
668 // wxFile.Open() normally complains if file can't be opened, we don't
669 // want it
670 wxLogNull logNo;
671
672 if ( !file.Open("bar") )
673 ... process error ourselves ...
674
675 // ~wxLogNull called, old log sink restored
676 }
677 */
678 class WXDLLIMPEXP_BASE wxLogNull
679 {
680 public:
681 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
682 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
683
684 private:
685 bool m_flagOld; // the previous value of the wxLog::ms_doLog
686 };
687
688 // ----------------------------------------------------------------------------
689 // chaining log target: installs itself as a log target and passes all
690 // messages to the real log target given to it in the ctor but also forwards
691 // them to the previously active one
692 //
693 // note that you don't have to call SetActiveTarget() with this class, it
694 // does it itself in its ctor
695 // ----------------------------------------------------------------------------
696
697 class WXDLLIMPEXP_BASE wxLogChain : public wxLog
698 {
699 public:
700 wxLogChain(wxLog *logger);
701 virtual ~wxLogChain();
702
703 // change the new log target
704 void SetLog(wxLog *logger);
705
706 // this can be used to temporarily disable (and then reenable) passing
707 // messages to the old logger (by default we do pass them)
708 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
709
710 // are we passing the messages to the previous log target?
711 bool IsPassingMessages() const { return m_bPassMessages; }
712
713 // return the previous log target (may be NULL)
714 wxLog *GetOldLog() const { return m_logOld; }
715
716 // override base class version to flush the old logger as well
717 virtual void Flush();
718
719 // call to avoid destroying the old log target
720 void DetachOldLog() { m_logOld = NULL; }
721
722 protected:
723 // pass the record to the old logger if needed
724 virtual void DoLogRecord(wxLogLevel level,
725 const wxString& msg,
726 const wxLogRecordInfo& info);
727
728 private:
729 // the current log target
730 wxLog *m_logNew;
731
732 // the previous log target
733 wxLog *m_logOld;
734
735 // do we pass the messages to the old logger?
736 bool m_bPassMessages;
737
738 wxDECLARE_NO_COPY_CLASS(wxLogChain);
739 };
740
741 // a chain log target which uses itself as the new logger
742
743 #define wxLogPassThrough wxLogInterposer
744
745 class WXDLLIMPEXP_BASE wxLogInterposer : public wxLogChain
746 {
747 public:
748 wxLogInterposer();
749
750 private:
751 wxDECLARE_NO_COPY_CLASS(wxLogInterposer);
752 };
753
754 // a temporary interposer which doesn't destroy the old log target
755 // (calls DetachOldLog)
756
757 class WXDLLIMPEXP_BASE wxLogInterposerTemp : public wxLogChain
758 {
759 public:
760 wxLogInterposerTemp();
761
762 private:
763 wxDECLARE_NO_COPY_CLASS(wxLogInterposerTemp);
764 };
765
766 #if wxUSE_GUI
767 // include GUI log targets:
768 #include "wx/generic/logg.h"
769 #endif // wxUSE_GUI
770
771 // ----------------------------------------------------------------------------
772 // wxLogger
773 // ----------------------------------------------------------------------------
774
775 // wxLogger is a helper class used by wxLogXXX() functions implementation,
776 // don't use it directly as it's experimental and subject to change (OTOH it
777 // might become public in the future if it's deemed to be useful enough)
778
779 // contains information about the context from which a log message originates
780 // and provides Log() vararg method which forwards to wxLog::OnLog() and passes
781 // this context to it
782 class wxLogger
783 {
784 public:
785 // ctor takes the basic information about the log record
786 wxLogger(wxLogLevel level,
787 const char *filename,
788 int line,
789 const char *func,
790 const char *component)
791 : m_level(level),
792 m_info(filename, line, func, component)
793 {
794 }
795
796 // store extra data in our log record and return this object itself (so
797 // that further calls to its functions could be chained)
798 template <typename T>
799 wxLogger& Store(const wxString& key, T val)
800 {
801 m_info.StoreValue(key, val);
802 return *this;
803 }
804
805 // hack for "overloaded" wxLogXXX() functions: calling this method
806 // indicates that we may have an extra first argument preceding the format
807 // string and that if we do have it, we should store it in m_info using the
808 // given key (while by default 0 value will be used)
809 wxLogger& MaybeStore(const wxString& key)
810 {
811 wxASSERT_MSG( m_optKey.empty(), "can only have one optional value" );
812 m_optKey = key;
813
814 m_info.StoreValue(key, 0);
815 return *this;
816 }
817
818
819 // non-vararg function used by wxVLogXXX():
820
821 // log the message at the level specified in the ctor if this log message
822 // is enabled
823 void LogV(const wxString& format, va_list argptr)
824 {
825 // remember that fatal errors can't be disabled
826 if ( m_level == wxLOG_FatalError ||
827 wxLog::IsLevelEnabled(m_level, m_info.component) )
828 DoCallOnLog(format, argptr);
829 }
830
831 // overloads used by functions with optional leading arguments (whose
832 // values are stored in the key passed to MaybeStore())
833 void LogV(long num, const wxString& format, va_list argptr)
834 {
835 Store(m_optKey, num);
836
837 LogV(format, argptr);
838 }
839
840 void LogV(void *ptr, const wxString& format, va_list argptr)
841 {
842 Store(m_optKey, wxPtrToUInt(ptr));
843
844 LogV(format, argptr);
845 }
846
847
848 // vararg functions used by wxLogXXX():
849
850 // will log the message at the level specified in the ctor
851 //
852 // notice that this function supposes that the caller already checked that
853 // the level was enabled and does no checks itself
854 WX_DEFINE_VARARG_FUNC_VOID
855 (
856 Log,
857 1, (const wxFormatString&),
858 DoLog, DoLogUtf8
859 )
860
861 // same as Log() but with an extra numeric or pointer parameters: this is
862 // used to pass an optional value by storing it in m_info under the name
863 // passed to MaybeStore() and is required to support "overloaded" versions
864 // of wxLogStatus() and wxLogSysError()
865 WX_DEFINE_VARARG_FUNC_VOID
866 (
867 Log,
868 2, (long, const wxFormatString&),
869 DoLogWithNum, DoLogWithNumUtf8
870 )
871
872 // unfortunately we can't use "void *" here as we get overload ambiguities
873 // with Log(wxFormatString, ...) when the first argument is a "char *" or
874 // "wchar_t *" then -- so we only allow passing wxObject here, which is
875 // ugly but fine in practice as this overload is only used by wxLogStatus()
876 // whose first argument is a wxFrame
877 WX_DEFINE_VARARG_FUNC_VOID
878 (
879 Log,
880 2, (wxObject *, const wxFormatString&),
881 DoLogWithPtr, DoLogWithPtrUtf8
882 )
883
884 // log the message at the level specified as its first argument
885 //
886 // as the macros don't have access to the level argument in this case, this
887 // function does check that the level is enabled itself
888 WX_DEFINE_VARARG_FUNC_VOID
889 (
890 LogAtLevel,
891 2, (wxLogLevel, const wxFormatString&),
892 DoLogAtLevel, DoLogAtLevelUtf8
893 )
894
895 // special versions for wxLogTrace() which is passed either string or
896 // integer mask as first argument determining whether the message should be
897 // logged or not
898 WX_DEFINE_VARARG_FUNC_VOID
899 (
900 LogTrace,
901 2, (const wxString&, const wxFormatString&),
902 DoLogTrace, DoLogTraceUtf8
903 )
904
905 #if WXWIN_COMPATIBILITY_2_8
906 WX_DEFINE_VARARG_FUNC_VOID
907 (
908 LogTrace,
909 2, (wxTraceMask, const wxFormatString&),
910 DoLogTraceMask, DoLogTraceMaskUtf8
911 )
912 #endif // WXWIN_COMPATIBILITY_2_8
913
914 #ifdef __WATCOMC__
915 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
916 WX_VARARG_WATCOM_WORKAROUND(void, Log,
917 1, (const wxString&),
918 (wxFormatString(f1)))
919 WX_VARARG_WATCOM_WORKAROUND(void, Log,
920 1, (const wxCStrData&),
921 (wxFormatString(f1)))
922 WX_VARARG_WATCOM_WORKAROUND(void, Log,
923 1, (const char*),
924 (wxFormatString(f1)))
925 WX_VARARG_WATCOM_WORKAROUND(void, Log,
926 1, (const wchar_t*),
927 (wxFormatString(f1)))
928
929 WX_VARARG_WATCOM_WORKAROUND(void, Log,
930 2, (long, const wxString&),
931 (f1, wxFormatString(f2)))
932 WX_VARARG_WATCOM_WORKAROUND(void, Log,
933 2, (long, const wxCStrData&),
934 (f1, wxFormatString(f2)))
935 WX_VARARG_WATCOM_WORKAROUND(void, Log,
936 2, (long, const char *),
937 (f1, wxFormatString(f2)))
938 WX_VARARG_WATCOM_WORKAROUND(void, Log,
939 2, (long, const wchar_t *),
940 (f1, wxFormatString(f2)))
941
942 WX_VARARG_WATCOM_WORKAROUND(void, Log,
943 2, (wxObject *, const wxString&),
944 (f1, wxFormatString(f2)))
945 WX_VARARG_WATCOM_WORKAROUND(void, Log,
946 2, (wxObject *, const wxCStrData&),
947 (f1, wxFormatString(f2)))
948 WX_VARARG_WATCOM_WORKAROUND(void, Log,
949 2, (wxObject *, const char *),
950 (f1, wxFormatString(f2)))
951 WX_VARARG_WATCOM_WORKAROUND(void, Log,
952 2, (wxObject *, const wchar_t *),
953 (f1, wxFormatString(f2)))
954
955 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
956 2, (wxLogLevel, const wxString&),
957 (f1, wxFormatString(f2)))
958 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
959 2, (wxLogLevel, const wxCStrData&),
960 (f1, wxFormatString(f2)))
961 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
962 2, (wxLogLevel, const char *),
963 (f1, wxFormatString(f2)))
964 WX_VARARG_WATCOM_WORKAROUND(void, LogAtLevel,
965 2, (wxLogLevel, const wchar_t *),
966 (f1, wxFormatString(f2)))
967
968 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
969 2, (const wxString&, const wxString&),
970 (f1, wxFormatString(f2)))
971 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
972 2, (const wxString&, const wxCStrData&),
973 (f1, wxFormatString(f2)))
974 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
975 2, (const wxString&, const char *),
976 (f1, wxFormatString(f2)))
977 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
978 2, (const wxString&, const wchar_t *),
979 (f1, wxFormatString(f2)))
980
981 #if WXWIN_COMPATIBILITY_2_8
982 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
983 2, (wxTraceMask, wxTraceMask),
984 (f1, wxFormatString(f2)))
985 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
986 2, (wxTraceMask, const wxCStrData&),
987 (f1, wxFormatString(f2)))
988 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
989 2, (wxTraceMask, const char *),
990 (f1, wxFormatString(f2)))
991 WX_VARARG_WATCOM_WORKAROUND(void, LogTrace,
992 2, (wxTraceMask, const wchar_t *),
993 (f1, wxFormatString(f2)))
994 #endif // WXWIN_COMPATIBILITY_2_8
995 #endif // __WATCOMC__
996
997 private:
998 #if !wxUSE_UTF8_LOCALE_ONLY
999 void DoLog(const wxChar *format, ...)
1000 {
1001 va_list argptr;
1002 va_start(argptr, format);
1003 DoCallOnLog(format, argptr);
1004 va_end(argptr);
1005 }
1006
1007 void DoLogWithNum(long num, const wxChar *format, ...)
1008 {
1009 Store(m_optKey, num);
1010
1011 va_list argptr;
1012 va_start(argptr, format);
1013 DoCallOnLog(format, argptr);
1014 va_end(argptr);
1015 }
1016
1017 void DoLogWithPtr(void *ptr, const wxChar *format, ...)
1018 {
1019 Store(m_optKey, wxPtrToUInt(ptr));
1020
1021 va_list argptr;
1022 va_start(argptr, format);
1023 DoCallOnLog(format, argptr);
1024 va_end(argptr);
1025 }
1026
1027 void DoLogAtLevel(wxLogLevel level, const wxChar *format, ...)
1028 {
1029 if ( !wxLog::IsLevelEnabled(level, m_info.component) )
1030 return;
1031
1032 va_list argptr;
1033 va_start(argptr, format);
1034 DoCallOnLog(level, format, argptr);
1035 va_end(argptr);
1036 }
1037
1038 void DoLogTrace(const wxString& mask, const wxChar *format, ...)
1039 {
1040 if ( !wxLog::IsAllowedTraceMask(mask) )
1041 return;
1042
1043 Store(wxLOG_KEY_TRACE_MASK, mask);
1044
1045 va_list argptr;
1046 va_start(argptr, format);
1047 DoCallOnLog(format, argptr);
1048 va_end(argptr);
1049 }
1050
1051 #if WXWIN_COMPATIBILITY_2_8
1052 void DoLogTraceMask(wxTraceMask mask, const wxChar *format, ...)
1053 {
1054 if ( (wxLog::GetTraceMask() & mask) != mask )
1055 return;
1056
1057 Store(wxLOG_KEY_TRACE_MASK, mask);
1058
1059 va_list argptr;
1060 va_start(argptr, format);
1061 DoCallOnLog(format, argptr);
1062 va_end(argptr);
1063 }
1064 #endif // WXWIN_COMPATIBILITY_2_8
1065 #endif // !wxUSE_UTF8_LOCALE_ONLY
1066
1067 #if wxUSE_UNICODE_UTF8
1068 void DoLogUtf8(const char *format, ...)
1069 {
1070 va_list argptr;
1071 va_start(argptr, format);
1072 DoCallOnLog(format, argptr);
1073 va_end(argptr);
1074 }
1075
1076 void DoLogWithNumUtf8(long num, const char *format, ...)
1077 {
1078 Store(m_optKey, num);
1079
1080 va_list argptr;
1081 va_start(argptr, format);
1082 DoCallOnLog(format, argptr);
1083 va_end(argptr);
1084 }
1085
1086 void DoLogWithPtrUtf8(void *ptr, const char *format, ...)
1087 {
1088 Store(m_optKey, wxPtrToUInt(ptr));
1089
1090 va_list argptr;
1091 va_start(argptr, format);
1092 DoCallOnLog(format, argptr);
1093 va_end(argptr);
1094 }
1095
1096 void DoLogAtLevelUtf8(wxLogLevel level, const char *format, ...)
1097 {
1098 if ( !wxLog::IsLevelEnabled(level, m_info.component) )
1099 return;
1100
1101 va_list argptr;
1102 va_start(argptr, format);
1103 DoCallOnLog(level, format, argptr);
1104 va_end(argptr);
1105 }
1106
1107 void DoLogTraceUtf8(const wxString& mask, const char *format, ...)
1108 {
1109 if ( !wxLog::IsAllowedTraceMask(mask) )
1110 return;
1111
1112 Store(wxLOG_KEY_TRACE_MASK, mask);
1113
1114 va_list argptr;
1115 va_start(argptr, format);
1116 DoCallOnLog(format, argptr);
1117 va_end(argptr);
1118 }
1119
1120 #if WXWIN_COMPATIBILITY_2_8
1121 void DoLogTraceMaskUtf8(wxTraceMask mask, const char *format, ...)
1122 {
1123 if ( (wxLog::GetTraceMask() & mask) != mask )
1124 return;
1125
1126 Store(wxLOG_KEY_TRACE_MASK, mask);
1127
1128 va_list argptr;
1129 va_start(argptr, format);
1130 DoCallOnLog(format, argptr);
1131 va_end(argptr);
1132 }
1133 #endif // WXWIN_COMPATIBILITY_2_8
1134 #endif // wxUSE_UNICODE_UTF8
1135
1136 void DoCallOnLog(wxLogLevel level, const wxString& format, va_list argptr)
1137 {
1138 wxLog::OnLog(level, wxString::FormatV(format, argptr), m_info);
1139 }
1140
1141 void DoCallOnLog(const wxString& format, va_list argptr)
1142 {
1143 wxLog::OnLog(m_level, wxString::FormatV(format, argptr), m_info);
1144 }
1145
1146
1147 const wxLogLevel m_level;
1148 wxLogRecordInfo m_info;
1149
1150 wxString m_optKey;
1151
1152 wxDECLARE_NO_COPY_CLASS(wxLogger);
1153 };
1154
1155 // ============================================================================
1156 // global functions
1157 // ============================================================================
1158
1159 // ----------------------------------------------------------------------------
1160 // get error code/error message from system in a portable way
1161 // ----------------------------------------------------------------------------
1162
1163 // return the last system error code
1164 WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
1165
1166 // return the error message for given (or last if 0) error code
1167 WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
1168
1169 // ----------------------------------------------------------------------------
1170 // define wxLog<level>() functions which can be used by application instead of
1171 // stdio, iostream &c for log messages for easy redirection
1172 // ----------------------------------------------------------------------------
1173
1174 /*
1175 The code below is unreadable because it (unfortunately unavoidably)
1176 contains a lot of macro magic but all it does is to define wxLogXXX() such
1177 that you can call them as vararg functions to log a message at the
1178 corresponding level.
1179
1180 More precisely, it defines:
1181
1182 - wxLog{FatalError,Error,Warning,Message,Verbose,Debug}() functions
1183 taking the format string and additional vararg arguments if needed.
1184 - wxLogGeneric(wxLogLevel level, const wxString& format, ...) which
1185 takes the log level explicitly.
1186 - wxLogSysError(const wxString& format, ...) and wxLogSysError(long
1187 err, const wxString& format, ...) which log a wxLOG_Error severity
1188 message with the error message corresponding to the system error code
1189 err or the last error.
1190 - wxLogStatus(const wxString& format, ...) which logs the message into
1191 the status bar of the main application window and its overload
1192 wxLogStatus(wxFrame *frame, const wxString& format, ...) which logs it
1193 into the status bar of the specified frame.
1194 - wxLogTrace(Mask mask, const wxString& format, ...) which only logs
1195 the message is the specified mask is enabled. This comes in two kinds:
1196 Mask can be a wxString or a long. Both are deprecated.
1197
1198 In addition, wxVLogXXX() versions of all the functions above are also
1199 defined. They take a va_list argument instead of "...".
1200 */
1201
1202 // creates wxLogger object for the current location
1203 #define wxMAKE_LOGGER(level) \
1204 wxLogger(wxLOG_##level, __FILE__, __LINE__, __WXFUNCTION__, wxLOG_COMPONENT)
1205
1206 // this macro generates the expression which logs whatever follows it in
1207 // parentheses at the level specified as argument
1208 #define wxDO_LOG(level) wxMAKE_LOGGER(level).Log
1209
1210 // this is the non-vararg equivalent
1211 #define wxDO_LOGV(level, format, argptr) \
1212 wxMAKE_LOGGER(level).LogV(format, argptr)
1213
1214 // this macro declares wxLog<level>() macro which logs whatever follows it if
1215 // logging at specified level is enabled (notice that if it is false, the
1216 // following arguments are not even evaluated which is good as it avoids
1217 // unnecessary overhead)
1218 //
1219 // Note: the strange if/else construct is needed to make the following code
1220 //
1221 // if ( cond )
1222 // wxLogError("!!!");
1223 // else
1224 // ...
1225 //
1226 // work as expected, without it the second "else" would match the "if"
1227 // inside wxLogError(). Unfortunately code like
1228 //
1229 // if ( cond )
1230 // wxLogError("!!!");
1231 //
1232 // now provokes "suggest explicit braces to avoid ambiguous 'else'"
1233 // warnings from g++ 4.3 and later with -Wparentheses on but they can be
1234 // easily fixed by adding curly braces around wxLogError() and at least
1235 // the code still does do the right thing.
1236 #define wxDO_LOG_IF_ENABLED(level) \
1237 if ( !wxLog::IsLevelEnabled(wxLOG_##level, wxLOG_COMPONENT) ) \
1238 {} \
1239 else \
1240 wxDO_LOG(level)
1241
1242 // wxLogFatalError() is special as it can't be disabled
1243 #define wxLogFatalError wxDO_LOG(FatalError)
1244 #define wxVLogFatalError(format, argptr) wxDO_LOGV(FatalError, format, argptr)
1245
1246 #define wxLogError wxDO_LOG_IF_ENABLED(Error)
1247 #define wxVLogError(format, argptr) wxDO_LOGV(Error, format, argptr)
1248
1249 #define wxLogWarning wxDO_LOG_IF_ENABLED(Warning)
1250 #define wxVLogWarning(format, argptr) wxDO_LOGV(Warning, format, argptr)
1251
1252 #define wxLogMessage wxDO_LOG_IF_ENABLED(Message)
1253 #define wxVLogMessage(format, argptr) wxDO_LOGV(Message, format, argptr)
1254
1255 // this one is special as it only logs if we're in verbose mode
1256 #define wxLogVerbose \
1257 if ( !(wxLog::IsLevelEnabled(wxLOG_Info, wxLOG_COMPONENT) && \
1258 wxLog::GetVerbose()) ) \
1259 {} \
1260 else \
1261 wxDO_LOG(Info)
1262 #define wxVLogVerbose(format, argptr) \
1263 if ( !(wxLog::IsLevelEnabled(wxLOG_Info, wxLOG_COMPONENT) && \
1264 wxLog::GetVerbose()) ) \
1265 {} \
1266 else \
1267 wxDO_LOGV(Info, format, argptr)
1268
1269 // deprecated synonyms for wxLogVerbose() and wxVLogVerbose()
1270 #define wxLogInfo wxLogVerbose
1271 #define wxVLogInfo wxVLogVerbose
1272
1273
1274 // another special case: the level is passed as first argument of the function
1275 // and so is not available to the macro
1276 //
1277 // notice that because of this, arguments of wxLogGeneric() are currently
1278 // always evaluated, unlike for the other log functions
1279 #define wxLogGeneric wxMAKE_LOGGER(Max).LogAtLevel
1280 #define wxVLogGeneric(level, format, argptr) \
1281 if ( !wxLog::IsLevelEnabled(wxLOG_##level, wxLOG_COMPONENT) ) \
1282 {} \
1283 else \
1284 wxDO_LOGV(level, format, argptr)
1285
1286
1287 // wxLogSysError() needs to stash the error code value in the log record info
1288 // so it needs special handling too; additional complications arise because the
1289 // error code may or not be present as the first argument
1290 #define wxLOG_KEY_SYS_ERROR_CODE "wx.sys_error"
1291
1292 #define wxLogSysError \
1293 if ( !wxLog::IsLevelEnabled(wxLOG_Error, wxLOG_COMPONENT) ) \
1294 {} \
1295 else \
1296 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).Log
1297
1298 // unfortunately we can't have overloaded macros so we can't define versions
1299 // both with and without error code argument and have to rely on LogV()
1300 // overloads in wxLogger to select between them
1301 #define wxVLogSysError \
1302 wxMAKE_LOGGER(Error).MaybeStore(wxLOG_KEY_SYS_ERROR_CODE).LogV
1303
1304 #if wxUSE_GUI
1305 // wxLogStatus() is similar to wxLogSysError() as it allows to optionally
1306 // specify the frame to which the message should go
1307 #define wxLOG_KEY_FRAME "wx.frame"
1308
1309 #define wxLogStatus \
1310 if ( !wxLog::IsLevelEnabled(wxLOG_Status, wxLOG_COMPONENT) ) \
1311 {} \
1312 else \
1313 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).Log
1314
1315 #define wxVLogStatus(format, argptr) \
1316 wxMAKE_LOGGER(Status).MaybeStore(wxLOG_KEY_FRAME).LogV
1317 #endif // wxUSE_GUI
1318
1319
1320 #else // !wxUSE_LOG
1321
1322 #undef wxUSE_LOG_DEBUG
1323 #define wxUSE_LOG_DEBUG 0
1324
1325 #undef wxUSE_LOG_TRACE
1326 #define wxUSE_LOG_TRACE 0
1327
1328 #if defined(__WATCOMC__) || defined(__MINGW32__)
1329 // Mingw has similar problem with wxLogSysError:
1330 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
1331 #else
1332 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
1333 #endif
1334
1335 // define macros for defining log functions which do nothing at all
1336 //
1337 // WX_WATCOM_ONLY_CODE is needed to work around
1338 // http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1339 #define wxDEFINE_EMPTY_LOG_FUNCTION(level) \
1340 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
1341 WX_WATCOM_ONLY_CODE( \
1342 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
1343 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
1344 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
1345 ) \
1346 inline void wxVLog##level(const wxString& WXUNUSED(format), \
1347 va_list WXUNUSED(argptr)) { } \
1348
1349 #define wxDEFINE_EMPTY_LOG_FUNCTION2(level, argclass) \
1350 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
1351 WX_WATCOM_OR_MINGW_ONLY_CODE( \
1352 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
1353 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
1354 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
1355 ) \
1356 inline void wxVLog##level(argclass WXUNUSED(arg), \
1357 const wxString& WXUNUSED(format), \
1358 va_list WXUNUSED(argptr)) {}
1359
1360 wxDEFINE_EMPTY_LOG_FUNCTION(FatalError);
1361 wxDEFINE_EMPTY_LOG_FUNCTION(Error);
1362 wxDEFINE_EMPTY_LOG_FUNCTION(SysError);
1363 wxDEFINE_EMPTY_LOG_FUNCTION2(SysError, long);
1364 wxDEFINE_EMPTY_LOG_FUNCTION(Warning);
1365 wxDEFINE_EMPTY_LOG_FUNCTION(Message);
1366 wxDEFINE_EMPTY_LOG_FUNCTION(Info);
1367 wxDEFINE_EMPTY_LOG_FUNCTION(Verbose);
1368
1369 wxDEFINE_EMPTY_LOG_FUNCTION2(Generic, wxLogLevel);
1370
1371 #if wxUSE_GUI
1372 wxDEFINE_EMPTY_LOG_FUNCTION(Status);
1373 wxDEFINE_EMPTY_LOG_FUNCTION2(Status, wxFrame *);
1374 #endif // wxUSE_GUI
1375
1376 // Empty Class to fake wxLogNull
1377 class WXDLLIMPEXP_BASE wxLogNull
1378 {
1379 public:
1380 wxLogNull() { }
1381 };
1382
1383 // Dummy macros to replace some functions.
1384 #define wxSysErrorCode() (unsigned long)0
1385 #define wxSysErrorMsg( X ) (const wxChar*)NULL
1386
1387 // Fake symbolic trace masks... for those that are used frequently
1388 #define wxTRACE_OleCalls wxEmptyString // OLE interface calls
1389
1390 #endif // wxUSE_LOG/!wxUSE_LOG
1391
1392
1393 // debug functions can be completely disabled in optimized builds
1394
1395 // if these log functions are disabled, we prefer to define them as (empty)
1396 // variadic macros as this completely removes them and their argument
1397 // evaluation from the object code but if this is not supported by compiler we
1398 // use empty inline functions instead (defining them as nothing would result in
1399 // compiler warnings)
1400 //
1401 // note that making wxVLogDebug/Trace() themselves (empty inline) functions is
1402 // a bad idea as some compilers are stupid enough to not inline even empty
1403 // functions if their parameters are complicated enough, but by defining them
1404 // as an empty inline function we ensure that even dumbest compilers optimise
1405 // them away
1406 #ifdef __BORLANDC__
1407 // but Borland gives "W8019: Code has no effect" for wxLogNop() so we need
1408 // to define it differently for it to avoid these warnings (same problem as
1409 // with wxUnusedVar())
1410 #define wxLogNop() { }
1411 #else
1412 inline void wxLogNop() { }
1413 #endif
1414
1415 #if wxUSE_LOG_DEBUG
1416 #define wxLogDebug wxDO_LOG_IF_ENABLED(Debug)
1417 #define wxVLogDebug(format, argptr) wxDO_LOGV(Debug, format, argptr)
1418 #else // !wxUSE_LOG_DEBUG
1419 #define wxVLogDebug(fmt, valist) wxLogNop()
1420
1421 #ifdef HAVE_VARIADIC_MACROS
1422 #define wxLogDebug(fmt, ...) wxLogNop()
1423 #else // !HAVE_VARIADIC_MACROS
1424 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug, 1, (const wxString&))
1425 #endif
1426 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1427
1428 #if wxUSE_LOG_TRACE
1429 #define wxLogTrace \
1430 if ( !wxLog::IsLevelEnabled(wxLOG_Trace, wxLOG_COMPONENT) ) \
1431 {} \
1432 else \
1433 wxMAKE_LOGGER(Trace).LogTrace
1434 #else // !wxUSE_LOG_TRACE
1435 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
1436
1437 #ifdef HAVE_VARIADIC_MACROS
1438 #define wxLogTrace(mask, fmt, ...) wxLogNop()
1439 #else // !HAVE_VARIADIC_MACROS
1440 #if WXWIN_COMPATIBILITY_2_8
1441 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (wxTraceMask, const wxString&))
1442 #endif
1443 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wxString&, const wxString&))
1444 #ifdef __WATCOMC__
1445 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1446 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const char*, const char*))
1447 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wchar_t*, const wchar_t*))
1448 #endif
1449 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
1450 #endif // wxUSE_LOG_TRACE/!wxUSE_LOG_TRACE
1451
1452 // wxLogFatalError helper: show the (fatal) error to the user in a safe way,
1453 // i.e. without using wxMessageBox() for example because it could crash
1454 void WXDLLIMPEXP_BASE
1455 wxSafeShowMessage(const wxString& title, const wxString& text);
1456
1457 // ----------------------------------------------------------------------------
1458 // debug only logging functions: use them with API name and error code
1459 // ----------------------------------------------------------------------------
1460
1461 #if wxUSE_LOG_DEBUG
1462 // make life easier for people using VC++ IDE: clicking on the message
1463 // will take us immediately to the place of the failed API
1464 #ifdef __VISUALC__
1465 #define wxLogApiError(api, rc) \
1466 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
1467 __FILE__, __LINE__, api, \
1468 (long)rc, wxSysErrorMsg(rc))
1469 #else // !VC++
1470 #define wxLogApiError(api, rc) \
1471 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
1472 wxT("error 0x%08lx (%s)."), \
1473 __FILE__, __LINE__, api, \
1474 (long)rc, wxSysErrorMsg(rc))
1475 #endif // VC++/!VC++
1476
1477 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
1478
1479 #else // !wxUSE_LOG_DEBUG
1480 #define wxLogApiError(api, err) wxLogNop()
1481 #define wxLogLastError(api) wxLogNop()
1482 #endif // wxUSE_LOG_DEBUG/!wxUSE_LOG_DEBUG
1483
1484 // wxCocoa has additiional trace masks
1485 #if defined(__WXCOCOA__)
1486 #include "wx/cocoa/log.h"
1487 #endif
1488
1489 #ifdef WX_WATCOM_ONLY_CODE
1490 #undef WX_WATCOM_ONLY_CODE
1491 #endif
1492
1493 #endif // _WX_LOG_H_
1494