This commit is contained in:
gabi
2014-11-01 02:12:12 +02:00
parent bf303fad19
commit 567e85e6d4
15 changed files with 169 additions and 89 deletions

View File

@@ -49,7 +49,7 @@ public:
std::unique_lock<std::mutex> ul(_mutex);
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;
}))
@@ -59,7 +59,7 @@ public:
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_all();
}
return true;
}
@@ -69,7 +69,7 @@ public:
template<typename TT>
void push(TT&& item)
{
while (!push(std::forward<TT>(item), std::chrono::hours(1)));
while (!push(std::forward<TT>(item), std::chrono::seconds(10)));
}
// Pop a copy of the front item in the queue into the given item ref.
@@ -92,7 +92,7 @@ public:
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_all();
}
return true;
}

View File

@@ -22,6 +22,10 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c
{}
inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) :logger(logger_name, { single_sink })
{}
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
@@ -108,9 +112,11 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
return msg_level >= _level.load();
}
inline void spdlog::logger::stop_logging()
inline void spdlog::logger::close()
{
set_level(level::OFF);
for (auto &sink : _sinks)
sink->close();
}

View File

@@ -4,6 +4,7 @@
#include <chrono>
#include <memory>
#include <vector>
#include <thread>
#include "../formatter.h"
#include "./log_msg.h"
@@ -301,8 +302,17 @@ private:
};
//Thread id
class t_formatter :public flag_formatter
{
void format(details::log_msg& msg) override
{
msg.formatted << std::this_thread::get_id();
}
};
class v_formatter :public flag_formatter
{
void format(details::log_msg& msg) override
{
@@ -343,7 +353,7 @@ private:
};
// Full info formatter
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
class full_formatter :public flag_formatter
{
void format(details::log_msg& msg) override
@@ -428,6 +438,10 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::t_formatter()));
break;
case('v') :
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::v_formatter()));
break;
case('a') :
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::a_formatter()));
break;

View File

@@ -77,12 +77,12 @@ public:
}
void stop_all()
void close_all()
{
std::lock_guard<std::mutex> lock(_mutex);
_level = level::OFF;
for (auto& l : _loggers)
l.second->stop_logging();
l.second->close();
}

View File

@@ -95,7 +95,7 @@ inline void spdlog::set_level(level::level_enum log_level)
return details::registry::instance().set_level(log_level);
}
inline void spdlog::stop()
inline void spdlog::close()
{
return details::registry::instance().stop_all();
return details::registry::instance().close_all();
}

View File

@@ -24,6 +24,7 @@ class logger
{
public:
logger(const std::string& logger_name, sink_ptr single_sink);
logger(const std::string& name, sinks_init_list);
template<class It>
logger(const std::string& name, const It& begin, const It& end);
@@ -42,7 +43,7 @@ public:
const std::string& name() const;
bool should_log(level::level_enum) const;
void stop_logging();
void close();
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
template <typename... Args> details::line_logger log(const Args&... args);

View File

@@ -33,6 +33,9 @@ public:
//Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::milliseconds& timeout);
void close() override;
protected:
void _sink_it(const details::log_msg& msg) override;
@@ -123,6 +126,11 @@ inline void spdlog::sinks::async_sink::shutdown(const std::chrono::milliseconds&
_shutdown();
}
inline void spdlog::sinks::async_sink::close()
{
_shutdown();
}
inline void spdlog::sinks::async_sink::_shutdown()
{
@@ -133,5 +141,8 @@ inline void spdlog::sinks::async_sink::_shutdown()
if (_back_thread.joinable())
_back_thread.join();
}
for (auto &s : _sinks)
s->close();
}

View File

@@ -28,6 +28,10 @@ public:
{
_file_helper.open(filename);
}
void close() override
{
_file_helper.close();
}
protected:
void _sink_it(const details::log_msg& msg) override
{
@@ -60,6 +64,11 @@ public:
_file_helper.open(calc_filename(_base_filename, 0, _extension));
}
void close() override
{
_file_helper.close();
}
protected:
void _sink_it(const details::log_msg& msg) override
{
@@ -140,6 +149,11 @@ public:
_file_helper.open(calc_filename(_base_filename, _extension));
}
void close() override
{
_file_helper.close();
}
protected:
void _sink_it(const details::log_msg& msg) override
{

View File

@@ -13,6 +13,9 @@ class null_sink : public base_sink<Mutex>
protected:
void _sink_it(const details::log_msg&) override
{}
void close() override
{}
};
typedef null_sink<details::null_mutex> null_sink_st;

View File

@@ -21,6 +21,9 @@ public:
virtual ~ostream_sink() = default;
void close() override
{}
protected:
virtual void _sink_it(const details::log_msg& msg) override
{

View File

@@ -11,6 +11,7 @@ class sink
public:
virtual ~sink() {}
virtual void log(const details::log_msg& msg) = 0;
virtual void close() = 0;
};
}
}

View File

@@ -15,6 +15,7 @@ class stdout_sink : public ostream_sink<Mutex>
{
public:
stdout_sink() : ostream_sink<Mutex>(std::cout) {}
};
typedef stdout_sink<details::null_mutex> stdout_sink_st;

View File

@@ -30,7 +30,7 @@ std::shared_ptr<logger> get(const std::string& name);
// Set global formatting
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %t");
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
void set_pattern(const std::string& format_string);
@@ -74,8 +74,8 @@ std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Arg
// Set global formatter object
void set_formatter(formatter_ptr f);
//Stop all loggers
void stop();
// Close all loggers and stop logging
void close();
//