lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Displaying messages during wx startup


From: Vadim Zeitlin
Subject: Re: [lmi] Displaying messages during wx startup
Date: Sat, 28 Mar 2009 00:56:02 +0100

On Fri, 27 Mar 2009 12:15:18 +0000 Greg Chicares <address@hidden> wrote:

GC> Consider the patch below, which attempts to display a warning message
GC> on the first line of the main function. Is there some way to make the
GC> wxLogWarning message get displayed, even before wx has been initialized?
GC> I suspect the answer is "no",

 I'm afraid currently it is although to be honest I don't have any
reasonable explanation as to why is it so -- wx surely could use
MessageBox() under Windows internally to ensure that the log messages are
always shown. I think it would be worthwhile to change wxLog to use
wxMessageOutputMessageBox instead of wxLogStderr by default in GUI MSW
programs, what do you think?

GC> which is okay by me as long as I can find a way to test whether it's
GC> not going to work--is there a way?

 There is but it's a rather roundabout one: wxLog target used by default
can be customized by overriding wxApp::CreateTraits() method to return a
custom wxAppTraits-derived object which, in turn, overrides its
CreateLogTarget() method. So if you define your own LMIAppTraits doing this
you could simply set a flag in your overridden CreateLogTarget() and then
just call the base class version. And then you could use this flag to check
if the real GUI log target was already created.

 A much simpler but somewhat uglier method might be to check if
dynamic_cast<wxLogGui *>(wxLog::GetActiveTarget()) is non-NULL.

 But neither of these hacks would be needed if wx just did the right thing
itself and always ensured that the message is shown. Do you see any reason
why it shouldn't do this?


GC> This reduced testcase is silly in itself, of course. What I'm really
GC> looking for is a way to make this function robust:
GC> 
GC> // from 'alert_wx.cpp':
GC> void warning_alert(std::string const& s)
GC> {
GC>     wxLogWarning(s.c_str());
GC>     wxLog::FlushActive();
GC> }

 This is completely OT but important enough to be mentioned IMO: I don't
know if there is some code elsewhere ensuring that 's' doesn't contain any
percent characters but even if there is it would still be much safer to use
wxLogWarning("%s", s.c_str()) here. Otherwise any occurrence of "%s" in the
string is almost certain to produce an immediate crash and any occurrence
of anything else (e.g. "%d" or "%g" or whatever) is even more pernicious as
it's going to corrupt the stack and will result in a crash in an unrelated
location.

 Regards,
VZ

reply via email to

[Prev in Thread] Current Thread [Next in Thread]