Skip to main content
deleted 46 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

ostream 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, so I implemented an ostream wrapper below. 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. Any feedback would be much appreciated.

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 below. 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. Any feedback would be much appreciated.

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.

Improved text formatting.
Source Link
glampert
  • 17.2k
  • 4
  • 30
  • 89

I want to be able to write logging data out to a network device with the same ease as using std::coutstd::cout. SoSo I implemented an ostreamostream wrapper below. TheThe wrapper uses the C file IO functions to simulate writing to the device. II am looking for feedback on whether or not I have implemented the ostreamostream API correctly. AnyAny feedback would be much appreciated.

Here is the code.

device_ostream.hpp:device_ostream.hpp:

device_ostream.cpp:device_ostream.cpp:

Code to exercise:Code to exercise:

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 below. 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. Any feedback would be much appreciated.

Here is the code.

device_ostream.hpp:

device_ostream.cpp:

Code to exercise:

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 below. 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. Any feedback would be much appreciated.

device_ostream.hpp:

device_ostream.cpp:

Code to exercise:

Source Link
arcomber
  • 2.4k
  • 7
  • 28
  • 46

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 below. 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. Any feedback would be much appreciated.

Here is the code.

device_ostream.hpp:

#ifndef DEVICE_OSTREAM_HPP__
#define DEVICE_OSTREAM_HPP__

#include <ostream>

#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 << "Hi there, how are you today???" << "\n";
  os << "Number: " << 33 << std::endl;  // std::endl causes sync to be called even if buffer not full yet
  os << "ABCDEFGHIJKLMNOPQRSTUVWXYZ" << std::endl;
  os << "abcdefghijklmnopqrstuvwxyz" << std::endl;
  os << "ABCDEFGHIJKLMNOPQRSTUVWXYZ" << std::endl;
  
  if(os)
    os << "abcdefghijklmnopqrstuvwxyz" << std::endl;

  return 0;
}