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