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