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