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