stackbuf move ctor

This commit is contained in:
gabime 2014-05-06 17:38:11 +03:00
parent 3463dcd1aa
commit b72098101e
6 changed files with 52 additions and 40 deletions

View File

@ -17,13 +17,13 @@ using namespace utils;
int main(int argc, char* argv[])
{
const unsigned int howmany = argc <= 1 ? 1000000:atoi(argv[1]);
logger cout_logger ("", sinks::stdout_sink());
cout_logger.set_min_level(c11log::level::TRACE);
cout_logger.info() << "Hello " << "man" << 123;
cout_logger.trace("This is very nice! ") << "Yes gabi.." << ":)";
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, 0);
auto nullsink = sinks::null_sink::get();
@ -35,16 +35,14 @@ int main(int argc, char* argv[])
auto start = system_clock::now();
for(unsigned int i = 1; i <= howmany ; ++i)
my_logger.info("Hello logger: ") << 4.5 << 123 << "asdasd" << 123 << 'f';
my_logger.info("Hello logger: ") << 4.5 <<'\t' << i << "\tasdasd:" << 123 << 'f';
//auto s = howmany - as->q().size();
auto s = howmany;
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
cout_logger.info("Total:") << format(s);
cout_logger.info("Total:") << format(howmany);
cout_logger.info("Delta:") << format(delta_d);
cout_logger.info("Rate:") << format(s/delta_d) << "/sec";
cout_logger.info("Rate:") << format(howmany/delta_d) << "/sec";
return 0;
}

View File

@ -39,18 +39,23 @@ public:
line_logger& operator=(const line_logger&) = delete;
line_logger& operator=(line_logger&&) = delete;
line_logger(line_logger&& other) :
_callback_logger(other._callback_logger),
_log_msg(other._log_msg),
// The move ctor should only be called on start of logging line,
// where no logging happened yet for this line so no need to copy the oss from the other
_oss(),
_enabled(other._enabled) {}
_log_msg(std::move(other._log_msg)),
_oss(std::move(other._oss)),
_enabled(other._enabled),
_empty(other._empty)
{
other.disable();
}
~line_logger()
{
//only if enabled and not empty
if (!_empty)
if (_enabled && !_empty)
{
_oss << os::eol();
_log_msg.msg_buf = _oss.buf();
@ -75,6 +80,11 @@ public:
return *this;
}
void disable()
{
_enabled = false;
}
private:

View File

@ -7,15 +7,8 @@ namespace details
struct log_msg
{
log_msg() = default;
log_msg(level::level_enum l):msg_level(l) {};
log_msg(const log_msg& other)
{
msg_buf = other.msg_buf;
msg_time = other.msg_time;
msg_header_size = other.msg_header_size;
msg_level = other.msg_level;
}
log_msg(level::level_enum l):msg_level(l) {};
bufpair_t msg_buf;
log_clock::time_point msg_time;
std::size_t msg_header_size;

View File

@ -8,7 +8,6 @@
// Fast memory storage
// stores its contents on the stack when possible, in vector<char> otherwise
// NOTE: User should be remember that returned buffer might be on the stack!!
namespace c11log
{
namespace details
@ -18,9 +17,12 @@ template<std::size_t STACK_SIZE=128>
class stack_buf
{
public:
stack_buf():_stack_size(0) {}
stack_buf():_v(),_stack_buf(), _stack_size(0) {}
~stack_buf() {};
stack_buf& operator=(const stack_buf other) = delete;
stack_buf& operator=(stack_buf&& other) = delete;
stack_buf(const bufpair_t& buf_to_copy):stack_buf()
{
append(buf_to_copy);
@ -36,18 +38,15 @@ public:
}
stack_buf(stack_buf&& other)
{
{
_stack_size = other._stack_size;
if(!other._v.empty())
_v = other._v;
_v = std::move(other._v);
else if(_stack_size)
std::copy(other._stack_buf.begin(), other._stack_buf.begin()+_stack_size, _stack_buf.begin());
other.clear();
other.clear();
}
stack_buf& operator=(const stack_buf& other) = delete;
stack_buf& operator=(stack_buf&& other) = delete;
void append(const char* buf, std::size_t buf_size)
{
//If we are aleady using _v, forget about the stack

View File

@ -16,10 +16,15 @@ public:
stack_devicebuf() = default;
~stack_devicebuf() = default;
stack_devicebuf(const stack_devicebuf& other) = delete;
stack_devicebuf(stack_devicebuf&& other) = delete;
stack_devicebuf& operator=(const stack_devicebuf&) = delete;
stack_devicebuf& operator=(stack_devicebuf&&) = delete;
stack_devicebuf& operator=(const stack_devicebuf&) = delete;
stack_devicebuf(const stack_devicebuf& other):std::basic_streambuf<char>(),_stackbuf(other._stackbuf)
{}
stack_devicebuf(stack_devicebuf&& other):std::basic_streambuf<char>(),_stackbuf(std::move(other._stackbuf))
{
other.clear();
}
bufpair_t buf() const
{
@ -63,9 +68,16 @@ public:
stack_oss():std::ostream(&_dev) {}
~stack_oss() = default;
stack_oss(const stack_oss& other) = delete;
stack_oss(stack_oss&& other) = delete;
stack_oss& operator=(const stack_oss& other) = delete;
stack_oss& operator=(const stack_oss& other) = delete;
stack_oss& operator=(const stack_oss&& other) = delete;
stack_oss(const stack_oss& other):std::basic_ios<char>(), std::ostream(&_dev), _dev(other._dev)
{}
stack_oss(stack_oss&& other):std::basic_ios<char>(), std::ostream(&_dev), _dev(std::move(other._dev))
{
other.clear();
}
bufpair_t buf() const
{

View File

@ -29,7 +29,7 @@ class async_sink : public base_sink
public:
using queue_type = c11log::details::blocking_queue<std::unique_ptr<details::log_msg, std::function<void(details::log_msg*)>>>;
using queue_type = details::blocking_queue<std::unique_ptr<details::log_msg, std::function<void(details::log_msg*)>>>;
explicit async_sink(const queue_type::size_type max_queue_size);
@ -82,7 +82,7 @@ inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
if(!_active || !msg_size)
return;
//re allocate on the heap the (stack based) message
auto new_msg = new details::log_msg(msg);
details::log_msg* new_msg = new details::log_msg(msg);
char *buf = new char[msg_size];
std::memcpy(buf, msg.msg_buf.first, msg_size);