mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
removed un needed mutex, and added copy ctors to line logger and friends
This commit is contained in:
@@ -2,72 +2,80 @@
|
||||
|
||||
#include<streambuf>
|
||||
#include<string>
|
||||
namespace c11log {
|
||||
namespace details {
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
class str_devicebuf:public std::streambuf {
|
||||
class str_devicebuf:public std::streambuf
|
||||
{
|
||||
public:
|
||||
str_devicebuf() = default;
|
||||
~str_devicebuf() = default;
|
||||
str_devicebuf(const str_devicebuf&) = delete;
|
||||
str_devicebuf& operator=(const str_devicebuf&) = delete;
|
||||
str_devicebuf() = default;
|
||||
~str_devicebuf() = default;
|
||||
str_devicebuf(const str_devicebuf& other):std::streambuf(),_str(other._str) {}
|
||||
str_devicebuf& operator=(const str_devicebuf other)
|
||||
{
|
||||
if(this != &other)
|
||||
_str = other._str;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _str;
|
||||
}
|
||||
const std::string& str_ref() const {
|
||||
return _str;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_str.clear();
|
||||
}
|
||||
void clear() {
|
||||
_str.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual int sync() override
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
virtual int sync() override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
||||
{
|
||||
_str.append(s, static_cast<unsigned int>(count));
|
||||
return count;
|
||||
}
|
||||
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override {
|
||||
_str.append(s, static_cast<unsigned int>(count));
|
||||
return count;
|
||||
}
|
||||
|
||||
virtual int_type overflow(int_type ch) override
|
||||
{
|
||||
if (ch != traits_type::eof())
|
||||
_str.append((char*)&ch, 1);
|
||||
return 1;
|
||||
}
|
||||
virtual int_type overflow(int_type ch) override {
|
||||
if (ch != traits_type::eof())
|
||||
_str.append((char*)&ch, 1);
|
||||
return 1;
|
||||
}
|
||||
private:
|
||||
std::string _str;
|
||||
std::string _str;
|
||||
};
|
||||
|
||||
class fast_oss:public std::ostream {
|
||||
class fast_oss:public std::ostream
|
||||
{
|
||||
public:
|
||||
fast_oss():std::ostream(&_dev){}
|
||||
~fast_oss() = default;
|
||||
fast_oss(const fast_oss&) = delete;
|
||||
fast_oss operator=(const fast_oss&) = delete;
|
||||
|
||||
fast_oss():std::ostream(&_dev) {}
|
||||
~fast_oss() = default;
|
||||
fast_oss(const fast_oss& other):std::basic_ios<char>(), std::ostream(),_dev(other._dev) {}
|
||||
fast_oss operator=(const fast_oss& other)
|
||||
{
|
||||
if(&other != this)
|
||||
_dev = other._dev;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
|
||||
const std::string str() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
const std::string str() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_dev.clear();
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
_dev.clear();
|
||||
}
|
||||
private:
|
||||
str_devicebuf _dev;
|
||||
str_devicebuf _dev;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -1,30 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include "../level.h"
|
||||
#include "../logger.h"
|
||||
#include "fast_oss.h"
|
||||
|
||||
namespace c11log {
|
||||
namespace c11log
|
||||
{
|
||||
class logger;
|
||||
namespace details {
|
||||
namespace details
|
||||
{
|
||||
|
||||
class line_logger {
|
||||
class line_logger
|
||||
{
|
||||
public:
|
||||
line_logger(logger* callback_logger, level::level_enum msg_level);
|
||||
line_logger(logger* callback_logger):_callback_logger(nullptr) {};
|
||||
line_logger(const line_logger&){};
|
||||
~line_logger();
|
||||
line_logger(logger* callback_logger, level::level_enum msg_level) {
|
||||
callback_logger->formatter_->format_header(callback_logger->logger_name_,
|
||||
msg_level,
|
||||
c11log::formatters::clock::now(),
|
||||
_oss);
|
||||
}
|
||||
line_logger(logger*):_callback_logger(nullptr) {};
|
||||
line_logger(const line_logger& other):
|
||||
_callback_logger(other._callback_logger),
|
||||
_oss(other._oss),
|
||||
_level(other._level) {};
|
||||
line_logger& operator=(const line_logger&) = delete;
|
||||
~line_logger() {
|
||||
if (_callback_logger) {
|
||||
_oss << '\n';
|
||||
_callback_logger->log_it_(_oss.str_ref());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
line_logger& operator<<(const T& msg)
|
||||
{
|
||||
if (_callback_logger)
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
template<typename T>
|
||||
line_logger& operator<<(const T& msg) {
|
||||
if (_callback_logger)
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
level::level_enum _level;
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
|
Reference in New Issue
Block a user