Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,230 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,855 people online right now. Registration is fast and FREE... Join Now!




log cout? custom ostream? ideas?

 
Reply to this topicStart new topic

log cout? custom ostream? ideas?

okyup
6 Mar, 2007 - 10:24 PM
Post #1

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
Say I want to log all or most of the stuff that I'm displaying on the screen.

Is there any way to override cout and have it print to both a file and the screen? Or perhaps a custom ostream that does that?

Or any other method?

Thanks for ideas.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Log Cout? Custom Ostream? Ideas?
7 Mar, 2007 - 04:37 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(okyup @ 6 Mar, 2007 - 11:24 PM) *

Say I want to log all or most of the stuff that I'm displaying on the screen.

Is there any way to override cout and have it print to both a file and the screen? Or perhaps a custom ostream that does that?

Or any other method?

Thanks for ideas.

Hi

I think you areaksing about capturing the stream used by cout?

I seem to recall that there was a way to "sniff" the stream so you can send output to a file. you might want to sniff around MSDN on the topic of streams
User is offlineProfile CardPM
+Quote Post

okyup
RE: Log Cout? Custom Ostream? Ideas?
7 Mar, 2007 - 12:14 PM
Post #3

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
Can't find any info on that.

Think I need a custom ostream after all. At least I have an opportunity to learn how to use them now. :x
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Log Cout? Custom Ostream? Ideas?
7 Mar, 2007 - 12:28 PM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Are you trying to write to both screen and file with a single command? If so, you can write a function/method that takes everything you need as a parameter, and performs both actions.
User is online!Profile CardPM
+Quote Post

ajwsurfer
RE: Log Cout? Custom Ostream? Ideas?
7 Mar, 2007 - 01:16 PM
Post #5

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Checkout this page for an example of something simular:

http://www.java2s.com/Code/Cpp/Overload/Ov...eamoperator.htm


User is offlineProfile CardPM
+Quote Post

okyup
RE: Log Cout? Custom Ostream? Ideas?
7 Mar, 2007 - 02:54 PM
Post #6

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
QUOTE(Amadeus @ 7 Mar, 2007 - 01:28 PM) *

Are you trying to write to both screen and file with a single command? If so, you can write a function/method that takes everything you need as a parameter, and performs both actions.
Yea that's what I was thinking. I like using insertion operator so much and hate printing with custom functions. crazy.gif That will be my backup strategy if custom ostream doesn't work out. ph34r.gif

QUOTE(ajwsurfer @ 7 Mar, 2007 - 02:16 PM) *

Checkout this page for an example of something simular:

http://www.java2s.com/Code/Cpp/Overload/Ov...eamoperator.htm
That is to send custom objects to the stream, not to change how the stream works. :-\ But thanks.
User is offlineProfile CardPM
+Quote Post

okyup
RE: Log Cout? Custom Ostream? Ideas?
12 Mar, 2007 - 07:28 PM
Post #7

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
I figured I would do this:
CODE
#ifndef LOG_H
#define LOG_H

#include <string>
#include <vector>

using namespace std;

class log {
    private:
        vector<string> logfile;
        string type;
    public:
        log();
        log(const string&);
        string getFullLog();
        string getLastLog();
        const log& operator << (const string&);
        const log& operator << (const char*);
        const log& operator << (const int&);
        const log& operator << (const char&);
        const log& operator << (const float&);
};

#endif


Then log and cout at the same time like this:
CODE
log lout;
lout << "blah blah";


Does anyone know if it could be worked so this would work??
CODE
lout << "blah blah" << "hello world!" << "how are you today?";

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month