mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-16 00:45:48 +08:00
88 lines
1.8 KiB
C++
88 lines
1.8 KiB
C++
#pragma once
|
|
|
|
// Fast ostringstream like supprt which return its string by ref and nothing more
|
|
|
|
#include<streambuf>
|
|
#include<string>
|
|
|
|
namespace c11log
|
|
{
|
|
namespace details
|
|
{
|
|
|
|
class str_devicebuf:public std::streambuf
|
|
{
|
|
public:
|
|
str_devicebuf() = default;
|
|
~str_devicebuf() = default;
|
|
|
|
str_devicebuf(const str_devicebuf& other) = delete;
|
|
str_devicebuf(str_devicebuf&& other) = delete;
|
|
str_devicebuf& operator=(const str_devicebuf&) = delete;
|
|
str_devicebuf& operator=(str_devicebuf&&) = delete;
|
|
|
|
|
|
const std::string& str_ref() const
|
|
{
|
|
return _str;
|
|
}
|
|
|
|
void reset_str()
|
|
{
|
|
_str.clear();
|
|
}
|
|
|
|
protected:
|
|
int sync() override
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// 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));
|
|
return count;
|
|
}
|
|
|
|
int_type overflow(int_type ch) override
|
|
{
|
|
if (ch != traits_type::eof())
|
|
xsputn((char*)&ch, 1);
|
|
return 1;
|
|
}
|
|
private:
|
|
std::string _str;
|
|
static constexpr std::streamsize k_initial_reserve = 128;
|
|
};
|
|
|
|
class fast_oss:public std::ostream
|
|
{
|
|
public:
|
|
fast_oss():std::ostream(&_dev) {}
|
|
~fast_oss() = default;
|
|
|
|
fast_oss(const fast_oss& other) = delete;
|
|
fast_oss(fast_oss&& other) = delete;
|
|
fast_oss& operator=(const fast_oss& other) = delete;
|
|
|
|
const std::string& str_ref() const
|
|
{
|
|
return _dev.str_ref();
|
|
}
|
|
void reset_str()
|
|
{
|
|
_dev.reset_str();
|
|
}
|
|
|
|
private:
|
|
str_devicebuf _dev;
|
|
};
|
|
}
|
|
}
|