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