|
|
std::clog
Hello all,
is there a way to redirect the global std::clog (or cout/cerr) to an
(absolute) global ofstream and how to do it?
Greetings from Erlangen
Tobias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
tmaier
|
2/23/2005 9:31:55 PM |
|
tmaier@optik.uni-erlangen.de (Tobias) writes:
> is there a way to redirect the global std::clog (or cout/cerr) to an
> (absolute) global ofstream and how to do it?
#include <iostream>
#include <ostream>
#include <fstream>
std::ofstream ofs("logfile");
int main()
{
std::streambuf * const save_clogbuf(std::clog.rdbuf(ofs.rdbuf()));
std::clog << "Hello world\n";
std::clog.rdbuf(save_clogbuf);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Thomas
|
2/23/2005 11:02:27 PM
|
|
Tobias wrote:
> is there a way to redirect the global std::clog (or cout/cerr) to an
> (absolute) global ofstream and how to do it?
You can simple change 'std::clog's internal stream buffer. Since a
global 'std::ofstream' is eventually destructed you probably want
to new it or just a 'std::filebuf' to install it: 'std::clog' is
flushed very late in the destruction process for global variables.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Dietmar
|
2/24/2005 3:20:19 PM
|
|
> #include <iostream>
> #include <ostream>
> #include <fstream>
>
> std::ofstream ofs("logfile");
>
> int main()
> {
> std::streambuf * const save_clogbuf(std::clog.rdbuf(ofs.rdbuf()));
> std::clog << "Hello world\n";
> std::clog.rdbuf(save_clogbuf);
> }
Thank You Thomas for Your proposal.
It principally works. Unfortunately the redirection seems to be
"invisible" for linked DLL modules. Thus my problem is staying alive:
I have for instance
[main.exe]
int main()
{
/*redirection of clog as above*/
clog << "Log something\n";
/*plugin module.dll*/
}
[module.dll]
{
/* any code */
clog << "Log something in module\n";
}
The std::clog of the dll still keeps directed to the default console.
How to solve this? The DLLs (under .NET) obviously have their own
globality, don't they? But somehow the std::cout/cerr/clog are really
globally unambiguous, regardless of where they are used --> the output
appears always in the same console window.
Greetings from Erlangen (Germany)
Tobias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
tmaier
|
2/24/2005 9:38:26 PM
|
|
Tobias wrote:
> > #include <iostream>
> > #include <ostream>
> > #include <fstream>
> >
> > std::ofstream ofs("logfile");
> >
> > int main()
> > {
> > std::streambuf * const
save_clogbuf(std::clog.rdbuf(ofs.rdbuf()));
> > std::clog << "Hello world\n";
> > std::clog.rdbuf(save_clogbuf);
> > }
> It principally works. Unfortunately the redirection seems to be
> "invisible" for linked DLL modules.
This is probably off-topic. You're dealing with something outside
of standard C++ here. In C++ there's no concept of a DLL (yet) so
the question if it shares the same clog object is rather hypothetical.
If you'd ask in a vc++ group I'd probably tell you to link the C++
runtime as a shared DLL, so it's shared between your exe and dll.
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
msalters
|
2/28/2005 8:21:14 PM
|
|
|
4 Replies
416 Views
(page loaded in 0.065 seconds)
|
|
|
|
|
|
|
|
|