Boost logo

Boost Users :

Subject: [Boost-users] [iostreams] Is it possible for a sink to get notified when the stream is closed?
From: Bob van Manen (bobm75_at_[hidden])
Date: 2009-03-27 13:43:48

Hi All,
I am trying out the Boost iostreams library to build a sink, text sent to
this sink is split up in to lines of text and each line is then being send
over to our logging server (the function I'm calling to do this is static).

So far I've built the write method as mentioned in the documentation for my
sink, and push all the characters I receive in a buffer inside my sink
class. Then I look for the end of line characters and figure out the lines
of text. The problem I'm having is when there is no end of line character
and the stream gets closed.

Is there a way to get notified when the stream gets closed? Is there a
better way to do this?

Here is the code I have:

class logging_sink
     typedef char char_type;
     typedef boost::iostreams::sink_tag category;

     std::streamsize write(const char* s, std::streamsize n)
          buffer_.append(s, n);
          // loop over buffer and find end of lines, send them to logging

     void close()
          // close is never called
          if (!buffer_.empty())

     std::string buffer_;


class logging_stream : public boost::iostreams::stream<logging_sink>
              : boost::iostreams::stream<logging_sink>(logging_sink())

logging_stream s;
s << "Hello World";

Thank you for your help,


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at