std::ostream wrapper for network logging device I want to be able to write logging data out to a network device with the same ease as using std::cout, so I implemented an ostream wrapper. The wrapper uses the C file IO functions to simulate writing to the device. I am looking for feedback on whether or not I have implemented the ostream API correctly.

device_ostream.hpp:

#ifndef DEVICE_OSTREAM_HPP__
#define DEVICE_OSTREAM_HPP__

#include &lt;ostream&gt;

#define BUF_SIZE 100

class device_buffer : public std::streambuf
{
public:
  device_buffer();
  ~device_buffer();
  int overflow(int ch = EOF);
  int sync();

private:
  char buffer_[BUF_SIZE];
  FILE* fp_;

};

class device_ostream : public std::ostream {
public:
  device_ostream() : std::ostream(new device_buffer) {}

  ~device_ostream() {
    delete rdbuf();
  }
};

#endif // DEVICE_OSTREAM_HPP__


device_ostream.cpp:

#include "device_ostream.hpp"

device_buffer::device_buffer() : fp_(0) {
  memset(buffer_, 0, sizeof(buffer_));
  setp(buffer_, buffer_+BUF_SIZE-1);
  fp_=fopen("test.bin","wb");  // simulate our device
}

device_buffer::~device_buffer() {
  if(fp_)
    fclose(fp_); // simulate disconnection from device
}

// ostream fwk notification that buffer is now full
// so write out data now
int device_buffer::overflow(int ch) {
  if(ch != EOF) {
    *pptr() = ch;
    pbump(1);
  }
  return sync();
}

// function to sync up with hardware device.  ie we actually write out data here
int device_buffer::sync() {
  if(fp_) {
    fwrite(buffer_, 1, size_t(pptr() - pbase()), fp_);
    // reset put ptr
    setp(pbase(), epptr());
    return 0;  // success
  }
  else
    return EOF; // return EOF on device failure
}


Code to exercise:

#include "device_ostream.hpp"

int main () {

  device_ostream os;
  os.write("WRITE", 5);

  os &lt;&lt; "Hi there, how are you today???" &lt;&lt; "\n";
  os &lt;&lt; "Number: " &lt;&lt; 33 &lt;&lt; std::endl;  // std::endl causes sync to be called even if buffer not full yet
  os &lt;&lt; "ABCDEFGHIJKLMNOPQRSTUVWXYZ" &lt;&lt; std::endl;
  os &lt;&lt; "abcdefghijklmnopqrstuvwxyz" &lt;&lt; std::endl;
  os &lt;&lt; "ABCDEFGHIJKLMNOPQRSTUVWXYZ" &lt;&lt; std::endl;

  if(os)
    os &lt;&lt; "abcdefghijklmnopqrstuvwxyz" &lt;&lt; std::endl;

  return 0;
}
You should not do this:

class device_ostream : public std::ostream {
public:
  device_ostream() : std::ostream(new device_buffer) {}

  ~device_ostream() {
    delete rdbuf();
  }
};


You do not know if the user of the class has reset the buffer while you were not watching.

device_ostream   data;
....
MyImprovedStreamBuffer   myBuffer;
data.rdbuf(&amp;myBuffer);               // Your destructor will not play well with that.


I would make it an internal member.

class device_ostream : public std::ostream
{
    device_buffer   buffer;
  public:
    device_ostream()
        : std::ostream(nullptr)
    {
        rdbuf(&amp;buffer);
    }
};


You should validate the return code of C functions.

fwrite(buffer_, 1, size_t(pptr() - pbase()), fp_);


Does not guarantee that it will write everything you asked for. Personally I would use a lower level write function so I can accurately check the error state and compensate accordingly.

std::size_t written = 0;
std::size_t size    = pptr() - pbase();

while(written != size)
{
    std::size output = write(fp_, buffer_ + written, size - written);
    if (output == -1)
    {
        if (errno == EINTR || errno == EAGAIN)
        {
             continue; // Ignore the error and try again.
        }
        // Something bad happened.
        abort(1); // or set the state of your stream to bad in
                  // some way (I used abort() simply because I am 
                  // to lazy to look up what to do. (you should).
    }
    written += output;
}


The value returned by overflow is incorrect.

According to the specs:


  In case of success, the character put is returned, converted to a value of type int_type using member traits_type::to_int_type.
  Otherwise, it returns the end-of-file value (EOF) either if called with this value as argument c or to signal a failure (some implementations may throw an exception instead).


On success you return 0.

The value returned by sync is incorrect.

According to the specs:


  Returns zero, which indicates success.
  A value of -1 would indicate failure.


On failure you return EOF. Yes I get that it is -1. But your making assumptions. Follow specs to the letter.

Things like this scare me:

  else
    return EOF; // return EOF on device failure


I have been bitten a couple of times were macros expand to more than a single statement. Which renders the block incorrect. As a result I always use '{}' for sub blocks. I recommend it for everything it will prevent those impossible to find errors.

Since we now live in 2014 (nearly 2015). You should be using the modern version of the compiler. So add override to functions that override virtual functions.

int overflow(int ch = EOF)  override;
int sync()                  override;


Definitely worth overriding the method streamsize xsputn (const char* s, streamsize n);