Agora  1.2.0
Agora project
async.h
Go to the documentation of this file.
1 // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
3 
4 #pragma once
5 
6 //
7 // Async logging using global thread pool
8 // All loggers created here share same global thread pool.
9 // Each log message is pushed to a queue along with a shared pointer to the
10 // logger.
11 // If a logger deleted while having pending messages in the queue, it's actual
12 // destruction will defer
13 // until all its messages are processed by the thread pool.
14 // This is because each message in the queue holds a shared_ptr to the
15 // originating logger.
16 
17 #include <spdlog/async_logger.h>
20 
21 #include <memory>
22 #include <mutex>
23 #include <functional>
24 
25 namespace spdlog {
26 
27 namespace details {
28 static const size_t default_async_q_size = 8192;
29 }
30 
31 // async logger factory - creates async loggers backed with thread pool.
32 // if a global thread pool doesn't already exist, create it with default queue
33 // size of 8192 items and single thread.
34 template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
36 {
37  template<typename Sink, typename... SinkArgs>
38  static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
39  {
40  auto &registry_inst = details::registry::instance();
41 
42  // create global thread pool if not already exists..
43 
44  auto &mutex = registry_inst.tp_mutex();
45  std::lock_guard<std::recursive_mutex> tp_lock(mutex);
46  auto tp = registry_inst.get_tp();
47  if (tp == nullptr)
48  {
49  tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
50  registry_inst.set_tp(tp);
51  }
52 
53  auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
54  auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
55  registry_inst.initialize_logger(new_logger);
56  return new_logger;
57  }
58 };
59 
62 
63 template<typename Sink, typename... SinkArgs>
64 inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
65 {
66  return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
67 }
68 
69 template<typename Sink, typename... SinkArgs>
70 inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
71 {
72  return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
73 }
74 
75 // set global thread pool.
76 inline void init_thread_pool(
77  size_t q_size, size_t thread_count, std::function<void()> on_thread_start, std::function<void()> on_thread_stop)
78 {
79  auto tp = std::make_shared<details::thread_pool>(q_size, thread_count, on_thread_start, on_thread_stop);
80  details::registry::instance().set_tp(std::move(tp));
81 }
82 
83 inline void init_thread_pool(size_t q_size, size_t thread_count, std::function<void()> on_thread_start)
84 {
85  init_thread_pool(q_size, thread_count, on_thread_start, [] {});
86 }
87 
88 inline void init_thread_pool(size_t q_size, size_t thread_count)
89 {
91  q_size, thread_count, [] {}, [] {});
92 }
93 
94 // get the global thread pool.
95 inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
96 {
98 }
99 } // namespace spdlog
fmt::v8::format_context
buffer_context< char > format_context
Definition: core.h:1798
udp_sink.h
spdlog::sinks::udp_sink_config
Definition: udp_sink.h:26
multi_sink_example
void multi_sink_example()
Definition: example.cpp:242
fmt::v8::format_to
auto format_to(OutputIt out, const text_style &ts, const S &format_str, Args &&... args) -> typename std::enable_if< enable, OutputIt >::type
Definition: color.h:628
file_size
static const size_t file_size
Definition: bench.cpp:36
spdlog::set_error_handler
SPDLOG_INLINE void set_error_handler(void(*handler)(const std::string &msg))
Definition: spdlog-inl.h:75
spdlog::details::default_async_q_size
static const size_t default_async_q_size
Definition: async.h:28
spdlog::file_event_handlers
Definition: common.h:308
bench_single_threaded
void bench_single_threaded(int iters)
Definition: bench.cpp:76
trace_example
void trace_example()
Definition: example.cpp:210
android_sink.h
fmt::v8::printf
auto printf(const S &fmt, const T &... args) -> int
Definition: printf.h:631
spdlog::level::trace
@ trace
Definition: common.h:213
spdlog::syslog_logger_mt
std::shared_ptr< logger > syslog_logger_mt(const std::string &logger_name, const std::string &syslog_ident="", int syslog_option=0, int syslog_facility=LOG_USER, bool enable_formatting=false)
Definition: syslog_sink.h:97
bench_mt
void bench_mt(int howmany, std::shared_ptr< spdlog::logger > log, size_t thread_count)
Definition: bench.cpp:168
daily_example
void daily_example()
Definition: example.cpp:133
spdlog::basic_logger_st
std::shared_ptr< logger > basic_logger_st(const std::string &logger_name, const filename_t &filename, bool truncate=false, const file_event_handlers &event_handlers={})
Definition: basic_file_sink.h:50
spdlog::error
void error(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:167
spdlog::rotating_logger_mt
std::shared_ptr< logger > rotating_logger_mt(const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open=false, const file_event_handlers &event_handlers={})
Definition: rotating_file_sink.h:63
spdlog::level::off
@ off
Definition: common.h:219
my_formatter_flag::format
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
Definition: example.cpp:315
spdlog::udp_logger_mt
std::shared_ptr< logger > udp_logger_mt(const std::string &logger_name, sinks::udp_sink_config skin_config)
Definition: udp_sink.h:69
basic_example
void basic_example()
Definition: example.cpp:119
thread_pool.h
format.h
spdlog::flush_every
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
Definition: spdlog-inl.h:70
spdlog::level::warn
@ warn
Definition: common.h:216
spdlog::default_logger
SPDLOG_INLINE std::shared_ptr< spdlog::logger > default_logger()
Definition: spdlog-inl.h:110
async.h
bench_disabled_macro
void bench_disabled_macro(benchmark::State &state, std::shared_ptr< spdlog::logger > logger)
Definition: latency.cpp:52
spdlog::critical
void critical(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:173
spdlog::stdout_color_mt
SPDLOG_INLINE std::shared_ptr< logger > stdout_color_mt(const std::string &logger_name, color_mode mode)
Definition: stdout_color_sinks-inl.h:16
bench_formatters
void bench_formatters()
Definition: formatter-bench.cpp:29
spdlog::set_level
SPDLOG_INLINE void set_level(level::level_enum log_level)
Definition: spdlog-inl.h:60
spdlog::set_pattern
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
Definition: spdlog-inl.h:30
env.h
fmt::v8::detail::buffer::clear
void clear()
Definition: core.h:832
bench
void bench(int howmany, std::shared_ptr< spdlog::logger > log)
Definition: bench.cpp:148
fmt_lib::formatter< my_type >::format
auto format(my_type my, format_context &ctx) -> decltype(ctx.out())
Definition: example.cpp:269
spdlog::sinks
Definition: common.h:112
null_sink.h
spdlog::logger
Definition: logger.h:54
rotating_files
static const size_t rotating_files
Definition: bench.cpp:37
spdlog::file_event_handlers::before_close
std::function< void(const filename_t &filename, std::FILE *file_stream)> before_close
Definition: common.h:312
main
int main(int, char *[])
Definition: example.cpp:33
fclose
fclose(fileID)
threads
Pilot RX by socket threads(=reference time)
spdlog::level::info
@ info
Definition: common.h:215
spdlog::details::os::now
SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT
Definition: os-inl.h:71
my_formatter_flag::clone
std::unique_ptr< custom_flag_formatter > clone() const override
Definition: example.cpp:321
spdlog::level::debug
@ debug
Definition: common.h:214
spdlog::logger::set_level
void set_level(level::level_enum log_level)
Definition: logger-inl.h:67
count
count
Definition: inspect_agora_results.m:96
rotating_file_sink.h
my_formatter_flag
Definition: example.cpp:312
Catch::cerr
std::ostream & cerr()
bench_threaded_logging
void bench_threaded_logging(size_t threads, int iters)
Definition: bench.cpp:40
spdlog::set_automatic_registration
SPDLOG_INLINE void set_automatic_registration(bool automatic_registration)
Definition: spdlog-inl.h:105
spdlog::warn
void warn(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:161
main
int main(int argc, char *argv[])
Definition: latency.cpp:77
custom_flags_example
void custom_flags_example()
Definition: example.cpp:327
spdlog::async_factory_impl
Definition: async.h:35
spdlog::to_hex
details::dump_info< typename Container::const_iterator > to_hex(const Container &container, size_t size_per_line=32)
Definition: bin_to_hex.h:72
FMT_STRING
#define FMT_STRING(s)
Definition: format.h:2383
stdout_logger_example
void stdout_logger_example()
Definition: example.cpp:110
spdlog::stopwatch
Definition: stopwatch.h:28
bench_c_string
void bench_c_string(benchmark::State &state, std::shared_ptr< spdlog::logger > logger)
Definition: latency.cpp:19
filename
filename
Definition: parse_all_dl.m:14
spdlog::null_logger_mt
std::shared_ptr< logger > null_logger_mt(const std::string &logger_name)
Definition: null_sink.h:29
fmt::v8::basic_memory_buffer
Definition: format.h:677
spdlog::daily_logger_mt
std::shared_ptr< logger > daily_logger_mt(const std::string &logger_name, const filename_t &filename, int hour=0, int minute=0, bool truncate=false, uint16_t max_files=0, const file_event_handlers &event_handlers={})
Definition: daily_file_sink.h:266
udp_example
void udp_example()
Definition: example.cpp:233
spdlog::apply_all
SPDLOG_INLINE void apply_all(const std::function< void(std::shared_ptr< logger >)> &fun)
Definition: spdlog-inl.h:85
spdlog::custom_flag_formatter
Definition: pattern_formatter.h:66
spdlog
Definition: async.h:25
fmt::v8::detail::fputs
void fputs(const Char *chars, FILE *stream)
Definition: color.h:478
syslog_example
void syslog_example()
Definition: example.cpp:290
SPDLOG_LOGGER_TRACE
#define SPDLOG_LOGGER_TRACE(logger,...)
Definition: spdlog.h:294
async_example
void async_example()
Definition: example.cpp:152
utils.h
SPDLOG_TRACE
#define SPDLOG_TRACE(...)
Definition: spdlog.h:295
daily_file_sink.h
nlohmann::json_v3_11_1NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON::detail2::begin
begin_tag begin(T &&...)
SPDLOG_LOGGER_DEBUG
#define SPDLOG_LOGGER_DEBUG(logger,...)
Definition: spdlog.h:302
spdlog::spdlog_ex::what
const char * what() const noexcept override
Definition: common-inl.h:67
spdlog::enable_backtrace
SPDLOG_INLINE void enable_backtrace(size_t n_messages)
Definition: spdlog-inl.h:35
spdlog::async_overflow_policy::overrun_oldest
@ overrun_oldest
i
for i
Definition: generate_data.m:107
spdlog::rotating_logger_st
std::shared_ptr< logger > rotating_logger_st(const std::string &logger_name, const filename_t &filename, size_t max_file_size, size_t max_files, bool rotate_on_open=false, const file_event_handlers &event_handlers={})
Definition: rotating_file_sink.h:71
main
int main(int argc, char *argv[])
Definition: bench.cpp:113
spdlog.h
my_type
Definition: example.cpp:258
basic_file_sink.h
stdout_color_sinks.h
SPDLOG_DEBUG
#define SPDLOG_DEBUG(...)
Definition: spdlog.h:303
spdlog::drop
SPDLOG_INLINE void drop(const std::string &name)
Definition: spdlog-inl.h:90
bin_to_hex.h
spdlog::source_loc
Definition: common.h:290
start
end start
Definition: inspect_agora_results.m:95
bench_mt
void bench_mt(int howmany, std::shared_ptr< spdlog::logger > log, int thread_count)
Definition: async_bench.cpp:162
spdlog::get
SPDLOG_INLINE std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog-inl.h:20
bench_logger
void bench_logger(benchmark::State &state, std::shared_ptr< spdlog::logger > logger)
Definition: latency.cpp:33
spdlog::details::async_msg
Definition: thread_pool.h:32
spdlog::details::make_unique
std::unique_ptr< T > make_unique(Args &&... args)
Definition: common.h:334
vector_example
void vector_example()
Definition: example.cpp:196
fmt::v8::basic_memory_buffer::append
void append(const ContiguousRange &range)
Definition: format.h:771
rotating_example
void rotating_example()
Definition: example.cpp:126
spdlog::info
void info(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:155
spdlog::spdlog_ex
Definition: common.h:276
spdlog::daily_logger_st
std::shared_ptr< logger > daily_logger_st(const std::string &logger_name, const filename_t &filename, int hour=0, int minute=0, bool truncate=false, uint16_t max_files=0, const file_event_handlers &event_handlers={})
Definition: daily_file_sink.h:281
registry.h
spdlog::sinks::null_sink_mt
null_sink< details::null_mutex > null_sink_mt
Definition: null_sink.h:23
user_defined_example
void user_defined_example()
Definition: example.cpp:275
matplotlibcpp::text
void text(Numeric x, Numeric y, const std::string &s="")
Definition: matplotlibcpp.h:1834
spdlog::basic_logger_mt
std::shared_ptr< logger > basic_logger_mt(const std::string &logger_name, const filename_t &filename, bool truncate=false, const file_event_handlers &event_handlers={})
Definition: basic_file_sink.h:43
binary_example
void binary_example()
Definition: example.cpp:176
ostr.h
fmt::v8::formatter
Definition: core.h:707
spdlog::set_default_logger
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition: spdlog-inl.h:120
spdlog::logger::info
void info(format_string_t< Args... > fmt, Args &&... args)
Definition: logger.h:156
count_lines
int count_lines(const char *filename)
Definition: async_bench.cpp:41
file_events_example
void file_events_example()
Definition: example.cpp:337
async_logger.h
function
function[avg_proc_duration, std_proc_duration]
Definition: parse_dl_file.m:1
spdlog::file_event_handlers::after_open
std::function< void(const filename_t &filename, std::FILE *file_stream)> after_open
Definition: common.h:311
bench_formatter
void bench_formatter(benchmark::State &state, std::string pattern)
Definition: formatter-bench.cpp:11
spdlog::file_event_handlers::after_close
std::function< void(const filename_t &filename)> after_close
Definition: common.h:313
spdlog::details::log_msg
Definition: log_msg.h:11
replace_default_logger_example
void replace_default_logger_example()
Definition: example.cpp:356
spdlog::async_factory_impl::create
static std::shared_ptr< async_logger > create(std::string logger_name, SinkArgs &&... args)
Definition: async.h:38
main
int main(int argc, char *argv[])
Definition: async_bench.cpp:72
bench_logger_fmt_string
void bench_logger_fmt_string(benchmark::State &state, std::shared_ptr< spdlog::logger > logger)
Definition: latency.cpp:42
std
Definition: json.hpp:5213
pattern_formatter.h
fmt
Definition: bin_to_hex.h:102
syslog_sink.h
spdlog::sinks::null_sink_st
null_sink< details::null_mutex > null_sink_st
Definition: null_sink.h:24
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:95
spdlog::shutdown
SPDLOG_INLINE void shutdown()
Definition: spdlog-inl.h:100
spdlog::details::registry::set_tp
void set_tp(std::shared_ptr< thread_pool > tp)
Definition: registry-inl.h:127
spdlog::cfg::load_env_levels
void load_env_levels()
Definition: env.h:28
SPDLOG_VER_MAJOR
#define SPDLOG_VER_MAJOR
Definition: version.h:6
spdlog::file_event_handlers::before_open
std::function< void(const filename_t &filename)> before_open
Definition: common.h:310
spdlog::create_async_nb
std::shared_ptr< spdlog::logger > create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
Definition: async.h:70
utils
Definition: utils.h:12
spdlog::dump_backtrace
SPDLOG_INLINE void dump_backtrace()
Definition: spdlog-inl.h:45
err_handler_example
void err_handler_example()
Definition: example.cpp:281
spdlog::create_async
std::shared_ptr< spdlog::logger > create_async(std::string logger_name, SinkArgs &&... sink_args)
Definition: async.h:64
thread_fun
void thread_fun(std::shared_ptr< spdlog::logger > logger, int howmany)
Definition: async_bench.cpp:154
SPDLOG_VER_PATCH
#define SPDLOG_VER_PATCH
Definition: version.h:8
l
l
Definition: parse_all_dl.m:71
my_type::i
int i
Definition: example.cpp:260
spdlog::null_logger_st
std::shared_ptr< logger > null_logger_st(const std::string &logger_name)
Definition: null_sink.h:37
spdlog::filename_t
std::string filename_t
Definition: common.h:122
SPDLOG_VER_MINOR
#define SPDLOG_VER_MINOR
Definition: version.h:7
verify_file
void verify_file(const char *filename, int expected_count)
Definition: async_bench.cpp:56
ranges.h
spdlog::debug
void debug(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:149
spdlog::init_thread_pool
void init_thread_pool(size_t q_size, size_t thread_count, std::function< void()> on_thread_start, std::function< void()> on_thread_stop)
Definition: async.h:76
max_threads
static const int max_threads
Definition: bench.cpp:38
stopwatch_example
void stopwatch_example()
Definition: example.cpp:225
load_levels_example
void load_levels_example()
Definition: example.cpp:140
spdlog::details::registry::get_tp
std::shared_ptr< thread_pool > get_tp()
Definition: registry-inl.h:133
my_type::my_type
my_type(int i)
Definition: example.cpp:261
main
int main(int argc, char *argv[])
Definition: formatter-bench.cpp:59
stopwatch.h
spdlog::log
void log(source_loc source, level::level_enum lvl, format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:131
utils::format
std::string format(const T &value)
Definition: utils.h:15
spdlog::details::registry::instance
static registry & instance()
Definition: registry-inl.h:291