From 31410c39ba238353905cf7509eab7be8b2c154b6 Mon Sep 17 00:00:00 2001 From: EdenGri Date: Mon, 3 Oct 2022 12:21:41 +0000 Subject: [PATCH] Fix after public code review: use a table instead dbConnector when accessing the table, use static const instead define, and add white space where needed --- common/logger.cpp | 24 ++++-------------------- common/logger.h | 6 +++--- common/loglevel.cpp | 4 ++-- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/common/logger.cpp b/common/logger.cpp index 1eabd0ef1..02d267d60 100644 --- a/common/logger.cpp +++ b/common/logger.cpp @@ -122,40 +122,24 @@ void Logger::linkToDbWithOutput( logger.m_settingChangeObservers.insert(std::make_pair(dbName, std::make_pair(prioNotify, outputNotify))); DBConnector db("CONFIG_DB", 0); - std::string key_prefix(CFG_LOGGER_TABLE_NAME); - key_prefix+="|"; - std::string key = key_prefix + dbName; - SWSS_LOG_DEBUG("Component %s register to logger with the key: %s", dbName.c_str(), key.c_str()); + swss::Table table(&db, CFG_LOGGER_TABLE_NAME); + SWSS_LOG_DEBUG("Component %s register to logger", dbName.c_str()); std::string prio, output; bool doUpdate = false; - auto prioPtr = db.hget(key, DAEMON_LOGLEVEL); - auto outputPtr = db.hget(key, DAEMON_LOGOUTPUT); - - if (prioPtr == nullptr) + if(!table.hget(dbName, DAEMON_LOGLEVEL, prio)) { prio = defPrio; doUpdate = true; } - else - { - prio = *prioPtr; - } - - if (outputPtr == nullptr) + if(!table.hget(dbName, DAEMON_LOGLEVEL, output)) { output = defOutput; doUpdate = true; - - } - else - { - output = *outputPtr; } if (doUpdate) { - swss::Table table(&db, CFG_LOGGER_TABLE_NAME); FieldValueTuple fvLevel(DAEMON_LOGLEVEL, prio); FieldValueTuple fvOutput(DAEMON_LOGOUTPUT, output); std::vectorfieldValues = { fvLevel, fvOutput }; diff --git a/common/logger.h b/common/logger.h index 07ffa9dd2..135879c96 100644 --- a/common/logger.h +++ b/common/logger.h @@ -24,6 +24,9 @@ namespace swss { #define SWSS_LOG_THROW(MSG, ...) swss::Logger::getInstance().wthrow(swss::Logger::SWSS_ERROR, ":- %s: " MSG, __FUNCTION__, ##__VA_ARGS__) +static constexpr const char * const DAEMON_LOGLEVEL = "LOGLEVEL"; +static constexpr const char * const DAEMON_LOGOUTPUT = "LOGOUTPUT"; + void err_exit(const char *fn, int ln, int e, const char *fmt, ...) #ifdef __GNUC__ __attribute__ ((format (printf, 4, 5))) @@ -47,9 +50,6 @@ class Logger { public: -#define DAEMON_LOGLEVEL "LOGLEVEL" -#define DAEMON_LOGOUTPUT "LOGOUTPUT" - enum Priority { SWSS_EMERG, diff --git a/common/loglevel.cpp b/common/loglevel.cpp index 6073cbf2c..fee3230fe 100644 --- a/common/loglevel.cpp +++ b/common/loglevel.cpp @@ -80,7 +80,7 @@ std::vector get_no_sai_keys(std::vector keys) return keys; } -void setAllLoglevel(swss::Table& logger_tbl, std::vector components,std::string loglevel) +void setAllLoglevel(swss::Table& logger_tbl, std::vector components, std::string loglevel) { for (const auto& component : components) { @@ -194,7 +194,7 @@ int swssloglevel(int argc, char** argv) keys.erase(std::remove_if(keys.begin(), keys.end(), filterOutSaiKeys), keys.end()); } - setAllLoglevel(logger_tbl,keys, loglevel); + setAllLoglevel(logger_tbl, keys, loglevel); exit(EXIT_SUCCESS); }