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