473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

output to 2 streams

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
19 1688

"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
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
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
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

"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
"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
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
"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
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
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
I would try your suggestion if I could get the Boost libraries installed.

Fraser.
Jul 22 '05 #12

"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
W2000 and BCB4.

Fraser.
Jul 22 '05 #14
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

"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

"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
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
"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
Thanks. It appears to work.

Fraser.
Jul 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Xizor | last post by:
Lets say I got something simple like: <?php echo "yo"; echo "word"; ?> I want all those echos to write to a file instead of printing to the screen
2
by: Maria Gaitani | last post by:
Hi! I have made a client and a server which are supposed to communicate by sending java objects. They don't. I have managed to send an object from the client to the server. But continuing after...
2
by: Patrick L. Nolan | last post by:
I'm going nuts trying to port an application from Linux to Windows. We have a python/Tkinter script which runs a C++ application. It starts it with popen4 and communicates through the two pipes....
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
6
by: ben | last post by:
Hi guyz, I want to know how to send output to screen(i.e. standard o/p)in UNIX using streams. I am reading a file char. by char. and i want to print it ultimately on the screen.I know how to...
7
by: Ira | last post by:
Hi, Using an embedded interpreter, how do I change it's default output streams (specifically the one used by PyErr_Print() which I'm guessing is the default error stream)? Cheers, Ira
37
by: nobody | last post by:
I am writing a framework that other developers will write plug-ins for. I would like for one of the features of the framework to be to intercept all text written to stdout/stderr and prepend...
18
by: v4vijayakumar | last post by:
is there any standard way to extract output from system(3) function? TIA.
11
by: aljaber | last post by:
hi, i am facing a problem with my program output here is the program /*********************************************\ * CD Database * * ...
19
by: deepak | last post by:
How the following code is working. main() { int a = 38, b = 13; unsigned long long c; c = a * (1<<b) * 32000; printf("%llu", c);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.