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