-
Notifications
You must be signed in to change notification settings - Fork 374
feat utest: introduce LoggingFixture #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||
| #pragma once | ||||||
|
|
||||||
| #include <userver/utest/utest.hpp> | ||||||
| #include <userver/logging/log.hpp> | ||||||
| #include <userver/logging/null_logger.hpp> | ||||||
| #include <userver/logging/impl/logger_base.hpp> | ||||||
|
|
||||||
| #include <atomic> | ||||||
| #include <concepts> | ||||||
| #include <iostream> | ||||||
| #include <mutex> | ||||||
| #include <string_view> | ||||||
| #include <thread> | ||||||
|
|
||||||
| USERVER_NAMESPACE_BEGIN | ||||||
|
|
||||||
| namespace utest { | ||||||
|
|
||||||
| namespace detail { | ||||||
|
|
||||||
| class CoutLogger final : public logging::impl::TextLogger { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think, is it decent to propose logger change: get TextLogger out of the impl namespace?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave it in We have an asynchronous logger |
||||||
| public: | ||||||
| CoutLogger() : TextLogger(logging::Format::kTskv) | ||||||
| { | ||||||
| SetLevel(logging::GetDefaultLoggerLevel()); // consider gtest_hooks.hpp usage | ||||||
| } | ||||||
|
|
||||||
| void Log(logging::Level level, logging::impl::formatters::LoggerItemRef item) override | ||||||
| { | ||||||
| UASSERT(dynamic_cast<logging::impl::TextLogItem*>(&item)); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure here also: may be it's enough to check via static_cast on the next line.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave the UASSERT as is. Looks right |
||||||
| auto& str = static_cast<logging::impl::TextLogItem&>(item); | ||||||
| std::lock_guard lock(m_mutex); | ||||||
| std::cout << std::this_thread::get_id() | ||||||
| << "\t" << logging::ToString(level) | ||||||
| << "\t" << std::string_view(str.log_line.begin(), str.log_line.end()) | ||||||
| << std::endl; | ||||||
| } | ||||||
| private: | ||||||
| std::mutex m_mutex; | ||||||
| }; | ||||||
|
|
||||||
| class LoggingFixtureEnvironment | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to public, rename to CoutLoggingFixture and inherit from:
Something like: namespace detail {
logging::LoggerPtr MakeCoutLogger() {
static CoutLogger g_cout;
return std::shared_ptr<logging::Logger>(nullptr, &g_cout);
}
}
template <class Base>
class CoutLoggingFixtureBase: public DefaultLogginFixture<Base> {
LoggingFixtureEnvironment()
{
SetDefaultLoggerRef(detail::MakeCoutLogger());
}
};
using CoutLoggingFixture = CoutLoggingFixtureBase<::testing::Test>; |
||||||
| { | ||||||
| CoutLogger m_logger; | ||||||
|
|
||||||
| static auto& NonOwningNullLogger() noexcept | ||||||
| { | ||||||
| static std::atomic<logging::impl::LoggerBase*> nullLoggerPtr(&logging::GetNullLogger()); | ||||||
| return nullLoggerPtr; | ||||||
| } | ||||||
|
|
||||||
| public: | ||||||
| LoggingFixtureEnvironment() | ||||||
| { | ||||||
| logging::impl::SetDefaultLoggerRef(m_logger); | ||||||
| } | ||||||
|
|
||||||
| ~LoggingFixtureEnvironment() | ||||||
| { | ||||||
| logging::impl::SetDefaultLoggerRef(*NonOwningNullLogger()); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| } // namespace detail | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw I'm not sure, but may be if we provide LoggingFixtureEx along with, we'd better get LoggingFixtureEnvironment outta detail namespace?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The the above comment, it places the Fixture to public |
||||||
|
|
||||||
| /// @ingroup userver_utest | ||||||
| /// | ||||||
| /// @brief Fixture for logger usage due to unit tests | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, also add references to
|
||||||
| struct LoggingFixture : ::testing::Test, detail::LoggingFixtureEnvironment {}; | ||||||
|
|
||||||
| template<typename DerivedEnvironment> | ||||||
| struct LoggingFixtureEx : ::testing::Test, DerivedEnvironment | ||||||
| { | ||||||
| static_assert(std::derived_from<DerivedEnvironment, detail::LoggingFixtureEnvironment>); | ||||||
| }; | ||||||
|
|
||||||
| } // namespace utest | ||||||
|
|
||||||
| USERVER_NAMESPACE_END | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <userver/utest/logging_fixture.hpp> | ||
| #include <userver/utest/utest.hpp> | ||
|
|
||
| USERVER_NAMESPACE_BEGIN | ||
|
|
||
| namespace utest | ||
| { | ||
|
|
||
| UTEST_F(LoggingFixture, DebugMessage) | ||
| { | ||
| LOG_DEBUG() << "Test message"; | ||
| } | ||
|
|
||
| UTEST_F(LoggingFixture, InfoMessage) | ||
| { | ||
| LOG_INFO() << "Test message"; | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we provide an example on DerivedEnvironment or the usage is obvious?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to have more examples than less %) Also use |
||
| } // namespace utest | ||
|
|
||
| USERVER_NAMESPACE_END | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this file into universal/utest/include/userver/utest/ directory (or even merge into universal/utest/include/userver/utest/default_logger_fixture.hpp)