Solved

Redirecting streams

Posted on 2003-11-11
2
344 Views
Last Modified: 2010-04-01
I have a log class that uses C-style I/O. This allows me to have the following situation (simplified):

class Log
{
public:
   ...
   void SetFile(FILE* f) { file = f; }
   ...
private:
   FILE* file;
};

So I can redirect the log output to any file (including stdout or stderr for example) and change it however i want at runtime. I was trying to convert this to use C++ stream I/O and I can't seem to find an equally elegant solution that would allow me to write to a file or cout/cerr etc and change it at runtime. All I can come up with is something like the following:

class Log
{
public:
   Log(const char* filename) : os(NULL), file(filename) {
      if (file)
         os.rdbuf(file.rdbuf());
   }
   Log(std::ostream& _os) : os.rdbuf(_os.rdbuf()) { }

   void SetFile(const char* filename) {
      if (file.is_open())
         file.close();
      file.open(filename);
      if (file)
         os.rdbuf(file.rdbuf());
   }
   void SetStream(std::ostream& _os) {
      if (file.is_open())
         file.close();
      os.rdbuf(_os.rdbuf());
   }
   ...
private:
   std::ostream os;
   std::ofstream file;
};

which is ok, but having to have both an ostream and an ofstream jerks me a little. I tried having just the ofstream but you can't change its rdbuf (at least not using the iostreams library in VC7). I also tried having an ostream* but this isn't really much better overall (although it too seems to work). Can someone enlighten me with a nicer solution? Yes this is a bit pedantic for a log class but hey it might come up somewhere more important one day ;)

Cheers
James
0
Comment
Question by:JLevick
2 Comments
 
LVL 4

Accepted Solution

by:
havman56 earned 125 total points
Comment Utility


why cant u used insertion operation on filestream

std::ofstream *file_ptr;
(*file_ptr)<<(*istringstream).rdbuf();

There are lot overloaded function << for output streams u can use appropriately rather than setfunction of urs.

havman56



0
 

Author Comment

by:JLevick
Comment Utility
thanks - my crappy docs didn't list all the overloads of the insertion operator, there is indeed one that does the job.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now