underscore first in members

This commit is contained in:
gabime 2014-02-22 10:34:42 +02:00
parent 4c8fd99e27
commit 005dc7e605
7 changed files with 104 additions and 104 deletions

View File

@ -21,17 +21,17 @@ public:
using clock = std::chrono::system_clock; using clock = std::chrono::system_clock;
explicit blocking_queue(size_type max_size) : explicit blocking_queue(size_type max_size) :
max_size_(max_size), _max_size(max_size),
q_(), _q(),
mutex_() { _mutex() {
} }
blocking_queue(const blocking_queue&) = delete; blocking_queue(const blocking_queue&) = delete;
blocking_queue& operator=(const blocking_queue&) = delete; blocking_queue& operator=(const blocking_queue&) = delete;
~blocking_queue() = default; ~blocking_queue() = default;
size_type size() { size_type size() {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
return q_.size(); return _q.size();
} }
// Push copy of item into the back of the queue. // Push copy of item into the back of the queue.
@ -39,17 +39,17 @@ public:
// Return: false on timeout, true on successful push. // Return: false on timeout, true on successful push.
template<typename Duration_Rep, typename Duration_Period, typename TT> template<typename Duration_Rep, typename Duration_Period, typename TT>
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) { bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
std::unique_lock<std::mutex> ul(mutex_); std::unique_lock<std::mutex> ul(_mutex);
if (q_.size() >= max_size_) { if (_q.size() >= _max_size) {
if (!item_popped_cond_.wait_until(ul, clock::now() + timeout, [this]() { if (!_item_popped_cond.wait_until(ul, clock::now() + timeout, [this]() {
return this->q_.size() < this->max_size_; return this->_q.size() < this->_max_size;
})) }))
return false; return false;
} }
q_.push(std::forward<TT>(item)); _q.push(std::forward<TT>(item));
if (q_.size() <= 1) { if (_q.size() <= 1) {
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly.. 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; return true;
} }
@ -66,18 +66,18 @@ public:
// Return: false on timeout , true on successful pop/ // Return: false on timeout , true on successful pop/
template<class Duration_Rep, class Duration_Period> template<class Duration_Rep, class Duration_Period>
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) { bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
std::unique_lock<std::mutex> ul(mutex_); std::unique_lock<std::mutex> ul(_mutex);
if (q_.empty()) { if (_q.empty()) {
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() { if (!_item_pushed_cond.wait_until(ul, clock::now() + timeout, [this]() {
return !this->q_.empty(); return !this->_q.empty();
})) }))
return false; return false;
} }
item = std::move(q_.front()); item = std::move(_q.front());
q_.pop(); _q.pop();
if (q_.size() >= max_size_ - 1) { if (_q.size() >= _max_size - 1) {
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly.. 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; return true;
} }
@ -91,18 +91,18 @@ public:
// Clear the queue // Clear the queue
void clear() { void clear() {
{ {
std::unique_lock<std::mutex> ul(mutex_); std::unique_lock<std::mutex> ul(_mutex);
queue_t().swap(q_); queue_t().swap(_q);
} }
item_popped_cond_.notify_all(); _item_popped_cond.notify_all();
} }
private: private:
size_type max_size_; size_type _max_size;
std::queue<T> q_; std::queue<T> _q;
std::mutex mutex_; std::mutex _mutex;
std::condition_variable item_pushed_cond_; std::condition_variable _item_pushed_cond;
std::condition_variable item_popped_cond_; std::condition_variable _item_popped_cond;
static constexpr auto one_hour = std::chrono::hours(1); static constexpr auto one_hour = std::chrono::hours(1);
}; };

View File

@ -14,7 +14,7 @@ public:
_callback_logger(callback_logger), _callback_logger(callback_logger),
_oss(), _oss(),
_level(msg_level) { _level(msg_level) {
callback_logger->formatter_->format_header(callback_logger->logger_name_, callback_logger->_formatter->format_header(callback_logger->_logger_name,
msg_level, msg_level,
c11log::formatters::clock::now(), c11log::formatters::clock::now(),
_oss); _oss);
@ -30,7 +30,7 @@ public:
~line_logger() { ~line_logger() {
if (_callback_logger) { if (_callback_logger) {
_oss << '\n'; _oss << '\n';
_callback_logger->log_it_(_oss.str_ref()); _callback_logger->_log_it(_oss.str_ref());
} }
} }

View File

@ -24,11 +24,11 @@ public:
typedef std::vector<sink_ptr_t> sinks_vector_t; typedef std::vector<sink_ptr_t> sinks_vector_t;
explicit logger(const std::string& name) : explicit logger(const std::string& name) :
logger_name_(name), _logger_name(name),
formatter_(new formatters::default_formatter()), _formatter(new formatters::default_formatter()),
sinks_(), _sinks(),
mutex_(), _mutex(),
atomic_level_(level::INFO) { _atomic_level(level::INFO) {
} }
~logger() = default; ~logger() = default;
@ -55,13 +55,13 @@ public:
private: private:
friend details::line_logger; friend details::line_logger;
std::string logger_name_ = ""; std::string _logger_name = "";
std::unique_ptr<c11log::formatters::formatter> formatter_; std::unique_ptr<c11log::formatters::formatter> _formatter;
sinks_vector_t sinks_; sinks_vector_t _sinks;
std::mutex mutex_; std::mutex _mutex;
std::atomic_int atomic_level_; 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) 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); return details::line_logger(this, msg_level);
else else
return details::line_logger(nullptr); 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) inline void c11log::logger::set_name(const std::string& name)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
logger_name_ = name; _logger_name = name;
} }
inline const std::string& c11log::logger::get_name() inline const std::string& c11log::logger::get_name()
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
return logger_name_; return _logger_name;
} }
inline void c11log::logger::add_sink(sink_ptr_t sink_ptr) inline void c11log::logger::add_sink(sink_ptr_t sink_ptr)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
sinks_.push_back(sink_ptr); _sinks.push_back(sink_ptr);
} }
inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr) inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
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::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter) inline void c11log::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
formatter_ = std::move(formatter); _formatter = std::move(formatter);
} }
inline void c11log::logger::set_level(c11log::level::level_enum level) 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 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 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()); level::level_enum level = static_cast<level::level_enum>(_atomic_level.load());
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
for (auto &sink : sinks_) for (auto &sink : _sinks)
sink->log(msg, level); sink->log(msg, level);
} }

View File

@ -25,16 +25,16 @@ public:
protected: protected:
void sink_it_(const std::string& msg) override; void _sink_it(const std::string& msg) override;
void thread_loop_(); void _thread_loop();
private: private:
c11log::logger::sinks_vector_t sinks_; c11log::logger::sinks_vector_t _sinks;
std::atomic<bool> active_; std::atomic<bool> _active;
c11log::details::blocking_queue<std::string> q_; c11log::details::blocking_queue<std::string> _q;
std::thread back_thread_; std::thread _back_thread;
//Clear all remaining messages(if any), stop the back_thread_ and join it //Clear all remaining messages(if any), stop the _back_thread and join it
void shutdown_(); void _shutdown();
}; };
} }
} }
@ -44,31 +44,31 @@ private:
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size) inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
:sinks_(), :_sinks(),
active_(true), _active(true),
q_(max_queue_size), _q(max_queue_size),
back_thread_(&async_sink::thread_loop_, this) _back_thread(&async_sink::_thread_loop, this)
{} {}
inline c11log::sinks::async_sink::~async_sink() 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); constexpr auto pop_timeout = std::chrono::seconds(1);
std::string msg; std::string msg;
while (active_) { while (_active) {
if (q_.pop(msg, pop_timeout)) { if (_q.pop(msg, pop_timeout)) {
for (auto &sink : sinks_) { for (auto &sink : _sinks) {
sink->log(msg, static_cast<level::level_enum>(_level.load())); sink->log(msg, static_cast<level::level_enum>(_level.load()));
if (!active_) if (!_active)
return; 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) 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) 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) inline void c11log::sinks::async_sink::shutdown(const std::chrono::seconds &timeout)
{ {
auto until = std::chrono::system_clock::now() + 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)); 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_) { if(_active) {
active_ = false; _active = false;
if (back_thread_.joinable()) if (_back_thread.joinable())
back_thread_.join(); _back_thread.join();
} }
} }

View File

@ -20,7 +20,7 @@ public:
void log(const std::string &msg, level::level_enum level) { void log(const std::string &msg, level::level_enum level) {
if (level >= _level) { if (level >= _level) {
sink_it_(msg); _sink_it(msg);
} }
}; };
@ -29,13 +29,13 @@ public:
} }
protected: 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}; std::atomic<int> _level {level::INFO};
}; };
class null_sink:public base_sink { class null_sink:public base_sink {
protected: protected:
void sink_it_(const std::string& ) override { void _sink_it(const std::string& ) override {
} }
}; };
} }

View File

@ -14,17 +14,17 @@ namespace sinks {
class simple_file_sink : public base_sink { class simple_file_sink : public base_sink {
public: public:
explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt") explicit simple_file_sink(const std::string &filename, const std::string& extension = "txt")
: mutex_(), : _mutex(),
_ofstream(filename + "." + extension, std::ofstream::app) { _ofstream(filename + "." + extension, std::ofstream::app) {
} }
protected: protected:
void sink_it_(const std::string& msg) override { void _sink_it(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
_ofstream << msg; _ofstream << msg;
_ofstream.flush(); _ofstream.flush();
} }
private: private:
std::mutex mutex_; std::mutex _mutex;
std::ofstream _ofstream; std::ofstream _ofstream;
}; };
@ -41,13 +41,13 @@ public:
_max_size(max_size), _max_size(max_size),
_max_files(max_files), _max_files(max_files),
_current_size(0), _current_size(0),
mutex_(), _mutex(),
_ofstream(_calc_filename(_base_filename, 0, _extension)) { _ofstream(_calc_filename(_base_filename, 0, _extension)) {
} }
protected: protected:
void sink_it_(const std::string& msg) override { void _sink_it(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
_current_size += msg.length(); _current_size += msg.length();
if (_current_size > _max_size) { if (_current_size > _max_size) {
_rotate(); _rotate();
@ -91,7 +91,7 @@ private:
std::size_t _max_size; std::size_t _max_size;
std::size_t _max_files; std::size_t _max_files;
std::size_t _current_size; std::size_t _current_size;
std::mutex mutex_; std::mutex _mutex;
std::ofstream _ofstream; std::ofstream _ofstream;
}; };
@ -104,13 +104,13 @@ public:
_base_filename(base_filename), _base_filename(base_filename),
_extension(extension), _extension(extension),
_midnight_tp (_calc_midnight_tp() ), _midnight_tp (_calc_midnight_tp() ),
mutex_(), _mutex(),
_ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) { _ofstream(_calc_filename(_base_filename, _extension), std::ofstream::app) {
} }
protected: protected:
void sink_it_(const std::string& msg) override { void _sink_it(const std::string& msg) override {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(_mutex);
if (std::chrono::system_clock::now() >= _midnight_tp) { if (std::chrono::system_clock::now() >= _midnight_tp) {
_ofstream.close(); _ofstream.close();
_ofstream.open(_calc_filename(_base_filename, _extension)); _ofstream.open(_calc_filename(_base_filename, _extension));
@ -142,7 +142,7 @@ private:
std::string _base_filename; std::string _base_filename;
std::string _extension; std::string _extension;
std::chrono::system_clock::time_point _midnight_tp; std::chrono::system_clock::time_point _midnight_tp;
std::mutex mutex_; std::mutex _mutex;
std::ofstream _ofstream; std::ofstream _ofstream;
}; };

View File

@ -11,7 +11,7 @@ public:
virtual ~ostream_sink() = default; virtual ~ostream_sink() = default;
protected: protected:
virtual void sink_it_(const std::string& msg) override { virtual void _sink_it(const std::string& msg) override {
_ostream << msg; _ostream << msg;
} }