initial reserver of std_devicebuf=128

This commit is contained in:
gabime 2014-03-17 15:43:13 +02:00
parent d8ddfe5c24
commit da82d8e245
2 changed files with 14 additions and 15 deletions

View File

@ -20,8 +20,6 @@ int main(int argc, char* argv[])
{ {
if(argc || argv) {}; if(argc || argv) {};
std::string s;
cout << "STRING cap: " << format(s.capacity()) << endl;
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0); auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
//auto fsink = std::make_shared<sinks::simple_file_sink>("simplelog", "txt"); //auto fsink = std::make_shared<sinks::simple_file_sink>("simplelog", "txt");

View File

@ -12,7 +12,7 @@ namespace details
class str_devicebuf:public std::streambuf class str_devicebuf:public std::streambuf
{ {
public: public:
str_devicebuf() = default; str_devicebuf() = default;
~str_devicebuf() = default; ~str_devicebuf() = default;
@ -33,31 +33,32 @@ public:
} }
protected: protected:
virtual int sync() override int sync() override
{ {
return 0; return 0;
} }
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override // copy the give buffer into the accumulated string.
{ // reserve initially 128 bytes which should be enough for common log lines
auto ssize = _str.size(); std::streamsize xsputn(const char_type* s, std::streamsize count) override
auto cap_left = _str.capacity() - ssize; {
if(cap_left < static_cast<std::size_t>(count)) if(_str.capacity() < k_initial_reserve)
_str.reserve(ssize + count + 128); {
_str.reserve(k_initial_reserve);
_str.append(s, static_cast<unsigned int>(count)); }
_str.append(s, static_cast<unsigned int>(count));
return count; return count;
} }
virtual int_type overflow(int_type ch) override int_type overflow(int_type ch) override
{ {
if (ch != traits_type::eof()) if (ch != traits_type::eof())
_str.append((char*)&ch, 1); xsputn((char*)&ch, 1);
return 1; return 1;
} }
private: private:
std::string _str; std::string _str;
static constexpr std::streamsize k_initial_reserve = 128;
}; };
class fast_oss:public std::ostream class fast_oss:public std::ostream