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