spdlog
Loading...
Searching...
No Matches
test_misc.cpp
Go to the documentation of this file.
1#include "includes.h"
2#include "test_sink.h"
4
5template<class T>
7{
8
11
12 spdlog::logger oss_logger("oss", oss_sink);
13 oss_logger.set_level(logger_level);
14 oss_logger.set_pattern("%v");
15 oss_logger.info(what);
16
17 return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
18}
19
20TEST_CASE("basic_logging ", "[basic_logging]")
21{
22 // const char
23 REQUIRE(log_info("Hello") == "Hello");
24 REQUIRE(log_info("").empty());
25
26 // std::string
27 REQUIRE(log_info(std::string("Hello")) == "Hello");
28 REQUIRE(log_info(std::string()).empty());
29
30 // Numbers
31 REQUIRE(log_info(5) == "5");
32 REQUIRE(log_info(5.6) == "5.6");
33
34 // User defined class
35 // REQUIRE(log_info(some_logged_class("some_val")) == "some_val");
36}
37
38TEST_CASE("log_levels", "[log_levels]")
39{
40 REQUIRE(log_info("Hello", spdlog::level::err).empty());
41 REQUIRE(log_info("Hello", spdlog::level::critical).empty());
42 REQUIRE(log_info("Hello", spdlog::level::info) == "Hello");
43 REQUIRE(log_info("Hello", spdlog::level::debug) == "Hello");
44 REQUIRE(log_info("Hello", spdlog::level::trace) == "Hello");
45}
46
57
68
81
82TEST_CASE("periodic flush", "[periodic_flush]")
83{
85 auto logger = spdlog::create<test_sink_mt>("periodic_flush");
86 auto test_sink = std::static_pointer_cast<test_sink_mt>(logger->sinks()[0]);
87
90 REQUIRE(test_sink->flush_counter() == 1);
93}
94
95TEST_CASE("clone-logger", "[clone]")
96{
98 auto test_sink = std::make_shared<test_sink_mt>();
99 auto logger = std::make_shared<spdlog::logger>("orig", test_sink);
100 logger->set_pattern("%v");
101 auto cloned = logger->clone("clone");
102
103 REQUIRE(cloned->name() == "clone");
104 REQUIRE(logger->sinks() == cloned->sinks());
105 REQUIRE(logger->level() == cloned->level());
106 REQUIRE(logger->flush_level() == cloned->flush_level());
107 logger->info("Some message 1");
108 cloned->info("Some message 2");
109
110 REQUIRE(test_sink->lines().size() == 2);
111 REQUIRE(test_sink->lines()[0] == "Some message 1");
112 REQUIRE(test_sink->lines()[1] == "Some message 2");
113
115}
116
117TEST_CASE("clone async", "[clone]")
118{
121 auto test_sink = std::make_shared<test_sink_st>();
122 auto logger = std::make_shared<spdlog::async_logger>("orig", test_sink, spdlog::thread_pool());
123 logger->set_pattern("%v");
124 auto cloned = logger->clone("clone");
125
126 REQUIRE(cloned->name() == "clone");
127 REQUIRE(logger->sinks() == cloned->sinks());
128 REQUIRE(logger->level() == cloned->level());
129 REQUIRE(logger->flush_level() == cloned->flush_level());
130
131 logger->info("Some message 1");
132 cloned->info("Some message 2");
133
135
136 REQUIRE(test_sink->lines().size() == 2);
137 REQUIRE(test_sink->lines()[0] == "Some message 1");
138 REQUIRE(test_sink->lines()[1] == "Some message 2");
139
141}
142
143TEST_CASE("to_hex", "[to_hex]")
144{
147 spdlog::logger oss_logger("oss", oss_sink);
148
149 std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
150 oss_logger.info("{}", spdlog::to_hex(v));
151
152 auto output = oss.str();
153 REQUIRE(ends_with(output, "0000: 09 0a 0b 0c ff ff" + std::string(spdlog::details::os::default_eol)));
154}
155
156TEST_CASE("to_hex_upper", "[to_hex]")
157{
160 spdlog::logger oss_logger("oss", oss_sink);
161
162 std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
163 oss_logger.info("{:X}", spdlog::to_hex(v));
164
165 auto output = oss.str();
166 REQUIRE(ends_with(output, "0000: 09 0A 0B 0C FF FF" + std::string(spdlog::details::os::default_eol)));
167}
168
169TEST_CASE("to_hex_no_delimiter", "[to_hex]")
170{
173 spdlog::logger oss_logger("oss", oss_sink);
174
175 std::vector<unsigned char> v{9, 0xa, 0xb, 0xc, 0xff, 0xff};
176 oss_logger.info("{:sX}", spdlog::to_hex(v));
177
178 auto output = oss.str();
179 REQUIRE(ends_with(output, "0000: 090A0B0CFFFF" + std::string(spdlog::details::os::default_eol)));
180}
181
182TEST_CASE("to_hex_show_ascii", "[to_hex]")
183{
186 spdlog::logger oss_logger("oss", oss_sink);
187
188 std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
189 oss_logger.info("{:Xsa}", spdlog::to_hex(v, 8));
190
191 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
192}
193
194TEST_CASE("to_hex_different_size_per_line", "[to_hex]")
195{
198 spdlog::logger oss_logger("oss", oss_sink);
199
200 std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
201
202 oss_logger.info("{:Xsa}", spdlog::to_hex(v, 10));
203 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF ...A.K.." + std::string(spdlog::details::os::default_eol)));
204
205 oss_logger.info("{:Xs}", spdlog::to_hex(v, 10));
206 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
207
208 oss_logger.info("{:Xsa}", spdlog::to_hex(v, 6));
209 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B ...A.K" + std::string(spdlog::details::os::default_eol) + "0006: FFFF .." +
211
212 oss_logger.info("{:Xs}", spdlog::to_hex(v, 6));
213 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4B" + std::string(spdlog::details::os::default_eol) + "0006: FFFF" +
215}
216
217TEST_CASE("to_hex_no_ascii", "[to_hex]")
218{
221 spdlog::logger oss_logger("oss", oss_sink);
222
223 std::vector<unsigned char> v{9, 0xa, 0xb, 0x41, 0xc, 0x4b, 0xff, 0xff};
224 oss_logger.info("{:Xs}", spdlog::to_hex(v, 8));
225
226 REQUIRE(ends_with(oss.str(), "0000: 090A0B410C4BFFFF" + std::string(spdlog::details::os::default_eol)));
227
228 oss_logger.info("{:Xsna}", spdlog::to_hex(v, 8));
229
231}
232
233TEST_CASE("default logger API", "[default logger]")
234{
237
239 spdlog::set_pattern("*** %v");
240
242 spdlog::trace("hello trace");
243 REQUIRE(oss.str() == "*** hello trace" + std::string(spdlog::details::os::default_eol));
244
245 oss.str("");
246 spdlog::debug("hello debug");
247 REQUIRE(oss.str() == "*** hello debug" + std::string(spdlog::details::os::default_eol));
248
249 oss.str("");
250 spdlog::info("Hello");
252
253 oss.str("");
254 spdlog::warn("Hello again {}", 2);
255 REQUIRE(oss.str() == "*** Hello again 2" + std::string(spdlog::details::os::default_eol));
256
257 oss.str("");
258 spdlog::error(123);
260
261 oss.str("");
262 spdlog::critical(std::string("some string"));
263 REQUIRE(oss.str() == "*** some string" + std::string(spdlog::details::os::default_eol));
264
265 oss.str("");
267 spdlog::debug("should not be logged");
268 REQUIRE(oss.str().empty());
271}
#define TEST_CASE(...)
Definition catch.hpp:15119
#define REQUIRE(...)
Definition catch.hpp:15083
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.h:156
void set_pattern(std::string pattern, pattern_time_type time_type=pattern_time_type::local)
Definition logger-inl.h:101
void set_level(level::level_enum log_level)
Definition logger-inl.h:67
T
Definition core.h:320
T make_shared(T... args)
static SPDLOG_CONSTEXPR const char * default_eol
Definition details/os.h:32
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition os-inl.h:369
SPDLOG_INLINE const string_view_t & to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition common-inl.h:23
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
Definition common-inl.h:33
SPDLOG_INLINE const char * to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
Definition common-inl.h:28
std::shared_ptr< spdlog::details::thread_pool > thread_pool()
Definition async.h:89
SPDLOG_INLINE void flush_every(std::chrono::seconds interval)
Definition spdlog-inl.h:70
void critical(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:173
void warn(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:161
void error(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:167
SPDLOG_INLINE void set_level(level::level_enum log_level)
Definition spdlog-inl.h:60
void init_thread_pool(size_t q_size, size_t thread_count, std::function< void()> on_thread_start)
Definition async.h:76
void trace(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:143
SPDLOG_INLINE std::shared_ptr< spdlog::logger > default_logger()
Definition spdlog-inl.h:110
void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:155
details::dump_info< typename Container::const_iterator > to_hex(const Container &container, size_t size_per_line=32)
Definition bin_to_hex.h:63
SPDLOG_INLINE void set_default_logger(std::shared_ptr< spdlog::logger > default_logger)
Definition spdlog-inl.h:120
SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type)
Definition spdlog-inl.h:30
void debug(fmt::format_string< Args... > fmt, Args &&...args)
Definition spdlog.h:149
SPDLOG_INLINE void drop_all()
Definition spdlog-inl.h:95
T sleep_for(T... args)
T str(T... args)
std::string log_info(const T &what, spdlog::level::level_enum logger_level=spdlog::level::info)
Definition test_misc.cpp:6
bool ends_with(std::string const &value, std::string const &ending)
Definition utils.cpp:73