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