mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
astyle+dos2unix
This commit is contained in:
@@ -1,125 +1,125 @@
|
||||
#pragma once
|
||||
|
||||
// blocking_queue:
|
||||
// A blocking multi-consumer/multi-producer thread safe queue.
|
||||
// Has max capacity and supports timeout on push or pop operations.
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class blocking_queue
|
||||
{
|
||||
public:
|
||||
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;
|
||||
|
||||
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 until there is room.
|
||||
template<typename TT>
|
||||
void push(TT&& item)
|
||||
{
|
||||
while (!push(std::forward<TT>(item), std::chrono::hours(1)));
|
||||
}
|
||||
|
||||
// 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, std::chrono::hours(1)));
|
||||
}
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
#pragma once
|
||||
|
||||
// blocking_queue:
|
||||
// A blocking multi-consumer/multi-producer thread safe queue.
|
||||
// Has max capacity and supports timeout on push or pop operations.
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <queue>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
template<typename T>
|
||||
class blocking_queue
|
||||
{
|
||||
public:
|
||||
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;
|
||||
|
||||
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 until there is room.
|
||||
template<typename TT>
|
||||
void push(TT&& item)
|
||||
{
|
||||
while (!push(std::forward<TT>(item), std::chrono::hours(1)));
|
||||
}
|
||||
|
||||
// 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, std::chrono::hours(1)));
|
||||
}
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,61 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
class logger;
|
||||
namespace details
|
||||
{
|
||||
class factory
|
||||
{
|
||||
public:
|
||||
using logger_ptr = std::shared_ptr<c11log::logger>;
|
||||
using logger_map = std::unordered_map<std::string, logger_ptr>;
|
||||
void add_logger(const std::string& name, logger_ptr);
|
||||
logger_ptr get_logger(const std::string &name);
|
||||
static c11log::details::factory& instance();
|
||||
private:
|
||||
std::mutex _loggers_mutex;
|
||||
logger_map _loggers;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void c11log::details::factory::add_logger(const std::string& name, logger_ptr logger_p)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
||||
_loggers.insert(logger_map::value_type(name, logger_p));
|
||||
}
|
||||
|
||||
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())
|
||||
return found->second;
|
||||
else
|
||||
return logger_ptr(nullptr);
|
||||
/*
|
||||
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;
|
||||
}
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
class logger;
|
||||
namespace details
|
||||
{
|
||||
class factory
|
||||
{
|
||||
public:
|
||||
using logger_ptr = std::shared_ptr<c11log::logger>;
|
||||
using logger_map = std::unordered_map<std::string, logger_ptr>;
|
||||
void add_logger(const std::string& name, logger_ptr);
|
||||
logger_ptr get_logger(const std::string &name);
|
||||
static c11log::details::factory& instance();
|
||||
private:
|
||||
std::mutex _loggers_mutex;
|
||||
logger_map _loggers;
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void c11log::details::factory::add_logger(const std::string& name, logger_ptr logger_p)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
||||
_loggers.insert(logger_map::value_type(name, logger_p));
|
||||
}
|
||||
|
||||
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())
|
||||
return found->second;
|
||||
else
|
||||
return logger_ptr(nullptr);
|
||||
/*
|
||||
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;
|
||||
}
|
||||
|
@@ -21,10 +21,10 @@ public:
|
||||
fast_buf():_stack_size(0) {}
|
||||
~fast_buf() {};
|
||||
|
||||
fast_buf(const bufpair_t& buf_to_copy):fast_buf()
|
||||
{
|
||||
append(buf_to_copy);
|
||||
}
|
||||
fast_buf(const bufpair_t& buf_to_copy):fast_buf()
|
||||
{
|
||||
append(buf_to_copy);
|
||||
}
|
||||
|
||||
fast_buf(const fast_buf& other)
|
||||
{
|
||||
@@ -46,7 +46,7 @@ public:
|
||||
}
|
||||
|
||||
fast_buf& operator=(const fast_buf& other) = delete;
|
||||
fast_buf& operator=(fast_buf&& other) = delete;
|
||||
fast_buf& operator=(fast_buf&& other) = delete;
|
||||
|
||||
void append(const char* buf, std::size_t size)
|
||||
{
|
||||
@@ -66,7 +66,7 @@ public:
|
||||
//Not enough stack space. Copy all to _v
|
||||
else
|
||||
{
|
||||
_v.reserve(_stack_size+size);
|
||||
_v.reserve(_stack_size+size);
|
||||
if(_stack_size)
|
||||
_v.insert(_v.end(), _stack_buf.begin(), _stack_buf.begin() +_stack_size);
|
||||
_v.insert(_v.end(), buf, buf+size);
|
||||
@@ -74,10 +74,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void append(const bufpair_t &buf)
|
||||
{
|
||||
append(buf.first, buf.second);
|
||||
}
|
||||
void append(const bufpair_t &buf)
|
||||
{
|
||||
append(buf.first, buf.second);
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
@@ -96,7 +96,7 @@ public:
|
||||
private:
|
||||
std::vector<char> _v;
|
||||
std::array<char, STACK_SIZE> _stack_buf;
|
||||
std::size_t _stack_size;
|
||||
std::size_t _stack_size;
|
||||
};
|
||||
|
||||
}
|
||||
|
@@ -1,106 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
// Faster than ostringstream--returns its string by ref
|
||||
|
||||
#include "c11log/details/fast_buf.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
class str_devicebuf:public std::streambuf
|
||||
{
|
||||
public:
|
||||
str_devicebuf() = default;
|
||||
~str_devicebuf() = default;
|
||||
|
||||
str_devicebuf(const str_devicebuf& other) = delete;
|
||||
str_devicebuf(str_devicebuf&& other) = delete;
|
||||
str_devicebuf& operator=(const str_devicebuf&) = delete;
|
||||
str_devicebuf& operator=(str_devicebuf&&) = delete;
|
||||
|
||||
/*
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _str;
|
||||
}
|
||||
*/
|
||||
bufpair_t buf()
|
||||
{
|
||||
return _fastbuf.get();
|
||||
}
|
||||
|
||||
void reset_str()
|
||||
{
|
||||
//_str.clear();
|
||||
_fastbuf.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
int sync() override
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// copy the give buffer into the accumulated string.
|
||||
// reserve initially 128 bytes which should be enough for common log lines
|
||||
std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
||||
{
|
||||
/*
|
||||
if(_str.capacity() < k_initial_reserve)
|
||||
{
|
||||
_str.reserve(k_initial_reserve);
|
||||
}
|
||||
_str.append(s, static_cast<unsigned int>(count));
|
||||
*/
|
||||
_fastbuf.append(s, static_cast<unsigned int>(count));
|
||||
return count;
|
||||
}
|
||||
|
||||
int_type overflow(int_type ch) override
|
||||
{
|
||||
|
||||
bool not_eofile = traits_type::not_eof(ch);
|
||||
if (not_eofile)
|
||||
{
|
||||
char c = traits_type::to_char_type(ch);
|
||||
xsputn(&c, 1);
|
||||
}
|
||||
return not_eofile;
|
||||
}
|
||||
private:
|
||||
//std::string _str;
|
||||
fast_buf<192> _fastbuf;
|
||||
};
|
||||
|
||||
class fast_oss:public std::ostream
|
||||
{
|
||||
public:
|
||||
fast_oss():std::ostream(&_dev) {}
|
||||
~fast_oss() = default;
|
||||
|
||||
fast_oss(const fast_oss& other) = delete;
|
||||
fast_oss(fast_oss&& other) = delete;
|
||||
fast_oss& operator=(const fast_oss& other) = delete;
|
||||
/*
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
*/
|
||||
bufpair_t buf()
|
||||
{
|
||||
return _dev.buf();
|
||||
}
|
||||
|
||||
void reset_str()
|
||||
{
|
||||
_dev.reset_str();
|
||||
}
|
||||
|
||||
private:
|
||||
str_devicebuf _dev;
|
||||
};
|
||||
}
|
||||
}
|
||||
#pragma once
|
||||
|
||||
// Faster than ostringstream--returns its string by ref
|
||||
|
||||
#include "c11log/details/fast_buf.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
|
||||
class str_devicebuf:public std::streambuf
|
||||
{
|
||||
public:
|
||||
str_devicebuf() = default;
|
||||
~str_devicebuf() = default;
|
||||
|
||||
str_devicebuf(const str_devicebuf& other) = delete;
|
||||
str_devicebuf(str_devicebuf&& other) = delete;
|
||||
str_devicebuf& operator=(const str_devicebuf&) = delete;
|
||||
str_devicebuf& operator=(str_devicebuf&&) = delete;
|
||||
|
||||
/*
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _str;
|
||||
}
|
||||
*/
|
||||
bufpair_t buf()
|
||||
{
|
||||
return _fastbuf.get();
|
||||
}
|
||||
|
||||
void reset_str()
|
||||
{
|
||||
//_str.clear();
|
||||
_fastbuf.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
int sync() override
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// copy the give buffer into the accumulated string.
|
||||
// reserve initially 128 bytes which should be enough for common log lines
|
||||
std::streamsize xsputn(const char_type* s, std::streamsize count) override
|
||||
{
|
||||
/*
|
||||
if(_str.capacity() < k_initial_reserve)
|
||||
{
|
||||
_str.reserve(k_initial_reserve);
|
||||
}
|
||||
_str.append(s, static_cast<unsigned int>(count));
|
||||
*/
|
||||
_fastbuf.append(s, static_cast<unsigned int>(count));
|
||||
return count;
|
||||
}
|
||||
|
||||
int_type overflow(int_type ch) override
|
||||
{
|
||||
|
||||
bool not_eofile = traits_type::not_eof(ch);
|
||||
if (not_eofile)
|
||||
{
|
||||
char c = traits_type::to_char_type(ch);
|
||||
xsputn(&c, 1);
|
||||
}
|
||||
return not_eofile;
|
||||
}
|
||||
private:
|
||||
//std::string _str;
|
||||
fast_buf<192> _fastbuf;
|
||||
};
|
||||
|
||||
class fast_oss:public std::ostream
|
||||
{
|
||||
public:
|
||||
fast_oss():std::ostream(&_dev) {}
|
||||
~fast_oss() = default;
|
||||
|
||||
fast_oss(const fast_oss& other) = delete;
|
||||
fast_oss(fast_oss&& other) = delete;
|
||||
fast_oss& operator=(const fast_oss& other) = delete;
|
||||
/*
|
||||
const std::string& str_ref() const
|
||||
{
|
||||
return _dev.str_ref();
|
||||
}
|
||||
*/
|
||||
bufpair_t buf()
|
||||
{
|
||||
return _dev.buf();
|
||||
}
|
||||
|
||||
void reset_str()
|
||||
{
|
||||
_dev.reset_str();
|
||||
}
|
||||
|
||||
private:
|
||||
str_devicebuf _dev;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -1,75 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include "../common_types.h"
|
||||
#include "../logger.h"
|
||||
#include "fast_oss.h"
|
||||
|
||||
|
||||
// line logger class. should be used by the logger as an rvalue only.
|
||||
// aggregates logging string until the end of the line and then calls the logger upon destruction
|
||||
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
//class logger;
|
||||
namespace details
|
||||
{
|
||||
|
||||
class line_logger
|
||||
{
|
||||
public:
|
||||
line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
|
||||
_callback_logger(callback_logger),
|
||||
_oss(),
|
||||
_level(msg_level),
|
||||
_enabled(enabled)
|
||||
{
|
||||
if(enabled)
|
||||
{
|
||||
callback_logger->_formatter->format_header(callback_logger->_logger_name,
|
||||
msg_level,
|
||||
log_clock::now(),
|
||||
_oss);
|
||||
}
|
||||
}
|
||||
|
||||
// No copy intended. Only move
|
||||
line_logger(const line_logger& other) = delete;
|
||||
line_logger& operator=(const line_logger&) = delete;
|
||||
line_logger& operator=(line_logger&&) = delete;
|
||||
|
||||
line_logger(line_logger&& other) :
|
||||
_callback_logger(other._callback_logger),
|
||||
// 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 string from the other
|
||||
_oss(),
|
||||
_level(other._level) {};
|
||||
|
||||
|
||||
~line_logger()
|
||||
{
|
||||
if (_enabled)
|
||||
{
|
||||
_oss << os::eol();
|
||||
_callback_logger->_log_it(_oss.buf(), _level);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
line_logger&& operator<<(const T& msg)
|
||||
{
|
||||
if (_enabled)
|
||||
_oss << msg;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
level::level_enum _level;
|
||||
bool _enabled;
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
} // Namespace c11log
|
||||
#pragma once
|
||||
|
||||
#include "../common_types.h"
|
||||
#include "../logger.h"
|
||||
#include "fast_oss.h"
|
||||
|
||||
|
||||
// line logger class. should be used by the logger as an rvalue only.
|
||||
// aggregates logging string until the end of the line and then calls the logger upon destruction
|
||||
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
//class logger;
|
||||
namespace details
|
||||
{
|
||||
|
||||
class line_logger
|
||||
{
|
||||
public:
|
||||
line_logger(logger* callback_logger, level::level_enum msg_level, bool enabled):
|
||||
_callback_logger(callback_logger),
|
||||
_oss(),
|
||||
_level(msg_level),
|
||||
_enabled(enabled)
|
||||
{
|
||||
if(enabled)
|
||||
{
|
||||
callback_logger->_formatter->format_header(callback_logger->_logger_name,
|
||||
msg_level,
|
||||
log_clock::now(),
|
||||
_oss);
|
||||
}
|
||||
}
|
||||
|
||||
// No copy intended. Only move
|
||||
line_logger(const line_logger& other) = delete;
|
||||
line_logger& operator=(const line_logger&) = delete;
|
||||
line_logger& operator=(line_logger&&) = delete;
|
||||
|
||||
line_logger(line_logger&& other) :
|
||||
_callback_logger(other._callback_logger),
|
||||
// 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 string from the other
|
||||
_oss(),
|
||||
_level(other._level) {};
|
||||
|
||||
|
||||
~line_logger()
|
||||
{
|
||||
if (_enabled)
|
||||
{
|
||||
_oss << os::eol();
|
||||
_callback_logger->_log_it(_oss.buf(), _level);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
line_logger&& operator<<(const T& msg)
|
||||
{
|
||||
if (_enabled)
|
||||
_oss << msg;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
logger* _callback_logger;
|
||||
details::fast_oss _oss;
|
||||
level::level_enum _level;
|
||||
bool _enabled;
|
||||
|
||||
};
|
||||
} //Namespace details
|
||||
} // Namespace c11log
|
||||
|
@@ -1,63 +1,63 @@
|
||||
#pragma once
|
||||
#include<string>
|
||||
#include<cstdio>
|
||||
#include<ctime>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
namespace os
|
||||
{
|
||||
|
||||
inline std::tm localtime(const std::time_t &time_tt)
|
||||
{
|
||||
|
||||
std::tm tm;
|
||||
#ifdef _WIN32
|
||||
localtime_s(&tm, &time_tt);
|
||||
#else
|
||||
localtime_r(&time_tt, &tm);
|
||||
#endif
|
||||
return tm;
|
||||
}
|
||||
|
||||
inline std::tm localtime()
|
||||
{
|
||||
std::time_t now_t = time(0);
|
||||
return localtime(now_t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
inline bool operator==(const std::tm& tm1, const std::tm& tm2)
|
||||
{
|
||||
return (tm1.tm_sec == tm2.tm_sec &&
|
||||
tm1.tm_min == tm2.tm_min &&
|
||||
tm1.tm_hour == tm2.tm_hour &&
|
||||
tm1.tm_mday == tm2.tm_mday &&
|
||||
tm1.tm_mon == tm2.tm_mon &&
|
||||
tm1.tm_year == tm2.tm_year &&
|
||||
tm1.tm_isdst == tm2.tm_isdst);
|
||||
}
|
||||
|
||||
inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
|
||||
{
|
||||
return !(tm1==tm2);
|
||||
}
|
||||
|
||||
constexpr inline const char* eol()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return "\r\n";
|
||||
#else
|
||||
return "\n";
|
||||
#endif
|
||||
}
|
||||
} //os
|
||||
} //details
|
||||
} //c11log
|
||||
|
||||
|
||||
|
||||
#pragma once
|
||||
#include<string>
|
||||
#include<cstdio>
|
||||
#include<ctime>
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
namespace details
|
||||
{
|
||||
namespace os
|
||||
{
|
||||
|
||||
inline std::tm localtime(const std::time_t &time_tt)
|
||||
{
|
||||
|
||||
std::tm tm;
|
||||
#ifdef _WIN32
|
||||
localtime_s(&tm, &time_tt);
|
||||
#else
|
||||
localtime_r(&time_tt, &tm);
|
||||
#endif
|
||||
return tm;
|
||||
}
|
||||
|
||||
inline std::tm localtime()
|
||||
{
|
||||
std::time_t now_t = time(0);
|
||||
return localtime(now_t);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
inline bool operator==(const std::tm& tm1, const std::tm& tm2)
|
||||
{
|
||||
return (tm1.tm_sec == tm2.tm_sec &&
|
||||
tm1.tm_min == tm2.tm_min &&
|
||||
tm1.tm_hour == tm2.tm_hour &&
|
||||
tm1.tm_mday == tm2.tm_mday &&
|
||||
tm1.tm_mon == tm2.tm_mon &&
|
||||
tm1.tm_year == tm2.tm_year &&
|
||||
tm1.tm_isdst == tm2.tm_isdst);
|
||||
}
|
||||
|
||||
inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
|
||||
{
|
||||
return !(tm1==tm2);
|
||||
}
|
||||
|
||||
constexpr inline const char* eol()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return "\r\n";
|
||||
#else
|
||||
return "\n";
|
||||
#endif
|
||||
}
|
||||
} //os
|
||||
} //details
|
||||
} //c11log
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user