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