By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,403 Members | 948 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,403 IT Pros & Developers. It's quick & easy.

output to 2 streams

P: n/a
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?

Fraser.
Jul 22 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?


How about this simple way -
std::ofstream out1("xxx.log");
....
std::cout << "Something to log and print";
out1 << "Something to log and print";

You could also use Macro trick to get this done, something like -

#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";

int main()
{
//...
PRINT ("Something to print");

}

-Sharad

Jul 22 '05 #2

P: n/a
this done, something like -

#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";


Should be --

#define PRINT(xxx) std::cout << xxx; \
out1 << xxx;
Jul 22 '05 #3

P: n/a
Can a ostream_iterator or ostreambuf_iterator be constructed from a
ostringstream? E.g.:
std::ostringstream oStr;
std::ostream_iterator it(oStr);

basic_ostringstream inherits from basic_ostream. ostream_iterator has a
constructor taking a basic_ostream.

Fraser.
Jul 22 '05 #4

P: n/a
An explicit specialisation is required.
std::ostream_iterator<std::basic_ostringstream::ch ar_type,
std::basic_ostringstream::char_type> it(oStr);

Fraser.
Jul 22 '05 #5

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
My program outputs to cout and I want it to output to a LOG file as well. Is there a simple way to do that?


I have submitted a library to boost, to be reviewed around the
beginning of September, which allows this to be done easily

struct splitter : boost::io::sink {
tee(std::ostream& first, std::ostream& second)
: first(first), second(second)
{ }
void write(const char* s, std::streamsize n)
{
first.write(s, n);
second.write(s, n);
}
std::ostream& first;
std::ostream& second;
};

int main()
{
std::ofstream log("log.txt");
boost::io::stream_facade<splitter> out;
out.open(splitter(log, std::cout));
out << "this goes to cout and to log\n";
}

The library is here: http://tinyurl.com/3m6ur

Jonathan
Jul 22 '05 #6

P: n/a
"Sharad Kala" <no******************@yahoo.com> wrote:
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?


You could also use Macro trick to get this done, something like -

#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";


This would be bad if xxx had side effects.. how about:

struct tee_stream
{
template<typename T>
tee_stream &operator<< (T const &t) {
std::cout << t;
other_stream << t;
return *this;
}
};
Jul 22 '05 #7

P: n/a
Old Wolf wrote:
"Sharad Kala" <no******************@yahoo.com> wrote:
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
> My program outputs to cout and I want it to output to a LOG file as
> well. Is there a simple way to do that?


You could also use Macro trick to get this done, something like -

#define PRINT(xxx) std::cout << "Something to log and print"; \
out1 << "Something to log and print";


This would be bad if xxx had side effects.. how about:

struct tee_stream
{
template<typename T>
tee_stream &operator<< (T const &t) {
std::cout << t;
other_stream << t;
return *this;
}
};


Try:

tee_stream mystream;
mystream << std::endl;

Jul 22 '05 #8

P: n/a
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message news:<41******@news.greennet.net>...
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?


Sure: program | tee LOG :)

Martin
Jul 22 '05 #9

P: n/a
Rolf Magnus <ra******@t-online.de> wrote:
Old Wolf wrote:
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote:

> My program outputs to cout and I want it to output to a LOG file as
> well. Is there a simple way to do that?

struct tee_stream
{
template<typename T>
tee_stream &operator<< (T const &t) {
std::cout << t;
other_stream << t;
return *this;
}
};


Try:

tee_stream mystream;
mystream << std::endl;


Interesting, why doesn't this work exactly?
Jul 22 '05 #10

P: n/a
Interesting, why doesn't this work exactly?


Because std::endl is a pointer to a function and is un-implemented for
tee_stream.
So a rough implementation to make it work with std::endl is to add this in
tee_stream class-
typedef ostream& (*manipT)(ostream&);
tee_stream & operator<< (manipT manip)
{
((*manip)(cout));
((*manip)(other_stream));
return *this;
}

-Sharad

Jul 22 '05 #11

P: n/a
I would try your suggestion if I could get the Boost libraries installed.

Fraser.
Jul 22 '05 #12

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
I would try your suggestion if I could get the Boost libraries installed.


What platform do you use?

BTW, the formal review for acceptance into Boost is going on right now. I
encourage
anyone who is interested to participate in the review process on the Boost
developers list (see http://www.boost.org/more/mailing_lists.htm#main).

Jonathan
Jul 22 '05 #13

P: n/a
W2000 and BCB4.

Fraser.
Jul 22 '05 #14

P: n/a
I have made improvements to the igzstream and ogzstream classes available
from the link to "zlib C++ wrapper" at the zlib home page. They fix a bug
with gzstreambuf::close() as well as mainly minor improvements. If you are
interested in seeing the 2 files I could send them by email.

Fraser.
Jul 22 '05 #15

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
I have made improvements to the igzstream and ogzstream classes available
from the link to "zlib C++ wrapper" at the zlib home page. They fix a bug
with gzstreambuf::close() as well as mainly minor improvements. If you are
interested in seeing the 2 files I could send them by email.


I'm sure the authors of the C++ wrapper would be interested. I don't use it in
my library.

Thanks,
Jonathan
Jul 22 '05 #16

P: n/a

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:41******@news.greennet.net...
W2000 and BCB4.


Yeah, BCB4 is way to old.

Jonathan
Jul 22 '05 #17

P: n/a
You have missed the lhs parameter with the operator<<. Thats probably a
typo.

If you try other manipulators and member functions the tee_streem member
functions and related globals have to be extended. I.e. dec, setf, setfill
etc.

Fraser.
"Sharad Kala" <no******************@yahoo.com> wrote in message
news:2p************@uni-berlin.de...
Interesting, why doesn't this work exactly?


Because std::endl is a pointer to a function and is un-implemented for
tee_stream.
So a rough implementation to make it work with std::endl is to add this in
tee_stream class-
typedef ostream& (*manipT)(ostream&);
tee_stream & operator<< (manipT manip)
{
((*manip)(cout));
((*manip)(other_stream));
return *this;
}

-Sharad

Jul 22 '05 #18

P: n/a
"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote:
My program outputs to cout and I want it to output to a LOG file as well.
Is there a simple way to do that?

Fraser.


Hi I have a class tee in my toolbox for this. I attach the header and cpp
here.

The solution is to implement a streambuf, which passes everything to 2 other
streambufs and a ostream, which uses this streambuf.

You use it like this:

std::ofstream logfile("logfile.log");
tee my_stream(logfile, std::cout);
my_stream << "put this message ito logfile.log and console" << std::endl;

the second stream defaults to std::cout (just like /usr/bin/tee in unix), so
it is enough to declare:

tee my_stream(logfile);
Tommi Mäkitalo
Jul 22 '05 #19

P: n/a
Thanks. It appears to work.

Fraser.
Jul 22 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.