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