From d520642a5482c3e2d82dfac2adbce00c0ed656be Mon Sep 17 00:00:00 2001 From: Kamil Cudnik Date: Tue, 10 Oct 2023 09:30:14 +0200 Subject: [PATCH] [syncd] Respect each api log level after sai discovery (#1303) Previously log after sai discovery log level was set to NOTICE not respecting actual API log level or what user set via swsslog --- meta/SaiInterface.cpp | 10 +++++++++ meta/SaiInterface.h | 5 +++++ syncd/SaiDiscovery.cpp | 48 +++++++++++++++++++++++++++++++++++++++++- syncd/SaiDiscovery.h | 6 ++++++ syncd/VendorSai.cpp | 19 +++++++++++++++++ syncd/VendorSai.h | 8 +++++++ 6 files changed, 95 insertions(+), 1 deletion(-) diff --git a/meta/SaiInterface.cpp b/meta/SaiInterface.cpp index 538896a6b..a07e97d4f 100644 --- a/meta/SaiInterface.cpp +++ b/meta/SaiInterface.cpp @@ -329,3 +329,13 @@ sai_status_t SaiInterface::switchMdioCl22Write( return SAI_STATUS_FAILURE; } + +sai_log_level_t SaiInterface::logGet( + _In_ sai_api_t api) +{ + SWSS_LOG_ENTER(); + + // default for all apis + + return SAI_LOG_LEVEL_NOTICE; +} diff --git a/meta/SaiInterface.h b/meta/SaiInterface.h index ba9b16e9a..fdd55ac00 100644 --- a/meta/SaiInterface.h +++ b/meta/SaiInterface.h @@ -316,5 +316,10 @@ namespace sairedis virtual sai_status_t logSet( _In_ sai_api_t api, _In_ sai_log_level_t log_level) = 0; + + public: // non SAI API + + virtual sai_log_level_t logGet( + _In_ sai_api_t api); }; } diff --git a/syncd/SaiDiscovery.cpp b/syncd/SaiDiscovery.cpp index 7ac75abca..7f88846d9 100644 --- a/syncd/SaiDiscovery.cpp +++ b/syncd/SaiDiscovery.cpp @@ -264,11 +264,13 @@ std::set SaiDiscovery::discover( { SWSS_LOG_TIMER("discover"); + auto levels = getApiLogLevel(); + setApiLogLevel(SAI_LOG_LEVEL_CRITICAL); discover(startRid, discovered_rids); - setApiLogLevel(SAI_LOG_LEVEL_NOTICE); + setApiLogLevel(levels); } SWSS_LOG_NOTICE("discovered objects count: %zu", discovered_rids.size()); @@ -323,3 +325,47 @@ void SaiDiscovery::setApiLogLevel( } } } + +void SaiDiscovery::setApiLogLevel( + _In_ const std::map& levels) +{ + SWSS_LOG_ENTER(); + + // We start from 1 since 0 is SAI_API_UNSPECIFIED. + + for (uint32_t api = 1; api < sai_metadata_enum_sai_api_t.valuescount; ++api) + { + auto it = levels.find((sai_api_t)api); + + sai_log_level_t logLevel = (it == levels.end()) ? SAI_LOG_LEVEL_NOTICE : it->second; + + sai_status_t status = m_sai->logSet((sai_api_t)api, logLevel); + + if (status == SAI_STATUS_SUCCESS) + { + SWSS_LOG_INFO("setting SAI loglevel %s on %s", + sai_serialize_log_level(logLevel).c_str(), + sai_serialize_api((sai_api_t)api).c_str()); + } + else + { + SWSS_LOG_INFO("set loglevel failed: %s", sai_serialize_status(status).c_str()); + } + } +} + +std::map SaiDiscovery::getApiLogLevel() +{ + SWSS_LOG_ENTER(); + + std::map levels; + + // We start from 1 since 0 is SAI_API_UNSPECIFIED. + + for (uint32_t api = 1; api < sai_metadata_enum_sai_api_t.valuescount; ++api) + { + levels[(sai_api_t)api] = m_sai->logGet((sai_api_t)api); + } + + return levels; +} diff --git a/syncd/SaiDiscovery.h b/syncd/SaiDiscovery.h index a5d02f1de..c48372b08 100644 --- a/syncd/SaiDiscovery.h +++ b/syncd/SaiDiscovery.h @@ -4,6 +4,7 @@ #include #include +#include #include namespace syncd @@ -50,6 +51,11 @@ namespace syncd void setApiLogLevel( _In_ sai_log_level_t logLevel); + void setApiLogLevel( + _In_ const std::map& levels); + + std::map getApiLogLevel(); + private: std::shared_ptr m_sai; diff --git a/syncd/VendorSai.cpp b/syncd/VendorSai.cpp index 7aaf58805..a0234aa7d 100644 --- a/syncd/VendorSai.cpp +++ b/syncd/VendorSai.cpp @@ -1681,5 +1681,24 @@ sai_status_t VendorSai::logSet( { SWSS_LOG_ENTER(); + m_logLevelMap[api] = log_level; + return m_globalApis.log_set(api, log_level); } + +sai_log_level_t VendorSai::logGet( + _In_ sai_api_t api) +{ + SWSS_LOG_ENTER(); + + auto it = m_logLevelMap.find(api); + + if (it != m_logLevelMap.end()) + { + return it->second; + } + + // no level defined yet, just return default + + return SAI_LOG_LEVEL_NOTICE; +} diff --git a/syncd/VendorSai.h b/syncd/VendorSai.h index b64a34fda..7a6c4937f 100644 --- a/syncd/VendorSai.h +++ b/syncd/VendorSai.h @@ -10,6 +10,7 @@ extern "C" { #include #include #include +#include namespace syncd { @@ -201,6 +202,11 @@ namespace syncd _In_ sai_api_t api, _In_ sai_log_level_t log_level) override; + public: // extra API + + virtual sai_log_level_t logGet( + _In_ sai_api_t api) override; + private: bool m_apiInitialized; @@ -212,5 +218,7 @@ namespace syncd sai_apis_t m_apis; sai_global_apis_t m_globalApis; + + std::map m_logLevelMap; }; }