]> git.saurik.com Git - wxWidgets.git/blame - include/wx/log.h
code using ms_PaintDC changed a little (a couple of asserts added, the behaviour
[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
12#ifndef __LOGH__
13#define __LOGH__
14
15#ifdef __GNUG__
0d3820b3
JS
16#pragma interface "log.h"
17#endif
c801d85f 18
06db8ebd
VZ
19#include "wx/intl.h"
20
9ef3052c
VZ
21// ----------------------------------------------------------------------------
22// constants
23// ----------------------------------------------------------------------------
24
25// different standard log levels (you may also define your own)
26enum
27{
28 wxLOG_FatalError, // program can't continue, abort immediately
29 wxLOG_Error, // a serious error, user must be informed about it
30 wxLOG_Warning, // user is normally informed about it but may be ignored
31 wxLOG_Message, // normal message (i.e. normal output of a non GUI app)
32 wxLOG_Info, // informational message (a.k.a. 'Verbose')
33 wxLOG_Status, // informational: might go to the status line of GUI app
34 wxLOG_Debug, // never shown to the user, disabled in release mode
35 wxLOG_Trace, // trace messages are also only enabled in debug mode
36 wxLOG_Progress, // used for progress indicator (not yet)
37 wxLOG_User = 100 // user defined levels start here
38};
39
40// meaning of different bits of the trace mask (which allows selectively
41// enable/disable some trace messages)
42#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete)
43#define wxTraceMessages 0x0002 // trace window messages/X callbacks
44#define wxTraceResAlloc 0x0004 // trace GDI resource allocation
45#define wxTraceRefCount 0x0008 // trace various ref counting operations
46
2049ba38 47#ifdef __WXMSW__
9ef3052c
VZ
48 #define wxTraceOleCalls 0x0100 // OLE interface calls
49#endif
50
51typedef unsigned long wxTraceMask;
52typedef unsigned long wxLogLevel;
53
c801d85f
KB
54// ----------------------------------------------------------------------------
55// derive from this class to redirect (or suppress, or ...) log messages
56// normally, only a single instance of this class exists but it's not enforced
c801d85f
KB
57// ----------------------------------------------------------------------------
58class WXDLLEXPORT wxLog
59{
60public:
c801d85f
KB
61 // ctor
62 wxLog();
63
64 // sink function
9ef3052c 65 static void OnLog(wxLogLevel level, const char *szString)
c801d85f
KB
66 { if ( ms_pLogger != 0 ) ms_pLogger->DoLog(level, szString); }
67
68 // message buffering
69 // flush shows all messages if they're not logged immediately
70 // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
71 // showing 17 modal dialogs one after another)
72 virtual void Flush();
06db8ebd 73 // call to Flush() may be optimized: call it only if this function
9ef3052c
VZ
74 // returns true (although Flush() also returns immediately if there
75 // is no messages, this functions is more efficient because inline)
c801d85f
KB
76 bool HasPendingMessages() const { return m_bHasMessages; }
77
78 // only one sink is active at each moment
275bf4c1
VZ
79 // get current log target, will call wxApp::CreateLogTarget() to create one
80 // if
c801d85f
KB
81 static wxLog *GetActiveTarget();
82 // change log target, pLogger = NULL disables logging,
83 // returns the previous log target
84 static wxLog *SetActiveTarget(wxLog *pLogger);
85
86 // functions controlling the default wxLog behaviour
87 // verbose mode is activated by standard command-line '-verbose' option
9ef3052c 88 void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; }
c801d85f
KB
89 // sets the format for timestamp prepended by wxLog::DoLog(): it's
90 // passed to strftime() function, see it's documentation for details.
9ef3052c
VZ
91 // no time stamp at all if szTF is NULL or empty
92 // NB: the string is not copied, so it's lifetime must be long enough!
93 void SetTimeStampFormat(const char *szTF) { m_szTimeFormat = szTF; }
94 // trace mask (see wxTraceXXX constants for details)
46eaa422 95 static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; }
275bf4c1
VZ
96 // should GetActiveTarget() try to create a new log object if the current
97 // is NULL?
98 static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; }
c801d85f
KB
99
100 // accessors
101 // gets the verbose status
9ef3052c
VZ
102 bool GetVerbose() const { return m_bVerbose; }
103 // get current time format
104 const char *GetTimeStampFormat() const { return m_szTimeFormat; }
105 // get trace mask
46eaa422 106 static wxTraceMask GetTraceMask() { return ms_ulTraceMask; }
c801d85f
KB
107
108 // make dtor virtual for all derived classes
109 virtual ~wxLog() { }
110
111protected:
112 bool m_bHasMessages;
113
9ef3052c
VZ
114 bool m_bVerbose; // FALSE => ignore LogInfo messages
115 const char *m_szTimeFormat; // format for strftime()
c801d85f 116
c801d85f
KB
117 // the logging functions that can be overriden
118 // default DoLog() prepends the time stamp and a prefix corresponding
119 // to the message to szString and then passes it to DoLogString()
9ef3052c 120 virtual void DoLog(wxLogLevel level, const char *szString);
c801d85f
KB
121 // default DoLogString does nothing but is not pure virtual because if
122 // you override DoLog() you might not need it at all
123 virtual void DoLogString(const char *szString);
06db8ebd 124
9ef3052c
VZ
125private:
126 // static variables
127 // ----------------
275bf4c1
VZ
128 static wxLog *ms_pLogger; // currently active log sink
129 static bool ms_bAutoCreate; // automatically create new log targets?
46eaa422 130 static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour
c801d85f
KB
131};
132
133// ----------------------------------------------------------------------------
134// "trivial" derivations of wxLog
135// ----------------------------------------------------------------------------
136
137// log everything to a "FILE *", stderr by default
138class WXDLLEXPORT wxLogStderr : public wxLog
139{
140public:
141 // redirect log output to a FILE
142 wxLogStderr(FILE *fp = NULL);
143
144private:
145 // implement sink function
146 virtual void DoLogString(const char *szString);
147
148 FILE *m_fp;
149};
150
151// log everything to an "ostream", cerr by default
152class WXDLLEXPORT wxLogStream : public wxLog
153{
154public:
155 // redirect log output to an ostream
156 wxLogStream(ostream *ostr = NULL);
157
158protected:
159 // implement sink function
160 virtual void DoLogString(const char *szString);
161
162 // @@ using ptr here to avoid including <iostream.h> from this file
163 ostream *m_ostr;
164};
165
c801d85f 166// log everything to a text window (GUI only of course)
9ef3052c 167class wxTextCtrl;
c801d85f
KB
168class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
169{
170public:
171 // we just create an ostream from wxTextCtrl and use it in base class
172 wxLogTextCtrl(wxTextCtrl *pTextCtrl);
173 ~wxLogTextCtrl();
174};
c801d85f
KB
175
176// ----------------------------------------------------------------------------
177// GUI log target, the default one for wxWindows programs
178// ----------------------------------------------------------------------------
179class WXDLLEXPORT wxLogGui : public wxLog
180{
181public:
182 // ctor
183 wxLogGui();
184
185 // show all messages that were logged since the last Flush()
186 virtual void Flush();
187
188protected:
9ef3052c 189 virtual void DoLog(wxLogLevel level, const char *szString);
c801d85f
KB
190
191 wxArrayString m_aMessages;
192 bool m_bErrors;
193};
194
9ef3052c
VZ
195// ----------------------------------------------------------------------------
196// (background) log window: this class forwards all log messages to the log
197// target which was active when it was instantiated, but also collects them
198// to the log window. This window has it's own menu which allows the user to
199// close it, clear the log contents or save it to the file.
200// ----------------------------------------------------------------------------
201class wxLogFrame;
06db8ebd 202class wxFrame;
9ef3052c
VZ
203class WXDLLEXPORT wxLogWindow : public wxLog
204{
205public:
a3622daa
VZ
206 wxLogWindow(const char *szTitle, // the title of the frame
207 bool bShow = TRUE, // show window immediately?
208 bool bPassToOld = TRUE); // pass log messages to the old target?
9ef3052c 209 ~wxLogWindow();
06db8ebd
VZ
210
211 // window operations
212 // show/hide the log window
9ef3052c 213 void Show(bool bShow = TRUE);
06db8ebd
VZ
214 // get the frame pointer (you shouldn't close it!)
215 wxFrame *GetFrame() const;
9ef3052c 216
2283a22c 217 // accessors
a3622daa 218 // the previous log target (may be NULL)
2283a22c 219 wxLog *GetOldLog() const { return m_pOldLog; }
a3622daa
VZ
220 // are we passing the messages to the previous log target?
221 bool IsPassingMessages() const { return m_bPassMessages; }
222
223 // we can pass the messages to the previous log target (we're in this mode by
224 // default: we collect all messages in the window, but also let the default
225 // processing take place)
226 void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; }
2283a22c 227
9ef3052c
VZ
228protected:
229 virtual void DoLog(wxLogLevel level, const char *szString);
230 virtual void DoLogString(const char *szString);
06db8ebd 231
9ef3052c 232private:
a3622daa
VZ
233 bool m_bPassMessages; // pass messages to m_pOldLog?
234 wxLog *m_pOldLog; // previous log target
235 wxLogFrame *m_pLogFrame; // the log frame
9ef3052c
VZ
236};
237
c801d85f
KB
238// ----------------------------------------------------------------------------
239// /dev/null log target: suppress logging until this object goes out of scope
240// ----------------------------------------------------------------------------
241
242// example of usage:
243/*
244void Foo() {
245 wxFile file;
246
247 // wxFile.Open() normally complains if file can't be opened, we don't want it
248 wxLogNull logNo;
249 if ( !file.Open("bar") )
250 ... process error ourselves ...
251
252 // ~wxLogNull called, old log sink restored
253}
254*/
255class WXDLLEXPORT wxLogNull
256{
257public:
258 // ctor saves old log target, dtor restores it
259 wxLogNull() { m_pPrevLogger = wxLog::SetActiveTarget(NULL); }
260 ~wxLogNull() { (void)wxLog::SetActiveTarget(m_pPrevLogger); }
261
262private:
263 wxLog *m_pPrevLogger; // old log target
264};
265
266// ============================================================================
267// global functions
268// ============================================================================
269
270// ----------------------------------------------------------------------------
271// Log functions should be used by application instead of stdio, iostream &c
272// for log messages for easy redirection
273// ----------------------------------------------------------------------------
274
275// define wxLog<level>
276// -------------------
277
c801d85f 278#define DECLARE_LOG_FUNCTION(level) \
a1530845
VZ
279 extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...)
280#define DECLARE_LOG_FUNCTION2(level, arg1) \
281 extern void WXDLLEXPORT wxLog##level(arg1, const char *szFormat, ...)
282
283// a generic function for all levels (level is passes as parameter)
284DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level);
c801d85f
KB
285
286// one function per each level
287DECLARE_LOG_FUNCTION(FatalError);
288DECLARE_LOG_FUNCTION(Error);
289DECLARE_LOG_FUNCTION(Warning);
290DECLARE_LOG_FUNCTION(Message);
291DECLARE_LOG_FUNCTION(Info);
292DECLARE_LOG_FUNCTION(Status);
293DECLARE_LOG_FUNCTION(Verbose);
294
295// additional one: as wxLogError, but also logs last system call error code
296// and the corresponding error message if available
297DECLARE_LOG_FUNCTION(SysError);
298
299// and another one which also takes the error code (for those broken APIs
300// that don't set the errno (like registry APIs in Win32))
a1530845 301DECLARE_LOG_FUNCTION2(SysError, long lErrCode);
c801d85f 302
9ef3052c 303// debug functions do nothing in release mode
b2aef89b 304#ifdef __WXDEBUG__
a1530845 305 DECLARE_LOG_FUNCTION(Debug);
9ef3052c
VZ
306
307 // first king of LogTrace is uncoditional: it doesn't check the level,
308 // while the second one does nothing if all of level bits are not set
309 // in wxLog::GetActive()->GetTraceMask().
a1530845
VZ
310 DECLARE_LOG_FUNCTION(Trace);
311 DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask);
9ef3052c 312#else //!debug
27c9fe75
VZ
313 // these functions do nothing
314 inline void wxLogDebug(const char *, ...) { }
315 inline void wxLogTrace(const char *, ...) { }
316 inline void wxLogTrace(wxTraceMask, const char *, ...) { }
9ef3052c 317#endif
c801d85f 318
c801d85f
KB
319
320// are we in 'verbose' mode?
321// (note that it's often handy to change this var manually from the
322// debugger, thus enabling/disabling verbose reporting for some
323// parts of the program only)
324WXDLLEXPORT_DATA(extern bool) g_bVerbose;
325
326// fwd decl to avoid including iostream.h here
327class ostream;
328
329// ----------------------------------------------------------------------------
330// get error code/error message from system in a portable way
331// ----------------------------------------------------------------------------
332
333// return the last system error code
334unsigned long WXDLLEXPORT wxSysErrorCode();
335// return the error message for given (or last if 0) error code
336const char* WXDLLEXPORT wxSysErrorMsg(unsigned long nErrCode = 0);
337
338// ----------------------------------------------------------------------------
339// debug only logging functions: use them with API name and error code
340// ----------------------------------------------------------------------------
341
b2aef89b 342#ifdef __WXDEBUG__
c801d85f
KB
343 #define wxLogApiError(api, rc) \
344 wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
345 __FILE__, __LINE__, api, \
346 rc, wxSysErrorMsg(rc))
9ef3052c 347 #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
c801d85f 348#else //!debug
27c9fe75 349 inline void wxLogApiError(const char *, long) { }
db138a4c 350 inline void wxLogLastError(const char *) { }
c801d85f
KB
351#endif //debug/!debug
352
353#endif //__LOGH__