mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
removed un needed mutex, and added copy ctors to line logger and friends
This commit is contained in:
parent
514526ed7a
commit
d60e971dec
@ -1,6 +1,6 @@
|
||||
SRC_DIR=../../src
|
||||
|
||||
_SOURCES = factory.cpp formatters.cpp line_logger.cpp os.cpp
|
||||
_SOURCES = factory.cpp formatters.cpp os.cpp
|
||||
|
||||
SOURCES = $(patsubst %,$(SRC_DIR)/%,$(_SOURCES))
|
||||
OBJS_RELEASE = $(patsubst %.cpp,release/%.o,$(_SOURCES))
|
||||
@ -8,7 +8,7 @@ OBJS_DEBUG = $(patsubst %.cpp,debug/%.o,$(_SOURCES))
|
||||
|
||||
|
||||
CXX = g++
|
||||
CXXFLAGS = -march=native -Wall -std=c++11 -pthread -I../../include
|
||||
CXXFLAGS = -march=native -Wall -Wextra -Wshadow -pedantic -std=c++11 -pthread -I../../include
|
||||
CXX_RELEASE_FLAGS = -O2 -flto -g
|
||||
CXX_DEBUG_FLAGS= -g
|
||||
|
||||
|
@ -2,40 +2,43 @@
|
||||
|
||||
#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;
|
||||
|
||||
const std::string& str_ref() const
|
||||
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;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
void clear() {
|
||||
_str.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual int sync() override
|
||||
{
|
||||
virtual int sync() override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
||||
{
|
||||
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
|
||||
{
|
||||
virtual int_type overflow(int_type ch) override {
|
||||
if (ch != traits_type::eof())
|
||||
_str.append((char*)&ch, 1);
|
||||
return 1;
|
||||
@ -44,13 +47,18 @@ private:
|
||||
std::string _str;
|
||||
};
|
||||
|
||||
class fast_oss:public std::ostream {
|
||||
class fast_oss:public std::ostream
|
||||
{
|
||||
public:
|
||||
fast_oss():std::ostream(&_dev){}
|
||||
fast_oss():std::ostream(&_dev) {}
|
||||
~fast_oss() = default;
|
||||
fast_oss(const fast_oss&) = delete;
|
||||
fast_oss operator=(const fast_oss&) = delete;
|
||||
|
||||
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
|
||||
{
|
||||
|
@ -1,22 +1,39 @@
|
||||
#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)
|
||||
{
|
||||
line_logger& operator<<(const T& msg) {
|
||||
if (_callback_logger)
|
||||
_oss << msg;
|
||||
return *this;
|
||||
@ -25,6 +42,7 @@ public:
|
||||
private:
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
level::level_enum _level;
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
|
@ -10,23 +10,24 @@
|
||||
|
||||
#include "level.h"
|
||||
#include "sinks/base_sink.h"
|
||||
#include "details/line_logger.h"
|
||||
#include "details/factory.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
class line_logger;
|
||||
}
|
||||
|
||||
|
||||
|
||||
namespace c11log {
|
||||
|
||||
class logger {
|
||||
class logger
|
||||
{
|
||||
public:
|
||||
|
||||
typedef std::shared_ptr<sinks::base_sink> sink_ptr_t;
|
||||
typedef std::vector<sink_ptr_t> sinks_vector_t;
|
||||
|
||||
explicit logger(const std::string& name) : logger_name_(name),
|
||||
formatter_(std::make_unique<formatters::default_formatter>())
|
||||
{
|
||||
formatter_(std::make_unique<formatters::default_formatter>()) {
|
||||
atomic_level_.store(level::INFO);
|
||||
}
|
||||
|
||||
@ -68,19 +69,16 @@ private:
|
||||
logger& get_logger(const std::string& name);
|
||||
}
|
||||
|
||||
#include "details/line_logger.h"
|
||||
//
|
||||
// Logger inline impl
|
||||
//
|
||||
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
|
||||
{
|
||||
|
||||
if (msg_level >= atomic_level_.load())
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (msg_level >= atomic_level_) {
|
||||
return details::line_logger(this, msg_level);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return details::line_logger(nullptr);
|
||||
}
|
||||
|
||||
|
@ -37,7 +37,7 @@ protected:
|
||||
|
||||
class null_sink:public base_sink {
|
||||
protected:
|
||||
void sink_it_(const std::string& msg) override
|
||||
void sink_it_(const std::string& ) override
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
@ -17,7 +17,8 @@
|
||||
|
||||
|
||||
#ifndef _MSC_VER
|
||||
namespace std {
|
||||
namespace std
|
||||
{
|
||||
template<typename T, typename ...Args>
|
||||
std::unique_ptr<T> make_unique( Args&& ...args )
|
||||
{
|
||||
|
@ -19,5 +19,6 @@ c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const
|
||||
c11log::details::factory & c11log::details::factory::instance()
|
||||
{
|
||||
static c11log::details::factory instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
@ -1,21 +0,0 @@
|
||||
#include "stdafx.h"
|
||||
#include "c11log/logger.h"
|
||||
|
||||
c11log::details::line_logger::line_logger(logger* callback_logger, level::level_enum msg_level) :
|
||||
_callback_logger(callback_logger)
|
||||
{
|
||||
if (callback_logger) {
|
||||
callback_logger->formatter_->format_header(callback_logger->logger_name_,
|
||||
msg_level,
|
||||
c11log::formatters::clock::now(),
|
||||
_oss);
|
||||
}
|
||||
}
|
||||
|
||||
c11log::details::line_logger::~line_logger()
|
||||
{
|
||||
if (_callback_logger) {
|
||||
_oss << '\n';
|
||||
_callback_logger->log_it_(_oss.str_ref());
|
||||
}
|
||||
}
|
@ -20,7 +20,7 @@ using std::string;
|
||||
using std::chrono::seconds;
|
||||
using Q = c11log::details::blocking_queue<string>;
|
||||
|
||||
void pusher(Q* q)
|
||||
void pusher(Q* )
|
||||
{
|
||||
auto &logger = c11log::get_logger("async");
|
||||
while(active)
|
||||
@ -29,7 +29,6 @@ void pusher(Q* q)
|
||||
++push_count;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -88,13 +87,11 @@ int main(int argc, char* argv[])
|
||||
auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
|
||||
//auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
|
||||
|
||||
async->add_sink(fsink);
|
||||
async->add_sink(null_sink);
|
||||
auto &logger = c11log::get_logger("async");
|
||||
logger.add_sink(async);
|
||||
|
||||
testq(qsize, pushers, poppers);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user