]> git.saurik.com Git - wxWidgets.git/blame - include/wx/log.h
added an extremely simple cell attr cache (yet it catches 80% of acccesses)
[wxWidgets.git] / include / wx / log.h
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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 license
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_LOG_H_
13#define _WX_LOG_H_
c801d85f 14
d6b9496a
VZ
15#ifdef __GNUG__
16 #pragma interface "log.h"
0d3820b3 17#endif
c801d85f 18
38830220 19#include "wx/setup.h"
d1af991f 20#include "wx/string.h"
38830220 21
546db2a8
VZ
22// ----------------------------------------------------------------------------
23// forward declarations
24// ----------------------------------------------------------------------------
25
26class WXDLLEXPORT wxTextCtrl;
27class WXDLLEXPORT wxLogFrame;
28class WXDLLEXPORT wxFrame;
29
30// ----------------------------------------------------------------------------
31// types
32// ----------------------------------------------------------------------------
33
34typedef unsigned long wxTraceMask;
35typedef unsigned long wxLogLevel;
36
37// ----------------------------------------------------------------------------
38// headers
39// ----------------------------------------------------------------------------
40
88ac883a
VZ
41#if wxUSE_LOG
42
c30aaf75
VZ
43#include <time.h> // for time_t
44
45#include "wx/dynarray.h"
d6b9496a 46
9ef3052c
VZ
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
51// different standard log levels (you may also define your own)
52enum
53{
d6b9496a
VZ
54 wxLOG_FatalError, // program can't continue, abort immediately
55 wxLOG_Error, // a serious error, user must be informed about it
56 wxLOG_Warning, // user is normally informed about it but may be ignored
57 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
58 wxLOG_Info, // informational message (a.k.a. 'Verbose')
59 wxLOG_Status, // informational: might go to the status line of GUI app
60 wxLOG_Debug, // never shown to the user, disabled in release mode
61 wxLOG_Trace, // trace messages are also only enabled in debug mode
62 wxLOG_Progress, // used for progress indicator (not yet)
63 wxLOG_User = 100 // user defined levels start here
9ef3052c
VZ
64};
65
d6b9496a
VZ
66// symbolic trace masks - wxLogTrace("foo", "some trace message...") will be
67// discarded unless the string "foo" has been added to the list of allowed
68// ones with AddTraceMask()
69
08298395
OK
70#define wxTRACE_MemAlloc wxT("memalloc") // trace memory allocation (new/delete)
71#define wxTRACE_Messages wxT("messages") // trace window messages/X callbacks
72#define wxTRACE_ResAlloc wxT("resalloc") // trace GDI resource allocation
73#define wxTRACE_RefCount wxT("refcount") // trace various ref counting operations
d6b9496a
VZ
74
75#ifdef __WXMSW__
08298395 76 #define wxTRACE_OleCalls wxT("ole") // OLE interface calls
d6b9496a
VZ
77#endif
78
79// the trace masks have been superceded by symbolic trace constants, they're
80// for compatibility only andwill be removed soon - do NOT use them
81
9ef3052c
VZ
82// meaning of different bits of the trace mask (which allows selectively
83// enable/disable some trace messages)
84#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
85#define wxTraceMessages 0x0002 // trace window messages/X callbacks
86#define wxTraceResAlloc 0x0004 // trace GDI resource allocation
87#define wxTraceRefCount 0x0008 // trace various ref counting operations
88
2049ba38 89#ifdef __WXMSW__
d6b9496a 90 #define wxTraceOleCalls 0x0100 // OLE interface calls
9ef3052c
VZ
91#endif
92
38830220 93#include "wx/ioswrap.h"
470b7da3 94
c801d85f
KB
95// ----------------------------------------------------------------------------
96// derive from this class to redirect (or suppress, or ...) log messages
97// normally, only a single instance of this class exists but it's not enforced
c801d85f 98// ----------------------------------------------------------------------------
d6b9496a 99
c801d85f
KB
100class WXDLLEXPORT wxLog
101{
102public:
d6b9496a
VZ
103 // ctor
104 wxLog();
105
106 // these functions allow to completely disable all log messages
107 // is logging disabled now?
108 static bool IsEnabled() { return ms_doLog; }
109 // change the flag state, return the previous one
110 static bool EnableLogging(bool doIt = TRUE)
111 { bool doLogOld = ms_doLog; ms_doLog = doIt; return doLogOld; }
112
113 // static sink function - see DoLog() for function to overload in the
114 // derived classes
9e3d3318 115 static void OnLog(wxLogLevel level, const wxChar *szString, time_t t)
d6b9496a
VZ
116 {
117 if ( IsEnabled() ) {
118 wxLog *pLogger = GetActiveTarget();
119 if ( pLogger )
120 pLogger->DoLog(level, szString, t);
121 }
803bf1c5 122 }
d6b9496a
VZ
123
124 // message buffering
125 // flush shows all messages if they're not logged immediately (FILE
126 // and iostream logs don't need it, but wxGuiLog does to avoid showing
127 // 17 modal dialogs one after another)
128 virtual void Flush();
129 // call to Flush() may be optimized: call it only if this function
130 // returns true (although Flush() also returns immediately if there is
131 // no messages, this functions is more efficient because inline)
132 bool HasPendingMessages() const { return m_bHasMessages; }
133
134 // only one sink is active at each moment
bbfa0322
VZ
135 // flush the active target if any
136 static void FlushActive()
137 {
138 wxLog *log = GetActiveTarget();
139 if ( log )
140 log->Flush();
141 }
d6b9496a
VZ
142 // get current log target, will call wxApp::CreateLogTarget() to
143 // create one if none exists
144 static wxLog *GetActiveTarget();
145 // change log target, pLogger may be NULL
146 static wxLog *SetActiveTarget(wxLog *pLogger);
147
148 // functions controlling the default wxLog behaviour
149 // verbose mode is activated by standard command-line '-verbose'
150 // option
151 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
152 // should GetActiveTarget() try to create a new log object if the
153 // current is NULL?
154 static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; }
155
156 // trace mask (see wxTraceXXX constants for details)
157 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
158 // add string trace mask
159 static void AddTraceMask(const wxString& str) { ms_aTraceMasks.Add(str); }
160 // add string trace mask
161 static void RemoveTraceMask(const wxString& str);
162
d2e1ef19
VZ
163 // sets the timestamp string: this is used as strftime() format string
164 // for the log targets which add time stamps to the messages - set it
165 // to NULL to disable time stamping completely.
166 static void SetTimestamp(const wxChar *ts) { ms_timestamp = ts; }
167
d6b9496a
VZ
168 // accessors
169 // gets the verbose status
170 bool GetVerbose() const { return m_bVerbose; }
171 // get trace mask
172 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
173 // is this trace mask in the list?
9e3d3318 174 static bool IsAllowedTraceMask(const wxChar *mask)
d6b9496a
VZ
175 { return ms_aTraceMasks.Index(mask) != wxNOT_FOUND; }
176
d2e1ef19
VZ
177 // get the current timestamp format string (may be NULL)
178 static const wxChar *GetTimestamp() { return ms_timestamp; }
179
180 // helpers
181 // put the time stamp into the string if ms_timestamp != NULL (don't
182 // change it otherwise)
183 static void TimeStamp(wxString *str);
184
d6b9496a
VZ
185 // make dtor virtual for all derived classes
186 virtual ~wxLog() { }
c801d85f
KB
187
188protected:
d6b9496a
VZ
189 bool m_bHasMessages; // any messages in the queue?
190 bool m_bVerbose; // FALSE => ignore LogInfo messages
c801d85f 191
d6b9496a
VZ
192 // the logging functions that can be overriden
193 // default DoLog() prepends the time stamp and a prefix corresponding
194 // to the message to szString and then passes it to DoLogString()
9e3d3318 195 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
d6b9496a
VZ
196 // default DoLogString does nothing but is not pure virtual because if
197 // you override DoLog() you might not need it at all
9e3d3318 198 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 199
d6b9496a
VZ
200private:
201 // static variables
202 // ----------------
06db8ebd 203
d6b9496a
VZ
204 static wxLog *ms_pLogger; // currently active log sink
205 static bool ms_doLog; // FALSE => all logging disabled
206 static bool ms_bAutoCreate; // create new log targets on demand?
fe7b1156 207
d2e1ef19
VZ
208 // format string for strftime(), if NULL, time stamping log messages is
209 // disabled
210 static const wxChar *ms_timestamp;
211
d6b9496a
VZ
212 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
213 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
c801d85f
KB
214};
215
216// ----------------------------------------------------------------------------
217// "trivial" derivations of wxLog
218// ----------------------------------------------------------------------------
219
220// log everything to a "FILE *", stderr by default
221class WXDLLEXPORT wxLogStderr : public wxLog
222{
223public:
d6b9496a
VZ
224 // redirect log output to a FILE
225 wxLogStderr(FILE *fp = (FILE *) NULL);
c801d85f
KB
226
227private:
d6b9496a 228 // implement sink function
9e3d3318 229 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 230
d6b9496a 231 FILE *m_fp;
c801d85f
KB
232};
233
4bf78aae 234#if wxUSE_STD_IOSTREAM
c801d85f
KB
235// log everything to an "ostream", cerr by default
236class WXDLLEXPORT wxLogStream : public wxLog
237{
238public:
d6b9496a
VZ
239 // redirect log output to an ostream
240 wxLogStream(ostream *ostr = (ostream *) NULL);
c801d85f
KB
241
242protected:
d6b9496a 243 // implement sink function
9e3d3318 244 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 245
d6b9496a
VZ
246 // using ptr here to avoid including <iostream.h> from this file
247 ostream *m_ostr;
c801d85f 248};
f5abe911 249#endif
c801d85f 250
dd85fc6b
VZ
251// the following log targets are only compiled in if the we're compiling the
252// GUI part (andnot just the base one) of the library, they're implemented in
253// src/generic/logg.cpp *and not src/common/log.cpp unlike all the rest)
254
e90c1d2a 255#if wxUSE_GUI
03f38c58 256
c801d85f 257// log everything to a text window (GUI only of course)
d2e1ef19 258class WXDLLEXPORT wxLogTextCtrl : public wxLog
c801d85f
KB
259{
260public:
d6b9496a 261 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
d2e1ef19
VZ
262
263private:
264 // implement sink function
265 virtual void DoLogString(const wxChar *szString, time_t t);
266
267 // the control we use
268 wxTextCtrl *m_pTextCtrl;
c801d85f 269};
c801d85f
KB
270
271// ----------------------------------------------------------------------------
272// GUI log target, the default one for wxWindows programs
273// ----------------------------------------------------------------------------
274class WXDLLEXPORT wxLogGui : public wxLog
275{
276public:
d6b9496a
VZ
277 // ctor
278 wxLogGui();
c801d85f 279
d6b9496a
VZ
280 // show all messages that were logged since the last Flush()
281 virtual void Flush();
c801d85f
KB
282
283protected:
9e3d3318 284 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
d6b9496a
VZ
285
286 // empty everything
287 void Clear();
c801d85f 288
f1df0927
VZ
289 wxArrayString m_aMessages; // the log message texts
290 wxArrayInt m_aSeverity; // one of wxLOG_XXX values
291 wxArrayLong m_aTimes; // the time of each message
d6b9496a
VZ
292 bool m_bErrors, // do we have any errors?
293 m_bWarnings; // any warnings?
c801d85f
KB
294};
295
9ef3052c
VZ
296// ----------------------------------------------------------------------------
297// (background) log window: this class forwards all log messages to the log
298// target which was active when it was instantiated, but also collects them
299// to the log window. This window has it's own menu which allows the user to
300// close it, clear the log contents or save it to the file.
301// ----------------------------------------------------------------------------
9ef3052c
VZ
302class WXDLLEXPORT wxLogWindow : public wxLog
303{
304public:
d6b9496a 305 wxLogWindow(wxFrame *pParent, // the parent frame (can be NULL)
9e3d3318 306 const wxChar *szTitle, // the title of the frame
d6b9496a
VZ
307 bool bShow = TRUE, // show window immediately?
308 bool bPassToOld = TRUE); // pass log messages to the old target?
309 ~wxLogWindow();
06db8ebd 310
d6b9496a 311 // window operations
06db8ebd 312 // show/hide the log window
d6b9496a 313 void Show(bool bShow = TRUE);
fe7b1156 314 // retrieve the pointer to the frame
d6b9496a 315 wxFrame *GetFrame() const;
9ef3052c 316
d6b9496a 317 // accessors
a3622daa 318 // the previous log target (may be NULL)
d6b9496a 319 wxLog *GetOldLog() const { return m_pOldLog; }
a3622daa 320 // are we passing the messages to the previous log target?
d6b9496a 321 bool IsPassingMessages() const { return m_bPassMessages; }
a3622daa 322
d6b9496a
VZ
323 // we can pass the messages to the previous log target (we're in this mode by
324 // default: we collect all messages in the window, but also let the default
325 // processing take place)
326 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
2283a22c 327
d6b9496a 328 // base class virtuals
fe7b1156 329 // we don't need it ourselves, but we pass it to the previous logger
d6b9496a 330 virtual void Flush();
fe7b1156 331
d6b9496a 332 // overridables
fe7b1156
VZ
333 // called immediately after the log frame creation allowing for
334 // any extra initializations
d6b9496a 335 virtual void OnFrameCreate(wxFrame *frame);
fe7b1156 336 // called right before the log frame is going to be deleted
d6b9496a 337 virtual void OnFrameDelete(wxFrame *frame);
fe7b1156 338
9ef3052c 339protected:
9e3d3318
OK
340 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
341 virtual void DoLogString(const wxChar *szString, time_t t);
06db8ebd 342
9ef3052c 343private:
d6b9496a
VZ
344 bool m_bPassMessages; // pass messages to m_pOldLog?
345 wxLog *m_pOldLog; // previous log target
346 wxLogFrame *m_pLogFrame; // the log frame
9ef3052c
VZ
347};
348
e90c1d2a 349#endif // wxUSE_GUI
03f38c58 350
c801d85f
KB
351// ----------------------------------------------------------------------------
352// /dev/null log target: suppress logging until this object goes out of scope
353// ----------------------------------------------------------------------------
354
355// example of usage:
356/*
d6b9496a
VZ
357 void Foo() {
358 wxFile file;
c801d85f 359
d6b9496a
VZ
360// wxFile.Open() normally complains if file can't be opened, we don't want it
361wxLogNull logNo;
362if ( !file.Open("bar") )
363... process error ourselves ...
c801d85f 364
d6b9496a 365// ~wxLogNull called, old log sink restored
c801d85f 366}
d6b9496a 367 */
c801d85f
KB
368class WXDLLEXPORT wxLogNull
369{
370public:
d6b9496a
VZ
371 wxLogNull() { m_flagOld = wxLog::EnableLogging(FALSE); }
372 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
c801d85f
KB
373
374private:
d6b9496a 375 bool m_flagOld; // the previous value of the wxLog::ms_doLog
c801d85f
KB
376};
377
378// ============================================================================
379// global functions
380// ============================================================================
381
382// ----------------------------------------------------------------------------
383// Log functions should be used by application instead of stdio, iostream &c
384// for log messages for easy redirection
385// ----------------------------------------------------------------------------
386
88ac883a
VZ
387// are we in 'verbose' mode?
388// (note that it's often handy to change this var manually from the
389// debugger, thus enabling/disabling verbose reporting for some
390// parts of the program only)
391WXDLLEXPORT_DATA(extern bool) g_bVerbose;
392
393// ----------------------------------------------------------------------------
394// get error code/error message from system in a portable way
395// ----------------------------------------------------------------------------
396
397// return the last system error code
398WXDLLEXPORT unsigned long wxSysErrorCode();
399// return the error message for given (or last if 0) error code
400WXDLLEXPORT const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
401
c801d85f
KB
402// define wxLog<level>
403// -------------------
404
c801d85f 405#define DECLARE_LOG_FUNCTION(level) \
9e3d3318 406extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
a1530845 407#define DECLARE_LOG_FUNCTION2(level, arg1) \
9e3d3318 408extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
a1530845 409
88ac883a
VZ
410#else // !wxUSE_LOG
411
412// log functions do nothing at all
413#define DECLARE_LOG_FUNCTION(level) \
546db2a8 414inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {}
88ac883a 415#define DECLARE_LOG_FUNCTION2(level, arg1) \
546db2a8 416inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {}
88ac883a
VZ
417
418#endif // wxUSE_LOG/!wxUSE_LOG
c801d85f 419
88ac883a
VZ
420// a generic function for all levels (level is passes as parameter)
421DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
c801d85f 422
88ac883a
VZ
423// one function per each level
424DECLARE_LOG_FUNCTION(FatalError);
425DECLARE_LOG_FUNCTION(Error);
426DECLARE_LOG_FUNCTION(Warning);
427DECLARE_LOG_FUNCTION(Message);
428DECLARE_LOG_FUNCTION(Info);
429DECLARE_LOG_FUNCTION(Verbose);
470b7da3 430
88ac883a
VZ
431// this function sends the log message to the status line of the top level
432// application frame, if any
433DECLARE_LOG_FUNCTION(Status);
470b7da3 434
88ac883a
VZ
435// this one is the same as previous except that it allows to explicitly
436// specify the frame to which the output should go
437DECLARE_LOG_FUNCTION2(Status, wxFrame *pFrame);
c801d85f 438
88ac883a
VZ
439// additional one: as wxLogError, but also logs last system call error code
440// and the corresponding error message if available
441DECLARE_LOG_FUNCTION(SysError);
c801d85f 442
88ac883a
VZ
443// and another one which also takes the error code (for those broken APIs
444// that don't set the errno (like registry APIs in Win32))
445DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
446
447// debug functions do nothing in release mode
b2aef89b 448#ifdef __WXDEBUG__
d6b9496a
VZ
449 DECLARE_LOG_FUNCTION(Debug);
450
451 // first king of LogTrace is uncoditional: it doesn't check the level,
452 DECLARE_LOG_FUNCTION(Trace);
453
454 // this second version will only log the message if the mask had been
455 // added to the list of masks with AddTraceMask()
456 DECLARE_LOG_FUNCTION2(Trace, const char *mask);
9ef3052c 457
d6b9496a
VZ
458 // the last one does nothing if all of level bits are not set
459 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
460 // string identifiers
461 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
9ef3052c 462#else //!debug
d6b9496a 463 // these functions do nothing in release builds
9e3d3318
OK
464 inline void wxLogDebug(const wxChar *, ...) { }
465 inline void wxLogTrace(const wxChar *, ...) { }
466 inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { }
467 inline void wxLogTrace(const wxChar *, const wxChar *, ...) { }
88ac883a 468#endif // debug/!debug
c801d85f 469
88ac883a
VZ
470// ----------------------------------------------------------------------------
471// debug only logging functions: use them with API name and error code
472// ----------------------------------------------------------------------------
c801d85f 473
9e3d3318 474#ifndef __TFILE__
e90c1d2a 475 #define __XFILE__(x) Tx)
88ac883a 476 #define __TFILE__ __XFILE__(__FILE__)
9e3d3318
OK
477#endif
478
e90babdf 479#ifdef __WXDEBUG__
42e69d6b
VZ
480 // make life easier for people using VC++ IDE: clicking on the message
481 // will take us immediately to the place of the failed API
482#ifdef __VISUALC__
4b7f2165
VZ
483 #define wxLogApiError(api, rc) \
484 wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
485 __TFILE__, __LINE__, _T(api), \
42e69d6b
VZ
486 rc, wxSysErrorMsg(rc))
487#else // !VC++
4b7f2165
VZ
488 #define wxLogApiError(api, rc) \
489 wxLogDebug(wxT("In file %s at line %d: '%s' failed with " \
490 "error 0x%08lx (%s)."), \
491 __TFILE__, __LINE__, _T(api), \
42e69d6b
VZ
492 rc, wxSysErrorMsg(rc))
493#endif // VC++/!VC++
494
495 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
496
c801d85f 497#else //!debug
9e3d3318
OK
498 inline void wxLogApiError(const wxChar *, long) { }
499 inline void wxLogLastError(const wxChar *) { }
c801d85f
KB
500#endif //debug/!debug
501
34138703 502#endif // _WX_LOG_H_