2
0
mirror of https://github.com/boostorg/redis.git synced 2026-01-19 04:42:09 +00:00
Files
redis/test/common.cpp
Anarthal (Rubén Pérez) f04d97ffa5 Updates the Logger interface to allow extensibility and type erasure (#273)
Removes all the logger::on_xxx functions
Removes the Logger template parameter to async_run
Adds a logger constructor that allows passing a std::function to customize logging behavior
Adds constructors to connection and basic_connection taking a logger
Deprecates config::logger_prefix
Deprecates the async_run overload taking a logger parameter
Deprecates the basic_connection::async_run overload not taking any config object
Deprecates the basic_connection::next_layer_type typedef
Makes the default log level logger::info
Makes the logging thread-safe
Cleans up deprecated functionality from examples
Adds docs on logging
Adds an example on how to integrate spdlog into Boost.Redis logging

close #213
2025-06-23 12:07:21 +02:00

72 lines
1.7 KiB
C++

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/consign.hpp>
#include "common.hpp"
#include <cstdlib>
#include <iostream>
#include <stdexcept>
namespace net = boost::asio;
struct run_callback {
std::shared_ptr<boost::redis::connection> conn;
boost::redis::operation op;
boost::system::error_code expected;
void operator()(boost::system::error_code const& ec) const
{
std::cout << "async_run: " << ec.message() << std::endl;
conn->cancel(op);
}
};
void run(
std::shared_ptr<boost::redis::connection> conn,
boost::redis::config cfg,
boost::system::error_code ec,
boost::redis::operation op)
{
conn->async_run(cfg, run_callback{conn, op, ec});
}
static std::string safe_getenv(const char* name, const char* default_value)
{
// MSVC doesn't like getenv
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
const char* res = std::getenv(name);
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
return res ? res : default_value;
}
std::string get_server_hostname() { return safe_getenv("BOOST_REDIS_TEST_SERVER", "localhost"); }
boost::redis::config make_test_config()
{
boost::redis::config cfg;
cfg.addr.host = get_server_hostname();
cfg.max_read_size = 1000000;
return cfg;
}
#ifdef BOOST_ASIO_HAS_CO_AWAIT
void run_coroutine_test(net::awaitable<void> op, std::chrono::steady_clock::duration timeout)
{
net::io_context ioc;
bool finished = false;
net::co_spawn(ioc, std::move(op), [&finished](std::exception_ptr p) {
if (p)
std::rethrow_exception(p);
finished = true;
});
ioc.run_for(timeout);
if (!finished)
throw std::runtime_error("Coroutine test did not finish");
}
#endif // BOOST_ASIO_HAS_CO_AWAIT