Explorar el Código

Address SonarLint findings in Logger class

Patrick-Christopher Mattulat hace 1 año
padre
commit
f259ad5070
Se han modificado 2 ficheros con 17 adiciones y 12 borrados
  1. 6 5
      include/ls-std/io/logging/Logger.hpp
  2. 11 7
      source/ls-std/io/logging/Logger.cpp

+ 6 - 5
include/ls-std/io/logging/Logger.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2023-04-12
+ * Changed:         2023-05-17
  *
  * */
 
@@ -17,6 +17,7 @@
 #include <ls-std/io/FileOutputStream.hpp>
 #include <ls-std/os/dynamic-goal.hpp>
 #include <string>
+#include <string_view>
 
 namespace ls::std::io
 {
@@ -30,7 +31,7 @@ namespace ls::std::io
       void debug(const ls::std::core::type::byte_type *_data);
       void error(const ls::std::core::type::byte_type *_data);
       void fatal(const ls::std::core::type::byte_type *_data);
-      [[nodiscard]] ls::std::io::LogLevel getLogLevel();
+      [[nodiscard]] ls::std::io::LogLevel getLogLevel() const;
       void hideLogLevel();
       void hideTimestamp();
       void info(const ls::std::core::type::byte_type *_data);
@@ -49,11 +50,11 @@ namespace ls::std::io
 
       void _assignWriter(const ::std::shared_ptr<ls::std::core::interface_type::IWriter> &_writer);
       [[nodiscard]] static ::std::string _buildCharacterChain(size_t _amount);
-      [[nodiscard]] static ::std::string _createFillContent(const ::std::string &_text);
-      [[nodiscard]] static ::std::string _generateTimeString(tm *_localTime);
+      [[nodiscard]] static ::std::string _createFillContent(::std::string_view _text);
+      [[nodiscard]] static ::std::string _generateTimeString(const tm *_localTime);
       [[nodiscard]] ::std::string _getLogLevelString(const ls::std::io::LogLevel &_logLevel) const;
       [[nodiscard]] ::std::string _getTimestampString() const;
-      void _log(const ls::std::core::type::byte_type *_data, const ls::std::io::LogLevel &_logLevel);
+      void _log(const ls::std::core::type::byte_type *_data, const ls::std::io::LogLevel &_logLevel) const;
       [[nodiscard]] static ::std::string _padRight(const ::std::string &_text);
   };
 }

+ 11 - 7
source/ls-std/io/logging/Logger.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2023-04-12
+ * Changed:         2023-05-17
  *
  * */
 
@@ -28,6 +28,7 @@ using std::localtime;
 using std::put_time;
 using std::shared_ptr;
 using std::string;
+using std::string_view;
 using std::stringstream;
 using std::time;
 
@@ -62,7 +63,7 @@ void Logger::fatal(const byte_type *_data)
   }
 }
 
-LogLevel Logger::getLogLevel()
+LogLevel Logger::getLogLevel() const
 {
   return this->logLevel;
 }
@@ -134,7 +135,7 @@ string Logger::_buildCharacterChain(size_t _amount)
   return fillContent;
 }
 
-string Logger::_createFillContent(const string &_text)
+string Logger::_createFillContent(string_view _text)
 {
   size_t padSize = 10;
   size_t fillSize = _text.size() > padSize ? 0 : padSize - _text.size();
@@ -148,7 +149,7 @@ string Logger::_createFillContent(const string &_text)
   return fillContent;
 }
 
-string Logger::_generateTimeString(tm *_localTime)
+string Logger::_generateTimeString(const tm *_localTime)
 {
   stringstream _stream{};
   _stream << put_time(_localTime, "%Y-%m-%d %H:%M:%S");
@@ -171,18 +172,21 @@ string Logger::_getLogLevelString(const LogLevel &_logLevel) const
 string Logger::_getTimestampString() const
 {
   time_t timestamp = ::time(nullptr);
-  tm *localTime = localtime(&timestamp);
+  struct tm localTime
+  {
+  };
+  localtime_r(&timestamp, &localTime);
   string timestampString{};
 
   if (this->displayTimestamp)
   {
-    timestampString = "[" + Logger::_generateTimeString(localTime) + "] ";
+    timestampString = "[" + Logger::_generateTimeString(&localTime) + "] ";
   }
 
   return timestampString;
 }
 
-void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
+void Logger::_log(const byte_type *_data, const LogLevel &_logLevel) const
 {
   string logLevelString = this->_getLogLevelString(_logLevel);
   string timestampString = this->_getTimestampString();