2014-01-25 17:09:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-03-19 01:23:24 +08:00
|
|
|
// Faster than ostringstream--returns its string by ref
|
2014-03-07 06:52:50 +08:00
|
|
|
|
2014-03-20 09:47:57 +08:00
|
|
|
#include "c11log/details/fast_buf.h"
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
|
|
|
|
class str_devicebuf:public std::streambuf
|
|
|
|
{
|
2014-03-20 09:47:57 +08:00
|
|
|
public:
|
2014-02-22 04:51:54 +08:00
|
|
|
str_devicebuf() = default;
|
|
|
|
~str_devicebuf() = default;
|
2014-03-04 07:23:38 +08:00
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
str_devicebuf(const str_devicebuf& other) = delete;
|
|
|
|
str_devicebuf(str_devicebuf&& other) = delete;
|
2014-03-07 06:06:34 +08:00
|
|
|
str_devicebuf& operator=(const str_devicebuf&) = delete;
|
|
|
|
str_devicebuf& operator=(str_devicebuf&&) = delete;
|
|
|
|
|
2014-03-20 09:47:57 +08:00
|
|
|
/*
|
2014-03-14 20:35:46 +08:00
|
|
|
const std::string& str_ref() const
|
|
|
|
{
|
2014-02-22 04:51:54 +08:00
|
|
|
return _str;
|
|
|
|
}
|
2014-03-20 09:47:57 +08:00
|
|
|
*/
|
|
|
|
bufpair_t buf()
|
|
|
|
{
|
|
|
|
return _fastbuf.get();
|
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-16 03:05:11 +08:00
|
|
|
void reset_str()
|
2014-03-14 20:35:46 +08:00
|
|
|
{
|
2014-03-20 09:47:57 +08:00
|
|
|
//_str.clear();
|
|
|
|
_fastbuf.clear();
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
|
|
|
|
protected:
|
2014-03-17 21:43:13 +08:00
|
|
|
int sync() override
|
2014-03-14 20:35:46 +08:00
|
|
|
{
|
2014-02-22 04:51:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-20 09:47:57 +08:00
|
|
|
// copy the give buffer into the accumulated string.
|
|
|
|
// reserve initially 128 bytes which should be enough for common log lines
|
|
|
|
std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
if(_str.capacity() < k_initial_reserve)
|
|
|
|
{
|
|
|
|
_str.reserve(k_initial_reserve);
|
|
|
|
}
|
|
|
|
_str.append(s, static_cast<unsigned int>(count));
|
|
|
|
*/
|
|
|
|
_fastbuf.append(s, static_cast<unsigned int>(count));
|
2014-02-22 04:51:54 +08:00
|
|
|
return count;
|
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-03-17 21:43:13 +08:00
|
|
|
int_type overflow(int_type ch) override
|
2014-03-14 20:35:46 +08:00
|
|
|
{
|
2014-03-19 01:23:24 +08:00
|
|
|
|
2014-03-20 09:47:57 +08:00
|
|
|
bool not_eofile = traits_type::not_eof(ch);
|
2014-03-19 01:23:24 +08:00
|
|
|
if (not_eofile)
|
2014-03-20 09:47:57 +08:00
|
|
|
{
|
|
|
|
char c = traits_type::to_char_type(ch);
|
2014-03-19 01:23:24 +08:00
|
|
|
xsputn(&c, 1);
|
2014-03-20 09:47:57 +08:00
|
|
|
}
|
|
|
|
return not_eofile;
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
private:
|
2014-03-20 09:47:57 +08:00
|
|
|
//std::string _str;
|
2014-03-20 10:20:08 +08:00
|
|
|
fast_buf<192> _fastbuf;
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
class fast_oss:public std::ostream
|
|
|
|
{
|
2014-01-25 17:09:04 +08:00
|
|
|
public:
|
2014-02-22 04:51:54 +08:00
|
|
|
fast_oss():std::ostream(&_dev) {}
|
|
|
|
~fast_oss() = default;
|
2014-03-04 07:23:38 +08:00
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
fast_oss(const fast_oss& other) = delete;
|
|
|
|
fast_oss(fast_oss&& other) = delete;
|
2014-03-07 06:06:34 +08:00
|
|
|
fast_oss& operator=(const fast_oss& other) = delete;
|
2014-03-20 09:47:57 +08:00
|
|
|
/*
|
2014-03-14 20:35:46 +08:00
|
|
|
const std::string& str_ref() const
|
|
|
|
{
|
2014-02-22 04:51:54 +08:00
|
|
|
return _dev.str_ref();
|
|
|
|
}
|
2014-03-20 09:47:57 +08:00
|
|
|
*/
|
|
|
|
bufpair_t buf()
|
|
|
|
{
|
|
|
|
return _dev.buf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_str()
|
|
|
|
{
|
|
|
|
_dev.reset_str();
|
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
|
|
|
|
private:
|
2014-02-22 04:51:54 +08:00
|
|
|
str_devicebuf _dev;
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
|
|
|
}
|
2014-02-04 02:28:19 +08:00
|
|
|
}
|