]>
Commit | Line | Data |
---|---|---|
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 | ||
26 | class WXDLLEXPORT wxTextCtrl; | |
27 | class WXDLLEXPORT wxLogFrame; | |
28 | class WXDLLEXPORT wxFrame; | |
29 | ||
30 | // ---------------------------------------------------------------------------- | |
31 | // types | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | typedef unsigned long wxTraceMask; | |
35 | typedef 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) | |
52 | enum | |
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 | ||
100 | class WXDLLEXPORT wxLog | |
101 | { | |
102 | public: | |
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 | // flush the active target if any | |
136 | static void FlushActive() | |
137 | { | |
138 | wxLog *log = GetActiveTarget(); | |
139 | if ( log ) | |
140 | log->Flush(); | |
141 | } | |
142 | // get current log target, will call wxApp::CreateLogTarget() to | |
143 | // create one if none exists | |
144 | static wxLog *GetActiveTarget(); | |
145 | // change log target, pLogger may be NULL | |
146 | static wxLog *SetActiveTarget(wxLog *pLogger); | |
147 | ||
148 | // functions controlling the default wxLog behaviour | |
149 | // verbose mode is activated by standard command-line '-verbose' | |
150 | // option | |
151 | void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; } | |
152 | // should GetActiveTarget() try to create a new log object if the | |
153 | // current is NULL? | |
154 | static void DontCreateOnDemand() { ms_bAutoCreate = FALSE; } | |
155 | ||
156 | // trace mask (see wxTraceXXX constants for details) | |
157 | static void SetTraceMask(wxTraceMask ulMask) { ms_ulTraceMask = ulMask; } | |
158 | // add string trace mask | |
159 | static void AddTraceMask(const wxString& str) { ms_aTraceMasks.Add(str); } | |
160 | // add string trace mask | |
161 | static void RemoveTraceMask(const wxString& str); | |
162 | ||
163 | // sets the timestamp string: this is used as strftime() format string | |
164 | // for the log targets which add time stamps to the messages - set it | |
165 | // to NULL to disable time stamping completely. | |
166 | static void SetTimestamp(const wxChar *ts) { ms_timestamp = ts; } | |
167 | ||
168 | // accessors | |
169 | // gets the verbose status | |
170 | bool GetVerbose() const { return m_bVerbose; } | |
171 | // get trace mask | |
172 | static wxTraceMask GetTraceMask() { return ms_ulTraceMask; } | |
173 | // is this trace mask in the list? | |
174 | static bool IsAllowedTraceMask(const wxChar *mask) | |
175 | { return ms_aTraceMasks.Index(mask) != wxNOT_FOUND; } | |
176 | ||
177 | // get the current timestamp format string (may be NULL) | |
178 | static const wxChar *GetTimestamp() { return ms_timestamp; } | |
179 | ||
180 | // helpers | |
181 | // put the time stamp into the string if ms_timestamp != NULL (don't | |
182 | // change it otherwise) | |
183 | static void TimeStamp(wxString *str); | |
184 | ||
185 | // make dtor virtual for all derived classes | |
186 | virtual ~wxLog() { } | |
187 | ||
188 | protected: | |
189 | bool m_bHasMessages; // any messages in the queue? | |
190 | bool m_bVerbose; // FALSE => ignore LogInfo messages | |
191 | ||
192 | // the logging functions that can be overriden | |
193 | // default DoLog() prepends the time stamp and a prefix corresponding | |
194 | // to the message to szString and then passes it to DoLogString() | |
195 | virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t); | |
196 | // default DoLogString does nothing but is not pure virtual because if | |
197 | // you override DoLog() you might not need it at all | |
198 | virtual void DoLogString(const wxChar *szString, time_t t); | |
199 | ||
200 | private: | |
201 | // static variables | |
202 | // ---------------- | |
203 | ||
204 | static wxLog *ms_pLogger; // currently active log sink | |
205 | static bool ms_doLog; // FALSE => all logging disabled | |
206 | static bool ms_bAutoCreate; // create new log targets on demand? | |
207 | ||
208 | // format string for strftime(), if NULL, time stamping log messages is | |
209 | // disabled | |
210 | static const wxChar *ms_timestamp; | |
211 | ||
212 | static wxTraceMask ms_ulTraceMask; // controls wxLogTrace behaviour | |
213 | static wxArrayString ms_aTraceMasks; // more powerful filter for wxLogTrace | |
214 | }; | |
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // "trivial" derivations of wxLog | |
218 | // ---------------------------------------------------------------------------- | |
219 | ||
220 | // log everything to a "FILE *", stderr by default | |
221 | class WXDLLEXPORT wxLogStderr : public wxLog | |
222 | { | |
223 | public: | |
224 | // redirect log output to a FILE | |
225 | wxLogStderr(FILE *fp = (FILE *) NULL); | |
226 | ||
227 | private: | |
228 | // implement sink function | |
229 | virtual void DoLogString(const wxChar *szString, time_t t); | |
230 | ||
231 | FILE *m_fp; | |
232 | }; | |
233 | ||
234 | #if wxUSE_STD_IOSTREAM | |
235 | // log everything to an "ostream", cerr by default | |
236 | class WXDLLEXPORT wxLogStream : public wxLog | |
237 | { | |
238 | public: | |
239 | // redirect log output to an ostream | |
240 | wxLogStream(ostream *ostr = (ostream *) NULL); | |
241 | ||
242 | protected: | |
243 | // implement sink function | |
244 | virtual void DoLogString(const wxChar *szString, time_t t); | |
245 | ||
246 | // using ptr here to avoid including <iostream.h> from this file | |
247 | ostream *m_ostr; | |
248 | }; | |
249 | #endif | |
250 | ||
251 | // the following log targets are only compiled in if the we're compiling the | |
252 | // GUI part (andnot just the base one) of the library, they're implemented in | |
253 | // src/generic/logg.cpp *and not src/common/log.cpp unlike all the rest) | |
254 | ||
255 | #if wxUSE_GUI | |
256 | ||
257 | // log everything to a text window (GUI only of course) | |
258 | class WXDLLEXPORT wxLogTextCtrl : public wxLog | |
259 | { | |
260 | public: | |
261 | wxLogTextCtrl(wxTextCtrl *pTextCtrl); | |
262 | ||
263 | private: | |
264 | // implement sink function | |
265 | virtual void DoLogString(const wxChar *szString, time_t t); | |
266 | ||
267 | // the control we use | |
268 | wxTextCtrl *m_pTextCtrl; | |
269 | }; | |
270 | ||
271 | // ---------------------------------------------------------------------------- | |
272 | // GUI log target, the default one for wxWindows programs | |
273 | // ---------------------------------------------------------------------------- | |
274 | class WXDLLEXPORT wxLogGui : public wxLog | |
275 | { | |
276 | public: | |
277 | // ctor | |
278 | wxLogGui(); | |
279 | ||
280 | // show all messages that were logged since the last Flush() | |
281 | virtual void Flush(); | |
282 | ||
283 | protected: | |
284 | virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t); | |
285 | ||
286 | // empty everything | |
287 | void Clear(); | |
288 | ||
289 | wxArrayString m_aMessages; | |
290 | wxArrayLong m_aTimes; | |
291 | bool m_bErrors, // do we have any errors? | |
292 | m_bWarnings; // any warnings? | |
293 | }; | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // (background) log window: this class forwards all log messages to the log | |
297 | // target which was active when it was instantiated, but also collects them | |
298 | // to the log window. This window has it's own menu which allows the user to | |
299 | // close it, clear the log contents or save it to the file. | |
300 | // ---------------------------------------------------------------------------- | |
301 | class WXDLLEXPORT wxLogWindow : public wxLog | |
302 | { | |
303 | public: | |
304 | wxLogWindow(wxFrame *pParent, // the parent frame (can be NULL) | |
305 | const wxChar *szTitle, // the title of the frame | |
306 | bool bShow = TRUE, // show window immediately? | |
307 | bool bPassToOld = TRUE); // pass log messages to the old target? | |
308 | ~wxLogWindow(); | |
309 | ||
310 | // window operations | |
311 | // show/hide the log window | |
312 | void Show(bool bShow = TRUE); | |
313 | // retrieve the pointer to the frame | |
314 | wxFrame *GetFrame() const; | |
315 | ||
316 | // accessors | |
317 | // the previous log target (may be NULL) | |
318 | wxLog *GetOldLog() const { return m_pOldLog; } | |
319 | // are we passing the messages to the previous log target? | |
320 | bool IsPassingMessages() const { return m_bPassMessages; } | |
321 | ||
322 | // we can pass the messages to the previous log target (we're in this mode by | |
323 | // default: we collect all messages in the window, but also let the default | |
324 | // processing take place) | |
325 | void PassMessages(bool bDoPass) { m_bPassMessages = bDoPass; } | |
326 | ||
327 | // base class virtuals | |
328 | // we don't need it ourselves, but we pass it to the previous logger | |
329 | virtual void Flush(); | |
330 | ||
331 | // overridables | |
332 | // called immediately after the log frame creation allowing for | |
333 | // any extra initializations | |
334 | virtual void OnFrameCreate(wxFrame *frame); | |
335 | // called right before the log frame is going to be deleted | |
336 | virtual void OnFrameDelete(wxFrame *frame); | |
337 | ||
338 | protected: | |
339 | virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t); | |
340 | virtual void DoLogString(const wxChar *szString, time_t t); | |
341 | ||
342 | private: | |
343 | bool m_bPassMessages; // pass messages to m_pOldLog? | |
344 | wxLog *m_pOldLog; // previous log target | |
345 | wxLogFrame *m_pLogFrame; // the log frame | |
346 | }; | |
347 | ||
348 | #endif // wxUSE_GUI | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // /dev/null log target: suppress logging until this object goes out of scope | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | // example of usage: | |
355 | /* | |
356 | void Foo() { | |
357 | wxFile file; | |
358 | ||
359 | // wxFile.Open() normally complains if file can't be opened, we don't want it | |
360 | wxLogNull logNo; | |
361 | if ( !file.Open("bar") ) | |
362 | ... process error ourselves ... | |
363 | ||
364 | // ~wxLogNull called, old log sink restored | |
365 | } | |
366 | */ | |
367 | class WXDLLEXPORT wxLogNull | |
368 | { | |
369 | public: | |
370 | wxLogNull() { m_flagOld = wxLog::EnableLogging(FALSE); } | |
371 | ~wxLogNull() { (void)wxLog::EnableLogging(m_flagOld); } | |
372 | ||
373 | private: | |
374 | bool m_flagOld; // the previous value of the wxLog::ms_doLog | |
375 | }; | |
376 | ||
377 | // ============================================================================ | |
378 | // global functions | |
379 | // ============================================================================ | |
380 | ||
381 | // ---------------------------------------------------------------------------- | |
382 | // Log functions should be used by application instead of stdio, iostream &c | |
383 | // for log messages for easy redirection | |
384 | // ---------------------------------------------------------------------------- | |
385 | ||
386 | // are we in 'verbose' mode? | |
387 | // (note that it's often handy to change this var manually from the | |
388 | // debugger, thus enabling/disabling verbose reporting for some | |
389 | // parts of the program only) | |
390 | WXDLLEXPORT_DATA(extern bool) g_bVerbose; | |
391 | ||
392 | // ---------------------------------------------------------------------------- | |
393 | // get error code/error message from system in a portable way | |
394 | // ---------------------------------------------------------------------------- | |
395 | ||
396 | // return the last system error code | |
397 | WXDLLEXPORT unsigned long wxSysErrorCode(); | |
398 | // return the error message for given (or last if 0) error code | |
399 | WXDLLEXPORT const wxChar* wxSysErrorMsg(unsigned long nErrCode = 0); | |
400 | ||
401 | // define wxLog<level> | |
402 | // ------------------- | |
403 | ||
404 | #define DECLARE_LOG_FUNCTION(level) \ | |
405 | extern void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) | |
406 | #define DECLARE_LOG_FUNCTION2(level, arg1) \ | |
407 | extern void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) | |
408 | ||
409 | #else // !wxUSE_LOG | |
410 | ||
411 | // log functions do nothing at all | |
412 | #define DECLARE_LOG_FUNCTION(level) \ | |
413 | inline void WXDLLEXPORT wxLog##level(const wxChar *szFormat, ...) {} | |
414 | #define DECLARE_LOG_FUNCTION2(level, arg1) \ | |
415 | inline void WXDLLEXPORT wxLog##level(arg1, const wxChar *szFormat, ...) {} | |
416 | ||
417 | #endif // wxUSE_LOG/!wxUSE_LOG | |
418 | ||
419 | // a generic function for all levels (level is passes as parameter) | |
420 | DECLARE_LOG_FUNCTION2(Generic, wxLogLevel level); | |
421 | ||
422 | // one function per each level | |
423 | DECLARE_LOG_FUNCTION(FatalError); | |
424 | DECLARE_LOG_FUNCTION(Error); | |
425 | DECLARE_LOG_FUNCTION(Warning); | |
426 | DECLARE_LOG_FUNCTION(Message); | |
427 | DECLARE_LOG_FUNCTION(Info); | |
428 | DECLARE_LOG_FUNCTION(Verbose); | |
429 | ||
430 | // this function sends the log message to the status line of the top level | |
431 | // application frame, if any | |
432 | DECLARE_LOG_FUNCTION(Status); | |
433 | ||
434 | // this one is the same as previous except that it allows to explicitly | |
435 | // specify the frame to which the output should go | |
436 | DECLARE_LOG_FUNCTION2(Status, wxFrame *pFrame); | |
437 | ||
438 | // additional one: as wxLogError, but also logs last system call error code | |
439 | // and the corresponding error message if available | |
440 | DECLARE_LOG_FUNCTION(SysError); | |
441 | ||
442 | // and another one which also takes the error code (for those broken APIs | |
443 | // that don't set the errno (like registry APIs in Win32)) | |
444 | DECLARE_LOG_FUNCTION2(SysError, long lErrCode); | |
445 | ||
446 | // debug functions do nothing in release mode | |
447 | #ifdef __WXDEBUG__ | |
448 | DECLARE_LOG_FUNCTION(Debug); | |
449 | ||
450 | // first king of LogTrace is uncoditional: it doesn't check the level, | |
451 | DECLARE_LOG_FUNCTION(Trace); | |
452 | ||
453 | // this second version will only log the message if the mask had been | |
454 | // added to the list of masks with AddTraceMask() | |
455 | DECLARE_LOG_FUNCTION2(Trace, const char *mask); | |
456 | ||
457 | // the last one does nothing if all of level bits are not set | |
458 | // in wxLog::GetActive()->GetTraceMask() - it's deprecated in favour of | |
459 | // string identifiers | |
460 | DECLARE_LOG_FUNCTION2(Trace, wxTraceMask mask); | |
461 | #else //!debug | |
462 | // these functions do nothing in release builds | |
463 | inline void wxLogDebug(const wxChar *, ...) { } | |
464 | inline void wxLogTrace(const wxChar *, ...) { } | |
465 | inline void wxLogTrace(wxTraceMask, const wxChar *, ...) { } | |
466 | inline void wxLogTrace(const wxChar *, const wxChar *, ...) { } | |
467 | #endif // debug/!debug | |
468 | ||
469 | // ---------------------------------------------------------------------------- | |
470 | // debug only logging functions: use them with API name and error code | |
471 | // ---------------------------------------------------------------------------- | |
472 | ||
473 | #ifndef __TFILE__ | |
474 | #define __XFILE__(x) Tx) | |
475 | #define __TFILE__ __XFILE__(__FILE__) | |
476 | #endif | |
477 | ||
478 | #ifdef __WXDEBUG__ | |
479 | // make life easier for people using VC++ IDE: clicking on the message | |
480 | // will take us immediately to the place of the failed API | |
481 | #ifdef __VISUALC__ | |
482 | #define wxLogApiError(api, rc) \ | |
483 | wxLogDebug(wxT("%s(%d): '%s' failed with error 0x%08lx (%s)."), \ | |
484 | __TFILE__, __LINE__, api, \ | |
485 | rc, wxSysErrorMsg(rc)) | |
486 | #else // !VC++ | |
487 | #define wxLogApiError(api, rc) \ | |
488 | wxLogDebug(wxT("In file %s at line %d: '%s' failed with " \ | |
489 | "error 0x%08lx (%s)."), \ | |
490 | __TFILE__, __LINE__, api, \ | |
491 | rc, wxSysErrorMsg(rc)) | |
492 | #endif // VC++/!VC++ | |
493 | ||
494 | #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode()) | |
495 | ||
496 | #else //!debug | |
497 | inline void wxLogApiError(const wxChar *, long) { } | |
498 | inline void wxLogLastError(const wxChar *) { } | |
499 | #endif //debug/!debug | |
500 | ||
501 | #endif // _WX_LOG_H_ |