Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // Copyright (c) 2025 Marcelo Zimbres Silva (mzimbres@gmail.com),
0003 // Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 
0009 #ifndef BOOST_REDIS_LOG_TO_STDERR_HPP
0010 #define BOOST_REDIS_LOG_TO_STDERR_HPP
0011 
0012 #include <algorithm>
0013 #include <cstddef>
0014 #include <cstdio>
0015 #include <string_view>
0016 
0017 namespace boost::redis::detail {
0018 
0019 // Shared by several ipp files
0020 inline void log_to_file(FILE* f, std::string_view msg, const char* prefix = "(Boost.Redis) ")
0021 {
0022    // If the message is empty, data() might return a null pointer
0023    const char* msg_ptr = msg.empty() ? "" : msg.data();
0024 
0025    // Precision should be an int when passed to fprintf. Technically,
0026    // message could be larger than INT_MAX. Impose a sane limit on message sizes
0027    // to prevent memory problems
0028    auto precision = static_cast<int>((std::min)(msg.size(), static_cast<std::size_t>(0xffffu)));
0029 
0030    // Log the message. None of our messages should contain NULL bytes, so this should be OK.
0031    // We choose fprintf over std::clog because it's safe in multi-threaded environments.
0032    std::fprintf(f, "%s%.*s\n", prefix, precision, msg_ptr);
0033 }
0034 
0035 }  // namespace boost::redis::detail
0036 
0037 #endif