Переглянути джерело

Merge branch 'configurable-logger-output' of public/ls-standard-library into development

patrick-christopher.mattulat 1 рік тому
батько
коміт
a106539b8c

+ 1 - 0
README.md

@@ -39,6 +39,7 @@ A __Date__ class comes with this submodule, which you can use to represent a dat
   order to use this feature the CMake option __LS_STD_BUILD_WITH_JNI__ has to be enabled - please note, that this
   feature is experimental and incomplete
 - a SystemTime class has been added to the library's time module for setting the local system time
+- Logger class is now configurable to silent log level or timestamp output
 
 #### Improvements ####
 

+ 9 - 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,19 @@ 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 hideTimestamp();
       void info(const ls::std::core::type::byte_type *_data);
       void setLogLevel(const ls::std::io::LogLevelValue &_logLevelValue);
+      void showLogLevel();
+      void showTimestamp();
       void trace(const ls::std::core::type::byte_type *_data);
       void warn(const ls::std::core::type::byte_type *_data);
 
     private:
 
+      bool displayLogLevel{};
+      bool displayTimestamp{};
       ls::std::io::LogLevel logLevel{};
       ::std::shared_ptr<ls::std::core::interface_type::IWriter> writer{};
 
@@ -45,6 +51,8 @@ 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;
+      [[nodiscard]] ::std::string _getTimestampString() 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);
   };

+ 48 - 4
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,16 @@ LogLevel Logger::getLogLevel()
   return this->logLevel;
 }
 
+void Logger::hideLogLevel()
+{
+  this->displayLogLevel = false;
+}
+
+void Logger::hideTimestamp()
+{
+  this->displayTimestamp = false;
+}
+
 void Logger::info(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::INFO)
@@ -80,6 +90,16 @@ void Logger::setLogLevel(const LogLevelValue &_logLevelValue)
   this->logLevel = _logLevelValue;
 }
 
+void Logger::showLogLevel()
+{
+  this->displayLogLevel = true;
+}
+
+void Logger::showTimestamp()
+{
+  this->displayTimestamp = true;
+}
+
 void Logger::trace(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::TRACE)
@@ -136,13 +156,37 @@ string Logger::_generateTimeString(tm *_localTime)
   return _stream.str();
 }
 
-void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
+string Logger::_getLogLevelString(const LogLevel &_logLevel) const
+{
+  string logLevelString{};
+
+  if (this->displayLogLevel)
+  {
+    logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
+  }
+
+  return logLevelString;
+}
+
+string Logger::_getTimestampString() const
 {
   time_t timestamp = ::time(nullptr);
   tm *localTime = localtime(&timestamp);
+  string timestampString{};
 
-  string logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
-  string message = "[" + Logger::_generateTimeString(localTime) + "] " + logLevelString + string(_data) + NewLine::getUnixNewLine();
+  if (this->displayTimestamp)
+  {
+    timestampString = "[" + Logger::_generateTimeString(localTime) + "] ";
+  }
+
+  return timestampString;
+}
+
+void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
+{
+  string logLevelString = this->_getLogLevelString(_logLevel);
+  string timestampString = this->_getTimestampString();
+  string message = timestampString + logLevelString + string(_data) + NewLine::getUnixNewLine();
   this->writer->write(message);
 }
 

+ 106 - 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
  *
  * */
 
@@ -19,6 +19,7 @@ using ls::std::io::FileOutputStream;
 using ls::std::io::FileReader;
 using ls::std::io::Logger;
 using ls::std::io::LogLevelValue;
+using ls::std::io::NewLine;
 using ls::std::test::TestHelper;
 using std::dynamic_pointer_cast;
 using std::make_shared;
@@ -86,6 +87,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -115,6 +118,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::ERR);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -144,6 +149,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::FATAL);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -170,6 +177,62 @@ 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, hideTimestamp)
+  {
+    string logName = "hide-time-stamp-output-fatal.log";
+    shared_ptr<IWriter> writer = createFileLogger(logName);
+
+    Logger logger{writer};
+    logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.hideTimestamp();
+    logger.fatal("test message");
+
+    // get content and check
+
+    dynamic_pointer_cast<FileOutputStream>(writer)->close();
+    string content = getContentFromLogFile(logName);
+
+    ASSERT_TRUE(content.find('[') == string::npos);
+  }
+
+  TEST_F(LoggerTest, hideInformation)
+  {
+    string logName = "hide-info-output-fatal.log";
+    shared_ptr<IWriter> writer = createFileLogger(logName);
+    string message = "test message";
+
+    Logger logger{writer};
+    logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.hideLogLevel();
+    logger.hideTimestamp();
+    logger.fatal(message.c_str());
+
+    // get content and check
+
+    dynamic_pointer_cast<FileOutputStream>(writer)->close();
+    string content = getContentFromLogFile(logName);
+
+    ASSERT_STREQ(string(message + NewLine::get()).c_str(), content.c_str());
+  }
+
   TEST_F(LoggerTest, info)
   {
     // write to log file
@@ -179,6 +242,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::INFO);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -207,6 +272,42 @@ 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, showTimestamp)
+  {
+    string logName = "hide-time-stamp-output-fatal.log";
+    shared_ptr<IWriter> writer = createFileLogger(logName);
+
+    Logger logger{writer};
+    logger.setLogLevel(LogLevelValue::DEBUG);
+    logger.showTimestamp();
+    logger.fatal("test message");
+
+    // get content and check
+
+    dynamic_pointer_cast<FileOutputStream>(writer)->close();
+    string content = getContentFromLogFile(logName);
+
+    ASSERT_TRUE(content.find('[') != string::npos);
+  }
+
   TEST_F(LoggerTest, trace)
   {
     // write to log file
@@ -216,6 +317,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::TRACE);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -245,6 +348,8 @@ namespace
 
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::WARN);
+    logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");