underscore first in members
This commit is contained in:
parent
4c8fd99e27
commit
005dc7e605
@ -21,17 +21,17 @@ public:
|
||||
using clock = std::chrono::system_clock;
|
||||
|
||||
explicit blocking_queue(size_type max_size) :
|
||||
max_size_(max_size),
|
||||
q_(),
|
||||
mutex_() {
|
||||
_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();
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _q.size();
|
||||
}
|
||||
|
||||
// Push copy of item into the back of the queue.
|
||||
@ -39,17 +39,17 @@ public:
|
||||
// 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_;
|
||||
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) {
|
||||
_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();
|
||||
_item_pushed_cond.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -66,18 +66,18 @@ public:
|
||||
// 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();
|
||||
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) {
|
||||
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();
|
||||
_item_popped_cond.notify_one();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -91,18 +91,18 @@ public:
|
||||
// Clear the queue
|
||||
void clear() {
|
||||
{
|
||||
std::unique_lock<std::mutex> ul(mutex_);
|
||||
queue_t().swap(q_);
|
||||
std::unique_lock<std::mutex> ul(_mutex);
|
||||
queue_t().swap(_q);
|
||||
}
|
||||
item_popped_cond_.notify_all();
|
||||
_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_;
|
||||
size_type _max_size;
|
||||
std::queue<T> _q;
|
||||
std::mutex _mutex;
|
||||
std::condition_variable _item_pushed_cond;
|
||||
std::condition_variable _item_popped_cond;
|
||||
static constexpr auto one_hour = std::chrono::hours(1);
|
||||
};
|
||||
|
||||
|
@ -14,7 +14,7 @@ public:
|
||||
_callback_logger(callback_logger),
|
||||
_oss(),
|
||||
_level(msg_level) {
|
||||
callback_logger->formatter_->format_header(callback_logger->logger_name_,
|
||||
callback_logger->_formatter->format_header(callback_logger->_logger_name,
|
||||
msg_level,
|
||||
c11log::formatters::clock::now(),
|
||||
_oss);
|
||||
@ -30,7 +30,7 @@ public:
|
||||
~line_logger() {
|
||||
if (_callback_logger) {
|
||||
_oss << '\n';
|
||||
_callback_logger->log_it_(_oss.str_ref());
|
||||
_callback_logger->_log_it(_oss.str_ref());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,11 +24,11 @@ public:
|
||||
typedef std::vector<sink_ptr_t> sinks_vector_t;
|
||||
|
||||
explicit logger(const std::string& name) :
|
||||
logger_name_(name),
|
||||
formatter_(new formatters::default_formatter()),
|
||||
sinks_(),
|
||||
mutex_(),
|
||||
atomic_level_(level::INFO) {
|
||||
_logger_name(name),
|
||||
_formatter(new formatters::default_formatter()),
|
||||
_sinks(),
|
||||
_mutex(),
|
||||
_atomic_level(level::INFO) {
|
||||
}
|
||||
|
||||
~logger() = default;
|
||||
@ -55,13 +55,13 @@ public:
|
||||
private:
|
||||
friend details::line_logger;
|
||||
|
||||
std::string logger_name_ = "";
|
||||
std::unique_ptr<c11log::formatters::formatter> formatter_;
|
||||
sinks_vector_t sinks_;
|
||||
std::mutex mutex_;
|
||||
std::atomic_int atomic_level_;
|
||||
std::string _logger_name = "";
|
||||
std::unique_ptr<c11log::formatters::formatter> _formatter;
|
||||
sinks_vector_t _sinks;
|
||||
std::mutex _mutex;
|
||||
std::atomic_int _atomic_level;
|
||||
|
||||
void log_it_(const std::string& msg);
|
||||
void _log_it(const std::string& msg);
|
||||
|
||||
};
|
||||
|
||||
@ -75,7 +75,7 @@ logger& get_logger(const std::string& name);
|
||||
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
|
||||
{
|
||||
|
||||
if (msg_level >= atomic_level_)
|
||||
if (msg_level >= _atomic_level)
|
||||
return details::line_logger(this, msg_level);
|
||||
else
|
||||
return details::line_logger(nullptr);
|
||||
@ -104,53 +104,53 @@ inline c11log::details::line_logger c11log::logger::fatal()
|
||||
|
||||
inline void c11log::logger::set_name(const std::string& name)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
logger_name_ = name;
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_logger_name = name;
|
||||
}
|
||||
|
||||
inline const std::string& c11log::logger::get_name()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return logger_name_;
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
return _logger_name;
|
||||
}
|
||||
|
||||
inline void c11log::logger::add_sink(sink_ptr_t sink_ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sinks_.push_back(sink_ptr);
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_sinks.push_back(sink_ptr);
|
||||
}
|
||||
|
||||
inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink_ptr), sinks_.end());
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
|
||||
}
|
||||
|
||||
inline void c11log::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
formatter_ = std::move(formatter);
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_formatter = std::move(formatter);
|
||||
}
|
||||
|
||||
inline void c11log::logger::set_level(c11log::level::level_enum level)
|
||||
{
|
||||
atomic_level_.store(level);
|
||||
_atomic_level.store(level);
|
||||
}
|
||||
|
||||
inline c11log::level::level_enum c11log::logger::get_level() const
|
||||
{
|
||||
return static_cast<c11log::level::level_enum>(atomic_level_.load());
|
||||
return static_cast<c11log::level::level_enum>(_atomic_level.load());
|
||||
}
|
||||
|
||||
inline bool c11log::logger::should_log(c11log::level::level_enum level) const
|
||||
{
|
||||
return level >= atomic_level_.load();
|
||||
return level >= _atomic_level.load();
|
||||
}
|
||||
inline void c11log::logger::log_it_(const std::string& msg)
|
||||
inline void c11log::logger::_log_it(const std::string& msg)
|
||||
{
|
||||
level::level_enum level = static_cast<level::level_enum>(atomic_level_.load());
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
for (auto &sink : sinks_)
|
||||
level::level_enum level = static_cast<level::level_enum>(_atomic_level.load());
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
for (auto &sink : _sinks)
|
||||
sink->log(msg, level);
|
||||
}
|
||||
|
||||
|
@ -25,16 +25,16 @@ public:
|
||||
|
||||
|
||||
protected:
|
||||
void sink_it_(const std::string& msg) override;
|
||||
void thread_loop_();
|
||||
void _sink_it(const std::string& msg) override;
|
||||
void _thread_loop();
|
||||
|
||||
private:
|
||||
c11log::logger::sinks_vector_t sinks_;
|
||||
std::atomic<bool> active_;
|
||||
c11log::details::blocking_queue<std::string> q_;
|
||||
std::thread back_thread_;
|
||||
//Clear all remaining messages(if any), stop the back_thread_ and join it
|
||||
void shutdown_();
|
||||
c11log::logger::sinks_vector_t _sinks;
|
||||
std::atomic<bool> _active;
|
||||
c11log::details::blocking_queue<std::string> _q;
|
||||
std::thread _back_thread;
|
||||
//Clear all remaining messages(if any), stop the _back_thread and join it
|
||||
void _shutdown();
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -44,31 +44,31 @@ private:
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
|
||||
:sinks_(),
|
||||
active_(true),
|
||||
q_(max_queue_size),
|
||||
back_thread_(&async_sink::thread_loop_, this)
|
||||
:_sinks(),
|
||||
_active(true),
|
||||
_q(max_queue_size),
|
||||
_back_thread(&async_sink::_thread_loop, this)
|
||||
{}
|
||||
|
||||
inline c11log::sinks::async_sink::~async_sink()
|
||||
{
|
||||
shutdown_();
|
||||
_shutdown();
|
||||
}
|
||||
inline void c11log::sinks::async_sink::sink_it_(const std::string& msg)
|
||||
inline void c11log::sinks::async_sink::_sink_it(const std::string& msg)
|
||||
{
|
||||
q_.push(msg);
|
||||
_q.push(msg);
|
||||
}
|
||||
|
||||
inline void c11log::sinks::async_sink::thread_loop_()
|
||||
inline void c11log::sinks::async_sink::_thread_loop()
|
||||
{
|
||||
constexpr auto pop_timeout = std::chrono::seconds(1);
|
||||
std::string msg;
|
||||
|
||||
while (active_) {
|
||||
if (q_.pop(msg, pop_timeout)) {
|
||||
for (auto &sink : sinks_) {
|
||||
while (_active) {
|
||||
if (_q.pop(msg, pop_timeout)) {
|
||||
for (auto &sink : _sinks) {
|
||||
sink->log(msg, static_cast<level::level_enum>(_level.load()));
|
||||
if (!active_)
|
||||
if (!_active)
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -77,30 +77,30 @@ inline void c11log::sinks::async_sink::thread_loop_()
|
||||
|
||||
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr_t sink)
|
||||
{
|
||||
sinks_.push_back(sink);
|
||||
_sinks.push_back(sink);
|
||||
}
|
||||
|
||||
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr_t sink_ptr)
|
||||
{
|
||||
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink_ptr), sinks_.end());
|
||||
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
|
||||
}
|
||||
|
||||
|
||||
inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
|
||||
{
|
||||
auto until = std::chrono::system_clock::now() + timeout;
|
||||
while (q_.size() > 0 && std::chrono::system_clock::now() < until) {
|
||||
while (_q.size() > 0 && std::chrono::system_clock::now() < until) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||
}
|
||||
shutdown_();
|
||||
_shutdown();
|
||||
}
|
||||
|
||||
inline void c11log::sinks::async_sink::shutdown_()
|
||||
inline void c11log::sinks::async_sink::_shutdown()
|
||||
{
|
||||
if(active_) {
|
||||
active_ = false;
|
||||
if (back_thread_.joinable())
|
||||
back_thread_.join();
|
||||
if(_active) {
|
||||
_active = false;
|
||||
if (_back_thread.joinable())
|
||||
_back_thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ public:
|
||||
|
||||
void log(const std::string &msg, level::level_enum level) {
|
||||
if (level >= _level) {
|
||||
sink_it_(msg);
|
||||
_sink_it(msg);
|
||||
}
|
||||
};
|
||||
|
||||
@ -29,13 +29,13 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void sink_it_(const std::string& msg) = 0;
|
||||
virtual void _sink_it(const std::string& msg) = 0;
|
||||
std::atomic<int> _level {level::INFO};
|
||||
};
|
||||
|
||||
class null_sink:public base_sink {
|
||||
protected:
|
||||
void sink_it_(const std::string& ) override {
|
||||
void _sink_it(const std::string& ) override {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -14,17 +14,17 @@ namespace sinks {
|
||||
class simple_file_sink : public base_sink {
|
||||
public:
|
||||
explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt")
|
||||
: mutex_(),
|
||||
: _mutex(),
|
||||
_ofstream(filename + "." + extension, std::ofstream::app) {
|
||||
}
|
||||
protected:
|
||||
void sink_it_(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
void _sink_it(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_ofstream << msg;
|
||||
_ofstream.flush();
|
||||
}
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::mutex _mutex;
|
||||
std::ofstream _ofstream;
|
||||
};
|
||||
|
||||
@ -41,13 +41,13 @@ public:
|
||||
_max_size(max_size),
|
||||
_max_files(max_files),
|
||||
_current_size(0),
|
||||
mutex_(),
|
||||
_mutex(),
|
||||
_ofstream(_calc_filename(_base_filename, 0, _extension)) {
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
void _sink_it(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_current_size += msg.length();
|
||||
if (_current_size > _max_size) {
|
||||
_rotate();
|
||||
@ -91,7 +91,7 @@ private:
|
||||
std::size_t _max_size;
|
||||
std::size_t _max_files;
|
||||
std::size_t _current_size;
|
||||
std::mutex mutex_;
|
||||
std::mutex _mutex;
|
||||
std::ofstream _ofstream;
|
||||
};
|
||||
|
||||
@ -104,13 +104,13 @@ public:
|
||||
_base_filename(base_filename),
|
||||
_extension(extension),
|
||||
_midnight_tp (_calc_midnight_tp() ),
|
||||
mutex_(),
|
||||
_mutex(),
|
||||
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) {
|
||||
}
|
||||
|
||||
protected:
|
||||
void sink_it_(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
void _sink_it(const std::string& msg) override {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (std::chrono::system_clock::now() >= _midnight_tp) {
|
||||
_ofstream.close();
|
||||
_ofstream.open(_calc_filename(_base_filename, _extension));
|
||||
@ -142,7 +142,7 @@ private:
|
||||
std::string _base_filename;
|
||||
std::string _extension;
|
||||
std::chrono::system_clock::time_point _midnight_tp;
|
||||
std::mutex mutex_;
|
||||
std::mutex _mutex;
|
||||
std::ofstream _ofstream;
|
||||
|
||||
};
|
||||
|
@ -11,7 +11,7 @@ public:
|
||||
virtual ~ostream_sink() = default;
|
||||
|
||||
protected:
|
||||
virtual void sink_it_(const std::string& msg) override {
|
||||
virtual void _sink_it(const std::string& msg) override {
|
||||
_ostream << msg;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user