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