]> git.saurik.com Git - wxWidgets.git/blame - include/wx/log.h
Updated testconf, fixed mistake in install.
[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
70#define wxTRACE_MemAlloc "memalloc" // trace memory allocation (new/delete)
71#define wxTRACE_Messages "messages" // trace window messages/X callbacks
72#define wxTRACE_ResAlloc "resalloc" // trace GDI resource allocation
73#define wxTRACE_RefCount "refcount" // trace various ref counting operations
74
75#ifdef __WXMSW__
76 #define wxTRACE_OleCalls "ole" // OLE interface calls
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
135 // get current log target, will call wxApp::CreateLogTarget() to
136 // create one if none exists
137 static wxLog *GetActiveTarget();
138 // change log target, pLogger may be NULL
139 static wxLog *SetActiveTarget(wxLog *pLogger);
140
141 // functions controlling the default wxLog behaviour
142 // verbose mode is activated by standard command-line '-verbose'
143 // option
144 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
145 // should GetActiveTarget() try to create a new log object if the
146 // current is NULL?
147 static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; }
148
149 // trace mask (see wxTraceXXX constants for details)
150 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
151 // add string trace mask
152 static void AddTraceMask(const wxString& str) { ms_aTraceMasks.Add(str); }
153 // add string trace mask
154 static void RemoveTraceMask(const wxString& str);
155
156 // accessors
157 // gets the verbose status
158 bool GetVerbose() const { return m_bVerbose; }
159 // get trace mask
160 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
161 // is this trace mask in the list?
9e3d3318 162 static bool IsAllowedTraceMask(const wxChar *mask)
d6b9496a
VZ
163 { return ms_aTraceMasks.Index(mask) != wxNOT_FOUND; }
164
165 // make dtor virtual for all derived classes
166 virtual ~wxLog() { }
c801d85f
KB
167
168protected:
d6b9496a
VZ
169 bool m_bHasMessages; // any messages in the queue?
170 bool m_bVerbose; // FALSE => ignore LogInfo messages
c801d85f 171
d6b9496a
VZ
172 // the logging functions that can be overriden
173 // default DoLog() prepends the time stamp and a prefix corresponding
174 // to the message to szString and then passes it to DoLogString()
9e3d3318 175 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
d6b9496a
VZ
176 // default DoLogString does nothing but is not pure virtual because if
177 // you override DoLog() you might not need it at all
9e3d3318 178 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 179
d6b9496a
VZ
180private:
181 // static variables
182 // ----------------
06db8ebd 183
d6b9496a
VZ
184 static wxLog *ms_pLogger; // currently active log sink
185 static bool ms_doLog; // FALSE => all logging disabled
186 static bool ms_bAutoCreate; // create new log targets on demand?
fe7b1156 187
d6b9496a
VZ
188 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
189 static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace
c801d85f
KB
190};
191
192// ----------------------------------------------------------------------------
193// "trivial" derivations of wxLog
194// ----------------------------------------------------------------------------
195
196// log everything to a "FILE *", stderr by default
197class WXDLLEXPORT wxLogStderr : public wxLog
198{
199public:
d6b9496a
VZ
200 // redirect log output to a FILE
201 wxLogStderr(FILE *fp = (FILE *) NULL);
c801d85f
KB
202
203private:
d6b9496a 204 // implement sink function
9e3d3318 205 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 206
d6b9496a 207 FILE *m_fp;
c801d85f
KB
208};
209
4bf78aae 210#if wxUSE_STD_IOSTREAM
c801d85f
KB
211// log everything to an "ostream", cerr by default
212class WXDLLEXPORT wxLogStream : public wxLog
213{
214public:
d6b9496a
VZ
215 // redirect log output to an ostream
216 wxLogStream(ostream *ostr = (ostream *) NULL);
c801d85f
KB
217
218protected:
d6b9496a 219 // implement sink function
9e3d3318 220 virtual void DoLogString(const wxChar *szString, time_t t);
c801d85f 221
d6b9496a
VZ
222 // using ptr here to avoid including <iostream.h> from this file
223 ostream *m_ostr;
c801d85f 224};
f5abe911 225#endif
c801d85f 226
03f38c58
VZ
227#ifndef wxUSE_NOGUI
228
4bf78aae 229#if wxUSE_STD_IOSTREAM
c801d85f
KB
230// log everything to a text window (GUI only of course)
231class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
232{
233public:
d6b9496a
VZ
234 // we just create an ostream from wxTextCtrl and use it in base class
235 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
236 ~wxLogTextCtrl();
c801d85f 237};
f5abe911 238#endif
c801d85f
KB
239
240// ----------------------------------------------------------------------------
241// GUI log target, the default one for wxWindows programs
242// ----------------------------------------------------------------------------
243class WXDLLEXPORT wxLogGui : public wxLog
244{
245public:
d6b9496a
VZ
246 // ctor
247 wxLogGui();
c801d85f 248
d6b9496a
VZ
249 // show all messages that were logged since the last Flush()
250 virtual void Flush();
c801d85f
KB
251
252protected:
9e3d3318 253 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
d6b9496a
VZ
254
255 // empty everything
256 void Clear();
c801d85f 257
d6b9496a
VZ
258 wxArrayString m_aMessages;
259 wxArrayLong m_aTimes;
260 bool m_bErrors, // do we have any errors?
261 m_bWarnings; // any warnings?
c801d85f
KB
262};
263
9ef3052c
VZ
264// ----------------------------------------------------------------------------
265// (background) log window: this class forwards all log messages to the log
266// target which was active when it was instantiated, but also collects them
267// to the log window. This window has it's own menu which allows the user to
268// close it, clear the log contents or save it to the file.
269// ----------------------------------------------------------------------------
9ef3052c
VZ
270class WXDLLEXPORT wxLogWindow : public wxLog
271{
272public:
d6b9496a 273 wxLogWindow(wxFrame *pParent, // the parent frame (can be NULL)
9e3d3318 274 const wxChar *szTitle, // the title of the frame
d6b9496a
VZ
275 bool bShow = TRUE, // show window immediately?
276 bool bPassToOld = TRUE); // pass log messages to the old target?
277 ~wxLogWindow();
06db8ebd 278
d6b9496a 279 // window operations
06db8ebd 280 // show/hide the log window
d6b9496a 281 void Show(bool bShow = TRUE);
fe7b1156 282 // retrieve the pointer to the frame
d6b9496a 283 wxFrame *GetFrame() const;
9ef3052c 284
d6b9496a 285 // accessors
a3622daa 286 // the previous log target (may be NULL)
d6b9496a 287 wxLog *GetOldLog() const { return m_pOldLog; }
a3622daa 288 // are we passing the messages to the previous log target?
d6b9496a 289 bool IsPassingMessages() const { return m_bPassMessages; }
a3622daa 290
d6b9496a
VZ
291 // we can pass the messages to the previous log target (we're in this mode by
292 // default: we collect all messages in the window, but also let the default
293 // processing take place)
294 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
2283a22c 295
d6b9496a 296 // base class virtuals
fe7b1156 297 // we don't need it ourselves, but we pass it to the previous logger
d6b9496a 298 virtual void Flush();
fe7b1156 299
d6b9496a 300 // overridables
fe7b1156
VZ
301 // called immediately after the log frame creation allowing for
302 // any extra initializations
d6b9496a 303 virtual void OnFrameCreate(wxFrame *frame);
fe7b1156 304 // called right before the log frame is going to be deleted
d6b9496a 305 virtual void OnFrameDelete(wxFrame *frame);
fe7b1156 306
9ef3052c 307protected:
9e3d3318
OK
308 virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t);
309 virtual void DoLogString(const wxChar *szString, time_t t);
06db8ebd 310
9ef3052c 311private:
d6b9496a
VZ
312 bool m_bPassMessages; // pass messages to m_pOldLog?
313 wxLog *m_pOldLog; // previous log target
314 wxLogFrame *m_pLogFrame; // the log frame
9ef3052c
VZ
315};
316
03f38c58
VZ
317#endif // wxUSE_NOGUI
318
c801d85f
KB
319// ----------------------------------------------------------------------------
320// /dev/null log target: suppress logging until this object goes out of scope
321// ----------------------------------------------------------------------------
322
323// example of usage:
324/*
d6b9496a
VZ
325 void Foo() {
326 wxFile file;
c801d85f 327
d6b9496a
VZ
328// wxFile.Open() normally complains if file can't be opened, we don't want it
329wxLogNull logNo;
330if ( !file.Open("bar") )
331... process error ourselves ...
c801d85f 332
d6b9496a 333// ~wxLogNull called, old log sink restored
c801d85f 334}
d6b9496a 335 */
c801d85f
KB
336class WXDLLEXPORT wxLogNull
337{
338public:
d6b9496a
VZ
339 wxLogNull() { m_flagOld = wxLog::EnableLogging(FALSE); }
340 ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); }
c801d85f
KB
341
342private:
d6b9496a 343 bool m_flagOld; // the previous value of the wxLog::ms_doLog
c801d85f
KB
344};
345
346// ============================================================================
347// global functions
348// ============================================================================
349
350// ----------------------------------------------------------------------------
351// Log functions should be used by application instead of stdio, iostream &c
352// for log messages for easy redirection
353// ----------------------------------------------------------------------------
354
88ac883a
VZ
355// are we in 'verbose' mode?
356// (note that it's often handy to change this var manually from the
357// debugger, thus enabling/disabling verbose reporting for some
358// parts of the program only)
359WXDLLEXPORT_DATA(extern bool) g_bVerbose;
360
361// ----------------------------------------------------------------------------
362// get error code/error message from system in a portable way
363// ----------------------------------------------------------------------------
364
365// return the last system error code
366WXDLLEXPORT unsigned long wxSysErrorCode();
367// return the error message for given (or last if 0) error code
368WXDLLEXPORT const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0);
369
c801d85f
KB
370// define wxLog<level>
371// -------------------
372
c801d85f 373#define DECLARE_LOG_FUNCTION(level) \
9e3d3318 374extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...)
a1530845 375#define DECLARE_LOG_FUNCTION2(level, arg1) \
9e3d3318 376extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...)
a1530845 377
88ac883a
VZ
378#else // !wxUSE_LOG
379
380// log functions do nothing at all
381#define DECLARE_LOG_FUNCTION(level) \
546db2a8 382inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {}
88ac883a 383#define DECLARE_LOG_FUNCTION2(level, arg1) \
546db2a8 384inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {}
88ac883a
VZ
385
386#endif // wxUSE_LOG/!wxUSE_LOG
c801d85f 387
88ac883a
VZ
388// a generic function for all levels (level is passes as parameter)
389DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
c801d85f 390
88ac883a
VZ
391// one function per each level
392DECLARE_LOG_FUNCTION(FatalError);
393DECLARE_LOG_FUNCTION(Error);
394DECLARE_LOG_FUNCTION(Warning);
395DECLARE_LOG_FUNCTION(Message);
396DECLARE_LOG_FUNCTION(Info);
397DECLARE_LOG_FUNCTION(Verbose);
470b7da3 398
88ac883a
VZ
399// this function sends the log message to the status line of the top level
400// application frame, if any
401DECLARE_LOG_FUNCTION(Status);
470b7da3 402
88ac883a
VZ
403// this one is the same as previous except that it allows to explicitly
404// specify the frame to which the output should go
405DECLARE_LOG_FUNCTION2(Status, wxFrame *pFrame);
c801d85f 406
88ac883a
VZ
407// additional one: as wxLogError, but also logs last system call error code
408// and the corresponding error message if available
409DECLARE_LOG_FUNCTION(SysError);
c801d85f 410
88ac883a
VZ
411// and another one which also takes the error code (for those broken APIs
412// that don't set the errno (like registry APIs in Win32))
413DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
414
415// debug functions do nothing in release mode
b2aef89b 416#ifdef __WXDEBUG__
d6b9496a
VZ
417 DECLARE_LOG_FUNCTION(Debug);
418
419 // first king of LogTrace is uncoditional: it doesn't check the level,
420 DECLARE_LOG_FUNCTION(Trace);
421
422 // this second version will only log the message if the mask had been
423 // added to the list of masks with AddTraceMask()
424 DECLARE_LOG_FUNCTION2(Trace, const char *mask);
9ef3052c 425
d6b9496a
VZ
426 // the last one does nothing if all of level bits are not set
427 // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of
428 // string identifiers
429 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
9ef3052c 430#else //!debug
d6b9496a 431 // these functions do nothing in release builds
9e3d3318
OK
432 inline void wxLogDebug(const wxChar *, ...) { }
433 inline void wxLogTrace(const wxChar *, ...) { }
434 inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { }
435 inline void wxLogTrace(const wxChar *, const wxChar *, ...) { }
88ac883a 436#endif // debug/!debug
c801d85f 437
88ac883a
VZ
438// ----------------------------------------------------------------------------
439// debug only logging functions: use them with API name and error code
440// ----------------------------------------------------------------------------
c801d85f 441
9e3d3318 442#ifndef __TFILE__
88ac883a
VZ
443 #define __XFILE__(x) _T(x)
444 #define __TFILE__ __XFILE__(__FILE__)
9e3d3318
OK
445#endif
446
e90babdf 447#ifdef __WXDEBUG__
42e69d6b
VZ
448 // make life easier for people using VC++ IDE: clicking on the message
449 // will take us immediately to the place of the failed API
450#ifdef __VISUALC__
451 #define wxLogApiError(api, rc) \
452 wxLogDebug(_T("%s(%d): '%s' failed with error 0x%08lx (%s)."), \
453 __TFILE__, __LINE__, api, \
454 rc, wxSysErrorMsg(rc))
455#else // !VC++
456 #define wxLogApiError(api, rc) \
457 wxLogDebug(_T("In file %s at line %d: '%s' failed with " \
458 "error 0x%08lx (%s)."), \
459 __TFILE__, __LINE__, api, \
460 rc, wxSysErrorMsg(rc))
461#endif // VC++/!VC++
462
463 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
464
c801d85f 465#else //!debug
9e3d3318
OK
466 inline void wxLogApiError(const wxChar *, long) { }
467 inline void wxLogLastError(const wxChar *) { }
c801d85f
KB
468#endif //debug/!debug
469
34138703 470#endif // _WX_LOG_H_