线程安全的日志类 basic_message_handler_log

sms88 2007-11-21 11:53:08
看了http://stl.winterxy.com/html/000068.html这文章,但是却没有找到它说的类basic_message_handler_log
通过google.com也没找到它的下载

谁有线程安全的日志类??
...全文
74 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzhxxx 2007-11-21
  • 打赏
  • 举报
回复

// message_handler_log.h
#include <ostream>
#include <sstream>
#include <memory>
#include <assert.h>

// forward declaration(s)
template< class char_type, class traits_type = std::char_traits< char_type> >
class basic_message_handler_log;

// represents the stream buffer for a message_handler_log (see below)
// Note: NOT thread-safe
template< class char_type , class traits_type = std::char_traits< char_type> >
class basic_message_handler_log_streambuf
: public std::basic_streambuf< char_type, traits_type>
{
private:
friend basic_message_handler_log< char_type, traits_type>;
typedef std::basic_streambuf< char_type, traits_type> streambuf_type;
typedef basic_message_handler_log< char_type, traits_type> ostream_type;
// base class
typedef std::basic_streambuf< char_type, traits_type> base_class;

enum
{ _BADOFF = -1 /* bad offset - for positioning functions */ };
protected:
// input, not allowed
virtual int_type pbackfail(int_type = traits_type::eof())
{
// only for output, not for input
assert( false);
return (traits_type::eof());
}
virtual int showmanyc()
{
// only for output, not for input
assert( false);
return 0;
}
virtual int_type underflow()
{
// only for output, not for input
assert( false);
return (traits_type::eof());
}
virtual int_type uflow()
{
// only for output, not for input
assert( false);
return (traits_type::eof());
}

virtual std::streamsize xsgetn(char_type * _S, std::streamsize _N)
{
// only for output, not for input
assert( false);
return 0;
}

// positioning, not allowed - we're a log
virtual pos_type seekoff(off_type, std::ios_base::seekdir,
std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
{
// we don't allow positioning
assert( false);
return (std::streampos( _BADOFF));
}
virtual pos_type seekpos(pos_type,
std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
{
// we don't allow positioning
assert( false);
return (std::streampos( _BADOFF));
}

// output functions

// called to write out from the internal
// buffer, into the external buffer
virtual int sync()
{
m_pOwnerStream->on_new_message( get_stream_buffer().str() );
m_pStreamBuffer = std::auto_ptr< string_stream_type>( new string_stream_type);
return 0;
}
virtual streambuf_type *setbuf( char_type * buffer, std::streamsize n)
{
// ... note: this function MUST be called
// before working with this stream buffer
// we don't use a buffer - we forward everything
assert( buffer == NULL && n == 0);
setp( NULL, NULL);
return this;
}
// write the characters from the buffer
// to their real destination
virtual int_type overflow(int_type nChar = traits_type::eof())
{
if ( traits_type::not_eof( nChar))
get_stream_buffer() << ( char_type)nChar;
return traits_type::not_eof( nChar);
}
virtual std::streamsize xsputn(const char_type *S, std::streamsize N)
{
get_stream_buffer().write( S, N);
return N;
}

public:
basic_message_handler_log_streambuf():m_pStreamBuffer( new string_stream_type) {}
private:
typedef std::basic_ostringstream< char_type> string_stream_type;
string_stream_type & get_stream_buffer()
{ return *m_pStreamBuffer; }
private:
// holds the Message, until it's flushed
std::auto_ptr< string_stream_type> m_pStreamBuffer;
// the Message Handler Log - where we write into
ostream_type * m_pOwnerStream;
};

// derive your class from this, and implement the PROTECTED on_new_message function
template< class char_type, class traits_type = std::char_traits< char_type> >
class basic_message_handler_log : public std::basic_ostream< char_type, traits_type>
{
typedef basic_message_handler_log_streambuf< char_type, traits_type> handler_streambuf_type;
friend handler_streambuf_type;
typedef std::basic_ostream< char_type, traits_type> base_class;
protected:
typedef std::basic_string< char_type> string_type;

basic_message_handler_log()
: m_StreamBuf(),
base_class( NULL)
{
m_StreamBuf.m_pOwnerStream = this;
init(&m_StreamBuf);
m_StreamBuf.pubsetbuf( NULL, 0);
}
virtual ~basic_message_handler_log() {}
protected:
virtual void on_new_message( const string_type & strNewMessage) = 0;
public:
// our stream buffer
handler_streambuf_type m_StreamBuf;
};

typedef basic_message_handler_log< char> message_handler_log;
typedef basic_message_handler_log< wchar_t> wmessage_handler_log;
sms88 2007-11-21
  • 打赏
  • 举报
回复
程序方程
他是从basic_message_handler_log 派生的,没见basic_message_handler_log 的定义
Fogers 2007-11-21
  • 打赏
  • 举报
回复
up
hzhxxx 2007-11-21
  • 打赏
  • 举报
回复


http://www.builder.com.cn/developer/code/story/0,3800066897,39106406,00.htm
hzhxxx 2007-11-21
  • 打赏
  • 举报
回复
关注中..

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧