]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4100] Log enums as strings where possible
authorAndrei Pavel <andrei@isc.org>
Thu, 23 Oct 2025 12:02:35 +0000 (15:02 +0300)
committerAndrei Pavel <andrei@isc.org>
Fri, 24 Oct 2025 14:07:29 +0000 (17:07 +0300)
13 files changed:
src/bin/d2/d2_queue_mgr.cc
src/bin/d2/d2_queue_mgr.h
src/bin/dhcp4/dhcp4_srv.cc
src/bin/dhcp6/dhcp6_srv.cc
src/hooks/d2/gss_tsig/tkey_exchange.cc
src/hooks/dhcp/mysql/mysql_lease_mgr.cc
src/hooks/dhcp/pgsql/pgsql_lease_mgr.cc
src/lib/asiodns/io_fetch.h
src/lib/cryptolink/cryptolink.h
src/lib/cryptolink/tests/hmac_unittests.cc
src/lib/d2srv/dns_client.h
src/lib/dhcp_ddns/ncr_io.h
src/lib/dhcpsrv/host.h

index f902b221b83ac9356f081fefc7e47efdfc048d43..4943fcd7efd39d3e00c2cb19d33cb8457af5ef5b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2024 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2025 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -78,7 +78,7 @@ D2QueueMgr::operator()(const dhcp_ddns::NameChangeListener::Result result,
                 // this is unexpected so we will treat it as a receive error.
                 // This is most likely an unforeseen programmatic issue.
                 LOG_ERROR(dhcp_to_d2_logger, DHCP_DDNS_QUEUE_MGR_UNEXPECTED_STOP)
-                          .arg(mgr_state_);
+                          .arg(D2QueueMgr::stateToText(mgr_state_));
                 stopListening(STOPPED_RECV_ERROR);
             }
 
index 4c730b83b146c8b3fabaf3ca707da57544abd03e..4d34b726b1ccf57f8e80ca33e8827a1e931c27f2 100644 (file)
 /// @file d2_queue_mgr.h This file defines the class D2QueueMgr.
 
 #include <asiolink/io_service.h>
-#include <exceptions/exceptions.h>
-#include <dhcp_ddns/ncr_msg.h>
 #include <dhcp_ddns/ncr_io.h>
+#include <dhcp_ddns/ncr_msg.h>
+#include <exceptions/exceptions.h>
 
-#include <boost/noncopyable.hpp>
 #include <deque>
 
+#include <boost/noncopyable.hpp>
+
 namespace isc {
 namespace d2 {
 
@@ -145,6 +146,25 @@ public:
       STOPPED = 6,
     };
 
+    /// @brief Convert enum to string.
+    ///
+    /// @param state input enum
+    ///
+    /// @return reference to static string
+    static std::string const& stateToText(State const& state) {
+        static std::vector<std::string> const text_vector {
+            "NOT_INITTED",
+            "INITTED",
+            "RUNNING",
+            "STOPPING",
+            "STOPPED_QUEUE_FULL",
+            "STOPPED_RECV_ERROR",
+            "STOPPED",
+        };
+        static std::string const unknown("UNKNOWN");
+        return (state < text_vector.size() ? text_vector[state] : unknown);
+    }
+
     /// @brief Constructor
     ///
     /// Creates a D2QueueMgr instance.  Note that the listener is not created
index 3dd73ec1d895c681f69289d69eafc259551f2605..b1de5c68a82a497f294e8246151dfe2261cf7888 100644 (file)
@@ -5106,11 +5106,11 @@ Dhcpv4Srv::stopD2() {
 }
 
 void
-Dhcpv4Srv::d2ClientErrorHandler(const
-                                dhcp_ddns::NameChangeSender::Result result,
+Dhcpv4Srv::d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::Result result,
                                 dhcp_ddns::NameChangeRequestPtr& ncr) {
-    LOG_ERROR(ddns4_logger, DHCP4_DDNS_REQUEST_SEND_FAILED).
-              arg(result).arg((ncr ? ncr->toText() : " NULL "));
+    LOG_ERROR(ddns4_logger, DHCP4_DDNS_REQUEST_SEND_FAILED)
+        .arg(NameChangeSender::resultToText(result))
+        .arg((ncr ? ncr->toText() : " NULL "));
     // We cannot communicate with kea-dhcp-ddns, suspend further updates.
     /// @todo We may wish to revisit this, but for now we will simply turn
     /// them off.
index 78145dedebb58f84c4362f7324f6913e78497238..c7cb73f36008648b79e3878262eb40153cfd9f6a 100644 (file)
@@ -5060,11 +5060,11 @@ Dhcpv6Srv::stopD2() {
 }
 
 void
-Dhcpv6Srv::d2ClientErrorHandler(const
-                                dhcp_ddns::NameChangeSender::Result result,
+Dhcpv6Srv::d2ClientErrorHandler(const dhcp_ddns::NameChangeSender::Result result,
                                 dhcp_ddns::NameChangeRequestPtr& ncr) {
-    LOG_ERROR(ddns6_logger, DHCP6_DDNS_REQUEST_SEND_FAILED).
-              arg(result).arg((ncr ? ncr->toText() : " NULL "));
+    LOG_ERROR(ddns6_logger, DHCP6_DDNS_REQUEST_SEND_FAILED)
+        .arg(NameChangeSender::resultToText(result))
+        .arg((ncr ? ncr->toText() : " NULL "));
     // We cannot communicate with kea-dhcp-ddns, suspend further updates.
     /// @todo We may wish to revisit this, but for now we will simply turn
     /// them off.
index c79aa15727147232e32ab34114de9922ed9c4c4d..f7293b7d4e175540256c1e0f4b9b778a3623377a 100644 (file)
@@ -256,7 +256,7 @@ TKeyExchangeImpl::operator()(IOFetch::Result result) {
 
     default:
         LOG_ERROR(gss_tsig_logger, TKEY_EXCHANGE_FAIL_IO_ERROR)
-            .arg(result);
+            .arg(IOFetch::resultToText(result));
         incrStats("tkey-error");
         callCallback(TKeyExchange::OTHER);
         return;
index 8668752619b39db2b6ae23f4b8ae0b3bec423197..7b3bfb3a71b22d40c387aa486f6048fec2e259c5 100644 (file)
@@ -2380,7 +2380,7 @@ bool
 MySqlLeaseMgr::addLease(const Lease6Ptr& lease) {
     LOG_DEBUG(mysql_lb_logger, MYSQL_LB_DBG_TRACE_DETAIL, MYSQL_LB_ADD_ADDR6)
         .arg(lease->addr_.toText())
-        .arg(lease->type_);
+        .arg(Lease::typeToText(lease->type_));
 
     lease->extended_info_action_ = Lease6::ACTION_IGNORE;
 
@@ -2847,7 +2847,7 @@ MySqlLeaseMgr::getLease6(Lease::Type lease_type,
                          const IOAddress& addr) const {
     LOG_DEBUG(mysql_lb_logger, MYSQL_LB_DBG_TRACE_DETAIL, MYSQL_LB_GET_ADDR6)
         .arg(addr.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     MYSQL_BIND inbind[2];
@@ -2928,7 +2928,7 @@ MySqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
     LOG_DEBUG(mysql_lb_logger, MYSQL_LB_DBG_TRACE_DETAIL, MYSQL_LB_GET_IAID_DUID)
         .arg(iaid)
         .arg(duid.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     MYSQL_BIND inbind[3];
@@ -2992,7 +2992,7 @@ MySqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
         .arg(iaid)
         .arg(subnet_id)
         .arg(duid.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     MYSQL_BIND inbind[4];
@@ -3386,7 +3386,7 @@ MySqlLeaseMgr::updateLease6(const Lease6Ptr& lease) {
 
     LOG_DEBUG(mysql_lb_logger, MYSQL_LB_DBG_TRACE_DETAIL, MYSQL_LB_UPDATE_ADDR6)
         .arg(lease->addr_.toText())
-        .arg(lease->type_);
+        .arg(Lease::typeToText(lease->type_));
 
     // Get the recorded action and reset it.
     Lease6::ExtendedInfoAction recorded_action = lease->extended_info_action_;
index 0aaddd7ebeef01fc4f50f24b6b3c8d11411601fa..1a315cf3b181e79e53052a583f7483985c22de49 100644 (file)
@@ -1821,7 +1821,7 @@ bool
 PgSqlLeaseMgr::addLease(const Lease6Ptr& lease) {
     LOG_DEBUG(pgsql_lb_logger, PGSQL_LB_DBG_TRACE_DETAIL, PGSQL_LB_ADD_ADDR6)
         .arg(lease->addr_.toText())
-        .arg(lease->type_);
+        .arg(Lease::typeToText(lease->type_));
 
     lease->extended_info_action_ = Lease6::ACTION_IGNORE;
 
@@ -2162,7 +2162,7 @@ PgSqlLeaseMgr::getLease6(Lease::Type lease_type,
                          const IOAddress& addr) const {
     LOG_DEBUG(pgsql_lb_logger, PGSQL_LB_DBG_TRACE_DETAIL, PGSQL_LB_GET_ADDR6)
         .arg(addr.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     PsqlBindArray bind_array;
@@ -2220,7 +2220,7 @@ PgSqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
     LOG_DEBUG(pgsql_lb_logger, PGSQL_LB_DBG_TRACE_DETAIL, PGSQL_LB_GET_IAID_DUID)
         .arg(iaid)
         .arg(duid.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     PsqlBindArray bind_array;
@@ -2255,7 +2255,7 @@ PgSqlLeaseMgr::getLeases6(Lease::Type lease_type, const DUID& duid,
         .arg(iaid)
         .arg(subnet_id)
         .arg(duid.toText())
-        .arg(lease_type);
+        .arg(Lease::typeToText(lease_type));
 
     // Set up the WHERE clause value
     PsqlBindArray bind_array;
@@ -2583,7 +2583,7 @@ PgSqlLeaseMgr::updateLease6(const Lease6Ptr& lease) {
 
     LOG_DEBUG(pgsql_lb_logger, PGSQL_LB_DBG_TRACE_DETAIL, PGSQL_LB_UPDATE_ADDR6)
         .arg(lease->addr_.toText())
-        .arg(lease->type_);
+        .arg(Lease::typeToText(lease->type_));
 
     // Get the recorded action and reset it.
     Lease6::ExtendedInfoAction recorded_action = lease->extended_info_action_;
index 8bdda974e814630de56ace2d61a8b9236b675c71..a2c8c92dd2787f1aa4cca60e3840443ed61a3eef 100644 (file)
@@ -17,9 +17,9 @@
 
 #include <boost/asio/coroutine.hpp>
 #include <boost/asio/deadline_timer.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
 #include <boost/shared_array.hpp>
 #include <boost/shared_ptr.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
 #include <boost/system/error_code.hpp>
 
 namespace isc {
@@ -60,9 +60,25 @@ public:
         SUCCESS = 0,       // Success, fetch completed
         TIME_OUT = 1,      // Failure, fetch timed out
         STOPPED = 2,       // Control code, fetch has been stopped
-        NOTSET = 3         // For testing, indicates value not set
+        NOTSET = 3,        // For testing, indicates value not set
     };
 
+    /// @brief Convert enum to string.
+    ///
+    /// @param result input enum
+    ///
+    /// @return reference to static string
+    static std::string const& resultToText(Result const& result) {
+        static std::vector<std::string> const text_vector {
+            "SUCCESS",
+            "TIME_OUT",
+            "STOPPED",
+            "NOTSET",
+        };
+        static std::string const unknown("UNKNOWN");
+        return (result < text_vector.size() ? text_vector[result] : unknown);
+    }
+
     /// @note The next enum is a "trick" to allow constants to be defined in a class
     /// declaration.
 
index 6dd375a2588ee087a33fca26333727b133a78b49..4749dc136b106831d5d35f5d06291df12e396070 100644 (file)
@@ -32,7 +32,7 @@ enum HashAlgorithm : int {
     SHA256 = 3,         // SHA-256
     SHA224 = 4,         // SHA-224
     SHA384 = 5,         // SHA-384
-    SHA512 = 6          // SHA-512
+    SHA512 = 6,         // SHA-512
 };
 
 /// @brief Forward declaration for createHash().
index d5c6a83ab2b1c3c4ad69a9c36b706074ffe9de53..b737f321c134831d83c240d6229fd3b24d6d1fd3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2025 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -6,29 +6,45 @@
 
 #include <config.h>
 
+#include <cryptolink/crypto_hmac.h>
+#include <cryptolink/cryptolink.h>
+#include <exceptions/exceptions.h>
+#include <util/buffer.h>
+#include <util/encode/encode.h>
+
 #include <string>
 #include <vector>
 
 #include <boost/lexical_cast.hpp>
+#include <boost/shared_ptr.hpp>
 
 #include <gtest/gtest.h>
 
-#include <util/encode/encode.h>
-
-#include <cryptolink/cryptolink.h>
-#include <cryptolink/crypto_hmac.h>
-
-#include <util/buffer.h>
-#include <exceptions/exceptions.h>
-
-#include <boost/shared_ptr.hpp>
-
 using boost::lexical_cast;
 using namespace isc::util;
 using namespace isc::util::encode;
 using namespace isc::cryptolink;
 
 namespace {
+    /// @brief Convert enum to string.
+    ///
+    /// @param algorithm input enum
+    ///
+    /// @return reference to static string
+    static std::string const& hashAlgorithmToText(HashAlgorithm const& algorithm) {
+        static std::vector<std::string> const text_vector {
+            "UNKNOWN_HASH",
+            "MD5",
+            "SHA1",
+            "SHA256",
+            "SHA224",
+            "SHA384",
+            "SHA512",
+        };
+        static std::string const unknown("UNKNOWN");
+        return (algorithm < text_vector.size() ? text_vector[algorithm] : unknown);
+    }
+
     /// @brief Fill a string with copies of an out of char range value
     /// @param data String to fill
     /// @param len Number of copies
@@ -479,8 +495,8 @@ doRFC4231Tests(HashAlgorithm hash_algorithm,
 
     for (std::vector<std::string>::size_type i = 0;
          i < data_list.size(); ++i) {
-        SCOPED_TRACE("RFC4231 HMAC test for algorithm ID: " +
-                     lexical_cast<std::string>(hash_algorithm) +
+        SCOPED_TRACE("RFC4231 HMAC test for algorithm " +
+                     hashAlgorithmToText(hash_algorithm) +
                      ", data ID: " + lexical_cast<std::string>(i));
         // Until #920 is resolved we have to skip truncation cases.
         if (data_list[i] == "Test With Truncation") {
index cf2f60f4acce65132227d7ef1462605f1c83e69d..5c1f2c2f2a397212cca8547ea3f3b06eff594887 100644 (file)
@@ -63,6 +63,23 @@ public:
         OTHER = 4,              ///< Other, unclassified error.
     };
 
+    /// @brief Convert enum to string.
+    ///
+    /// @param status input enum
+    ///
+    /// @return reference to static string
+    static std::string const& statusToText(Status const& status) {
+        static std::vector<std::string> const text_vector {
+            "SUCCESS",
+            "TIMEOUT",
+            "IO_STOPPED",
+            "INVALID_RESPONSE",
+            "OTHER",
+        };
+        static std::string const unknown("UNKNOWN");
+        return (status < text_vector.size() ? text_vector[status] : unknown);
+    }
+
     /// @brief Callback for the @c DNSClient class.
     ///
     /// This is an abstract class which represents the external callback for the
index f759d4332bc0a6ddbd1b598339225f0ab749c4fb..db0d572cfc749d1d2ee49145fb5f6c76b9ba8479 100644 (file)
 #include <dhcp_ddns/ncr_msg.h>
 #include <exceptions/exceptions.h>
 
-#include <boost/scoped_ptr.hpp>
-#include <boost/enable_shared_from_this.hpp>
-
 #include <deque>
 #include <mutex>
 
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/scoped_ptr.hpp>
+
 namespace isc {
 namespace dhcp_ddns {
 
@@ -482,6 +482,22 @@ public:
         ERROR = 3,
     };
 
+    /// @brief Convert enum to string.
+    ///
+    /// @param result input enum
+    ///
+    /// @return reference to static string
+    static std::string const& resultToText(Result const& result) {
+        static std::vector<std::string> const text_vector {
+            "SUCCESS",
+            "TIME_OUT",
+            "STOPPED",
+            "ERROR",
+        };
+        static std::string const unknown("UNKNOWN");
+        return (result < text_vector.size() ? text_vector[result] : unknown);
+    }
+
     /// @brief Abstract class for defining application layer send callbacks.
     ///
     /// Applications which will send NameChangeRequests must provide a
index 620afe1391005887fdb76a9cce2e6677c7a918b8..1ef85c25e322dd027464c49da8e92e3a54967d03 100644 (file)
@@ -339,7 +339,7 @@ public:
         IDENT_DUID = 1,
         IDENT_CIRCUIT_ID = 2,
         IDENT_CLIENT_ID = 3,
-        IDENT_FLEX = 4,  ///< Flexible host identifier.
+        IDENT_FLEX = 4, ///< Flexible host identifier.
     };
 
     /// @brief Constant pointing to the last identifier of the