Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:08:25

0001 /* Copyright (c) 2018-2024 Marcelo Zimbres Silva (mzimbres@gmail.com)
0002  *
0003  * Distributed under the Boost Software License, Version 1.0. (See
0004  * accompanying file LICENSE.txt)
0005  */
0006 
0007 #ifndef BOOST_REDIS_LOGGER_HPP
0008 #define BOOST_REDIS_LOGGER_HPP
0009 
0010 #include <boost/redis/response.hpp>
0011 #include <boost/asio/ip/tcp.hpp>
0012 #include <string>
0013 
0014 namespace boost::system {class error_code;}
0015 
0016 namespace boost::redis {
0017 
0018 /** @brief Logger class
0019  *  @ingroup high-level-api
0020  *
0021  *  The class can be passed to the connection objects to log to `std::clog`
0022  *
0023  *  Notice that currently this class has no stable interface. Users
0024  *  that don't want any logging can disable it by contructing a logger
0025  *  with logger::level::emerg to the connection.
0026  */
0027 class logger {
0028 public:
0029    /** @brief Syslog-like log levels
0030     *  @ingroup high-level-api
0031     */
0032    enum class level
0033    {
0034       /// Disabled
0035       disabled,
0036 
0037       /// Emergency
0038       emerg,
0039 
0040       /// Alert
0041       alert,
0042 
0043       /// Critical
0044       crit,
0045 
0046       /// Error
0047       err,
0048 
0049       /// Warning
0050       warning,
0051 
0052       /// Notice
0053       notice,
0054 
0055       /// Info
0056       info,
0057 
0058       /// Debug
0059       debug
0060    };
0061 
0062    /** @brief Constructor
0063     *  @ingroup high-level-api
0064     *
0065     *  @param l Log level.
0066     */
0067    logger(level l = level::debug)
0068    : level_{l}
0069    {}
0070 
0071    /** @brief Called when the resolve operation completes.
0072     *  @ingroup high-level-api
0073     *
0074     *  @param ec Error returned by the resolve operation.
0075     *  @param res Resolve results.
0076     */
0077    void on_resolve(system::error_code const& ec, asio::ip::tcp::resolver::results_type const& res);
0078 
0079    /** @brief Called when the connect operation completes.
0080     *  @ingroup high-level-api
0081     *
0082     *  @param ec Error returned by the connect operation.
0083     *  @param ep Endpoint to which the connection connected.
0084     */
0085    void on_connect(system::error_code const& ec, asio::ip::tcp::endpoint const& ep);
0086 
0087    /** @brief Called when the ssl handshake operation completes.
0088     *  @ingroup high-level-api
0089     *
0090     *  @param ec Error returned by the handshake operation.
0091     */
0092    void on_ssl_handshake(system::error_code const& ec);
0093 
0094    /** @brief Called when the write operation completes.
0095     *  @ingroup high-level-api
0096     *
0097     *  @param ec Error code returned by the write operation.
0098     *  @param payload The payload written to the socket.
0099     */
0100    void on_write(system::error_code const& ec, std::string const& payload);
0101 
0102    /** @brief Called when the read operation completes.
0103     *  @ingroup high-level-api
0104     *
0105     *  @param ec Error code returned by the read operation.
0106     *  @param n Number of bytes read.
0107     */
0108    void on_read(system::error_code const& ec, std::size_t n);
0109 
0110    /** @brief Called when the `HELLO` request completes.
0111     *  @ingroup high-level-api
0112     *
0113     *  @param ec Error code returned by the async_exec operation.
0114     *  @param resp Response sent by the Redis server.
0115     */
0116    void on_hello(system::error_code const& ec, generic_response const& resp);
0117 
0118    /** @brief Sets a prefix to every log message
0119     *  @ingroup high-level-api
0120     *
0121     *  @param prefix The prefix.
0122     */
0123    void set_prefix(std::string_view prefix)
0124    {
0125       prefix_ = prefix;
0126    }
0127 
0128    void trace(std::string_view message);
0129    void trace(std::string_view op, system::error_code const& ec);
0130 
0131 private:
0132    void write_prefix();
0133    level level_;
0134    std::string_view prefix_;
0135 };
0136 
0137 } // boost::redis
0138 
0139 #endif // BOOST_REDIS_LOGGER_HPP