Howto use wxLogChain?

245 views
Skip to first unread message

VEGH, Janos

unread,
Jun 21, 2006, 5:32:35 PM6/21/06
to wx-u...@lists.wxwidgets.org
Hi,

is there an example on how to use wxLogChain?
I cannot figure out from the docs.

This is waht I tried:

filebuf fb;
fb.open ("MyOutput.log",ios::out);
ostream s1(&fb);
wxLogStream LS(&s1);
frame->m_logOld = wxLog::SetActiveTarget(&LS);

wxTextCtrl *Log = new wxTextCtrl(frame, -1,"somethg");
frame->m_cout_redirect = new wxStreamToTextRedirector (Log);
wxLogChain *LC = new wxLogChain(new wxLogTextCtrl(Log));
int i = 22;
s1 << "Hello " << i << endl; // This goes to the file

cout << "Hello, text!" << endl; // this goes to the text control

// But, they are not combined

Thanks

Janos

Vadim Zeitlin

unread,
Jun 21, 2006, 8:22:10 PM6/21/06
to wx-u...@lists.wxwidgets.org
On Wed, 21 Jun 2006 23:32:35 +0200 "VEGH, Janos" <ve...@fastmail.fm> wrote:

VJ> is there an example on how to use wxLogChain?

Basically you need to just create it passing it a log target. But I don't
think it does what you think it does (provided I understand correctly what
you think, of course). wxLogChain sends the messages generated using wxLog
(e.g. wxLogError &c) to both the log target associated with it and the
previously set log target.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/


VEGH, Janos

unread,
Jun 22, 2006, 2:22:20 AM6/22/06
to wx-u...@lists.wxwidgets.org

On Thu, 22 Jun 2006 02:22:10 +0200, "Vadim Zeitlin"
<va...@wxwindows.org> said:
> But I
> don't
> think it does what you think it does (provided I understand correctly
> what
> you think, of course). wxLogChain sends the messages generated using
> wxLog
> (e.g. wxLogError &c) to both the log target associated with it and the
> previously set log target.
Well this is exactly what I wanted.
There are two goals:
1./ to display the log messages in a text control
2./ to write the same messages simultaneously to a log file

1a./ In addition, I also have some non-wx contribution, logging to cout,
which I would like to integrate with the wx loggging.

Is this possible? 1 and 1a is OK, but I cannot figure out how can I
put the messages appearing in the LogWindow, write in a log file, too

Regards

Janos

Vadim Zeitlin

unread,
Jun 22, 2006, 2:00:21 PM6/22/06
to wx-u...@lists.wxwidgets.org
On Thu, 22 Jun 2006 08:22:20 +0200 "VEGH, Janos" <ve...@users.sf.net> wrote:

VJ> Well this is exactly what I wanted.
VJ> There are two goals:
VJ> 1./ to display the log messages in a text control

First set the text ctrl as the active target using wxLog::SetActiveTarget().

VJ> 2./ to write the same messages simultaneously to a log file

Then create a wxLogChain with a wxLogStderr.

VEGH, Janos

unread,
Jun 22, 2006, 4:03:42 PM6/22/06
to wx-u...@lists.wxwidgets.org

On Thu, 22 Jun 2006 20:00:21 +0200, "Vadim Zeitlin"
<va...@wxwindows.org> said:
> VJ> 1./ to display the log messages in a text control
>
> First set the text ctrl as the active target using
> wxLog::SetActiveTarget().
>
> VJ> 2./ to write the same messages simultaneously to a log file
>
> Then create a wxLogChain with a wxLogStderr.
>
I use wxMSW2.6.3, MSVC 7.1
I have the following code fragment (in bool MyApp::OnInit()):

wxTextCtrl *Log = new wxTextCtrl(frame, -1,"Soome\nthing\n");
frame->m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(Log));
wxFFile ff("MyOutput.log","w");
wxLogChain *LC = new wxLogChain(new wxLogStderr(ff.fp()));
// frame->m_cout_redirect = new wxStreamToTextRedirector (Log);
ff.Write("\nDirect write\n");
wxLogMessage("This is an error!");
std::cout << "\n\nThis is from the console!\n" << endl;


After executing this, the file MyOutput.log contains

Direct write
21:25:58: This is an error!

the log text control

Soome
thing
21:25:58: This is an error!

interestingly enough, this is what I can can cut and paste,
but on the screeen it looks like

Soomething21:25:58: This is an error!

but I need to scroll to the beginning; initially

58: This is an error!

can be seen on the screen.
When I uncomment the line with wxStreamToTextRedirector, the content of
the text control will be

is from the console!

which can be scrolled to (with cut and paste)

Soome
thing
21:38:40: This is an error!


This is from the console!

this also appears without newlines, as

Soomething21:38:40: This is an error!This is from the console!

while the file content remained the same.


So, my problems are:
1./ the newline characters are somehow there, but not effective. What is
going on here?
2./ the redirected stream goes OK to the text control, but it is not
chained to the file. Shall it?
3./ The positioning of the wxTextCtrl seems strange to me

Thanks

Janos


Vadim Zeitlin

unread,
Jun 22, 2006, 5:35:47 PM6/22/06
to wx-u...@lists.wxwidgets.org
On Thu, 22 Jun 2006 22:03:42 +0200 "VEGH, Janos" <ve...@users.sf.net> wrote:

VJ> So, my problems are:
VJ> 1./ the newline characters are somehow there, but not effective. What is
VJ> going on here?

Don't know. This probably doesn't have anything to do with wxLogChain
though.

VJ> 2./ the redirected stream goes OK to the text control, but it is not
VJ> chained to the file. Shall it?

No. wxStreamToTextRedirector does what its name says: redirects stream
(cout) to a text control.

VJ> 3./ The positioning of the wxTextCtrl seems strange to me

Again, there is something weird here, but it doesn't seem to have anything
to do with wxLogChain so it would be better to debug it separately.

VEGH, Janos

unread,
Jun 23, 2006, 2:51:49 AM6/23/06
to wx-u...@lists.wxwidgets.org

On Thu, 22 Jun 2006 23:35:47 +0200, "Vadim Zeitlin"

> VJ> 1./ the newline characters are somehow there, but not effective. What is
> VJ> going on here?
>
> Don't know. This probably doesn't have anything to do with wxLogChain
> though.

I do not know. In other circumstances the text is handled by wxTextCtrl
in
the normal way. This special way of passing strings leads to an
erroneous display,
so maybe it is not independent from the way as the strings are passed.

> VJ> 2./ the redirected stream goes OK to the text control, but it is not
> VJ> chained to the file. Shall it?
>
> No. wxStreamToTextRedirector does what its name says: redirects stream
> (cout) to a text control.

so, the conclusion is: presently I can:
see the the wxLog messages in a text control mixed with "foreign" log
messages
I cannot:
write these messages, appearing in a wxLogWindow, to a file.
So you say, I presently cannot reach my goal,
i.e. to see wxLog messages and cout logging together, in a log window
AND
to save this output to a file.

I was also thinking on the solution that I derive a class from wxLog
and use it as the log target, and in its DoLogString I write directly
to both the text control and the file. Do you see something against
this?

> VJ> 3./ The positioning of the wxTextCtrl seems strange to me
>
> Again, there is something weird here, but it doesn't seem to have
> anything
> to do with wxLogChain so it would be better to debug it separately.
>

I basically agree, except that this bug manifests itself under these
conditions,
as far as I know, under other conditions wxTextCtrl works well.
BTW: the code fragment I sent should only be inserted in the OnInit() in
the minimal sample,
so it could be good starting point for debugging.

Regards

Janos

Reply all
Reply to author
Forward
0 new messages