forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_logging_ostream_test.cc
96 lines (81 loc) · 3.26 KB
/
text_logging_ostream_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* clang-format off to disable clang-format-includes */
#include "drake/common/text_logging.h"
/* clang-format on */
#include <ostream>
#include <sstream>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
// The BUILD.bazel rules must supply this flag. This test code is compiled and
// run twice -- once with spdlog, and once without.
#ifndef TEXT_LOGGING_TEST_SPDLOG
#error Missing a required definition to compile this test case.
#endif
// Check for the expected HAVE_SPDLOG value.
#if TEXT_LOGGING_TEST_SPDLOG
#ifndef HAVE_SPDLOG
#error Missing HAVE_SPDLOG.
#endif
#else
#ifdef HAVE_SPDLOG
#error Unwanted HAVE_SPDLOG.
#endif
#endif
#ifdef HAVE_SPDLOG
#include <spdlog/sinks/dist_sink.h>
#include <spdlog/sinks/ostream_sink.h>
#endif // HAVE_SPDLOG
#include "drake/common/fmt_ostream.h"
namespace {
class Streamable {
[[maybe_unused]] // If we don't have spdlog, this function is dead code.
friend std::ostream& operator<<(std::ostream& os, const Streamable& c) {
return os << "OK";
}
};
using drake::fmt_streamed;
// Call each API function and macro to ensure that all of them compile.
// These should all compile and run both with and without spdlog.
GTEST_TEST(TextLoggingTest, SmokeTestStreamable) {
Streamable obj;
drake::log()->trace("drake::log()->trace test: {} {}", "OK",
fmt_streamed(obj));
drake::log()->debug("drake::log()->debug test: {} {}", "OK",
fmt_streamed(obj));
drake::log()->info("drake::log()->info test: {} {}", "OK", fmt_streamed(obj));
drake::log()->warn("drake::log()->warn test: {} {}", "OK", fmt_streamed(obj));
drake::log()->error("drake::log()->error test: {} {}", "OK",
fmt_streamed(obj));
drake::log()->critical("drake::log()->critical test: {} {}", "OK",
fmt_streamed(obj));
DRAKE_LOGGER_TRACE("DRAKE_LOGGER_TRACE macro test: {}, {}", "OK",
fmt_streamed(obj));
DRAKE_LOGGER_DEBUG("DRAKE_LOGGER_DEBUG macro test: {}, {}", "OK",
fmt_streamed(obj));
}
// We must run this test last because it changes the default configuration.
GTEST_TEST(TextLoggingTest, ZZZ_ChangeDefaultSink) {
// The getter should never return nullptr, even with spdlog disabled.
drake::logging::sink* const sink_base = drake::logging::get_dist_sink();
ASSERT_NE(sink_base, nullptr);
// The remainder of the test case only makes sense when spdlog is enabled.
#if TEXT_LOGGING_TEST_SPDLOG
// Our API promises that the result always has this subtype.
auto* const sink = dynamic_cast<spdlog::sinks::dist_sink_mt*>(sink_base);
ASSERT_NE(sink, nullptr);
// Redirect all logs to a memory stream.
std::ostringstream messages;
auto custom_sink = std::make_shared<spdlog::sinks::ostream_sink_st>(
messages, true /* flush */);
sink->set_sinks({custom_sink});
drake::log()->info("This is some good info!");
EXPECT_THAT(messages.str(), testing::EndsWith(
"[console] [info] This is some good info!\n"));
#endif
}
} // namespace
// To enable compiling without depending on @spdlog, we need to provide our own
// main routine. The default drake_cc_googletest_main depends on @spdlog.
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}