ClickHouse/src/Common/tests/gtest_log.cpp

53 lines
1.7 KiB
C++
Raw Normal View History

2020-05-29 00:09:12 +00:00
#include <string>
#include <vector>
#include <common/logger_useful.h>
#include <gtest/gtest.h>
#include <Poco/Logger.h>
#include <Poco/AutoPtr.h>
#include <Poco/NullChannel.h>
2021-09-03 10:07:40 +00:00
#include <Poco/StreamChannel.h>
#include <sstream>
2020-05-29 00:09:12 +00:00
TEST(Logger, Log)
{
Poco::Logger::root().setLevel("none");
Poco::Logger::root().setChannel(Poco::AutoPtr<Poco::NullChannel>(new Poco::NullChannel()));
2020-05-30 21:57:37 +00:00
Poco::Logger * log = &Poco::Logger::get("Log");
2020-05-29 00:09:12 +00:00
/// This test checks that we don't pass this string to fmtlib, because it is the only argument.
EXPECT_NO_THROW(LOG_INFO(log, "Hello {} World"));
}
2021-09-03 10:07:40 +00:00
TEST(Logger, TestLog)
{
{ /// Test logs visible for test level
2021-09-03 14:57:29 +00:00
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
2021-09-04 12:46:41 +00:00
auto * log = &Poco::Logger::create("TestLogger", my_channel.get());
2021-09-03 14:57:29 +00:00
log->setLevel("test");
2021-09-03 10:07:40 +00:00
LOG_TEST(log, "Hello World");
EXPECT_EQ(oss.str(), "Hello World\n");
2021-09-03 14:57:29 +00:00
Poco::Logger::destroy("TestLogger");
2021-09-03 10:07:40 +00:00
}
{ /// Test logs invisible for other levels
2021-09-03 14:57:29 +00:00
for (const auto & level : {"trace", "debug", "information", "warning", "error", "fatal"})
2021-09-03 10:07:40 +00:00
{
2021-09-03 14:57:29 +00:00
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
auto my_channel = Poco::AutoPtr<Poco::StreamChannel>(new Poco::StreamChannel(oss));
2021-09-04 12:46:41 +00:00
auto * log = &Poco::Logger::create(std::string{level} + "_Logger", my_channel.get());
2021-09-03 14:57:29 +00:00
log->setLevel(level);
2021-09-03 10:07:40 +00:00
LOG_TEST(log, "Hello World");
EXPECT_EQ(oss.str(), "");
2021-09-03 14:57:29 +00:00
Poco::Logger::destroy(std::string{level} + "_Logger");
2021-09-03 10:07:40 +00:00
}
}
}