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
{
public:
     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
service
     }

     void close()
     {
          // close is never called
          if (!buffer_.empty())
              log_message(buffer_);
     }

private:
     std::string buffer_;

};

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

logging_stream s;
s << "Hello World";
s.close();

Thank you for your help,

bob



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net