Solved

Redirecting streams

Posted on 2003-11-11
2
346 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
ID: 9722086


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
ID: 9745634
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

786 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