]>
git.saurik.com Git - wxWidgets.git/blob - interface/log.h
4e127526da29175af0c3806fde2197caa9f342be
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxLogWindow class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 This class represents a background log window: to be precise, it collects all
14 log messages in the log frame which it manages but also passes them on to the
15 log target which was active at the moment of its creation. This allows, for
16 example, to show all the log messages in a frame but still continue to process
17 them normally by showing the standard log dialog.
25 class wxLogWindow
: public wxLogInterposer
29 Creates the log frame window and starts collecting the messages in it.
32 The parent window for the log frame, may be @NULL
34 The title for the log frame
36 @true to show the frame initially (default), otherwise
37 Show() must be called later.
39 @true to process the log messages normally in addition to
40 logging them in the log frame (default), @false to only log them in the
43 wxLogWindow(wxFrame parent
, const wxChar title
, bool show
= true,
44 bool passToOld
= true);
47 Returns the associated log frame window. This may be used to position or resize
48 it but use Show() to show or hide it.
53 Called if the user closes the window interactively, will not be
54 called if it is destroyed for another reason (such as when program
56 Return @true from here to allow the frame to close, @false to
57 prevent this from happening.
61 virtual bool OnFrameClose(wxFrame frame
);
64 Called immediately after the log frame creation allowing for
65 any extra initializations.
67 virtual void OnFrameCreate(wxFrame frame
);
70 Called right before the log frame is going to be deleted: will
71 always be called unlike OnFrameClose().
73 virtual void OnFrameDelete(wxFrame frame
);
76 Shows or hides the frame.
78 void Show(bool show
= true);
83 @class wxLogInterposerTemp
86 A special version of wxLogChain which uses itself as the
87 new log target. It forwards log messages to the previously installed one in
89 processing them itself. Unlike wxLogInterposer, it doesn't
90 delete the old target which means it can be used to temporarily redirect log
93 As per wxLogInterposer, this class must be derived from to implement
95 and/or wxLog::DoLogString methods.
100 class wxLogInterposerTemp
: public wxLogChain
104 The default constructor installs this object as the current active log target.
113 This simple class allows to chain log sinks, that is to install a new sink but
114 keep passing log messages to the old one instead of replacing it completely as
115 wxLog::SetActiveTarget does.
117 It is especially useful when you want to divert the logs somewhere (for
118 example to a file or a log window) but also keep showing the error messages
119 using the standard dialogs as wxLogGui does by default.
124 wxLogChain *logChain = new wxLogChain(new wxLogStderr);
126 // all the log messages are sent to stderr and also processed as usually
129 // don't delete logChain directly as this would leave a dangling
130 // pointer as active log target, use SetActiveTarget() instead
131 delete wxLog::SetActiveTarget(...something else or @NULL...);
137 class wxLogChain
: public wxLog
141 Sets the specified @c logger (which may be @NULL) as the default log
142 target but the log messages are also passed to the previous log target if any.
144 wxLogChain(wxLog
* logger
);
147 Destroys the previous log target.
152 Detaches the old log target so it won't be destroyed when the wxLogChain object
158 Returns the pointer to the previously active log target (which may be @NULL).
163 Returns @true if the messages are passed to the previously active log
164 target (default) or @false if PassMessages()
167 bool IsPassingMessages();
170 By default, the log messages are passed to the previously active log target.
171 Calling this function with @false parameter disables this behaviour
172 (presumably temporarily, as you shouldn't use wxLogChain at all otherwise) and
173 it can be reenabled by calling it again with @a passMessages set to @true.
175 void PassMessages(bool passMessages
);
178 Sets another log target to use (may be @NULL). The log target specified
179 in the @ref ctor() constructor or in a previous call to
180 this function is deleted.
181 This doesn't change the old log target value (the one the messages are
182 forwarded to) which still remains the same as was active when wxLogChain
185 void SetLog(wxLog
* logger
);
193 This is the default log target for the GUI wxWidgets applications. It is passed
194 to wxLog::SetActiveTarget at the program
195 startup and is deleted by wxWidgets during the program shut down.
200 class wxLogGui
: public wxLog
214 This class can be used to redirect the log messages to a C++ stream.
216 Please note that this class is only available if wxWidgets was compiled with
217 the standard iostream library support (@c wxUSE_STD_IOSTREAM must be on).
223 wxLogStderr, wxStreamToTextRedirector
225 class wxLogStream
: public wxLog
229 Constructs a log target which sends all the log messages to the given
230 output stream. If it is @NULL, the messages are sent to @c cerr.
232 wxLogStream(std::ostream ostr
= NULL
);
240 This class can be used to redirect the log messages to a C file stream (not to
241 be confused with C++ streams). It is the default log target for the non-GUI
242 wxWidgets applications which send all the output to @c stderr.
250 class wxLogStderr
: public wxLog
254 Constructs a log target which sends all the log messages to the given
255 @c FILE. If it is @NULL, the messages are sent to @c stderr.
257 wxLogStderr(FILE fp
= NULL
);
265 wxLogBuffer is a very simple implementation of log sink which simply collects
266 all the logged messages in a string (except the debug messages which are output
267 in the usual way immediately as we're presumably not interested in collecting
268 them for later). The messages from different log function calls are separated
271 All the messages collected so far can be shown to the user (and the current
272 buffer cleared) by calling the overloaded wxLogBuffer::Flush
278 class wxLogBuffer
: public wxLog
282 Shows all the messages collected so far to the user (using a message box in the
283 GUI applications or by printing them out to the console in text mode) and
284 clears the internal buffer.
286 virtual void Flush();
289 Returns the current buffer contains. Messages from different log function calls
290 are separated with the new lines in the buffer.
291 The buffer can be cleared by Flush() which will
292 also show the current contents to the user.
294 const wxString
GetBuffer();
299 @class wxLogInterposer
302 A special version of wxLogChain which uses itself as the
303 new log target. It forwards log messages to the previously installed one in
305 processing them itself.
307 Unlike wxLogChain which is usually used directly as is,
308 this class must be derived from to implement wxLog::DoLog
309 and/or wxLog::DoLogString methods.
311 wxLogInterposer destroys the previous log target in its destructor. If you
312 don't want this to happen, use wxLogInterposerTemp instead.
317 class wxLogInterposer
: public wxLogChain
321 The default constructor installs this object as the current active log target.
330 Using these target all the log messages can be redirected to a text control.
331 The text control must have been created with @c wxTE_MULTILINE style by the
338 wxTextCtrl, wxStreamToTextRedirector
340 class wxLogTextCtrl
: public wxLog
344 Constructs a log target which sends all the log messages to the given text
345 control. The @a textctrl parameter cannot be @NULL.
347 wxLogTextCtrl(wxTextCtrl textctrl
);
355 wxLog class defines the interface for the @e log targets used by wxWidgets
356 logging functions as explained in the @ref overview_wxlogoverview "wxLog
358 The only situations when you need to directly use this class is when you want
359 to derive your own log target because the existing ones don't satisfy your
360 needs. Another case is if you wish to customize the behaviour of the standard
361 logging classes (all of which respect the wxLog settings): for example, set
362 which trace messages are logged and which are not or change (or even remove
363 completely) the timestamp on the messages.
365 Otherwise, it is completely hidden behind the @e wxLogXXX() functions and
366 you may not even know about its existence.
368 See @ref overview_wxlogoverview "log overview" for the descriptions of wxWidgets
375 wxLog::RemoveTraceMask, wxLog::GetTraceMasks
381 Add the @a mask to the list of allowed masks for
384 @see RemoveTraceMask(), GetTraceMasks()
386 static void AddTraceMask(const wxString
& mask
);
389 Removes all trace masks previously set with
392 @see RemoveTraceMask()
394 static void ClearTraceMasks();
397 The functions below allow some limited customization of wxLog behaviour
398 without writing a new log target class (which, aside of being a matter of
399 several minutes, allows you to do anything you want).
400 The verbose messages are the trace messages which are not disabled in the
401 release mode and are generated by wxLogVerbose. They
402 are not normally shown to the user because they present little interest, but
403 may be activated, for example, in order to help the user find some program
405 As for the (real) trace messages, their handling depends on the settings of
406 the (application global) @e trace mask. There are two ways to specify it:
407 either by using SetTraceMask() and
408 GetTraceMask() and using
409 wxLogTrace which takes an integer mask or by using
410 AddTraceMask() for string trace masks.
411 The difference between bit-wise and string trace masks is that a message using
412 integer trace mask will only be logged if all bits of the mask are set in the
413 current mask while a message using string mask will be logged simply if the
414 mask had been added before to the list of allowed ones.
417 will do something only if the current trace mask contains both
418 @c wxTraceRefCount and @c wxTraceOle, but
420 will log the message if it was preceded by
422 Using string masks is simpler and allows to easily add custom ones, so this is
423 the preferred way of working with trace messages. The integer trace mask is
424 kept for compatibility and for additional (but very rarely needed) flexibility
426 The standard trace masks are given in wxLogTrace
428 Finally, the @e wxLog::DoLog() function automatically prepends a time stamp
429 to all the messages. The format of the time stamp may be changed: it can be
430 any string with % specifications fully described in the documentation of the
431 standard @e strftime() function. For example, the default format is
432 "[%d/%b/%y %H:%M:%S] " which gives something like "[17/Sep/98 22:10:16] "
433 (without quotes) for the current date. Setting an empty string as the time
434 format disables timestamping of the messages completely.
435 @b NB: Timestamping is disabled for Visual C++ users in debug builds by
436 default because otherwise it would be impossible to directly go to the line
437 from which the log message was generated by simply clicking in the debugger
438 window on the corresponding error message. If you wish to enable it, please use
439 SetTimestamp() explicitly.
462 SetRepetitionCounting()
464 GetRepetitionCounting()
469 Disables time stamping of the log messages.
470 This function is new since wxWidgets version 2.9
472 void SetTimestamp(const wxString
& format
);
475 Called to process the message of the specified severity. @a msg is the text
476 of the message as specified in the call of @e wxLogXXX() function which
477 generated it and @a timestamp is the moment when the message was generated.
478 The base class version prepends the timestamp to the message, adds a prefix
479 corresponding to the log level and then calls
480 DoLogString() with the resulting string.
482 virtual void DoLog(wxLogLevel level
, const wxString
& msg
,
486 Called to log the specified string. The timestamp is already included in the
487 string but still passed to this function.
488 A simple implementation may just send the string to @c stdout or, better,
491 virtual void DoLogString(const wxString
& msg
, time_t timestamp
);
494 Instructs wxLog to not create new log targets on the fly if there is none
495 currently. (Almost) for internal use only: it is supposed to be called by the
496 application shutdown code.
497 Note that this function also calls
500 static void DontCreateOnDemand();
503 Shows all the messages currently in buffer and clears it. If the buffer
504 is already empty, nothing happens.
506 virtual void Flush();
509 Flushes the current log target if any, does nothing if there is none.
513 static void FlushActive();
516 Returns the pointer to the active log target (may be @NULL).
518 static wxLog
* GetActiveTarget();
521 Returns the current log level limit.
523 static wxLogLevel
GetLogLevel();
526 Returns whether the repetition counting mode is enabled.
528 static bool GetRepetitionCounting();
531 Returns the current timestamp format string.
533 static const wxString
GetTimestamp();
536 Returns the current trace mask, see Customization() section
539 static wxTraceMask
GetTraceMask();
542 Returns the currently allowed list of string trace masks.
546 static const wxArrayString
GetTraceMasks();
549 Returns whether the verbose mode is currently active.
551 static bool GetVerbose();
554 The functions in this section work with and manipulate the active log target.
555 The OnLog() is called by the @e wxLogXXX() functions
556 and invokes the DoLog() of the active log target if any.
557 Get/Set methods are used to install/query the current active target and,
558 finally, DontCreateOnDemand() disables the
559 automatic creation of a standard log target if none actually exists. It is
560 only useful when the application is terminating and shouldn't be used in other
561 situations because it may easily lead to a loss of messages.
577 Returns @true if the @a mask is one of allowed masks for
579 See also: AddTraceMask(),
582 static bool IsAllowedTraceMask(const wxString
& mask
);
585 There are two functions which must be implemented by any derived class to
586 actually process the log messages: DoLog() and
587 DoLogString(). The second function receives a string
588 which just has to be output in some way and the easiest way to write a new log
589 target is to override just this function in the derived class. If more control
590 over the output format is needed, then the first function must be overridden
591 which allows to construct custom messages depending on the log level or even
592 do completely different things depending on the message severity (for example,
593 throw away all messages except warnings and errors, show warnings on the
594 screen and forward the error messages to the user's (or programmer's) cell
595 phone - maybe depending on whether the timestamp tells us if it is day or
596 night in the current time zone).
597 There also functions to support message buffering. Why are they needed?
598 Some of wxLog implementations, most notably the standard wxLogGui class,
599 buffer the messages (for example, to avoid showing the user a zillion of modal
600 message boxes one after another -- which would be really annoying).
601 Flush() shows them all and clears the buffer contents.
602 This function doesn't do anything if the buffer is already empty.
610 Forwards the message at specified level to the @e DoLog() function of the
611 active log target if there is any, does nothing otherwise.
613 static void OnLog(wxLogLevel level
, const wxString
& message
);
616 Remove the @a mask from the list of allowed masks for
618 See also: AddTraceMask()
620 static void RemoveTraceMask(const wxString
& mask
);
623 Resumes logging previously suspended by a call to
624 Suspend(). All messages logged in the meanwhile will be
627 static void Resume();
630 Sets the specified log target as the active one. Returns the pointer to the
631 previous active log target (may be @NULL). To suppress logging use a new
632 instance of wxLogNull not @NULL. If the active log target is set to @NULL a
633 new default log target will be created when logging occurs.
635 static wxLog
* SetActiveTarget(wxLog
* logtarget
);
638 Specifies that log messages with level logLevel should be ignored
639 and not sent to the active log target.
641 static void SetLogLevel(wxLogLevel logLevel
);
644 Enables logging mode in which a log message is logged once, and in case exactly
645 the same message successively repeats one or more times, only the number of
646 repetitions is logged.
648 static void SetRepetitionCounting(bool repetCounting
= true);
651 Sets the timestamp format prepended by the default log targets to all
652 messages. The string may contain any normal characters as well as %
653 prefixed format specificators, see @e strftime() manual for details.
654 Passing an empty string to this function disables message time stamping.
656 static void SetTimestamp(const wxString
& format
);
659 Sets the trace mask, see Customization()
662 static void SetTraceMask(wxTraceMask mask
);
665 Activates or deactivates verbose mode in which the verbose messages are
666 logged as the normal ones instead of being silently dropped.
668 static void SetVerbose(bool verbose
= true);
671 Suspends the logging until Resume() is called. Note that
672 the latter must be called the same number of times as the former to undo it,
673 i.e. if you call Suspend() twice you must call Resume() twice as well.
674 Note that suspending the logging means that the log sink won't be be flushed
675 periodically, it doesn't have any effect if the current log target does the
676 logging immediately without waiting for Flush() to be
677 called (the standard GUI log target only shows the log dialog when it is
678 flushed, so Suspend() works as expected with it).
680 @see Resume(), wxLogNull
682 static void Suspend();
690 This class allows to temporarily suspend logging. All calls to the log
691 functions during the life time of an object of this class are just ignored.
693 In particular, it can be used to suppress the log messages given by wxWidgets
694 itself but it should be noted that it is rarely the best way to cope with this
695 problem as @b all log messages are suppressed, even if they indicate a
696 completely different error than the one the programmer wanted to suppress.
698 For instance, the example of the overview:
703 // wxFile.Open() normally complains if file can't be opened, we don't want it
706 if ( !file.Open("bar") )
707 ... process error ourselves ...
708 } // ~wxLogNull called, old log sink restored
710 wxLogMessage("..."); // ok
713 would be better written as:
718 // don't try to open file if it doesn't exist, we are prepared to deal with
719 // this ourselves - but all other errors are not expected
720 if ( wxFile::Exists("bar") )
722 // gives an error message if the file couldn't be opened
735 class wxLogNull
: public wxLog
749 // ============================================================================
750 // Global functions/macros
751 // ============================================================================
754 This function shows a message to the user in a safe way and should be safe to
755 call even before the application has been initialized or if it is currently in
756 some other strange state (for example, about to crash). Under Windows this
757 function shows a message box using a native dialog instead of
758 wxMessageBox (which might be unsafe to call), elsewhere
759 it simply prints the message to the standard output using the title as prefix.
762 The title of the message box shown to the user or the prefix
763 of the message string
765 The text to show to the user
769 void wxSafeShowMessage(const wxString
& title
,
770 const wxString
& text
);