[iostreams] Is it possible for a sink to get notified when the stream is closed?

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

Bob van Manen wrote, On 27.3.2009 18:43:
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; Make this
struct category : boost::iostreams::sink_tag , boost::iostreams::closable_tag {};
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
{ public: logging_stream() : boost::iostreams::stream (logging_sink()) {} }; logging_stream s; s << "Hello World"; s.close();
-- VH
participants (2)
-
Bob van Manen
-
Václav Haisman