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