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