Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 09:00:18

0001 /* Copyright (c) 2018-2025 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_LOG_UTILS_HPP
0008 #define BOOST_REDIS_LOG_UTILS_HPP
0009 
0010 #include <boost/redis/logger.hpp>
0011 
0012 #include <boost/core/ignore_unused.hpp>
0013 #include <boost/system/error_code.hpp>
0014 
0015 #include <cstddef>
0016 #include <string>
0017 #include <string_view>
0018 #include <type_traits>
0019 
0020 namespace boost::redis::detail {
0021 
0022 // Internal trait that defines how to log different types.
0023 // The base template applies to types convertible to string_view
0024 template <class T>
0025 struct log_traits {
0026    // log should convert the input value to string and append it to the supplied buffer
0027    static inline void log(std::string& to, std::string_view value) { to += value; }
0028 };
0029 
0030 // Formatting size_t and error codes is shared between almost all FSMs, so it's defined here.
0031 // Support for types used only in one FSM should be added in the relevant FSM file.
0032 template <>
0033 struct log_traits<std::size_t> {
0034    static inline void log(std::string& to, std::size_t value) { to += std::to_string(value); }
0035 };
0036 
0037 template <>
0038 struct log_traits<system::error_code> {
0039    static inline void log(std::string& to, system::error_code value)
0040    {
0041       // Using error_code::what() includes any source code info
0042       // that the error may contain, making the messages too long.
0043       // This implementation was taken from error_code::what()
0044       to += value.message();
0045       to += " [";
0046       to += value.to_string();
0047       to += ']';
0048    }
0049 };
0050 
0051 template <class... Args>
0052 void format_log_args(std::string& to, const Args&... args)
0053 {
0054    auto dummy = {(log_traits<Args>::log(to, args), 0)...};
0055    ignore_unused(dummy);
0056 }
0057 
0058 // Logs a message with the specified severity to the logger.
0059 // Formatting won't be performed if the logger's level is inferior to lvl.
0060 // args are stringized using log_traits, and concatenated.
0061 template <class Arg0, class... Rest>
0062 void log(buffered_logger& to, logger::level lvl, const Arg0& arg0, const Rest&... arg_rest)
0063 {
0064    // Severity check
0065    if (to.lgr.lvl < lvl)
0066       return;
0067 
0068    // Optimization: if we get passed a single string, don't copy it to the buffer
0069    if constexpr (sizeof...(Rest) == 0u && std::is_convertible_v<Arg0, std::string_view>) {
0070       to.lgr.fn(lvl, arg0);
0071    } else {
0072       to.buffer.clear();
0073       format_log_args(to.buffer, arg0, arg_rest...);
0074       to.lgr.fn(lvl, to.buffer);
0075    }
0076 }
0077 
0078 // Shorthand for each log level we use
0079 template <class... Args>
0080 void log_debug(buffered_logger& to, const Args&... args)
0081 {
0082    log(to, logger::level::debug, args...);
0083 }
0084 
0085 template <class... Args>
0086 void log_info(buffered_logger& to, const Args&... args)
0087 {
0088    log(to, logger::level::info, args...);
0089 }
0090 
0091 template <class... Args>
0092 void log_err(buffered_logger& to, const Args&... args)
0093 {
0094    log(to, logger::level::err, args...);
0095 }
0096 
0097 }  // namespace boost::redis::detail
0098 
0099 #endif  // BOOST_REDIS_LOGGER_HPP