Răsfoiți Sursa

Add log level configuration to Logger class

Log level can now be made invisible, which is the default
behaviour.
Patrick-Christopher Mattulat 1 an în urmă
părinte
comite
18ae8e56d2

+ 5 - 1
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-02-23
+ * Changed:         2023-04-12
  *
  * */
 
@@ -31,13 +31,16 @@ namespace ls::std::io
       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();
+      void hideLogLevel();
       void info(const ls::std::core::type::byte_type *_data);
       void setLogLevel(const ls::std::io::LogLevelValue &_logLevelValue);
+      void showLogLevel();
       void trace(const ls::std::core::type::byte_type *_data);
       void warn(const ls::std::core::type::byte_type *_data);
 
     private:
 
+      bool displayLogLevel{};
       ls::std::io::LogLevel logLevel{};
       ::std::shared_ptr<ls::std::core::interface_type::IWriter> writer{};
 
@@ -45,6 +48,7 @@ namespace ls::std::io
       [[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]] ::std::string _getLogLevelString(const ls::std::io::LogLevel &_logLevel) const;
       void _log(const ls::std::core::type::byte_type *_data, const ls::std::io::LogLevel &_logLevel);
       [[nodiscard]] static ::std::string _padRight(const ::std::string &_text);
   };

+ 24 - 2
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-02-23
+ * Changed:         2023-04-12
  *
  * */
 
@@ -67,6 +67,11 @@ LogLevel Logger::getLogLevel()
   return this->logLevel;
 }
 
+void Logger::hideLogLevel()
+{
+  this->displayLogLevel = false;
+}
+
 void Logger::info(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::INFO)
@@ -80,6 +85,11 @@ void Logger::setLogLevel(const LogLevelValue &_logLevelValue)
   this->logLevel = _logLevelValue;
 }
 
+void Logger::showLogLevel()
+{
+  this->displayLogLevel = true;
+}
+
 void Logger::trace(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::TRACE)
@@ -136,12 +146,24 @@ string Logger::_generateTimeString(tm *_localTime)
   return _stream.str();
 }
 
+string Logger::_getLogLevelString(const LogLevel &_logLevel) const
+{
+  string logLevelString{};
+
+  if (this->displayLogLevel)
+  {
+    logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
+  }
+
+  return logLevelString;
+}
+
 void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
 {
   time_t timestamp = ::time(nullptr);
   tm *localTime = localtime(&timestamp);
 
-  string logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
+  string logLevelString = this->_getLogLevelString(_logLevel);
   string message = "[" + Logger::_generateTimeString(localTime) + "] " + logLevelString + string(_data) + NewLine::getUnixNewLine();
   this->writer->write(message);
 }

+ 43 - 1
test/cases/io/logging/LoggerTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2023-03-25
+ * Changed:         2023-04-12
  *
  * */
 
@@ -86,6 +86,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.showLogLevel();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -115,6 +116,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::ERR);
+    logger.showLogLevel();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -144,6 +146,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::FATAL);
+    logger.showLogLevel();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -170,6 +173,24 @@ namespace
     ASSERT_EQ(LogLevelValue::INFO, logger.getLogLevel().getValue());
   }
 
+  TEST_F(LoggerTest, hideLogLevel)
+  {
+    string logName = "hide-log-level-output-fatal.log";
+    shared_ptr<IWriter> writer = createFileLogger(logName);
+
+    Logger logger{writer};
+    logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.hideLogLevel();
+    logger.fatal("test message");
+
+    // get content and check
+
+    dynamic_pointer_cast<FileOutputStream>(writer)->close();
+    string content = getContentFromLogFile(logName);
+
+    ASSERT_TRUE(content.find("FATAL") == string::npos);
+  }
+
   TEST_F(LoggerTest, info)
   {
     // write to log file
@@ -179,6 +200,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::INFO);
+    logger.showLogLevel();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -207,6 +229,24 @@ namespace
     ASSERT_EQ(LogLevelValue::ERR, logger.getLogLevel().getValue());
   }
 
+  TEST_F(LoggerTest, showLogLevel)
+  {
+    string logName = "show-log-level-output-fatal.log";
+    shared_ptr<IWriter> writer = createFileLogger(logName);
+
+    Logger logger{writer};
+    logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.showLogLevel();
+    logger.fatal("test message");
+
+    // get content and check
+
+    dynamic_pointer_cast<FileOutputStream>(writer)->close();
+    string content = getContentFromLogFile(logName);
+
+    ASSERT_TRUE(content.find("FATAL") != string::npos);
+  }
+
   TEST_F(LoggerTest, trace)
   {
     // write to log file
@@ -216,6 +256,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::TRACE);
+    logger.showLogLevel();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -245,6 +286,7 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::WARN);
+    logger.showLogLevel();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");