]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/log.h
avoid a bug in Carbon headers
[wxWidgets.git] / include / wx / log.h
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/log.h
3// Purpose: Assorted wxLogXXX functions, and wxLog (sink for logs)
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_LOG_H_
13#define _WX_LOG_H_
14
15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "log.h"
17#endif
18
19#include "wx/defs.h"
20
21#if wxUSE_LOG
22
23#include "wx/string.h"
24#include "wx/arrstr.h"
25
26// ----------------------------------------------------------------------------
27// forward declarations
28// ----------------------------------------------------------------------------
29
30#if wxUSE_GUI
31 class WXDLLIMPEXP_CORE wxTextCtrl;
32 class WXDLLIMPEXP_CORE wxLogFrame;
33 class WXDLLIMPEXP_CORE wxFrame;
34#endif // wxUSE_GUI
35
36// ----------------------------------------------------------------------------
37// types
38// ----------------------------------------------------------------------------
39
40typedef unsigned long wxTraceMask;
41typedef unsigned long wxLogLevel;
42
43// ----------------------------------------------------------------------------
44// headers
45// ----------------------------------------------------------------------------
46
47#ifndef __WXWINCE__
48#include <time.h> // for time_t
49#endif
50
51#include "wx/dynarray.h"
52
53#ifndef wxUSE_LOG_DEBUG
54# ifdef __WXDEBUG__
55# define wxUSE_LOG_DEBUG 1
56# else // !__WXDEBUG__
57# define wxUSE_LOG_DEBUG 0
58# endif
59#endif
60
61// ----------------------------------------------------------------------------
62// constants
63// ----------------------------------------------------------------------------
64
65// different standard log levels (you may also define your own)
66enum
67{
68 wxLOG_FatalError, // program can't continue, abort immediately
69 wxLOG_Error, // a serious error, user must be informed about it
70 wxLOG_Warning, // user is normally informed about it but may be ignored
71 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
72 wxLOG_Status, // informational: might go to the status line of GUI app
73 wxLOG_Info, // informational message (a.k.a. 'Verbose')
74 wxLOG_Debug, // never shown to the user, disabled in release mode
75 wxLOG_Trace, // trace messages are also only enabled in debug mode
76 wxLOG_Progress, // used for progress indicator (not yet)
77 wxLOG_User = 100, // user defined levels start here
78 wxLOG_Max = 10000
79};
80
81// symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
82// discarded unless the string "foo" has been added to the list of allowed
83// ones with AddTraceMask()
84
85#define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
86#define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
87#define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
88#define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
89
90#ifdef __WXMSW__
91 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
92#endif
93
94// the trace masks have been superceded by symbolic trace constants, they're
95// for compatibility only andwill be removed soon - do NOT use them
96
97// meaning of different bits of the trace mask (which allows selectively
98// enable/disable some trace messages)
99#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
100#define wxTraceMessages 0x0002 // trace window messages/X callbacks
101#define wxTraceResAlloc 0x0004 // trace GDI resource allocation
102#define wxTraceRefCount 0x0008 // trace various ref counting operations
103
104#ifdef __WXMSW__
105 #define wxTraceOleCalls 0x0100 // OLE interface calls
106#endif
107
108#include "wx/iosfwrap.h"
109
110// ----------------------------------------------------------------------------
111// derive from this class to redirect (or suppress, or ...) log messages
112// normally, only a single instance of this class exists but it's not enforced
113// ----------------------------------------------------------------------------
114
115class WXDLLIMPEXP_BASE wxLog
116{
117public:
118 // ctor
119 wxLog();
120
121 // Internal buffer.
122 // Allow replacement of the fixed size static buffer with
123 // a user allocated one. Pass in NULL to restore the
124 // built in static buffer.
125 static wxChar *SetLogBuffer( wxChar *buf, size_t size = 0 );
126
127 // these functions allow to completely disable all log messages
128 // is logging disabled now?
129 static bool IsEnabled() { return ms_doLog; }
130 // change the flag state, return the previous one
131 static bool EnableLogging(bool doIt = TRUE)
132 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
133
134 // static sink function - see DoLog() for function to overload in the
135 // derived classes
136 static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
137 {
138 if ( IsEnabled() && ms_logLevel >= level ) {
139 wxLog *pLogger = GetActiveTarget();
140 if ( pLogger )
141 pLogger->DoLog(level, szString, t);
142 }
143 }
144
145 // message buffering
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)
149 virtual void Flush();
150
151 // flush the active target if any
152 static void FlushActive()
153 {
154 if ( !ms_suspendCount )
155 {
156 wxLog *log = GetActiveTarget();
157 if ( log )
158 log->Flush();
159 }
160 }
161
162 // only one sink is active at each moment
163 // get current log target, will call wxApp::CreateLogTarget() to
164 // create one if none exists
165 static wxLog *GetActiveTarget();
166 // change log target, pLogger may be NULL
167 static wxLog *SetActiveTarget(wxLog *pLogger);
168
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)
172 static void Suspend() { ms_suspendCount++; }
173 // must be called for each Suspend()!
174 static void Resume() { ms_suspendCount--; }
175
176 // functions controlling the default wxLog behaviour
177 // verbose mode is activated by standard command-line '-verbose'
178 // option
179 static void SetVerbose(bool bVerbose = TRUE) { ms_bVerbose = bVerbose; }
180
181 // Set log level. Log messages with level > logLevel will not be logged.
182 static void SetLogLevel(wxLogLevel logLevel) { ms_logLevel = logLevel; }
183
184 // should GetActiveTarget() try to create a new log object if the
185 // current is NULL?
186 static void DontCreateOnDemand();
187
188 // trace mask (see wxTraceXXX constants for details)
189 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
190 // add string trace mask
191 static void AddTraceMask(const wxString& str)
192 { ms_aTraceMasks.push_back(str); }
193 // add string trace mask
194 static void RemoveTraceMask(const wxString& str);
195 // remove all string trace masks
196 static void ClearTraceMasks();
197 // get string trace masks
198 static const wxArrayString &GetTraceMasks() { return ms_aTraceMasks; }
199
200 // sets the timestamp string: this is used as strftime() format string
201 // for the log targets which add time stamps to the messages - set it
202 // to NULL to disable time stamping completely.
203 static void SetTimestamp(const wxChar *ts) { ms_timestamp = ts; }
204
205
206 // accessors
207 // gets the verbose status
208 static bool GetVerbose() { return ms_bVerbose; }
209 // get trace mask
210 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
211 // is this trace mask in the list?
212 static bool IsAllowedTraceMask(const wxChar *mask);
213 // return the current loglevel limit
214 static wxLogLevel GetLogLevel() { return ms_logLevel; }
215
216 // get the current timestamp format string (may be NULL)
217 static const wxChar *GetTimestamp() { return ms_timestamp; }
218
219
220 // helpers
221 // put the time stamp into the string if ms_timestamp != NULL (don't
222 // change it otherwise)
223 static void TimeStamp(wxString *str);
224
225 // make dtor virtual for all derived classes
226 virtual ~wxLog() { }
227
228
229 // this method exists for backwards compatibility only, don't use
230 bool HasPendingMessages() const { return TRUE; }
231
232protected:
233 // the logging functions that can be overriden
234 // default DoLog() prepends the time stamp and a prefix corresponding
235 // to the message to szString and then passes it to DoLogString()
236 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
237 // default DoLogString does nothing but is not pure virtual because if
238 // you override DoLog() you might not need it at all
239 virtual void DoLogString(const wxChar *szString, time_t t);
240
241private:
242 // static variables
243 // ----------------
244
245 static wxLog *ms_pLogger; // currently active log sink
246 static bool ms_doLog; // FALSE => all logging disabled
247 static bool ms_bAutoCreate; // create new log targets on demand?
248 static bool ms_bVerbose; // FALSE => ignore LogInfo messages
249
250 static wxLogLevel ms_logLevel; // limit logging to levels <= ms_logLevel
251
252 static size_t ms_suspendCount; // if positive, logs are not flushed
253
254 // format string for strftime(), if NULL, time stamping log messages is
255 // disabled
256 static const wxChar *ms_timestamp;
257
258 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
259 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
260};
261
262// ----------------------------------------------------------------------------
263// "trivial" derivations of wxLog
264// ----------------------------------------------------------------------------
265
266// log everything to a "FILE *", stderr by default
267class WXDLLIMPEXP_BASE wxLogStderr : public wxLog
268{
269 DECLARE_NO_COPY_CLASS(wxLogStderr)
270
271public:
272 // redirect log output to a FILE
273 wxLogStderr(FILE *fp = (FILE *) NULL);
274
275protected:
276 // implement sink function
277 virtual void DoLogString(const wxChar *szString, time_t t);
278
279 FILE *m_fp;
280};
281
282#if wxUSE_STD_IOSTREAM
283
284// log everything to an "ostream", cerr by default
285class WXDLLIMPEXP_BASE wxLogStream : public wxLog
286{
287public:
288 // redirect log output to an ostream
289 wxLogStream(wxSTD ostream *ostr = (wxSTD ostream *) NULL);
290
291protected:
292 // implement sink function
293 virtual void DoLogString(const wxChar *szString, time_t t);
294
295 // using ptr here to avoid including <iostream.h> from this file
296 wxSTD ostream *m_ostr;
297};
298
299#endif // wxUSE_STD_IOSTREAM
300
301// ----------------------------------------------------------------------------
302// /dev/null log target: suppress logging until this object goes out of scope
303// ----------------------------------------------------------------------------
304
305// example of usage:
306/*
307 void Foo()
308 {
309 wxFile file;
310
311 // wxFile.Open() normally complains if file can't be opened, we don't
312 // want it
313 wxLogNull logNo;
314
315 if ( !file.Open("bar") )
316 ... process error ourselves ...
317
318 // ~wxLogNull called, old log sink restored
319 }
320 */
321class WXDLLIMPEXP_BASE wxLogNull
322{
323public:
324 wxLogNull() : m_flagOld(wxLog::EnableLogging(FALSE)) { }
325 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
326
327private:
328 bool m_flagOld; // the previous value of the wxLog::ms_doLog
329};
330
331// ----------------------------------------------------------------------------
332// chaining log target: installs itself as a log target and passes all
333// messages to the real log target given to it in the ctor but also forwards
334// them to the previously active one
335//
336// note that you don't have to call SetActiveTarget() with this class, it
337// does it itself in its ctor
338// ----------------------------------------------------------------------------
339
340class WXDLLIMPEXP_BASE wxLogChain : public wxLog
341{
342public:
343 wxLogChain(wxLog *logger);
344 virtual ~wxLogChain();
345
346 // change the new log target
347 void SetLog(wxLog *logger);
348
349 // this can be used to temporarily disable (and then reenable) passing
350 // messages to the old logger (by default we do pass them)
351 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
352
353 // are we passing the messages to the previous log target?
354 bool IsPassingMessages() const { return m_bPassMessages; }
355
356 // return the previous log target (may be NULL)
357 wxLog *GetOldLog() const { return m_logOld; }
358
359 // override base class version to flush the old logger as well
360 virtual void Flush();
361
362protected:
363 // pass the chain to the old logger if needed
364 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
365
366private:
367 // the current log target
368 wxLog *m_logNew;
369
370 // the previous log target
371 wxLog *m_logOld;
372
373 // do we pass the messages to the old logger?
374 bool m_bPassMessages;
375
376 DECLARE_NO_COPY_CLASS(wxLogChain)
377};
378
379// a chain log target which uses itself as the new logger
380class WXDLLIMPEXP_BASE wxLogPassThrough : public wxLogChain
381{
382public:
383 wxLogPassThrough();
384
385private:
386 DECLARE_NO_COPY_CLASS(wxLogPassThrough)
387};
388
389#if wxUSE_GUI
390 // include GUI log targets:
391 #include "wx/generic/logg.h"
392#endif // wxUSE_GUI
393
394// ============================================================================
395// global functions
396// ============================================================================
397
398// ----------------------------------------------------------------------------
399// Log functions should be used by application instead of stdio, iostream &c
400// for log messages for easy redirection
401// ----------------------------------------------------------------------------
402
403// ----------------------------------------------------------------------------
404// get error code/error message from system in a portable way
405// ----------------------------------------------------------------------------
406
407// return the last system error code
408WXDLLIMPEXP_BASE unsigned long wxSysErrorCode();
409
410// return the error message for given (or last if 0) error code
411WXDLLIMPEXP_BASE const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
412
413// ----------------------------------------------------------------------------
414// define wxLog<level>
415// ----------------------------------------------------------------------------
416
417#define DECLARE_LOG_FUNCTION(level) \
418extern void WXDLLIMPEXP_BASE wxVLog##level(const wxChar *szFormat, \
419 va_list argptr); \
420extern void WXDLLIMPEXP_BASE wxLog##level(const wxChar *szFormat, \
421 ...) ATTRIBUTE_PRINTF_1
422#define DECLARE_LOG_FUNCTION2_EXP(level, arg, expdecl) \
423extern void expdecl wxVLog##level(arg, const wxChar *szFormat, \
424 va_list argptr); \
425extern void expdecl wxLog##level(arg, const wxChar *szFormat, \
426 ...) ATTRIBUTE_PRINTF_2
427#define DECLARE_LOG_FUNCTION2(level, arg) \
428 DECLARE_LOG_FUNCTION2_EXP(level, arg, WXDLLIMPEXP_BASE)
429
430#else // !wxUSE_LOG
431
432// log functions do nothing at all
433#define DECLARE_LOG_FUNCTION(level) \
434inline void wxVLog##level(const wxChar *szFormat, \
435 va_list argptr) { } \
436inline void wxLog##level(const wxChar *szFormat, ...) { }
437#define DECLARE_LOG_FUNCTION2(level, arg) \
438inline void wxVLog##level(arg, const wxChar *szFormat, \
439 va_list argptr) {} \
440inline void wxLog##level(arg, const wxChar *szFormat, ...) { }
441
442// Empty Class to fake wxLogNull
443class WXDLLIMPEXP_BASE wxLogNull
444{
445public:
446 wxLogNull() { }
447};
448
449// Dummy macros to replace some functions.
450#define wxSysErrorCode() (unsigned long)0
451#define wxSysErrorMsg( X ) (const wxChar*)NULL
452
453// Fake symbolic trace masks... for those that are used frequently
454#define wxTRACE_OleCalls wxEmptyString // OLE interface calls
455
456#endif // wxUSE_LOG/!wxUSE_LOG
457
458// a generic function for all levels (level is passes as parameter)
459DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
460
461// one function per each level
462DECLARE_LOG_FUNCTION(FatalError);
463DECLARE_LOG_FUNCTION(Error);
464DECLARE_LOG_FUNCTION(Warning);
465DECLARE_LOG_FUNCTION(Message);
466DECLARE_LOG_FUNCTION(Info);
467DECLARE_LOG_FUNCTION(Verbose);
468
469// this function sends the log message to the status line of the top level
470// application frame, if any
471DECLARE_LOG_FUNCTION(Status);
472
473#if wxUSE_GUI
474 // this one is the same as previous except that it allows to explicitly
475 // specify the frame to which the output should go
476 DECLARE_LOG_FUNCTION2_EXP(Status, wxFrame *pFrame, WXDLLIMPEXP_CORE);
477#endif // wxUSE_GUI
478
479// additional one: as wxLogError, but also logs last system call error code
480// and the corresponding error message if available
481DECLARE_LOG_FUNCTION(SysError);
482
483// and another one which also takes the error code (for those broken APIs
484// that don't set the errno (like registry APIs in Win32))
485DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
486
487// debug functions do nothing in release mode
488#if wxUSE_LOG_DEBUG
489 DECLARE_LOG_FUNCTION(Debug);
490
491 // first kind of LogTrace is unconditional: it doesn't check the level,
492 DECLARE_LOG_FUNCTION(Trace);
493
494 // this second version will only log the message if the mask had been
495 // added to the list of masks with AddTraceMask()
496 DECLARE_LOG_FUNCTION2(Trace, const wxChar *mask);
497
498 // the last one does nothing if all of level bits are not set
499 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
500 // string identifiers
501 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
502#else //!debug
503 // these functions do nothing in release builds
504 inline void wxVLogDebug(const wxChar *, va_list) { }
505 inline void wxLogDebug(const wxChar *, ...) { }
506 inline void wxVLogTrace(const wxChar *, va_list) { }
507 inline void wxLogTrace(const wxChar *, ...) { }
508 inline void wxVLogTrace(wxTraceMask, const wxChar *, va_list) { }
509 inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { }
510 inline void wxVLogTrace(const wxChar *, const wxChar *, va_list) { }
511 inline void wxLogTrace(const wxChar *, const wxChar *, ...) { }
512#endif // debug/!debug
513
514// wxLogFatalError helper: show the (fatal) error to the user in a safe way,
515// i.e. without using wxMessageBox() for example because it could crash
516void WXDLLIMPEXP_BASE
517wxSafeShowMessage(const wxString& title, const wxString& text);
518
519// ----------------------------------------------------------------------------
520// debug only logging functions: use them with API name and error code
521// ----------------------------------------------------------------------------
522
523#ifdef __WXDEBUG__
524 // make life easier for people using VC++ IDE: clicking on the message
525 // will take us immediately to the place of the failed API
526#ifdef __VISUALC__
527 #define wxLogApiError(api, rc) \
528 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
529 __TFILE__, __LINE__, api, \
530 (long)rc, wxSysErrorMsg(rc))
531#else // !VC++
532 #define wxLogApiError(api, rc) \
533 wxLogDebug(wxT("In file %s at line %d: '%s' failed with " \
534 "error 0x%08lx (%s)."), \
535 __TFILE__, __LINE__, api, \
536 (long)rc, wxSysErrorMsg(rc))
537#endif // VC++/!VC++
538
539 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
540
541#else //!debug
542 inline void wxLogApiError(const wxChar *, long) { }
543 inline void wxLogLastError(const wxChar *) { }
544#endif //debug/!debug
545
546#endif // _WX_LOG_H_
547