log_invoke.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // Copyright (c) 2023-2025 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MQTT5_LOG_INVOKE_HPP
  8. #define BOOST_MQTT5_LOG_INVOKE_HPP
  9. #include <boost/mqtt5/logger_traits.hpp>
  10. #include <boost/mqtt5/property_types.hpp>
  11. #include <boost/mqtt5/reason_codes.hpp>
  12. #include <boost/mqtt5/types.hpp>
  13. #include <boost/asio/ip/tcp.hpp>
  14. #include <boost/system/error_code.hpp>
  15. #include <string_view>
  16. #include <type_traits>
  17. namespace boost::mqtt5::detail {
  18. namespace asio = boost::asio;
  19. using boost::system::error_code;
  20. template <typename LoggerType>
  21. class log_invoke {
  22. LoggerType _logger;
  23. public:
  24. explicit log_invoke(LoggerType logger = {}) :
  25. _logger(std::move(logger))
  26. {}
  27. void at_resolve(
  28. error_code ec, std::string_view host, std::string_view port,
  29. const asio::ip::tcp::resolver::results_type& eps
  30. ) {
  31. if constexpr (has_at_resolve<LoggerType>)
  32. _logger.at_resolve(ec, host, port, eps);
  33. }
  34. void at_tcp_connect(error_code ec, asio::ip::tcp::endpoint ep) {
  35. if constexpr (has_at_tcp_connect<LoggerType>)
  36. _logger.at_tcp_connect(ec, ep);
  37. }
  38. void at_tls_handshake(error_code ec, asio::ip::tcp::endpoint ep) {
  39. if constexpr (has_at_tls_handshake<LoggerType>)
  40. _logger.at_tls_handshake(ec, ep);
  41. }
  42. void at_ws_handshake(error_code ec, asio::ip::tcp::endpoint ep) {
  43. if constexpr (has_at_ws_handshake<LoggerType>)
  44. _logger.at_ws_handshake(ec, ep);
  45. }
  46. void at_connack(
  47. reason_code rc,
  48. bool session_present, const connack_props& ca_props
  49. ) {
  50. if constexpr (has_at_connack<LoggerType>)
  51. _logger.at_connack(rc, session_present, ca_props);
  52. }
  53. void at_disconnect(reason_code rc, const disconnect_props& dc_props) {
  54. if constexpr (has_at_disconnect<LoggerType>)
  55. _logger.at_disconnect(rc, dc_props);
  56. }
  57. void at_transport_error(error_code ec) {
  58. if constexpr (has_at_transport_error<LoggerType>)
  59. _logger.at_transport_error(ec);
  60. }
  61. };
  62. } // end namespace boost::mqtt5::detail
  63. #endif // !BOOST_MQTT5_LOG_INVOKE_HPP