mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
astyle
This commit is contained in:
@@ -10,105 +10,102 @@
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
namespace c11log {
|
||||
namespace details {
|
||||
|
||||
template<typename T>
|
||||
class blocking_queue
|
||||
{
|
||||
class blocking_queue {
|
||||
public:
|
||||
using queue_t = std::queue<T>;
|
||||
using size_type = typename queue_t::size_type;
|
||||
using clock = std::chrono::system_clock;
|
||||
using queue_t = std::queue<T>;
|
||||
using size_type = typename queue_t::size_type;
|
||||
using clock = std::chrono::system_clock;
|
||||
|
||||
explicit blocking_queue(size_type max_size) :
|
||||
max_size_(max_size),
|
||||
q_(),
|
||||
mutex_()
|
||||
{}
|
||||
blocking_queue(const blocking_queue&) = delete;
|
||||
blocking_queue& operator=(const blocking_queue&) = delete;
|
||||
~blocking_queue() = default;
|
||||
explicit blocking_queue(size_type max_size) :
|
||||
max_size_(max_size),
|
||||
q_(),
|
||||
mutex_() {
|
||||
}
|
||||
blocking_queue(const blocking_queue&) = delete;
|
||||
blocking_queue& operator=(const blocking_queue&) = delete;
|
||||
~blocking_queue() = default;
|
||||
|
||||
size_type size() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return q_.size();
|
||||
}
|
||||
size_type size() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return q_.size();
|
||||
}
|
||||
|
||||
// Push copy of item into the back of the queue.
|
||||
// If the queue is full, block the calling thread util there is room or timeout have passed.
|
||||
// Return: false on timeout, true on successful push.
|
||||
template<typename Duration_Rep, typename Duration_Period, typename TT>
|
||||
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
if (q_.size() >= max_size_) {
|
||||
if (!item_popped_cond_.wait_until(ul, clock::now() + timeout, [this]() {
|
||||
return this->q_.size() < this->max_size_;
|
||||
}))
|
||||
return false;
|
||||
}
|
||||
q_.push(std::forward<TT>(item));
|
||||
if (q_.size() <= 1) {
|
||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
||||
item_pushed_cond_.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Push copy of item into the back of the queue.
|
||||
// If the queue is full, block the calling thread util there is room or timeout have passed.
|
||||
// Return: false on timeout, true on successful push.
|
||||
template<typename Duration_Rep, typename Duration_Period, typename TT>
|
||||
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
if (q_.size() >= max_size_) {
|
||||
if (!item_popped_cond_.wait_until(ul, clock::now() + timeout, [this]() {
|
||||
return this->q_.size() < this->max_size_;
|
||||
}))
|
||||
return false;
|
||||
}
|
||||
q_.push(std::forward<TT>(item));
|
||||
if (q_.size() <= 1) {
|
||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
||||
item_pushed_cond_.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Push copy of item into the back of the queue.
|
||||
// If the queue is full, block the calling thread until there is room.
|
||||
template<typename TT>
|
||||
void push(TT&& item) {
|
||||
while (!push(std::forward<TT>(item), one_hour));
|
||||
}
|
||||
// Push copy of item into the back of the queue.
|
||||
// If the queue is full, block the calling thread until there is room.
|
||||
template<typename TT>
|
||||
void push(TT&& item) {
|
||||
while (!push(std::forward<TT>(item), one_hour));
|
||||
}
|
||||
|
||||
// Pop a copy of the front item in the queue into the given item ref.
|
||||
// If the queue is empty, block the calling thread util there is item to pop or timeout have passed.
|
||||
// Return: false on timeout , true on successful pop/
|
||||
template<class Duration_Rep, class Duration_Period>
|
||||
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
if (q_.empty()) {
|
||||
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() {
|
||||
return !this->q_.empty();
|
||||
}))
|
||||
return false;
|
||||
}
|
||||
item = std::move(q_.front());
|
||||
q_.pop();
|
||||
if (q_.size() >= max_size_ - 1) {
|
||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
||||
item_popped_cond_.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Pop a copy of the front item in the queue into the given item ref.
|
||||
// If the queue is empty, block the calling thread util there is item to pop or timeout have passed.
|
||||
// Return: false on timeout , true on successful pop/
|
||||
template<class Duration_Rep, class Duration_Period>
|
||||
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
if (q_.empty()) {
|
||||
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() {
|
||||
return !this->q_.empty();
|
||||
}))
|
||||
return false;
|
||||
}
|
||||
item = std::move(q_.front());
|
||||
q_.pop();
|
||||
if (q_.size() >= max_size_ - 1) {
|
||||
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
||||
item_popped_cond_.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Pop a copy of the front item in the queue into the given item ref.
|
||||
// If the queue is empty, block the calling thread util there is item to pop.
|
||||
void pop(T& item) {
|
||||
while (!pop(item, one_hour));
|
||||
}
|
||||
// Pop a copy of the front item in the queue into the given item ref.
|
||||
// If the queue is empty, block the calling thread util there is item to pop.
|
||||
void pop(T& item) {
|
||||
while (!pop(item, one_hour));
|
||||
}
|
||||
|
||||
// Clear the queue
|
||||
void clear() {
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
queue_t().swap(q_);
|
||||
}
|
||||
item_popped_cond_.notify_all();
|
||||
}
|
||||
// Clear the queue
|
||||
void clear() {
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
queue_t().swap(q_);
|
||||
}
|
||||
item_popped_cond_.notify_all();
|
||||
}
|
||||
|
||||
private:
|
||||
size_type max_size_;
|
||||
std::queue<T> q_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable item_pushed_cond_;
|
||||
std::condition_variable item_popped_cond_;
|
||||
const std::chrono::hours one_hour {
|
||||
1
|
||||
};
|
||||
size_type max_size_;
|
||||
std::queue<T> q_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable item_pushed_cond_;
|
||||
std::condition_variable item_popped_cond_;
|
||||
const std::chrono::hours one_hour {
|
||||
1
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
@@ -24,20 +24,19 @@ private:
|
||||
|
||||
inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
||||
auto found = _loggers.find(name);
|
||||
if (found == _loggers.end()) {
|
||||
auto new_logger_ptr = std::make_shared<c11log::logger>(name);
|
||||
_loggers.insert(std::make_pair(name, new_logger_ptr));
|
||||
return new_logger_ptr;
|
||||
}
|
||||
else {
|
||||
return found->second;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
||||
auto found = _loggers.find(name);
|
||||
if (found == _loggers.end()) {
|
||||
auto new_logger_ptr = std::make_shared<c11log::logger>(name);
|
||||
_loggers.insert(std::make_pair(name, new_logger_ptr));
|
||||
return new_logger_ptr;
|
||||
} else {
|
||||
return found->second;
|
||||
}
|
||||
}
|
||||
|
||||
inline c11log::details::factory & c11log::details::factory::instance()
|
||||
{
|
||||
static c11log::details::factory instance;
|
||||
return instance;
|
||||
static c11log::details::factory instance;
|
||||
return instance;
|
||||
}
|
||||
|
@@ -2,80 +2,71 @@
|
||||
|
||||
#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& other):std::streambuf(),_str(other._str) {}
|
||||
str_devicebuf& operator=(const str_devicebuf other)
|
||||
{
|
||||
if(this != &other)
|
||||
_str = other._str;
|
||||
return *this;
|
||||
}
|
||||
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& 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;
|
||||
}
|
||||
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;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -4,50 +4,47 @@
|
||||
#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):
|
||||
_callback_logger(callback_logger),
|
||||
_oss(),
|
||||
_level(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(logger* callback_logger, level::level_enum msg_level):
|
||||
_callback_logger(callback_logger),
|
||||
_oss(),
|
||||
_level(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& operator=(const line_logger&) = delete;
|
||||
|
||||
~line_logger() {
|
||||
if (_callback_logger) {
|
||||
_oss << '\n';
|
||||
_callback_logger->log_it_(_oss.str_ref());
|
||||
}
|
||||
}
|
||||
~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;
|
||||
level::level_enum _level;
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
level::level_enum _level;
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
|
@@ -1,17 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
namespace c11log{
|
||||
namespace details{
|
||||
struct null_mutex
|
||||
{
|
||||
void lock()
|
||||
{}
|
||||
void unlock()
|
||||
{}
|
||||
bool try_lock()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
namespace c11log {
|
||||
namespace details {
|
||||
struct null_mutex {
|
||||
void lock() {
|
||||
}
|
||||
void unlock() {
|
||||
}
|
||||
bool try_lock() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@@ -3,17 +3,14 @@
|
||||
#include<cstdio>
|
||||
#include<ctime>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
namespace os
|
||||
{
|
||||
std::tm localtime(const std::time_t &time_tt);
|
||||
std::tm localtime();
|
||||
|
||||
}
|
||||
}
|
||||
namespace c11log {
|
||||
namespace details {
|
||||
namespace os {
|
||||
std::tm localtime(const std::time_t &time_tt);
|
||||
std::tm localtime();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +21,7 @@ inline std::tm c11log::details::os::localtime(const std::time_t &time_tt)
|
||||
#ifdef _MSC_VER
|
||||
localtime_s(&tm, &time_tt);
|
||||
#else
|
||||
localtime_r(&time_tt, &tm);
|
||||
localtime_r(&time_tt, &tm);
|
||||
#endif
|
||||
return tm;
|
||||
}
|
||||
|
Reference in New Issue
Block a user