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