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