Agora  1.2.0
Agora project
test_sink.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2018 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
10 #include "spdlog/fmt/fmt.h"
11 #include <chrono>
12 #include <mutex>
13 #include <thread>
14 
15 namespace spdlog {
16 namespace sinks {
17 
18 template<class Mutex>
19 class test_sink : public base_sink<Mutex>
20 {
21  const size_t lines_to_save = 100;
22 
23 public:
24  size_t msg_counter()
25  {
26  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
27  return msg_counter_;
28  }
29 
30  size_t flush_counter()
31  {
32  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
33  return flush_counter_;
34  }
35 
36  void set_delay(std::chrono::milliseconds delay)
37  {
38  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
39  delay_ = delay;
40  }
41 
42  // return last output without the eol
43  std::vector<std::string> lines()
44  {
45  std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
46  return lines_;
47  }
48 
49 protected:
50  void sink_it_(const details::log_msg &msg) override
51  {
52  memory_buf_t formatted;
53  base_sink<Mutex>::formatter_->format(msg, formatted);
54  // save the line without the eol
55  auto eol_len = strlen(details::os::default_eol);
56  if (lines_.size() < lines_to_save)
57  {
58  lines_.emplace_back(formatted.begin(), formatted.end() - eol_len);
59  }
60  msg_counter_++;
61  std::this_thread::sleep_for(delay_);
62  }
63 
64  void flush_() override
65  {
67  }
68 
69  size_t msg_counter_{0};
70  size_t flush_counter_{0};
71  std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
72  std::vector<std::string> lines_;
73 };
74 
77 
78 } // namespace sinks
79 } // namespace spdlog
ROTATING_LOG
#define ROTATING_LOG
Definition: test_file_logging.cpp:7
win_eventlog_sink.h
spdlog::level::to_string_view
const SPDLOG_INLINE string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common-inl.h:23
fmt::v8::detail::value_type
remove_cvref_t< decltype(*detail::range_begin(std::declval< Range >()))> value_type
Definition: ranges.h:226
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
test_path
static const char *const test_path
Definition: test_pattern_formatter.cpp:401
custom_test_flag::some_txt
std::string some_txt
Definition: test_pattern_formatter.cpp:362
try_create_dir
bool try_create_dir(const spdlog::filename_t &path, const spdlog::filename_t &normalized_path)
Definition: test_create_dir.cpp:9
ends_with
bool ends_with(std::string const &value, std::string const &ending)
Definition: utils.cpp:73
spdlog::details::file_helper::size
size_t size() const
Definition: file_helper-inl.h:122
spdlog::details::padding_info
Definition: pattern_formatter.h:23
spdlog::file_event_handlers
Definition: common.h:308
spdlog::details::file_helper::open
void open(const filename_t &fname, bool truncate=false)
Definition: file_helper-inl.h:32
mm_gui.w
w
Definition: mm_gui.py:119
require_message_count
void require_message_count(const std::string &filename, const std::size_t messages)
Definition: utils.cpp:49
CHECK_THROWS_AS
#define CHECK_THROWS_AS(expr, exceptionType)
Definition: catch.hpp:17677
spdlog::level::trace
@ trace
Definition: common.h:213
spdlog::sinks::test_sink::lines
std::vector< std::string > lines()
Definition: test_sink.h:43
test_rotate
static void test_rotate(int days_to_run, uint16_t max_days, uint16_t expected_n_files)
Definition: test_daily_logger.cpp:166
file_contents
std::string file_contents(const std::string &filename)
Definition: utils.cpp:24
spdlog::error
void error(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:167
base_sink.h
test_pad6
void test_pad6(std::size_t n, const char *expected)
Definition: test_fmt_helper.cpp:31
TEST_FILENAME
#define TEST_FILENAME
Definition: test_macros.cpp:11
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
spdlog::sinks::test_sink_mt
test_sink< std::mutex > test_sink_mt
Definition: test_sink.h:75
TEST_CASE
#define TEST_CASE(...)
Definition: catch.hpp:17695
spdlog::details::os::sleep_for_millis
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition: os-inl.h:376
test_split_ext
static void test_split_ext(const spdlog::filename_t::value_type *fname, const spdlog::filename_t::value_type *expect_base, const spdlog::filename_t::value_type *expect_ext)
Definition: test_file_helper.cpp:67
custom_test_flag::format
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
Definition: test_pattern_formatter.cpp:348
spdlog::logger::set_formatter
void set_formatter(std::unique_ptr< formatter > f)
Definition: logger-inl.h:84
spdlog::sinks::daily_filename_format_calculator::calc_filename
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
Definition: daily_file_sink.h:49
spdlog::details::os::path_exists
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os-inl.h:187
spdlog::sinks::test_sink::lines_to_save
const size_t lines_to_save
Definition: test_sink.h:21
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
spdlog::sinks::test_sink::delay_
std::chrono::milliseconds delay_
Definition: test_sink.h:71
spdlog::critical
void critical(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:173
includes.h
spdlog::details::file_helper
Definition: file_helper.h:16
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
fmt::v8::basic_string_view
Definition: core.h:448
custom_test_flag::get_padding_info
spdlog::details::padding_info get_padding_info()
Definition: test_pattern_formatter.cpp:357
env.h
spdlog::sinks::test_sink::flush_
void flush_() override
Definition: test_sink.h:64
REQUIRE_THROWS_AS
#define REQUIRE_THROWS_AS(expr, exceptionType)
Definition: catch.hpp:17663
spdlog::sinks::daily_filename_calculator::calc_filename
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
Definition: daily_file_sink.h:31
spdlog::details::file_helper::reopen
void reopen(bool truncate)
Definition: file_helper-inl.h:76
custom_test_flag::custom_test_flag
custom_test_flag(std::string txt)
Definition: test_pattern_formatter.cpp:344
spdlog::logger
Definition: logger.h:54
spdlog::sinks::test_sink::sink_it_
void sink_it_(const details::log_msg &msg) override
Definition: test_sink.h:50
spdlog::logger::debug
void debug(format_string_t< Args... > fmt, Args &&... args)
Definition: logger.h:150
spdlog::sinks::test_sink_st
test_sink< details::null_mutex > test_sink_st
Definition: test_sink.h:76
spdlog::file_event_handlers::before_close
std::function< void(const filename_t &filename, std::FILE *file_stream)> before_close
Definition: common.h:312
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
spdlog::level::level_enum
level_enum
Definition: common.h:211
spdlog::level::debug
@ debug
Definition: common.h:214
spdlog::details::flag_formatter::padinfo_
padding_info padinfo_
Definition: pattern_formatter.h:61
flags
list flags
Definition: .ycm_extra_conf.py:39
spdlog::logger::set_level
void set_level(level::level_enum log_level)
Definition: logger-inl.h:67
spdlog::logger::enable_backtrace
void enable_backtrace(size_t n_messages)
Definition: logger-inl.h:108
spdlog::sinks::test_sink::lines_
std::vector< std::string > lines_
Definition: test_sink.h:72
count_files
std::size_t count_files(const std::string &folder)
Definition: utils.cpp:109
tested_logger_name2
static const char *const tested_logger_name2
Definition: test_registry.cpp:4
spdlog::set_automatic_registration
SPDLOG_INLINE void set_automatic_registration(bool automatic_registration)
Definition: spdlog-inl.h:105
spdlog::pattern_formatter
Definition: pattern_formatter.h:77
spdlog::warn
void warn(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:161
spdlog::level::err
@ err
Definition: common.h:217
spdlog::details::mpmc_blocking_queue
Definition: mpmc_blocking_q.h:22
create_msg
static spdlog::details::log_msg create_msg(std::chrono::seconds offset)
Definition: test_daily_logger.cpp:158
spdlog::sinks::base_sink::log
void log(const details::log_msg &msg) final
Definition: base_sink-inl.h:26
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
spdlog::log_clock
std::chrono::system_clock log_clock
Definition: common.h:126
spdlog::logger::dump_backtrace
void dump_backtrace()
Definition: logger-inl.h:119
T
T
Definition: simulate_performance.m:4
test_sink.h
SIMPLE_LOG
#define SIMPLE_LOG
Definition: test_file_logging.cpp:6
spdlog::level::from_str
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition: common-inl.h:33
filename
filename
Definition: parse_all_dl.m:14
argv.h
null_mutex.h
custom_ex
Definition: test_errors.cpp:45
fmt_helper.h
fmt::v8::basic_memory_buffer
Definition: format.h:677
millis_from
static milliseconds millis_from(const test_clock::time_point &tp0)
Definition: test_mpmc_q.cpp:6
fmt::v8::runtime
auto runtime(const S &s) -> basic_runtime< char_t< S >>
Definition: core.h:3098
custom_test_flag::clone
std::unique_ptr< custom_flag_formatter > clone() const override
Definition: test_pattern_formatter.cpp:364
spdlog::details::file_helper::close
void close()
Definition: file_helper-inl.h:93
write_with_helper
static void write_with_helper(file_helper &helper, size_t howmany)
Definition: test_file_helper.cpp:10
spdlog::details::os::create_dir
SPDLOG_INLINE bool create_dir(const filename_t &path)
Definition: os-inl.h:545
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::sinks::test_sink::set_delay
void set_delay(std::chrono::milliseconds delay)
Definition: test_sink.h:36
failing_sink
Definition: test_errors.cpp:11
spdlog
Definition: async.h:25
fmt::v8::detail::fputs
void fputs(const Char *chars, FILE *stream)
Definition: color.h:478
spdlog::sinks::test_sink
Definition: test_sink.h:19
SPDLOG_LOGGER_TRACE
#define SPDLOG_LOGGER_TRACE(logger,...)
Definition: spdlog.h:294
spdlog::sinks::rotating_file_sink::calc_filename
static filename_t calc_filename(const filename_t &filename, std::size_t index)
Definition: rotating_file_sink-inl.h:55
SPDLOG_TRACE
#define SPDLOG_TRACE(...)
Definition: spdlog.h:295
SIMPLE_LOG
#define SIMPLE_LOG
Definition: test_errors.cpp:8
spdlog::sinks::base_sink
Definition: base_sink.h:19
SIMPLE_ASYNC_LOG
#define SIMPLE_ASYNC_LOG
Definition: test_errors.cpp:9
SPDLOG_LOGGER_DEBUG
#define SPDLOG_LOGGER_DEBUG(logger,...)
Definition: spdlog.h:302
spdlog::level::to_short_c_str
const SPDLOG_INLINE char * to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition: common-inl.h:28
failing_sink::flush_
void flush_() final
Definition: test_errors.cpp:19
spdlog::details::file_helper::filename
const filename_t & filename() const
Definition: file_helper-inl.h:131
spdlog::async_overflow_policy::overrun_oldest
@ overrun_oldest
i
for i
Definition: generate_data.m:107
fmt::v8::to_string
auto to_string(const T &value) -> std::string
Definition: format.h:2882
REQUIRE
#define REQUIRE(...)
Definition: catch.hpp:17659
fmt::v8::detail::buffer::begin
auto begin() -> T *
Definition: core.h:813
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
basic_file_sink.h
SPDLOG_DEBUG
#define SPDLOG_DEBUG(...)
Definition: spdlog.h:303
test_pad9
void test_pad9(std::size_t n, const char *expected)
Definition: test_fmt_helper.cpp:43
spdlog::details::os::localtime
SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
Definition: os-inl.h:84
spdlog::drop
SPDLOG_INLINE void drop(const std::string &name)
Definition: spdlog-inl.h:90
spdlog::details::mpmc_blocking_queue::dequeue_for
bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration)
Definition: mpmc_blocking_q.h:54
bin_to_hex.h
spdlog::source_loc
Definition: common.h:290
tested_logger_name
static const char *const tested_logger_name
Definition: test_registry.cpp:3
l1
l1
Definition: parse_all_dl.m:58
spdlog::sinks::daily_file_sink
Definition: daily_file_sink.h:115
start
end start
Definition: inspect_agora_results.m:95
spdlog::get
SPDLOG_INLINE std::shared_ptr< logger > get(const std::string &name)
Definition: spdlog-inl.h:20
TEST_FILENAME
#define TEST_FILENAME
Definition: test_async.cpp:6
spdlog::sinks::daily_file_sink_st
daily_file_sink< details::null_mutex > daily_file_sink_st
Definition: daily_file_sink.h:256
TEST_FILENAME
#define TEST_FILENAME
Definition: test_file_helper.cpp:6
spdlog::cfg::load_argv_levels
void load_argv_levels(int argc, const char **argv)
Definition: argv.h:24
get_filesize
std::size_t get_filesize(const std::string &filename)
Definition: utils.cpp:61
spdlog::details::fmt_helper::pad3
void pad3(T n, memory_buf_t &dest)
Definition: fmt_helper.h:126
fmt::v8::detail::buffer::end
auto end() -> T *
Definition: core.h:814
fmt::v8::basic_memory_buffer::append
void append(const ContiguousRange &range)
Definition: format.h:771
spdlog::info
void info(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:155
spdlog::spdlog_ex
Definition: common.h:276
spdlog::details::file_helper::write
void write(const memory_buf_t &buf)
Definition: file_helper-inl.h:112
spdlog::sinks::base_sink::set_formatter
void set_formatter(std::unique_ptr< spdlog::formatter > sink_formatter) final
Definition: base_sink-inl.h:47
spdlog::async_overflow_policy::block
@ block
spdlog::trace
void trace(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:143
spdlog::register_logger
SPDLOG_INLINE void register_logger(std::shared_ptr< logger > logger)
Definition: spdlog-inl.h:80
spdlog::sinks::test_sink::flush_counter_
size_t flush_counter_
Definition: test_sink.h:70
custom_daily_file_name_calculator::calc_filename
static spdlog::filename_t calc_filename(const spdlog::filename_t &basename, const tm &now_tm)
Definition: test_daily_logger.cpp:54
custom_daily_file_name_calculator
Definition: test_daily_logger.cpp:52
n
n
Definition: simulate_performance.m:1
spdlog::set_default_logger
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition: spdlog-inl.h:120
log_info
std::string log_info(const T &what, spdlog::level::level_enum logger_level=spdlog::level::info)
Definition: test_misc.cpp:6
SPDLOG_FILENAME_T
#define SPDLOG_FILENAME_T(s)
Definition: common.h:123
spdlog::details::file_helper::flush
void flush()
Definition: file_helper-inl.h:85
spdlog::logger::info
void info(format_string_t< Args... > fmt, Args &&... args)
Definition: logger.h:156
prepare_logdir
void prepare_logdir()
Definition: utils.cpp:10
count_lines
int count_lines(const char *filename)
Definition: async_bench.cpp:41
test_clock
std::chrono::high_resolution_clock test_clock
Definition: test_mpmc_q.cpp:4
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
spdlog::pattern_time_type::local
@ local
spdlog::sinks::test_sink::msg_counter
size_t msg_counter()
Definition: test_sink.h:24
spdlog::details::os::default_eol
static const SPDLOG_CONSTEXPR char * default_eol
Definition: os.h:32
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
test_pad3
void test_pad3(uint32_t n, const char *expected)
Definition: test_fmt_helper.cpp:19
spdlog::pattern_time_type
pattern_time_type
Definition: common.h:267
spdlog::memory_buf_t
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition: common.h:155
spdlog::thread_pool
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition: async.h:95
REQUIRE_FALSE
#define REQUIRE_FALSE(...)
Definition: catch.hpp:17660
spdlog::cfg::load_env_levels
void load_env_levels()
Definition: env.h:28
spdlog::sinks::test_sink::msg_counter_
size_t msg_counter_
Definition: test_sink.h:69
custom_test_flag
Definition: test_pattern_formatter.cpp:341
spdlog::sinks::dup_filter_sink_st
dup_filter_sink< details::null_mutex > dup_filter_sink_st
Definition: dup_filter_sink.h:91
spdlog::file_event_handlers::before_open
std::function< void(const filename_t &filename)> before_open
Definition: common.h:310
INFO
#define INFO(msg)
Definition: catch.hpp:17690
spdlog::sinks::test_sink::flush_counter
size_t flush_counter()
Definition: test_sink.h:30
l
l
Definition: parse_all_dl.m:71
dup_filter_sink.h
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::level::critical
@ critical
Definition: common.h:218
spdlog::details::padding_info::width_
size_t width_
Definition: pattern_formatter.h:44
spdlog::drop_all
SPDLOG_INLINE void drop_all()
Definition: spdlog-inl.h:95
spdlog::debug
void debug(format_string_t< Args... > fmt, Args &&... args)
Definition: spdlog.h:149
spdlog::details::fmt_helper::pad6
void pad6(T n, memory_buf_t &dest)
Definition: fmt_helper.h:143
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
spdlog::details::fmt_helper::pad2
void pad2(int n, memory_buf_t &dest)
Definition: fmt_helper.h:101
failing_sink::sink_it_
void sink_it_(const spdlog::details::log_msg &) final
Definition: test_errors.cpp:14
fmt.h
log_to_str
static std::string log_to_str(const std::string &msg, const Args &... args)
Definition: test_pattern_formatter.cpp:8
spdlog::pattern_formatter::format
void format(const details::log_msg &msg, memory_buf_t &dest) override
Definition: pattern_formatter-inl.h:1057
spdlog::details::os::dir_name
SPDLOG_INLINE filename_t dir_name(const filename_t &path)
Definition: os-inl.h:584
utils::format
std::string format(const T &value)
Definition: utils.h:15
spdlog::sinks::dup_filter_sink_mt
dup_filter_sink< std::mutex > dup_filter_sink_mt
Definition: dup_filter_sink.h:90
spdlog::logger::set_pattern
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition: logger-inl.h:101
spdlog::details::fmt_helper::pad9
void pad9(T n, memory_buf_t &dest)
Definition: fmt_helper.h:149
test_pad2
void test_pad2(int n, const char *expected)
Definition: test_fmt_helper.cpp:7