]> git.saurik.com Git - wxWidgets.git/blame - include/wx/log.h
rename fobr95.{cpp,h} files to foobar.{cpp,h}
[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
204abcd4
PC
17#if wxUSE_THREADS
18 class WXDLLIMPEXP_FWD_BASE wxCriticalSection;
19#endif
a2d38265 20
0b4f47a3
DS
21// ----------------------------------------------------------------------------
22// common constants for use in wxUSE_LOG/!wxUSE_LOG
23// ----------------------------------------------------------------------------
24
25// the trace masks have been superceded by symbolic trace constants, they're
26// for compatibility only andwill be removed soon - do NOT use them
27
28// meaning of different bits of the trace mask (which allows selectively
29// enable/disable some trace messages)
30#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
31#define wxTraceMessages 0x0002 // trace window messages/X callbacks
32#define wxTraceResAlloc 0x0004 // trace GDI resource allocation
33#define wxTraceRefCount 0x0008 // trace various ref counting operations
34
35#ifdef __WXMSW__
36 #define wxTraceOleCalls 0x0100 // OLE interface calls
37#endif
38
546db2a8
VZ
39// ----------------------------------------------------------------------------
40// types
41// ----------------------------------------------------------------------------
42
1782be31 43// NB: these types are needed even if wxUSE_LOG == 0
546db2a8
VZ
44typedef unsigned long wxTraceMask;
45typedef unsigned long wxLogLevel;
46
47// ----------------------------------------------------------------------------
48// headers
49// ----------------------------------------------------------------------------
50
b6e4e44a 51#include "wx/string.h"
c9f78968 52#include "wx/strvararg.h"
b6e4e44a 53
1782be31
VZ
54#if wxUSE_LOG
55
1782be31
VZ
56#include "wx/arrstr.h"
57
9b4da627 58#ifndef __WXPALMOS5__
0e0126c2 59#ifndef __WXWINCE__
1782be31 60 #include <time.h> // for time_t
0e0126c2 61#endif
9b4da627 62#endif // ! __WXPALMOS5__
c30aaf75
VZ
63
64#include "wx/dynarray.h"
d6b9496a 65
edc73852
RD
66#ifndef wxUSE_LOG_DEBUG
67# ifdef __WXDEBUG__
68# define wxUSE_LOG_DEBUG 1
69# else // !__WXDEBUG__
70# define wxUSE_LOG_DEBUG 0
71# endif
72#endif
73
1782be31
VZ
74// ----------------------------------------------------------------------------
75// forward declarations
76// ----------------------------------------------------------------------------
77
78#if wxUSE_GUI
b5dbe15d
VS
79 class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
80 class WXDLLIMPEXP_FWD_CORE wxLogFrame;
81 class WXDLLIMPEXP_FWD_CORE wxFrame;
82 class WXDLLIMPEXP_FWD_CORE wxWindow;
1782be31
VZ
83#endif // wxUSE_GUI
84
9ef3052c
VZ
85// ----------------------------------------------------------------------------
86// constants
87// ----------------------------------------------------------------------------
88
89// different standard log levels (you may also define your own)
90adbcca 90enum wxLogLevelValues
9ef3052c 91{
d6b9496a
VZ
92 wxLOG_FatalError, // program can't continue, abort immediately
93 wxLOG_Error, // a serious error, user must be informed about it
94 wxLOG_Warning, // user is normally informed about it but may be ignored
95 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
d6b9496a 96 wxLOG_Status, // informational: might go to the status line of GUI app
edc73852 97 wxLOG_Info, // informational message (a.k.a. 'Verbose')
d6b9496a
VZ
98 wxLOG_Debug, // never shown to the user, disabled in release mode
99 wxLOG_Trace, // trace messages are also only enabled in debug mode
100 wxLOG_Progress, // used for progress indicator (not yet)
edc73852 101 wxLOG_User = 100, // user defined levels start here
65ca8c0b 102 wxLOG_Max = 10000
9ef3052c
VZ
103};
104
d6b9496a
VZ
105// symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
106// discarded unless the string "foo" has been added to the list of allowed
107// ones with AddTraceMask()
108
08298395
OK
109#define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
110#define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
111#define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
112#define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
d6b9496a
VZ
113
114#ifdef __WXMSW__
08298395 115 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
d6b9496a
VZ
116#endif
117
65f19af1 118#include "wx/iosfwrap.h"
470b7da3 119
c801d85f
KB
120// ----------------------------------------------------------------------------
121// derive from this class to redirect (or suppress, or ...) log messages
122// normally, only a single instance of this class exists but it's not enforced
c801d85f 123// ----------------------------------------------------------------------------
d6b9496a 124
bddd7a8d 125class WXDLLIMPEXP_BASE wxLog
c801d85f
KB
126{
127public:
d6b9496a 128 // ctor
6fb99eb3 129 wxLog(){}
d6b9496a
VZ
130
131 // these functions allow to completely disable all log messages
d1b20379
DS
132
133 // is logging disabled now?
d6b9496a 134 static bool IsEnabled() { return ms_doLog; }
d1b20379
DS
135
136 // change the flag state, return the previous one
137 static bool EnableLogging(bool doIt = true)
2e7f3845 138 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
d6b9496a
VZ
139
140 // static sink function - see DoLog() for function to overload in the
141 // derived classes
5a20d2ce 142 static void OnLog(wxLogLevel level, const wxString& szString, time_t t);
d6b9496a
VZ
143
144 // message buffering
d1b20379
DS
145
146 // flush shows all messages if they're not logged immediately (FILE
147 // and iostream logs don't need it, but wxGuiLog does to avoid showing
148 // 17 modal dialogs one after another)
d6b9496a 149 virtual void Flush();
d6b9496a 150
d1b20379 151 // flush the active target if any
bbfa0322
VZ
152 static void FlushActive()
153 {
2ed3265e
VZ
154 if ( !ms_suspendCount )
155 {
156 wxLog *log = GetActiveTarget();
1ec5cbf3 157 if ( log )
2ed3265e
VZ
158 log->Flush();
159 }
bbfa0322 160 }
1ec5cbf3
VZ
161
162 // only one sink is active at each moment
d1b20379
DS
163 // get current log target, will call wxApp::CreateLogTarget() to
164 // create one if none exists
d6b9496a 165 static wxLog *GetActiveTarget();
d1b20379
DS
166
167 // change log target, pLogger may be NULL
d6b9496a
VZ
168 static wxLog *SetActiveTarget(wxLog *pLogger);
169
d1b20379
DS
170 // suspend the message flushing of the main target until the next call
171 // to Resume() - this is mainly for internal use (to prevent wxYield()
172 // from flashing the messages)
2ed3265e 173 static void Suspend() { ms_suspendCount++; }
d1b20379
DS
174
175 // must be called for each Suspend()!
2ed3265e
VZ
176 static void Resume() { ms_suspendCount--; }
177
d6b9496a 178 // functions controlling the default wxLog behaviour
d1b20379
DS
179 // verbose mode is activated by standard command-line '-verbose'
180 // option
181 static void SetVerbose(bool bVerbose = true) { ms_bVerbose = bVerbose; }
edc73852 182
d1b20379 183 // Set log level. Log messages with level > logLevel will not be logged.
edc73852
RD
184 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
185
d1b20379
DS
186 // should GetActiveTarget() try to create a new log object if the
187 // current is NULL?
36bd6902 188 static void DontCreateOnDemand();
d6b9496a 189
e94cd97d
DE
190 // Make GetActiveTarget() create a new log object again.
191 static void DoCreateOnDemand();
192
f9837791
VZ
193 // log the count of repeating messages instead of logging the messages
194 // multiple times
195 static void SetRepetitionCounting(bool bRepetCounting = true)
2e7f3845 196 { ms_bRepetCounting = bRepetCounting; }
f9837791
VZ
197
198 // gets duplicate counting status
199 static bool GetRepetitionCounting() { return ms_bRepetCounting; }
200
d1b20379 201 // trace mask (see wxTraceXXX constants for details)
d6b9496a 202 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
d1b20379
DS
203
204 // add string trace mask
f96233d5 205 static void AddTraceMask(const wxString& str);
d1b20379
DS
206
207 // add string trace mask
d6b9496a 208 static void RemoveTraceMask(const wxString& str);
d1b20379
DS
209
210 // remove all string trace masks
36bd6902 211 static void ClearTraceMasks();
d1b20379 212
f96233d5
VZ
213 // get string trace masks: note that this is MT-unsafe if other threads can
214 // call AddTraceMask() concurrently
215 static const wxArrayString& GetTraceMasks() { return ms_aTraceMasks; }
d6b9496a 216
7b1bf3ad
VZ
217 // sets the time stamp string format: this is used as strftime() format
218 // string for the log targets which add time stamps to the messages; set
219 // it to empty string to disable time stamping completely.
d993e05b 220 static void SetTimestamp(const wxString& ts) { ms_timestamp = ts; }
d2e1ef19 221
7b1bf3ad
VZ
222 // disable time stamping of log messages
223 static void DisableTimestamp() { SetTimestamp(wxEmptyString); }
224
edc73852 225
d6b9496a 226 // accessors
d1b20379
DS
227
228 // gets the verbose status
fd7718b2 229 static bool GetVerbose() { return ms_bVerbose; }
d1b20379
DS
230
231 // get trace mask
d6b9496a 232 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
d1b20379
DS
233
234 // is this trace mask in the list?
5a20d2ce 235 static bool IsAllowedTraceMask(const wxString& mask);
d1b20379
DS
236
237 // return the current loglevel limit
edc73852 238 static wxLogLevel GetLogLevel() { return ms_logLevel; }
d6b9496a 239
d1b20379 240 // get the current timestamp format string (may be NULL)
d993e05b 241 static const wxString& GetTimestamp() { return ms_timestamp; }
d2e1ef19 242
edc73852 243
d2e1ef19 244 // helpers
d1b20379
DS
245
246 // put the time stamp into the string if ms_timestamp != NULL (don't
247 // change it otherwise)
d2e1ef19
VZ
248 static void TimeStamp(wxString *str);
249
d6b9496a 250 // make dtor virtual for all derived classes
f9837791 251 virtual ~wxLog();
c801d85f 252
c801d85f 253
1ec5cbf3 254 // this method exists for backwards compatibility only, don't use
d1b20379 255 bool HasPendingMessages() const { return true; }
1ec5cbf3 256
2e7f3845
VZ
257#if WXWIN_COMPATIBILITY_2_6
258 // this function doesn't do anything any more, don't call it
259 wxDEPRECATED( static wxChar *SetLogBuffer(wxChar *buf, size_t size = 0) );
260#endif
261
1ec5cbf3 262protected:
d6b9496a 263 // the logging functions that can be overriden
d1b20379
DS
264
265 // default DoLog() prepends the time stamp and a prefix corresponding
266 // to the message to szString and then passes it to DoLogString()
5a20d2ce
VS
267 virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t);
268#if WXWIN_COMPATIBILITY_2_8
269 // these shouldn't be used by new code
5d88a6b5
VZ
270 wxDEPRECATED_BUT_USED_INTERNALLY(
271 virtual void DoLog(wxLogLevel level, const char *szString, time_t t)
272 );
273
274 wxDEPRECATED_BUT_USED_INTERNALLY(
275 virtual void DoLog(wxLogLevel level, const wchar_t *wzString, time_t t)
276 );
277#endif // WXWIN_COMPATIBILITY_2_8
5a20d2ce
VS
278
279 void LogString(const wxString& szString, time_t t)
280 { DoLogString(szString, t); }
d1b20379
DS
281
282 // default DoLogString does nothing but is not pure virtual because if
283 // you override DoLog() you might not need it at all
5a20d2ce
VS
284 virtual void DoLogString(const wxString& szString, time_t t);
285#if WXWIN_COMPATIBILITY_2_8
286 // these shouldn't be used by new code
287 virtual void DoLogString(const char *WXUNUSED(szString),
288 time_t WXUNUSED(t)) {}
289 virtual void DoLogString(const wchar_t *WXUNUSED(szString),
290 time_t WXUNUSED(t)) {}
b99891b0
VZ
291#endif // WXWIN_COMPATIBILITY_2_8
292
293 // this macro should be used in the derived classes to avoid warnings about
294 // hiding the other DoLog() overloads when overriding DoLog(wxString) --
295 // but don't use it with MSVC which doesn't give this warning but does give
296 // warning when a deprecated function is overridden
297#if WXWIN_COMPATIBILITY_2_8 && !defined(__VISUALC__)
298 #define wxSUPPRESS_DOLOG_HIDE_WARNING() \
299 virtual void DoLog(wxLogLevel, const char *, time_t) { } \
300 virtual void DoLog(wxLogLevel, const wchar_t *, time_t) { }
301
302 #define wxSUPPRESS_DOLOGSTRING_HIDE_WARNING() \
303 virtual void DoLogString(const char *, time_t) { } \
304 virtual void DoLogString(const wchar_t *, time_t) { }
305#else
306 #define wxSUPPRESS_DOLOG_HIDE_WARNING()
307 #define wxSUPPRESS_DOLOGSTRING_HIDE_WARNING()
5a20d2ce 308#endif
c801d85f 309
2064113c 310 // log a message indicating the number of times the previous message was
0250efd6
VZ
311 // repeated if ms_prevCounter > 0, does nothing otherwise; return the old
312 // value of ms_prevCounter
dbaa16de 313 unsigned LogLastRepeatIfNeeded();
f9837791 314
d6b9496a 315private:
dbaa16de
VZ
316 // implement of LogLastRepeatIfNeeded(): it assumes that the
317 // caller had already locked ms_prevCS
318 unsigned LogLastRepeatIfNeededUnlocked();
319
d6b9496a
VZ
320 // static variables
321 // ----------------
06db8ebd 322
2064113c
VZ
323 // if true, don't log the same message multiple times, only log it once
324 // with the number of times it was repeated
f9837791 325 static bool ms_bRepetCounting;
2064113c 326
204abcd4
PC
327#if wxUSE_THREADS
328 static wxCriticalSection ms_prevCS; // protects the ms_prev values below
329#endif
f9837791 330 static wxString ms_prevString; // previous message that was logged
2064113c 331 static unsigned ms_prevCounter; // how many times it was repeated
f9837791
VZ
332 static time_t ms_prevTimeStamp;// timestamp of the previous message
333 static wxLogLevel ms_prevLevel; // level of the previous message
334
d6b9496a 335 static wxLog *ms_pLogger; // currently active log sink
d1b20379 336 static bool ms_doLog; // false => all logging disabled
d6b9496a 337 static bool ms_bAutoCreate; // create new log targets on demand?
d1b20379 338 static bool ms_bVerbose; // false => ignore LogInfo messages
fe7b1156 339
edc73852
RD
340 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
341
2ed3265e
VZ
342 static size_t ms_suspendCount; // if positive, logs are not flushed
343
d2e1ef19
VZ
344 // format string for strftime(), if NULL, time stamping log messages is
345 // disabled
d993e05b 346 static wxString ms_timestamp;
d2e1ef19 347
f96233d5
VZ
348#if wxUSE_THREADS
349 static wxCriticalSection ms_traceCS; // protects ms_aTraceMasks
350#endif
d6b9496a
VZ
351 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
352 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
c801d85f
KB
353};
354
355// ----------------------------------------------------------------------------
356// "trivial" derivations of wxLog
357// ----------------------------------------------------------------------------
358
d3fc1755
VZ
359// log everything to a buffer
360class WXDLLIMPEXP_BASE wxLogBuffer : public wxLog
361{
362public:
363 wxLogBuffer() { }
364
365 // get the string contents with all messages logged
366 const wxString& GetBuffer() const { return m_str; }
367
368 // show the buffer contents to the user in the best possible way (this uses
369 // wxMessageOutputMessageBox) and clear it
370 virtual void Flush();
371
372protected:
5a20d2ce
VS
373 virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t);
374 virtual void DoLogString(const wxString& szString, time_t t);
d3fc1755 375
b99891b0
VZ
376 wxSUPPRESS_DOLOG_HIDE_WARNING()
377 wxSUPPRESS_DOLOGSTRING_HIDE_WARNING()
378
d3fc1755
VZ
379private:
380 wxString m_str;
381
382 DECLARE_NO_COPY_CLASS(wxLogBuffer)
383};
384
0f8218d7 385
c801d85f 386// log everything to a "FILE *", stderr by default
bddd7a8d 387class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
c801d85f
KB
388{
389public:
d6b9496a
VZ
390 // redirect log output to a FILE
391 wxLogStderr(FILE *fp = (FILE *) NULL);
c801d85f 392
03147cd0 393protected:
d6b9496a 394 // implement sink function
5a20d2ce 395 virtual void DoLogString(const wxString& szString, time_t t);
c801d85f 396
b99891b0
VZ
397 wxSUPPRESS_DOLOGSTRING_HIDE_WARNING()
398
d6b9496a 399 FILE *m_fp;
d3fc1755
VZ
400
401 DECLARE_NO_COPY_CLASS(wxLogStderr)
c801d85f
KB
402};
403
4bf78aae 404#if wxUSE_STD_IOSTREAM
03147cd0 405
c801d85f 406// log everything to an "ostream", cerr by default
bddd7a8d 407class WXDLLIMPEXP_BASE wxLogStream : public wxLog
c801d85f
KB
408{
409public:
d6b9496a 410 // redirect log output to an ostream
dd107c50 411 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
c801d85f
KB
412
413protected:
d6b9496a 414 // implement sink function
5a20d2ce 415 virtual void DoLogString(const wxString& szString, time_t t);
c801d85f 416
b99891b0
VZ
417 wxSUPPRESS_DOLOGSTRING_HIDE_WARNING()
418
d6b9496a 419 // using ptr here to avoid including <iostream.h> from this file
dd107c50 420 wxSTD ostream *m_ostr;
c801d85f 421};
03147cd0
VZ
422
423#endif // wxUSE_STD_IOSTREAM
c801d85f 424
03147cd0
VZ
425// ----------------------------------------------------------------------------
426// /dev/null log target: suppress logging until this object goes out of scope
427// ----------------------------------------------------------------------------
428
429// example of usage:
430/*
431 void Foo()
432 {
433 wxFile file;
434
435 // wxFile.Open() normally complains if file can't be opened, we don't
436 // want it
437 wxLogNull logNo;
438
439 if ( !file.Open("bar") )
440 ... process error ourselves ...
441
442 // ~wxLogNull called, old log sink restored
443 }
444 */
bddd7a8d 445class WXDLLIMPEXP_BASE wxLogNull
03147cd0
VZ
446{
447public:
d1b20379 448 wxLogNull() : m_flagOld(wxLog::EnableLogging(false)) { }
be52b341 449 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
03147cd0
VZ
450
451private:
452 bool m_flagOld; // the previous value of the wxLog::ms_doLog
453};
454
455// ----------------------------------------------------------------------------
456// chaining log target: installs itself as a log target and passes all
457// messages to the real log target given to it in the ctor but also forwards
458// them to the previously active one
459//
460// note that you don't have to call SetActiveTarget() with this class, it
461// does it itself in its ctor
462// ----------------------------------------------------------------------------
463
bddd7a8d 464class WXDLLIMPEXP_BASE wxLogChain : public wxLog
03147cd0
VZ
465{
466public:
467 wxLogChain(wxLog *logger);
8b30a4e4 468 virtual ~wxLogChain();
03147cd0
VZ
469
470 // change the new log target
471 void SetLog(wxLog *logger);
472
473 // this can be used to temporarily disable (and then reenable) passing
474 // messages to the old logger (by default we do pass them)
475 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
476
477 // are we passing the messages to the previous log target?
478 bool IsPassingMessages() const { return m_bPassMessages; }
479
480 // return the previous log target (may be NULL)
481 wxLog *GetOldLog() const { return m_logOld; }
482
483 // override base class version to flush the old logger as well
484 virtual void Flush();
485
47fe7ff3
JS
486 // call to avoid destroying the old log target
487 void DetachOldLog() { m_logOld = NULL; }
488
03147cd0
VZ
489protected:
490 // pass the chain to the old logger if needed
5a20d2ce 491 virtual void DoLog(wxLogLevel level, const wxString& szString, time_t t);
03147cd0 492
b99891b0
VZ
493 wxSUPPRESS_DOLOG_HIDE_WARNING()
494
03147cd0
VZ
495private:
496 // the current log target
497 wxLog *m_logNew;
498
499 // the previous log target
500 wxLog *m_logOld;
501
502 // do we pass the messages to the old logger?
503 bool m_bPassMessages;
22f3361e
VZ
504
505 DECLARE_NO_COPY_CLASS(wxLogChain)
03147cd0
VZ
506};
507
508// a chain log target which uses itself as the new logger
47fe7ff3
JS
509
510#define wxLogPassThrough wxLogInterposer
511
512class WXDLLIMPEXP_BASE wxLogInterposer : public wxLogChain
513{
514public:
515 wxLogInterposer();
516
517private:
518 DECLARE_NO_COPY_CLASS(wxLogInterposer)
519};
520
521// a temporary interposer which doesn't destroy the old log target
522// (calls DetachOldLog)
523
524class WXDLLIMPEXP_BASE wxLogInterposerTemp : public wxLogChain
03147cd0
VZ
525{
526public:
47fe7ff3 527 wxLogInterposerTemp();
fc7a2a60
VZ
528
529private:
47fe7ff3 530 DECLARE_NO_COPY_CLASS(wxLogInterposerTemp)
03147cd0
VZ
531};
532
8ca28fb7 533#if wxUSE_GUI
7e8c564c
VS
534 // include GUI log targets:
535 #include "wx/generic/logg.h"
e90c1d2a 536#endif // wxUSE_GUI
03f38c58 537
c801d85f
KB
538// ============================================================================
539// global functions
540// ============================================================================
541
542// ----------------------------------------------------------------------------
543// Log functions should be used by application instead of stdio, iostream &c
544// for log messages for easy redirection
545// ----------------------------------------------------------------------------
546
88ac883a
VZ
547// ----------------------------------------------------------------------------
548// get error code/error message from system in a portable way
549// ----------------------------------------------------------------------------
550
551// return the last system error code
bddd7a8d 552WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
c11d62a6 553
88ac883a 554// return the error message for given (or last if 0) error code
bddd7a8d 555WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
88ac883a 556
c11d62a6 557// ----------------------------------------------------------------------------
c801d85f 558// define wxLog<level>
c11d62a6 559// ----------------------------------------------------------------------------
c801d85f 560
2523e9b7 561#define DECLARE_LOG_FUNCTION(level) \
81727065 562 extern void WXDLLIMPEXP_BASE \
d1f6e2cf
VS
563 wxDoLog##level##Wchar(const wxChar *format, ...); \
564 extern void WXDLLIMPEXP_BASE \
565 wxDoLog##level##Utf8(const char *format, ...); \
2523e9b7 566 WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \
1528e0b8 567 1, (const wxFormatString&), \
d1f6e2cf 568 wxDoLog##level##Wchar, wxDoLog##level##Utf8) \
2523e9b7
VS
569 DECLARE_LOG_FUNCTION_WATCOM(level) \
570 extern void WXDLLIMPEXP_BASE wxVLog##level(const wxString& format, \
571 va_list argptr)
572
573#ifdef __WATCOMC__
574 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351;
575 // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to
576 // something too big for Borland C++ to handle
577 #define DECLARE_LOG_FUNCTION_WATCOM(level) \
59a14f69
VS
578 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
579 1, (const wxString&), \
580 (wxFormatString(f1))) \
581 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
582 1, (const wxCStrData&), \
583 (wxFormatString(f1))) \
584 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
d1f6e2cf 585 1, (const char*), \
59a14f69
VS
586 (wxFormatString(f1))) \
587 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
d1f6e2cf 588 1, (const wchar_t*), \
59a14f69 589 (wxFormatString(f1)))
2523e9b7
VS
590#else
591 #define DECLARE_LOG_FUNCTION_WATCOM(level)
592#endif
593
c9f78968 594
2523e9b7 595#define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
d1f6e2cf
VS
596 extern void expdecl wxDoLog##level##Wchar(argclass arg, \
597 const wxChar *format, ...); \
598 extern void expdecl wxDoLog##level##Utf8(argclass arg, \
599 const char *format, ...); \
2523e9b7 600 WX_DEFINE_VARARG_FUNC_VOID(wxLog##level, \
1528e0b8 601 2, (argclass, const wxFormatString&), \
d1f6e2cf 602 wxDoLog##level##Wchar, wxDoLog##level##Utf8) \
2523e9b7 603 DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \
c9f78968 604 extern void expdecl wxVLog##level(argclass arg, \
81727065 605 const wxString& format, \
2523e9b7
VS
606 va_list argptr)
607
608#ifdef __WATCOMC__
609 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351;
610 // can't use WX_WATCOM_ONLY_CODE here because the macro would expand to
611 // something too big for Borland C++ to handle
612 #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl) \
59a14f69
VS
613 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
614 2, (argclass, const wxString&), \
615 (f1, wxFormatString(f2))) \
616 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
617 2, (argclass, const wxCStrData&), \
618 (f1, wxFormatString(f2))) \
619 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
2523e9b7 620 2, (argclass, const char*), \
59a14f69
VS
621 (f1, wxFormatString(f2))) \
622 WX_VARARG_WATCOM_WORKAROUND(void, wxLog##level, \
2523e9b7 623 2, (argclass, const wchar_t*), \
59a14f69 624 (f1, wxFormatString(f2)))
2523e9b7
VS
625#else
626 #define DECLARE_LOG_FUNCTION2_EXP_WATCOM(level, argclass, arg, expdecl)
627#endif
628
c9f78968 629
88ac883a
VZ
630#else // !wxUSE_LOG
631
82e77a80
VS
632#ifdef __WATCOMC__
633 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
634 #define WX_WATCOM_ONLY_CODE( x ) x
635#else
636 #define WX_WATCOM_ONLY_CODE( x )
637#endif
638
99fda03a
VS
639#if defined(__WATCOMC__) || defined(__MINGW32__)
640 // Mingw has similar problem with wxLogSysError:
641 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x ) x
642#else
643 #define WX_WATCOM_OR_MINGW_ONLY_CODE( x )
644#endif
645
88ac883a 646// log functions do nothing at all
2523e9b7
VS
647#define DECLARE_LOG_FUNCTION(level) \
648 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxString&)) \
649 WX_WATCOM_ONLY_CODE( \
650 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const char*)) \
651 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wchar_t*)) \
652 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 1, (const wxCStrData&)) \
653 ) \
81727065 654 inline void wxVLog##level(const wxString& WXUNUSED(format), \
59a14f69 655 va_list WXUNUSED(argptr)) { } \
c9f78968 656
2523e9b7
VS
657#define DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, expdecl) \
658 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxString&)) \
99fda03a 659 WX_WATCOM_OR_MINGW_ONLY_CODE( \
2523e9b7
VS
660 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const char*)) \
661 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wchar_t*)) \
662 WX_DEFINE_VARARG_FUNC_NOP(wxLog##level, 2, (argclass, const wxCStrData&)) \
663 ) \
c9f78968 664 inline void wxVLog##level(argclass WXUNUSED(arg), \
59a14f69
VS
665 const wxString& WXUNUSED(format), \
666 va_list WXUNUSED(argptr)) {}
2523e9b7 667
e30285ab 668// Empty Class to fake wxLogNull
bddd7a8d 669class WXDLLIMPEXP_BASE wxLogNull
e30285ab
VZ
670{
671public:
886dd7d2 672 wxLogNull() { }
e30285ab
VZ
673};
674
675// Dummy macros to replace some functions.
676#define wxSysErrorCode() (unsigned long)0
677#define wxSysErrorMsg( X ) (const wxChar*)NULL
678
679// Fake symbolic trace masks... for those that are used frequently
fda7962d 680#define wxTRACE_OleCalls wxEmptyString // OLE interface calls
e30285ab 681
88ac883a 682#endif // wxUSE_LOG/!wxUSE_LOG
bdeb1f0d 683
33936026
WS
684#define DECLARE_LOG_FUNCTION2(level, argclass, arg) \
685 DECLARE_LOG_FUNCTION2_EXP(level, argclass, arg, WXDLLIMPEXP_BASE)
0b4f47a3 686
463c2e5b 687// VC6 produces a warning if we a macro expanding to nothing to
2523e9b7 688// DECLARE_LOG_FUNCTION2:
463c2e5b 689#if defined(__VISUALC__) && __VISUALC__ < 1300
2523e9b7 690 // "not enough actual parameters for macro 'DECLARE_LOG_FUNCTION2_EXP'"
463c2e5b
VS
691 #pragma warning(disable:4003)
692#endif
c801d85f 693
88ac883a 694// a generic function for all levels (level is passes as parameter)
33936026 695DECLARE_LOG_FUNCTION2(Generic, wxLogLevel, level);
c801d85f 696
88ac883a
VZ
697// one function per each level
698DECLARE_LOG_FUNCTION(FatalError);
699DECLARE_LOG_FUNCTION(Error);
700DECLARE_LOG_FUNCTION(Warning);
701DECLARE_LOG_FUNCTION(Message);
702DECLARE_LOG_FUNCTION(Info);
703DECLARE_LOG_FUNCTION(Verbose);
470b7da3 704
88ac883a
VZ
705// this function sends the log message to the status line of the top level
706// application frame, if any
2523e9b7 707DECLARE_LOG_FUNCTION(Status);
470b7da3 708
886dd7d2
VZ
709#if wxUSE_GUI
710 // this one is the same as previous except that it allows to explicitly
b5dbe15d 711 class WXDLLIMPEXP_FWD_CORE wxFrame;
886dd7d2 712 // specify the frame to which the output should go
2523e9b7 713 DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *, pFrame, WXDLLIMPEXP_CORE);
886dd7d2 714#endif // wxUSE_GUI
c801d85f 715
88ac883a
VZ
716// additional one: as wxLogError, but also logs last system call error code
717// and the corresponding error message if available
2523e9b7 718DECLARE_LOG_FUNCTION(SysError);
c801d85f 719
88ac883a
VZ
720// and another one which also takes the error code (for those broken APIs
721// that don't set the errno (like registry APIs in Win32))
2523e9b7
VS
722DECLARE_LOG_FUNCTION2(SysError, long, lErrCode);
723#ifdef __WATCOMC__
724// workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
725DECLARE_LOG_FUNCTION2(SysError, unsigned long, lErrCode);
726#endif
88ac883a
VZ
727
728// debug functions do nothing in release mode
bdeb1f0d 729#if wxUSE_LOG && wxUSE_LOG_DEBUG
d6b9496a
VZ
730 DECLARE_LOG_FUNCTION(Debug);
731
b103e4f3
VZ
732 // there is no more unconditional LogTrace: it is not different from
733 // LogDebug and it creates overload ambiguities
734 //DECLARE_LOG_FUNCTION(Trace);
d6b9496a 735
b103e4f3
VZ
736 // this version only logs the message if the mask had been added to the
737 // list of masks with AddTraceMask()
2523e9b7
VS
738 DECLARE_LOG_FUNCTION2(Trace, const wxString&, mask);
739#ifdef __WATCOMC__
740 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
741 DECLARE_LOG_FUNCTION2(Trace, const char*, mask);
742 DECLARE_LOG_FUNCTION2(Trace, const wchar_t*, mask);
743#endif
744
b103e4f3
VZ
745 // and this one does nothing if all of level bits are not set in
746 // wxLog::GetActive()->GetTraceMask() -- it's deprecated in favour of
d6b9496a 747 // string identifiers
2523e9b7
VS
748 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask, mask);
749#ifdef __WATCOMC__
750 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
751 DECLARE_LOG_FUNCTION2(Trace, int, mask);
752#endif
bdeb1f0d 753#else //!debug || !wxUSE_LOG
388a1f66
VZ
754 // these functions do nothing in release builds, but don't define them as
755 // nothing as it could result in different code structure in debug and
756 // release and this could result in trouble when these macros are used
757 // inside if/else
758 //
759 // note that making wxVLogDebug/Trace() themselves (empty inline) functions
760 // is a bad idea as some compilers are stupid enough to not inline even
761 // empty functions if their parameters are complicated enough, but by
762 // defining them as an empty inline function we ensure that even dumbest
763 // compilers optimise them away
764 inline void wxLogNop() { }
765
766 #define wxVLogDebug(fmt, valist) wxLogNop()
767 #define wxVLogTrace(mask, fmt, valist) wxLogNop()
ca766534
VS
768
769 #ifdef HAVE_VARIADIC_MACROS
388a1f66
VZ
770 // unlike the inline functions below, this completely removes the
771 // wxLogXXX calls from the object file:
772 #define wxLogDebug(fmt, ...) wxLogNop()
773 #define wxLogTrace(mask, fmt, ...) wxLogNop()
ca766534 774 #else // !HAVE_VARIADIC_MACROS
c9f78968 775 //inline void wxLogDebug(const wxString& fmt, ...) {}
44be939a 776 WX_DEFINE_VARARG_FUNC_NOP(wxLogDebug, 1, (const wxString&))
c9f78968
VS
777 //inline void wxLogTrace(wxTraceMask, const wxString& fmt, ...) {}
778 //inline void wxLogTrace(const wxString&, const wxString& fmt, ...) {}
44be939a
VS
779 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (wxTraceMask, const wxString&))
780 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wxString&, const wxString&))
781 #ifdef __WATCOMC__
782 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
783 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const char*, const char*))
784 WX_DEFINE_VARARG_FUNC_NOP(wxLogTrace, 2, (const wchar_t*, const wchar_t*))
785 #endif
ca766534 786 #endif // HAVE_VARIADIC_MACROS/!HAVE_VARIADIC_MACROS
88ac883a 787#endif // debug/!debug
c801d85f 788
463c2e5b
VS
789#if defined(__VISUALC__) && __VISUALC__ < 1300
790 #pragma warning(default:4003)
791#endif
792
c11d62a6
VZ
793// wxLogFatalError helper: show the (fatal) error to the user in a safe way,
794// i.e. without using wxMessageBox() for example because it could crash
bddd7a8d 795void WXDLLIMPEXP_BASE
886dd7d2 796wxSafeShowMessage(const wxString& title, const wxString& text);
c11d62a6 797
88ac883a
VZ
798// ----------------------------------------------------------------------------
799// debug only logging functions: use them with API name and error code
800// ----------------------------------------------------------------------------
c801d85f 801
e90babdf 802#ifdef __WXDEBUG__
42e69d6b
VZ
803 // make life easier for people using VC++ IDE: clicking on the message
804 // will take us immediately to the place of the failed API
805#ifdef __VISUALC__
4b7f2165
VZ
806 #define wxLogApiError(api, rc) \
807 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
0accd1cf 808 __FILE__, __LINE__, api, \
8b94d999 809 (long)rc, wxSysErrorMsg(rc))
42e69d6b 810#else // !VC++
4b7f2165 811 #define wxLogApiError(api, rc) \
18da7cf2
VZ
812 wxLogDebug(wxT("In file %s at line %d: '%s' failed with ") \
813 wxT("error 0x%08lx (%s)."), \
0accd1cf 814 __FILE__, __LINE__, api, \
8b94d999 815 (long)rc, wxSysErrorMsg(rc))
42e69d6b
VZ
816#endif // VC++/!VC++
817
818 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
819
c801d85f 820#else //!debug
388a1f66
VZ
821 #define wxLogApiError(api, err) wxLogNop()
822 #define wxLogLastError(api) wxLogNop()
c801d85f
KB
823#endif //debug/!debug
824
a619fa3f
DE
825// wxCocoa has additiional trace masks
826#if defined(__WXCOCOA__)
827#include "wx/cocoa/log.h"
828#endif
829
82e77a80
VS
830#ifdef WX_WATCOM_ONLY_CODE
831 #undef WX_WATCOM_ONLY_CODE
832#endif
833
34138703 834#endif // _WX_LOG_H_
04662def 835