9

In a C++ program, we have 3 streams: stdin, stdout, and stderr. Can I override these in a console application and use them in a application that uses forms?

For example, if in some base class, I have cout<< "...", can I "redirect" to something visual (like a Windows Form)?

2
  • 3
    Ok, I mean something visual, not the console
    – Bakudan
    Commented Aug 24, 2011 at 0:37
  • Note, that usually overriding streams behaviour is done by implementing std::streambuf interface. That's rather complex task, so you should probably use other answers provided.
    – Basilevs
    Commented Aug 24, 2011 at 3:58

3 Answers 3

11

What I would recommend doing is having a class which wraps around an iostream like this :

#include <iostream>
#define LOG Log()

class Log
{
   public:
      Log(){}
      ~Log()
      {
         // Add an newline.
         std::cout << std::endl;
      }


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

Then, whenever you want to change where the data goes, you just change the class behavior. Here is how you use the class:

 LOG << "Use this like an iostream.";

[edit] As potato swatter suggested, I'll add an example with something other than cout:

#include <sstream>
#define LOG Log()

// An example with a string stream.
class Log
{
   private:
      static std::stringstream buf;
   public:
      Log(){}
      ~Log()
      {
         // Add an newline.
         buf << std::endl;
      }


      template<typename T>
      Log &operator << (const T &t)
      {
         buf << t;
         return * this;
      }
};

// Define the static member, somewhere in an implementation file.
std::stringstream Log::buf;

As for why you should try this instead of inheriting from something like a string stream, mainly because you can easily change where the Logger outputs to dynamically. For instance, you could have three different output streams, and use a static member variable to swap between at runtime:

class Log
{
   private:
      static int outputIndex = 0;
      // Add a few static streams in here.
      static std::stringstream bufOne;
      static std::stringstream bufTwo;
      static std::stringstream bufThree;
   public:
      // Constructor/ destructor goes here.

      template<typename T>
      Log &operator << (const T &t)
      {
         // Switch between different outputs.
         switch (outputIndex)
         {
            case 1:
               bufOne << t;
               break;
            case 2:
               bufTwo << t;
            case 3:
               bufThree << t;
            default:
               std::cout << t;
               break;
         }
         return * this;
      }

      static void setOutputIndex(int _outputIndex)
      {
          outputIndex = _outputIndex;
      }
};

// In use
LOG << "Print to stream 1";
Log::setOutputIndex(2);
LOG << "Print to stream 2";
Log::setOutputIndex(3);
LOG << "Print to stream 3";
Log::setOutputIndex(0);
LOG << "Print to cout";

This can easily be expanded to create a powerful way of dealing with logging. You could add filestreams, use std::cerr, etc.

2
  • 1
    Wouldn't it be easier to just make a new ostream derived class and use that? Commented Aug 24, 2011 at 1:22
  • 2
    This is basically starting from scratch. What if, for example, the existing code uses format manipulators or endl? Also, this example doesn't illustrate how to interface with something besides cout. Commented Aug 24, 2011 at 2:23
3

Here's the code I use to redirect std::cout to a GUI on Windows:

struct STDOUT_BLOCK : SLIST_ENTRY
{
    char sz[];
};

class capturebuf : public std::stringbuf
{
protected:
    virtual int sync()
    {
        if (g_threadUI && g_hwndProgressDialog) {
            // ensure NUL termination
            overflow(0);
            // allocate space
            STDOUT_BLOCK* pBlock = (STDOUT_BLOCK*)_aligned_malloc(sizeof *pBlock + pptr() - pbase(), MEMORY_ALLOCATION_ALIGNMENT);
            // copy buffer into string
            strcpy(pBlock->sz, pbase());
            // clear buffer
            str(std::string());
            // queue string
            ::InterlockedPushEntrySList(g_slistStdout, pBlock);
            // kick to log window
            ::PostMessageA(g_hwndProgressDialog, WM_APP, 0, 0);
        }
        return __super::sync();
    }
};

Then inside main():

capturebuf altout;
std::cout.set_rdbuf(&altout);

Of course, you then need to handle the WM_APP message in your window procedure and pull the strings off the SList. But this handles the cout redirection part.

As jweyrich correctly notes, you need to change the streambuf* back before altout goes out of scope. This code will do so:

struct scoped_cout_streambuf_association
{
    std::streambuf* orig;
    scoped_cout_streambuf_association( std::streambuf& buf )
        : orig(std::cout.rdbuf())
    {
        std::cout.rdbuf(&buf);
    }

    ~scoped_cout_streambuf_association()
    {
        std::cout.rdbuf(orig);
    }
};

And inside main:

capturebuf altout;
scoped_cout_streambuf_association redirect(altout);
3
  • Don't forget to restore the original rdbuf before exiting, otherwise it might crash.
    – jweyrich
    Commented Aug 24, 2011 at 3:24
  • There is no function set_rdbuf, just two overloads of ios::rdbuf. Commented Aug 24, 2011 at 3:55
  • @Potatoswatter: There is in Visual C++, and this code is windows-specific, although the general idea is quite portable the details aren't.
    – Ben Voigt
    Commented Aug 24, 2011 at 13:10
1

The usual way to put an iostream interface on something else is to use the stringstream/istringstream/ostringstream classes in <sstream>.

The best way is to change the use of cin, cout, etc to parameters of type istream & and ostream &. If that is really impossible, you can modify cin.rdbuf() to point at the stream you wish to modify, but that is extremely hackish and won't work with multithreading.

Copy text from the form into an istringstream, then pass it to the existing code as an istream &. When you're done, read the results from the stringstream passed as ostream & that replaced cout and copy it into the form.

Do not declare arguments of type istringstream & or ostringstream &. Use the base classes.

If you really want, you could subclass std::basic_stringbuf< char > and override the sync and underflow virtual functions to tie streams directly to text fields. But that's fairly advanced and likely not worth it.

4
  • 1
    You don't modify cin.rdbuf(), you call it. And it's not "hackish", it's exactly how the standard library iostreams are designed to be used.
    – Ben Voigt
    Commented Aug 24, 2011 at 3:05
  • @Ben: I'm referring to modifying the underlying buffer pointer of the global variable cin to point at a local variable, so that a called function can access the local through a global. That is very hackish. Commented Aug 24, 2011 at 3:09
  • Again, the standard iostreams are designed to have their streambuf objects replaced, and they provide public APIs to do so cleanly. That's how polymorphism is implemented in the standard library iostreams.
    – Ben Voigt
    Commented Aug 24, 2011 at 3:11
  • @Ben: Merely calling rdbuf isn't the issue here, using a library global variable as a function argument is. I think this is fairly clear… Commented Aug 24, 2011 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.