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