|
|
@@ -13,7 +13,6 @@
|
|
|
#include <map>
|
|
|
#include <stdexcept>
|
|
|
|
|
|
-#include "expect/expect.h"
|
|
|
#include "logger/logger.h"
|
|
|
//#include "properties.hpp"
|
|
|
|
|
|
@@ -48,75 +47,31 @@ namespace {
|
|
|
}
|
|
|
|
|
|
namespace logging {
|
|
|
- class multiple_bindings : public std::logic_error {
|
|
|
- using std::logic_error::logic_error;
|
|
|
- };
|
|
|
-
|
|
|
- // extern logger_impl& _logger_impl_shared_instance();
|
|
|
- extern logger_impl& (*_default_logger_impl)(void);
|
|
|
- static _binding _impl_binding = nullptr;
|
|
|
-
|
|
|
- bool bind_logger_impl(_binding impl) {
|
|
|
- expects(_impl_binding == nullptr, multiple_bindings,
|
|
|
- "Only one logger implementation may be bound");
|
|
|
- _impl_binding = impl;
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- logger_impl& _i_get_shared_instance() {
|
|
|
- if (_impl_binding == nullptr) {
|
|
|
- return _default_logger_impl();
|
|
|
- } else {
|
|
|
- return _impl_binding();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-namespace logging {
|
|
|
- logger& logger::instance() {
|
|
|
- static logger instance;
|
|
|
- return instance;
|
|
|
- }
|
|
|
-
|
|
|
- logger& logger::instance(std::string const & name) {
|
|
|
- using p_logger = std::unique_ptr<logger>;
|
|
|
- using store = std::map<std::string, p_logger>;
|
|
|
- static store instances;
|
|
|
- store::iterator it;
|
|
|
- if ((it = instances.find(name)) != instances.end()) {
|
|
|
- return *(it->second);
|
|
|
- }
|
|
|
- return *(instances.emplace(std::make_pair(name, p_logger(new logger(name)))).first->second);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- logger::logger(std::string const & name)
|
|
|
- : min_level_(LDEBUG)
|
|
|
- , logger_name_(name)
|
|
|
- , impl_(_i_get_shared_instance())
|
|
|
+ logger::logger(std::string const & name, std::shared_ptr<logger_impl> impl)
|
|
|
+ : min_level_(LDEBUG), logger_name_(name), impl_(impl)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
logger::~logger() {
|
|
|
- flush();
|
|
|
+ if (impl_) impl_->flush();
|
|
|
}
|
|
|
|
|
|
- void logger::log(log_level ll, location_info info, std::string const & msg) {
|
|
|
- impl_.write({ now( ), ll, info, logger_name_.c_str(), msg });
|
|
|
+ void logger::log(log_level ll, location_info info,
|
|
|
+ std::string const & msg) {
|
|
|
+ impl_->write({ now( ), ll, info, logger_name_.c_str(), msg });
|
|
|
}
|
|
|
|
|
|
bool logger::should_log( log_level ll ) const {
|
|
|
- return ll < min_level_ && impl_.should_log( ll );
|
|
|
+ return ll >= min_level_ && impl_->should_log( ll );
|
|
|
}
|
|
|
|
|
|
void logger::flush() {
|
|
|
- impl_.flush();
|
|
|
+ impl_->flush();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
namespace logging {
|
|
|
-
|
|
|
bool logger_impl::should_log( log_level ll ) const {
|
|
|
- return ll < min_log_level;
|
|
|
+ return ll >= min_log_level;
|
|
|
}
|
|
|
}
|