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