]>
git.saurik.com Git - wxWidgets.git/blob - interface/log.h
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
35 The title for the log frame
38 @true to show the frame initially (default), otherwise
39 Show() must be called later.
42 @true to process the log messages normally in addition to
43 logging them in the log frame (default), @false to only log them in the
46 wxLogWindow(wxFrame parent
, const wxChar title
, bool show
= @
true,
47 bool passToOld
= @
true);
50 Returns the associated log frame window. This may be used to position or resize
51 it but use Show() to show or hide it.
56 Called if the user closes the window interactively, will not be
57 called if it is destroyed for another reason (such as when program
60 Return @true from here to allow the frame to close, @false to
61 prevent this from happening.
65 virtual bool OnFrameClose(wxFrame frame
);
68 Called immediately after the log frame creation allowing for
69 any extra initializations.
71 virtual void OnFrameCreate(wxFrame frame
);
74 Called right before the log frame is going to be deleted: will
75 always be called unlike OnFrameClose().
77 virtual void OnFrameDelete(wxFrame frame
);
80 Shows or hides the frame.
82 void Show(bool show
= @
true);
87 @class wxLogInterposerTemp
90 A special version of wxLogChain which uses itself as the
91 new log target. It forwards log messages to the previously installed one in
93 processing them itself. Unlike wxLogInterposer, it doesn't
94 delete the old target which means it can be used to temporarily redirect log
97 As per wxLogInterposer, this class must be derived from to implement
99 and/or wxLog::DoLogString methods.
104 class wxLogInterposerTemp
: public wxLogChain
108 The default constructor installs this object as the current active log target.
117 This simple class allows to chain log sinks, that is to install a new sink but
118 keep passing log messages to the old one instead of replacing it completely as
119 wxLog::SetActiveTarget does.
121 It is especially useful when you want to divert the logs somewhere (for
122 example to a file or a log window) but also keep showing the error messages
123 using the standard dialogs as wxLogGui does by default.
128 wxLogChain *logChain = new wxLogChain(new wxLogStderr);
130 // all the log messages are sent to stderr and also processed as usually
133 // don't delete logChain directly as this would leave a dangling
134 // pointer as active log target, use SetActiveTarget() instead
135 delete wxLog::SetActiveTarget(...something else or @NULL...);
141 class wxLogChain
: public wxLog
145 Sets the specified @c logger (which may be @NULL) as the default log
146 target but the log messages are also passed to the previous log target if any.
148 wxLogChain(wxLog
* logger
);
151 Destroys the previous log target.
156 Detaches the old log target so it won't be destroyed when the wxLogChain object
162 Returns the pointer to the previously active log target (which may be @NULL).
167 Returns @true if the messages are passed to the previously active log
168 target (default) or @false if PassMessages()
171 bool IsPassingMessages();
174 By default, the log messages are passed to the previously active log target.
175 Calling this function with @false parameter disables this behaviour
176 (presumably temporarily, as you shouldn't use wxLogChain at all otherwise) and
177 it can be reenabled by calling it again with @e passMessages set to @true.
179 void PassMessages(bool passMessages
);
182 Sets another log target to use (may be @NULL). The log target specified
183 in the @ref ctor() constructor or in a previous call to
184 this function is deleted.
186 This doesn't change the old log target value (the one the messages are
187 forwarded to) which still remains the same as was active when wxLogChain
190 void SetLog(wxLog
* logger
);
198 This is the default log target for the GUI wxWidgets applications. It is passed
199 to wxLog::SetActiveTarget at the program
200 startup and is deleted by wxWidgets during the program shut down.
205 class wxLogGui
: public wxLog
219 This class can be used to redirect the log messages to a C++ stream.
221 Please note that this class is only available if wxWidgets was compiled with
222 the standard iostream library support (@c wxUSE_STD_IOSTREAM must be on).
228 wxLogStderr, wxStreamToTextRedirector
230 class wxLogStream
: public wxLog
234 Constructs a log target which sends all the log messages to the given
235 output stream. If it is @NULL, the messages are sent to @c cerr.
237 wxLogStream(std::ostream ostr
= @NULL
);
245 This class can be used to redirect the log messages to a C file stream (not to
246 be confused with C++ streams). It is the default log target for the non-GUI
247 wxWidgets applications which send all the output to @c stderr.
255 class wxLogStderr
: public wxLog
259 Constructs a log target which sends all the log messages to the given
260 @c FILE. If it is @NULL, the messages are sent to @c stderr.
262 wxLogStderr(FILE fp
= @NULL
);
270 wxLogBuffer is a very simple implementation of log sink which simply collects
271 all the logged messages in a string (except the debug messages which are output
272 in the usual way immediately as we're presumably not interested in collecting
273 them for later). The messages from different log function calls are separated
276 All the messages collected so far can be shown to the user (and the current
277 buffer cleared) by calling the overloaded wxLogBuffer::Flush
283 class wxLogBuffer
: public wxLog
287 Shows all the messages collected so far to the user (using a message box in the
288 GUI applications or by printing them out to the console in text mode) and
289 clears the internal buffer.
291 virtual void Flush();
294 Returns the current buffer contains. Messages from different log function calls
295 are separated with the new lines in the buffer.
297 The buffer can be cleared by Flush() which will
298 also show the current contents to the user.
300 const wxString
GetBuffer();
305 @class wxLogInterposer
308 A special version of wxLogChain which uses itself as the
309 new log target. It forwards log messages to the previously installed one in
311 processing them itself.
313 Unlike wxLogChain which is usually used directly as is,
314 this class must be derived from to implement wxLog::DoLog
315 and/or wxLog::DoLogString methods.
317 wxLogInterposer destroys the previous log target in its destructor. If you
318 don't want this to happen, use wxLogInterposerTemp instead.
323 class wxLogInterposer
: public wxLogChain
327 The default constructor installs this object as the current active log target.
336 Using these target all the log messages can be redirected to a text control.
337 The text control must have been created with @c wxTE_MULTILINE style by the
344 wxTextCtrl, wxStreamToTextRedirector
346 class wxLogTextCtrl
: public wxLog
350 Constructs a log target which sends all the log messages to the given text
351 control. The @e textctrl parameter cannot be @NULL.
353 wxLogTextCtrl(wxTextCtrl textctrl
);
361 wxLog class defines the interface for the @e log targets used by wxWidgets
362 logging functions as explained in the @ref overview_wxlogoverview "wxLog
364 The only situations when you need to directly use this class is when you want
365 to derive your own log target because the existing ones don't satisfy your
366 needs. Another case is if you wish to customize the behaviour of the standard
367 logging classes (all of which respect the wxLog settings): for example, set
368 which trace messages are logged and which are not or change (or even remove
369 completely) the timestamp on the messages.
371 Otherwise, it is completely hidden behind the @e wxLogXXX() functions and
372 you may not even know about its existence.
374 See @ref overview_wxlogoverview "log overview" for the descriptions of wxWidgets
381 wxLog::RemoveTraceMask, wxLog::GetTraceMasks
387 Add the @e mask to the list of allowed masks for
390 @sa RemoveTraceMask(), GetTraceMasks()
392 static void AddTraceMask(const wxString
& mask
);
395 Removes all trace masks previously set with
398 @sa RemoveTraceMask()
400 static void ClearTraceMasks();
403 The functions below allow some limited customization of wxLog behaviour
404 without writing a new log target class (which, aside of being a matter of
405 several minutes, allows you to do anything you want).
407 The verbose messages are the trace messages which are not disabled in the
408 release mode and are generated by wxLogVerbose. They
409 are not normally shown to the user because they present little interest, but
410 may be activated, for example, in order to help the user find some program
413 As for the (real) trace messages, their handling depends on the settings of
414 the (application global) @e trace mask. There are two ways to specify it:
415 either by using SetTraceMask() and
416 GetTraceMask() and using
417 wxLogTrace which takes an integer mask or by using
418 AddTraceMask() for string trace masks.
420 The difference between bit-wise and string trace masks is that a message using
421 integer trace mask will only be logged if all bits of the mask are set in the
422 current mask while a message using string mask will be logged simply if the
423 mask had been added before to the list of allowed ones.
426 will do something only if the current trace mask contains both
427 @c wxTraceRefCount and @c wxTraceOle, but
428 will log the message if it was preceded by
429 Using string masks is simpler and allows to easily add custom ones, so this is
430 the preferred way of working with trace messages. The integer trace mask is
431 kept for compatibility and for additional (but very rarely needed) flexibility
434 The standard trace masks are given in wxLogTrace
437 Finally, the @e wxLog::DoLog() function automatically prepends a time stamp
438 to all the messages. The format of the time stamp may be changed: it can be
439 any string with % specifications fully described in the documentation of the
440 standard @e strftime() function. For example, the default format is
441 "[%d/%b/%y %H:%M:%S] " which gives something like "[17/Sep/98 22:10:16] "
442 (without quotes) for the current date. Setting an empty string as the time
443 format disables timestamping of the messages completely.
445 @b NB: Timestamping is disabled for Visual C++ users in debug builds by
446 default because otherwise it would be impossible to directly go to the line
447 from which the log message was generated by simply clicking in the debugger
448 window on the corresponding error message. If you wish to enable it, please use
449 SetTimestamp() explicitly.
473 SetRepetitionCounting()
475 GetRepetitionCounting()
480 Disables time stamping of the log messages.
482 This function is new since wxWidgets version 2.9
484 void SetTimestamp(const wxString
& format
);
487 Called to process the message of the specified severity. @e msg is the text
488 of the message as specified in the call of @e wxLogXXX() function which
489 generated it and @e timestamp is the moment when the message was generated.
491 The base class version prepends the timestamp to the message, adds a prefix
492 corresponding to the log level and then calls
493 DoLogString() with the resulting string.
495 virtual void DoLog(wxLogLevel level
, const wxString
& msg
,
499 Called to log the specified string. The timestamp is already included in the
500 string but still passed to this function.
502 A simple implementation may just send the string to @c stdout or, better,
505 virtual void DoLogString(const wxString
& msg
, time_t timestamp
);
508 Instructs wxLog to not create new log targets on the fly if there is none
509 currently. (Almost) for internal use only: it is supposed to be called by the
510 application shutdown code.
512 Note that this function also calls
515 static void DontCreateOnDemand();
518 Shows all the messages currently in buffer and clears it. If the buffer
519 is already empty, nothing happens.
521 virtual void Flush();
524 Flushes the current log target if any, does nothing if there is none.
528 static void FlushActive();
531 Returns the pointer to the active log target (may be @NULL).
533 static wxLog
* GetActiveTarget();
536 Returns the current log level limit.
538 static wxLogLevel
GetLogLevel();
541 Returns whether the repetition counting mode is enabled.
543 static bool GetRepetitionCounting();
546 Returns the current timestamp format string.
548 static const wxString
GetTimestamp();
551 Returns the current trace mask, see Customization() section
554 static wxTraceMask
GetTraceMask();
557 Returns the currently allowed list of string trace masks.
561 static const wxArrayString
GetTraceMasks();
564 Returns whether the verbose mode is currently active.
566 static bool GetVerbose();
569 The functions in this section work with and manipulate the active log target.
570 The OnLog() is called by the @e wxLogXXX() functions
571 and invokes the DoLog() of the active log target if any.
572 Get/Set methods are used to install/query the current active target and,
573 finally, DontCreateOnDemand() disables the
574 automatic creation of a standard log target if none actually exists. It is
575 only useful when the application is terminating and shouldn't be used in other
576 situations because it may easily lead to a loss of messages.
593 Returns @true if the @e mask is one of allowed masks for
596 See also: AddTraceMask(),
599 static bool IsAllowedTraceMask(const wxString
& mask
);
602 There are two functions which must be implemented by any derived class to
603 actually process the log messages: DoLog() and
604 DoLogString(). The second function receives a string
605 which just has to be output in some way and the easiest way to write a new log
606 target is to override just this function in the derived class. If more control
607 over the output format is needed, then the first function must be overridden
608 which allows to construct custom messages depending on the log level or even
609 do completely different things depending on the message severity (for example,
610 throw away all messages except warnings and errors, show warnings on the
611 screen and forward the error messages to the user's (or programmer's) cell
612 phone - maybe depending on whether the timestamp tells us if it is day or
613 night in the current time zone).
615 There also functions to support message buffering. Why are they needed?
616 Some of wxLog implementations, most notably the standard wxLogGui class,
617 buffer the messages (for example, to avoid showing the user a zillion of modal
618 message boxes one after another -- which would be really annoying).
619 Flush() shows them all and clears the buffer contents.
620 This function doesn't do anything if the buffer is already empty.
629 Forwards the message at specified level to the @e DoLog() function of the
630 active log target if there is any, does nothing otherwise.
632 static void OnLog(wxLogLevel level
, const wxString
& message
);
635 Remove the @e mask from the list of allowed masks for
638 See also: AddTraceMask()
640 static void RemoveTraceMask(const wxString
& mask
);
643 Resumes logging previously suspended by a call to
644 Suspend(). All messages logged in the meanwhile will be
647 static void Resume();
650 Sets the specified log target as the active one. Returns the pointer to the
651 previous active log target (may be @NULL). To suppress logging use a new
652 instance of wxLogNull not @NULL. If the active log target is set to @NULL a
653 new default log target will be created when logging occurs.
655 static wxLog
* SetActiveTarget(wxLog
* logtarget
);
658 Specifies that log messages with level logLevel should be ignored
659 and not sent to the active log target.
661 static void SetLogLevel(wxLogLevel logLevel
);
664 Enables logging mode in which a log message is logged once, and in case exactly
665 the same message successively repeats one or more times, only the number of
666 repetitions is logged.
668 static void SetRepetitionCounting(bool repetCounting
= @
true);
671 Sets the timestamp format prepended by the default log targets to all
672 messages. The string may contain any normal characters as well as %
673 prefixed format specificators, see @e strftime() manual for details.
674 Passing an empty string to this function disables message time stamping.
676 static void SetTimestamp(const wxString
& format
);
679 Sets the trace mask, see Customization()
682 static void SetTraceMask(wxTraceMask mask
);
685 Activates or deactivates verbose mode in which the verbose messages are
686 logged as the normal ones instead of being silently dropped.
688 static void SetVerbose(bool verbose
= @
true);
691 Suspends the logging until Resume() is called. Note that
692 the latter must be called the same number of times as the former to undo it,
693 i.e. if you call Suspend() twice you must call Resume() twice as well.
695 Note that suspending the logging means that the log sink won't be be flushed
696 periodically, it doesn't have any effect if the current log target does the
697 logging immediately without waiting for Flush() to be
698 called (the standard GUI log target only shows the log dialog when it is
699 flushed, so Suspend() works as expected with it).
701 @sa Resume(), wxLogNull
703 static void Suspend();
711 This class allows to temporarily suspend logging. All calls to the log
712 functions during the life time of an object of this class are just ignored.
714 In particular, it can be used to suppress the log messages given by wxWidgets
715 itself but it should be noted that it is rarely the best way to cope with this
716 problem as @b all log messages are suppressed, even if they indicate a
717 completely different error than the one the programmer wanted to suppress.
719 For instance, the example of the overview:
724 // wxFile.Open() normally complains if file can't be opened, we don't want it
727 if ( !file.Open("bar") )
728 ... process error ourselves ...
729 } // ~wxLogNull called, old log sink restored
731 wxLogMessage("..."); // ok
734 would be better written as:
739 // don't try to open file if it doesn't exist, we are prepared to deal with
740 // this ourselves - but all other errors are not expected
741 if ( wxFile::Exists("bar") )
743 // gives an error message if the file couldn't be opened
756 class wxLogNull
: public wxLog
770 // ============================================================================
771 // Global functions/macros
772 // ============================================================================
775 This function shows a message to the user in a safe way and should be safe to
776 call even before the application has been initialized or if it is currently in
777 some other strange state (for example, about to crash). Under Windows this
778 function shows a message box using a native dialog instead of
779 wxMessageBox (which might be unsafe to call), elsewhere
780 it simply prints the message to the standard output using the title as prefix.
783 The title of the message box shown to the user or the prefix
784 of the message string
787 The text to show to the user
791 void wxSafeShowMessage(const wxString
& title
,
792 const wxString
& text
);