Browse Source

Address SonarLint findings in LogLevel class

Patrick-Christopher Mattulat 1 year ago
parent
commit
2f84ace0a9
2 changed files with 31 additions and 29 deletions
  1. 10 9
      include/ls-std/io/logging/LogLevel.hpp
  2. 21 20
      source/ls-std/io/logging/LogLevel.cpp

+ 10 - 9
include/ls-std/io/logging/LogLevel.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2023-02-22
+ * Changed:         2023-05-16
  *
  * */
 
@@ -13,6 +13,7 @@
 #include "LogLevelValue.hpp"
 #include <ls-std/core/Class.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
+#include <string_view>
 #include <unordered_map>
 
 namespace ls::std::io
@@ -24,15 +25,15 @@ namespace ls::std::io
       explicit LogLevel(const ls::std::io::LogLevelValue &_value);
       LogLevel();
       ~LogLevel() noexcept override;
-      ;
+
       ls::std::io::LogLevel &operator=(const ls::std::io::LogLevelValue &_value);
-      bool operator<(const ls::std::io::LogLevelValue &_value);
-      bool operator<=(const ls::std::io::LogLevelValue &_value);
-      bool operator>(const ls::std::io::LogLevelValue &_value);
-      bool operator>=(const ls::std::io::LogLevelValue &_value);
-      bool operator==(const ls::std::io::LogLevelValue &_value);
+      bool operator<(const ls::std::io::LogLevelValue &_value) const;
+      bool operator<=(const ls::std::io::LogLevelValue &_value) const;
+      bool operator>(const ls::std::io::LogLevelValue &_value) const;
+      bool operator>=(const ls::std::io::LogLevelValue &_value) const;
+      bool operator==(const ls::std::io::LogLevelValue &_value) const;
 
-      [[nodiscard]] ls::std::io::LogLevelValue getValue();
+      [[nodiscard]] ls::std::io::LogLevelValue getValue() const;
       void setLogLevel(const ls::std::io::LogLevelValue &_value);
       void setLogLevel(const ::std::string &_value);
       [[nodiscard]] ::std::string toString() const;
@@ -44,7 +45,7 @@ namespace ls::std::io
 
       [[nodiscard]] ls::std::io::LogLevelValue _getValueFromString(const ::std::string &_value);
       void _init();
-      [[nodiscard]] bool _isValidLogLevelString(const ::std::string &_value);
+      [[nodiscard]] bool _isValidLogLevelString(::std::string_view _value) const;
   };
 }
 

+ 21 - 20
source/ls-std/io/logging/LogLevel.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2023-02-24
+ * Changed:         2023-05-16
  *
  * */
 
@@ -18,6 +18,7 @@ using ls::std::io::LogLevelValue;
 using std::find_if;
 using std::pair;
 using std::string;
+using std::string_view;
 
 LogLevel::LogLevel(const LogLevelValue &_value) : Class("LogLevel"), value(_value)
 {
@@ -37,32 +38,32 @@ LogLevel &LogLevel::operator=(const LogLevelValue &_value)
   return *this;
 }
 
-bool LogLevel::operator<(const LogLevelValue &_value)
+bool LogLevel::operator<(const LogLevelValue &_value) const
 {
   return this->value < _value;
 }
 
-bool LogLevel::operator<=(const LogLevelValue &_value)
+bool LogLevel::operator<=(const LogLevelValue &_value) const
 {
   return this->value <= _value;
 }
 
-bool LogLevel::operator>(const LogLevelValue &_value)
+bool LogLevel::operator>(const LogLevelValue &_value) const
 {
   return this->value > _value;
 }
 
-bool LogLevel::operator>=(const LogLevelValue &_value)
+bool LogLevel::operator>=(const LogLevelValue &_value) const
 {
   return this->value >= _value;
 }
 
-bool LogLevel::operator==(const LogLevelValue &_value)
+bool LogLevel::operator==(const LogLevelValue &_value) const
 {
   return this->value == _value;
 }
 
-LogLevelValue LogLevel::getValue()
+LogLevelValue LogLevel::getValue() const
 {
   return this->value;
 }
@@ -74,7 +75,7 @@ void LogLevel::setLogLevel(const LogLevelValue &_value)
 
 void LogLevel::setLogLevel(const string &_value)
 {
-  if (this->_isValidLogLevelString(_value))
+  if (this->_isValidLogLevelString(string_view{_value}))
   {
     this->value = _getValueFromString(_value);
   }
@@ -91,29 +92,29 @@ string LogLevel::toString() const
 
 LogLevelValue LogLevel::_getValueFromString(const string &_value)
 {
-  const auto &iterator = find_if(this->level.begin(), this->level.end(), [_value](const pair<uint8_t, string> &_logLevelString) { return _logLevelString.second == _value; });
-  pair<uint8_t, string> levelPair = *iterator;
+  const auto &iterator = find_if(this->level.begin(), this->level.end(), [&_value](const pair<uint8_t, string> &_logLevelString) { return _logLevelString.second == _value; });
+  const auto &[logLevelEnumValue, logLevelStringRepresentation] = *iterator;
 
-  return iterator != level.end() ? (LogLevelValue) levelPair.first : LogLevelValue{};
+  return iterator != level.end() ? (LogLevelValue) logLevelEnumValue : LogLevelValue{};
 }
 
 void LogLevel::_init()
 {
-  this->level.insert({LogLevelValue::FATAL, "FATAL"});
-  this->level.insert({LogLevelValue::ERR, "ERROR"});
-  this->level.insert({LogLevelValue::WARN, "WARN"});
-  this->level.insert({LogLevelValue::INFO, "INFO"});
-  this->level.insert({LogLevelValue::DEBUG, "DEBUG"});
-  this->level.insert({LogLevelValue::TRACE, "TRACE"});
+  this->level.try_emplace(LogLevelValue::FATAL, "FATAL");
+  this->level.try_emplace(LogLevelValue::ERR, "ERROR");
+  this->level.try_emplace(LogLevelValue::WARN, "WARN");
+  this->level.try_emplace(LogLevelValue::INFO, "INFO");
+  this->level.try_emplace(LogLevelValue::DEBUG, "DEBUG");
+  this->level.try_emplace(LogLevelValue::TRACE, "TRACE");
 }
 
-bool LogLevel::_isValidLogLevelString(const string &_value)
+bool LogLevel::_isValidLogLevelString(string_view _value) const
 {
   bool isValidString{};
 
-  for (const auto &logLevelString : this->level)
+  for (const auto &[logLevelEnumValue, logLevelStringRepresentation] : this->level)
   {
-    isValidString = logLevelString.second == _value;
+    isValidString = logLevelStringRepresentation == _value;
 
     if (isValidString)
     {