mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
remode stack_oss
This commit is contained in:
@@ -12,10 +12,10 @@ public:
|
||||
explicit file_flush_helper(const std::size_t flush_every):
|
||||
_flush_every(flush_every),
|
||||
_write_counter(0) {};
|
||||
|
||||
void write(const bufpair_t& msg, std::ofstream& ofs)
|
||||
|
||||
void write(const std::string& msg, std::ofstream& ofs)
|
||||
{
|
||||
ofs.write(msg.first, msg.second);
|
||||
ofs.write(msg.data(), msg.size());
|
||||
if(++_write_counter == _flush_every)
|
||||
{
|
||||
ofs.flush();
|
||||
|
@@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include "../common_types.h"
|
||||
#include "../logger.h"
|
||||
#include "stack_oss.h"
|
||||
|
||||
|
||||
// line_logger class.
|
||||
// aggregates single log line (on the stack if possibe) and calls the logger upon destruction
|
||||
@@ -30,7 +31,7 @@ public:
|
||||
_log_msg.msg_level,
|
||||
_log_msg.msg_time,
|
||||
_oss);
|
||||
_log_msg.msg_header_size = _oss.size();
|
||||
_log_msg.msg_header_size = _oss.str().size();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +44,7 @@ public:
|
||||
line_logger(line_logger&& other) :
|
||||
_callback_logger(other._callback_logger),
|
||||
_log_msg(std::move(other._log_msg)),
|
||||
_oss(std::move(other._oss)),
|
||||
_oss(std::move(other._oss.str())),
|
||||
_enabled(other._enabled),
|
||||
_empty(other._empty)
|
||||
{
|
||||
@@ -58,7 +59,7 @@ public:
|
||||
if (_enabled && !_empty)
|
||||
{
|
||||
_oss << os::eol();
|
||||
_log_msg.msg_buf = _oss.buf();
|
||||
_log_msg.str = _oss.str();
|
||||
_callback_logger->_log_it(_log_msg);
|
||||
}
|
||||
}
|
||||
@@ -90,7 +91,8 @@ public:
|
||||
private:
|
||||
logger* _callback_logger;
|
||||
log_msg _log_msg;
|
||||
details::stack_oss _oss;
|
||||
//details::stack_oss _oss;
|
||||
std::ostringstream _oss;
|
||||
bool _enabled;
|
||||
bool _empty;
|
||||
};
|
||||
|
@@ -7,12 +7,53 @@ namespace details
|
||||
struct log_msg
|
||||
{
|
||||
log_msg() = default;
|
||||
log_msg(level::level_enum l):msg_level(l) {};
|
||||
|
||||
bufpair_t msg_buf;
|
||||
log_clock::time_point msg_time;
|
||||
log_msg(level::level_enum l):
|
||||
msg_level(l),
|
||||
msg_time(),
|
||||
msg_header_size(0),
|
||||
str() {}
|
||||
|
||||
|
||||
log_msg(const log_msg& other):
|
||||
msg_level(other.msg_level),
|
||||
msg_time(other.msg_time),
|
||||
msg_header_size(other.msg_header_size),
|
||||
str(other.str) {}
|
||||
|
||||
log_msg(log_msg&& other):log_msg()
|
||||
{
|
||||
swap(*this, other);
|
||||
}
|
||||
|
||||
friend void swap(log_msg& l, log_msg& r)
|
||||
{
|
||||
using std::swap;
|
||||
swap(l.msg_level, r.msg_level);
|
||||
swap(l.msg_time, r.msg_time);
|
||||
swap(l.msg_header_size, r.msg_header_size);
|
||||
swap(l.str, r.str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
log_msg& operator=(log_msg other)
|
||||
{
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
void clear()
|
||||
{
|
||||
msg_header_size = 0;
|
||||
str.clear();
|
||||
}
|
||||
|
||||
level::level_enum msg_level;
|
||||
log_clock::time_point msg_time;
|
||||
std::size_t msg_header_size;
|
||||
level::level_enum msg_level;
|
||||
std::string str;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -1,110 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
// 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
|
||||
{
|
||||
|
||||
template<std::size_t STACK_SIZE=128>
|
||||
class stack_buf
|
||||
{
|
||||
public:
|
||||
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);
|
||||
}
|
||||
|
||||
stack_buf(const stack_buf& other)
|
||||
{
|
||||
_stack_size = other._stack_size;
|
||||
if(!other._v.empty())
|
||||
_v = other._v;
|
||||
else if(_stack_size)
|
||||
std::copy(other._stack_buf.begin(), other._stack_buf.begin()+_stack_size, _stack_buf.begin());
|
||||
}
|
||||
|
||||
stack_buf(stack_buf&& other)
|
||||
{
|
||||
_stack_size = other._stack_size;
|
||||
if(!other._v.empty())
|
||||
_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();
|
||||
}
|
||||
|
||||
void append(const char* buf, std::size_t buf_size)
|
||||
{
|
||||
//If we are aleady using _v, forget about the stack
|
||||
if(!_v.empty())
|
||||
{
|
||||
_v.insert(_v.end(), buf, buf+ buf_size);
|
||||
}
|
||||
//Try use the stack
|
||||
else
|
||||
{
|
||||
if(_stack_size+buf_size <= STACK_SIZE)
|
||||
{
|
||||
std::memcpy(&_stack_buf[_stack_size], buf, buf_size);
|
||||
_stack_size+=buf_size;
|
||||
}
|
||||
//Not enough stack space. Copy all to _v
|
||||
else
|
||||
{
|
||||
_v.reserve(_stack_size+buf_size);
|
||||
if(_stack_size)
|
||||
_v.insert(_v.end(), _stack_buf.begin(), _stack_buf.begin() +_stack_size);
|
||||
_v.insert(_v.end(), buf, buf+buf_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void append(const bufpair_t &buf)
|
||||
{
|
||||
append(buf.first, buf.second);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_stack_size = 0;
|
||||
_v.clear();
|
||||
}
|
||||
|
||||
bufpair_t get() const
|
||||
{
|
||||
if(!_v.empty())
|
||||
return bufpair_t(_v.data(), _v.size());
|
||||
else
|
||||
return bufpair_t(_stack_buf.data(), _stack_size);
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
if(!_v.empty())
|
||||
return _v.size();
|
||||
else
|
||||
return _stack_size;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<char> _v;
|
||||
std::array<char, STACK_SIZE> _stack_buf;
|
||||
std::size_t _stack_size;
|
||||
};
|
||||
|
||||
}
|
||||
} //namespace c11log { namespace details {
|
@@ -1,101 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
// Faster than ostringstream--returns its string by ref
|
||||
#include <ostream>
|
||||
#include "c11log/details/stack_buf.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
class stack_devicebuf:public std::streambuf
|
||||
{
|
||||
public:
|
||||
using Base = std::streambuf;
|
||||
stack_devicebuf() = default;
|
||||
~stack_devicebuf() = default;
|
||||
|
||||
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
|
||||
{
|
||||
return _stackbuf.get();
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return _stackbuf.size();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_stackbuf.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
// copy the give buffer into the accumulated fast buffer
|
||||
std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
||||
{
|
||||
_stackbuf.append(s, static_cast<unsigned int>(count));
|
||||
return count;
|
||||
}
|
||||
|
||||
int_type overflow(int_type ch) override
|
||||
{
|
||||
if (traits_type::not_eof(ch))
|
||||
{
|
||||
char c = traits_type::to_char_type(ch);
|
||||
xsputn(&c, 1);
|
||||
}
|
||||
return ch;
|
||||
}
|
||||
private:
|
||||
stack_buf<192> _stackbuf;
|
||||
};
|
||||
|
||||
class stack_oss:public std::ostream
|
||||
{
|
||||
public:
|
||||
stack_oss():std::ostream(&_dev) {}
|
||||
~stack_oss() = default;
|
||||
|
||||
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
|
||||
{
|
||||
return _dev.buf();
|
||||
}
|
||||
|
||||
std::size_t size() const
|
||||
{
|
||||
return _dev.size();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_dev.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
stack_devicebuf _dev;
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user