Loading...
Searching...
No Matches
cpp17_spdlog.cpp
1//
2// Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
3// Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
4//
5// Distributed under the Boost Software License, Version 1.0. (See accompanying
6// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7//
8
9#include <boost/redis/connection.hpp>
10#include <boost/redis/logger.hpp>
11
12#include <boost/asio/detached.hpp>
13#include <boost/system/error_code.hpp>
14
15#include <cstddef>
16#include <iostream>
17#include <spdlog/spdlog.h>
18#include <string_view>
19
20namespace asio = boost::asio;
21namespace redis = boost::redis;
22
23// Maps a Boost.Redis log level to a spdlog log level
24static spdlog::level::level_enum to_spdlog_level(redis::logger::level lvl)
25{
26 switch (lvl) {
27 // spdlog doesn't include the emerg and alert syslog levels,
28 // so we convert them to the highest supported level.
29 // Similarly, notice is similar to info
30 case redis::logger::level::emerg:
31 case redis::logger::level::alert:
32 case redis::logger::level::crit: return spdlog::level::critical;
33 case redis::logger::level::err: return spdlog::level::err;
34 case redis::logger::level::warning: return spdlog::level::warn;
35 case redis::logger::level::notice:
36 case redis::logger::level::info: return spdlog::level::info;
37 case redis::logger::level::debug:
38 default: return spdlog::level::debug;
39 }
40}
41
42// This function glues Boost.Redis logging and spdlog.
43// It should have the signature shown here. It will be invoked
44// by Boost.Redis whenever a message is to be logged.
45static void do_log(redis::logger::level level, std::string_view msg)
46{
47 spdlog::log(to_spdlog_level(level), "(Boost.Redis) {}", msg);
48}
49
50auto main(int argc, char* argv[]) -> int
51{
52 if (argc != 3) {
53 std::cerr << "Usage: " << argv[0] << " <server-host> <server-port>\n";
54 exit(1);
55 }
56
57 try {
58 // Create an execution context, required to create any I/O objects
59 asio::io_context ioc;
60
61 // Create a connection to connect to Redis, and pass it a custom logger.
62 // Boost.Redis will call do_log whenever it needs to log a message.
63 // Note that the function will only be called for messages with level >= info
64 // (i.e. filtering is done by Boost.Redis).
66 ioc,
67 redis::logger{redis::logger::level::info, do_log}
68 };
69
70 // Configuration to connect to the server
71 redis::config cfg;
72 cfg.addr.host = argv[1];
73 cfg.addr.port = argv[2];
74
75 // Run the connection with the specified configuration.
76 // This will establish the connection and keep it healthy
77 conn.async_run(cfg, asio::detached);
78
79 // Execute a request
81 req.push("PING", "Hello world");
82
84
85 conn.async_exec(req, resp, [&](boost::system::error_code ec, std::size_t /* bytes_read*/) {
86 if (ec) {
87 spdlog::error("Request failed: {}", ec.what());
88 exit(1);
89 } else {
90 spdlog::info("PING: {}", std::get<0>(resp).value());
91 }
92 conn.cancel();
93 });
94
95 // Actually run our example. Nothing will happen until we call run()
96 ioc.run();
97
98 } catch (std::exception const& e) {
99 spdlog::error("Error: {}", e.what());
100 return 1;
101 }
102}
A basic_connection that type erases the executor.
Creates Redis requests.
Definition request.hpp:46
void push(std::string_view cmd, Ts const &... args)
Appends a new command to the end of the request.
Definition request.hpp:147
address addr
Address of the Redis server.
Definition config.hpp:35
std::string port
Redis port.
Definition config.hpp:24
std::string host
Redis host.
Definition config.hpp:22
std::tuple< adapter::result< Ts >... > response
Response with compile-time size.
Definition response.hpp:25
Configure parameters used by the connection classes.
Definition config.hpp:30
Defines logging configuration.
Definition logger.hpp:20