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

@ -8,7 +8,7 @@
#include <atomic> #include <atomic>
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "spdlog/sinks/file_sinks.h" #include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/stdout_sinks.h" #include "spdlog/sinks/async_sink.h"
#include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/null_sink.h"
#include "utils.h" #include "utils.h"
@ -20,82 +20,57 @@ using namespace spdlog::sinks;
using namespace utils; using namespace utils;
void bench(int howmany, std::shared_ptr<spdlog::logger> log) void bench(int howmany, std::shared_ptr<spdlog::logger> log);
{ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
cout << log->name() << "...\t\t" << flush;
auto start = system_clock::now();
for (auto i = 0; i < howmany; ++i)
{
log->info("Hello logger: msg number ") << i;
}
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
cout << format(int(howmany / delta_d)) << "/sec" << endl;
}
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
cout << log->name() << "...\t\t" << flush;
std::atomic<int > msg_counter{0};
vector<thread> threads;
auto start = system_clock::now();
for (int t = 0; t < thread_count; ++t)
{
threads.push_back(std::thread([&]() {
while(msg_counter++ < howmany)
log->info("Hello logger: msg number ") << msg_counter;
}));
}
for(auto &t:threads)
{
t.join();
};
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
cout << format(int(howmany / delta_d)) << "/sec" << endl;
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int howmany = 123456;
int threads = 4;
int flush_interval = 1000;
int file_size = 30 * 1024 * 1024;
int rotating_files = 5;
try { try {
int howmany = argc <= 1 ? 250000 : atoi(argv[1]); if(argc > 1)
int threads = argc <= 2 ? 4 : atoi(argv[2]); howmany = atoi(argv[1]);
int flush_interval = 100; if (argc > 2)
threads = atoi(argv[2]);
cout << "*******************************************************************************\n";
cout << "Single thread, " << format(howmany) << " iterations, flush every " << flush_interval << " lines"<< endl;
cout << "*******************************************************************************\n";
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files, flush_interval);
bench(howmany, rotating_st);
cout << "*******************************************************************************\n"; auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", flush_interval);
cout << "Single thread, " << format(howmany) << " iterations, flush every " << flush_interval << " lines"<< endl; bench(howmany, daily_st);
cout << "*******************************************************************************\n";
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", 1024 * 1024 * 5, 5, flush_interval); bench(howmany, spdlog::create<null_sink_st>("null_st"));
bench(howmany, rotating_st);
auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", flush_interval); cout << "\n*******************************************************************************\n";
bench(howmany, daily_st); cout << threads << " threads sharing same logger, " << format(howmany) << " iterations, flush every " << flush_interval << " lines" << endl;
cout << "*******************************************************************************\n";
bench(howmany, spdlog::create<null_sink_st>("null_st")); auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", file_size, rotating_files, flush_interval);
bench_mt(howmany, rotating_mt, threads);
cout << "\n*******************************************************************************\n";
cout << threads << " threads, " << format(howmany) << " iterations, flush every " << flush_interval << " lines" << endl;
cout << "*******************************************************************************\n";
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "logs/rotating_mt", 1024 * 1024 * 5, 5, flush_interval); auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", flush_interval);
bench_mt(howmany, rotating_mt, threads); bench_mt(howmany, daily_mt, threads);
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", flush_interval);
bench_mt(howmany, daily_mt, threads);
bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads); auto async_sink = std::make_shared<sinks::async_sink>(1000);
auto file_sink = std::make_shared<sinks::rotating_file_sink_mt>("logs/async_logger", "txt", file_size, rotating_files, flush_interval);
async_sink->add_sink(file_sink);
auto async_logger = std::make_shared<spdlog::logger>("async_logger", async_sink );
bench_mt(howmany, async_logger, threads);
bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads);
} }
catch (std::exception &ex) catch (std::exception &ex)
{ {
@ -105,3 +80,53 @@ int main(int argc, char* argv[])
return 0; return 0;
} }
void bench(int howmany, std::shared_ptr<spdlog::logger> log)
{
cout << log->name() << "...\t\t" << flush;
auto start = system_clock::now();
for (auto i = 0; i < howmany; ++i)
{
log->info("Hello logger: msg number ") << i << "**************************" << i + 1 << "," << 1 + 2;;
}
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
cout << format(int(howmany / delta_d)) << "/sec" << endl;
log->close();
}
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
{
cout << log->name() << "...\t\t" << flush;
std::atomic<int > msg_counter {0};
vector<thread> threads;
auto start = system_clock::now();
for (int t = 0; t < thread_count; ++t)
{
threads.push_back(std::thread([&]() {
while (true)
{
int counter = ++msg_counter;
if (counter > howmany) break;
log->info("Hello logger: msg number ") << counter;
}
}));
}
for(auto &t:threads)
{
t.join();
};
auto delta = system_clock::now() - start;
auto delta_d = duration_cast<duration<double>> (delta).count();
cout << format(int(howmany / delta_d)) << "/sec" << endl;
log->close();
}

View File

@ -6,7 +6,7 @@
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
int main(int, char* []) int main_(int, char* [])
{ {
namespace spd = spdlog; namespace spd = spdlog;
@ -33,12 +33,13 @@ int main(int, char* [])
console->warn("This should!"); console->warn("This should!");
// Change format pattern to all loggers // Change format pattern to all loggers
spd::set_pattern(" **** %Y-%m-%d %H:%M:%S.%e %l **** %t"); spd::set_pattern(" **** %Y-%m-%d %H:%M:%S.%e %l **** %v");
spd::get("console")->warn("This is another message with different format"); spd::get("console")->warn("This is another message with different format");
} }
catch (const spd::spdlog_ex& ex) catch (const spd::spdlog_ex& ex)
{ {
std::cout << "Log failed: " << ex.what() << std::endl; std::cout << "Log failed: " << ex.what() << std::endl;
} }
return 0;
} }

View File

@ -49,7 +49,7 @@ public:
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;
})) }))
@ -59,7 +59,7 @@ public:
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_all();
} }
return true; return true;
} }
@ -69,7 +69,7 @@ public:
template<typename TT> template<typename TT>
void push(TT&& item) 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. // 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) 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_all();
} }
return true; 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) inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
{ {
_formatter = 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(); return msg_level >= _level.load();
} }
inline void spdlog::logger::stop_logging() inline void spdlog::logger::close()
{ {
set_level(level::OFF); set_level(level::OFF);
for (auto &sink : _sinks)
sink->close();
} }

View File

@ -4,6 +4,7 @@
#include <chrono> #include <chrono>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <thread>
#include "../formatter.h" #include "../formatter.h"
#include "./log_msg.h" #include "./log_msg.h"
@ -301,8 +302,17 @@ private:
}; };
//Thread id
class t_formatter :public flag_formatter 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 void format(details::log_msg& msg) override
{ {
@ -343,7 +353,7 @@ private:
}; };
// Full info formatter // 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 class full_formatter :public flag_formatter
{ {
void format(details::log_msg& msg) override 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())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::t_formatter()));
break; break;
case('v') :
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::v_formatter()));
break;
case('a') : case('a') :
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::a_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::a_formatter()));
break; break;

View File

@ -77,12 +77,12 @@ public:
} }
void stop_all() void close_all()
{ {
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
_level = level::OFF; _level = level::OFF;
for (auto& l : _loggers) 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); 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: public:
logger(const std::string& logger_name, sink_ptr single_sink);
logger(const std::string& name, sinks_init_list); logger(const std::string& name, sinks_init_list);
template<class It> template<class It>
logger(const std::string& name, const It& begin, const It& end); logger(const std::string& name, const It& begin, const It& end);
@ -42,7 +43,7 @@ public:
const std::string& name() const; const std::string& name() const;
bool should_log(level::level_enum) 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(level::level_enum lvl, const Args&... args);
template <typename... Args> details::line_logger log(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 //Wait to remaining items (if any) in the queue to be written and shutdown
void shutdown(const std::chrono::milliseconds& timeout); void shutdown(const std::chrono::milliseconds& timeout);
void close() override;
protected: protected:
void _sink_it(const details::log_msg& msg) override; 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(); _shutdown();
} }
inline void spdlog::sinks::async_sink::close()
{
_shutdown();
}
inline void spdlog::sinks::async_sink::_shutdown() inline void spdlog::sinks::async_sink::_shutdown()
{ {
@ -133,5 +141,8 @@ inline void spdlog::sinks::async_sink::_shutdown()
if (_back_thread.joinable()) if (_back_thread.joinable())
_back_thread.join(); _back_thread.join();
} }
for (auto &s : _sinks)
s->close();
} }

View File

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

View File

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

View File

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

View File

@ -11,6 +11,7 @@ class sink
public: public:
virtual ~sink() {} virtual ~sink() {}
virtual void log(const details::log_msg& msg) = 0; 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: public:
stdout_sink() : ostream_sink<Mutex>(std::cout) {} stdout_sink() : ostream_sink<Mutex>(std::cout) {}
}; };
typedef stdout_sink<details::null_mutex> stdout_sink_st; 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 // 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); 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 // Set global formatter object
void set_formatter(formatter_ptr f); void set_formatter(formatter_ptr f);
//Stop all loggers // Close all loggers and stop logging
void stop(); void close();
// //