Bladeren bron

Add timestamp configuration to Logger class

Timestamp can now be made invisible, which is the default
behaviour.
Patrick-Christopher Mattulat 1 jaar geleden
bovenliggende
commit
e4127ed540
3 gewijzigde bestanden met toevoegingen van 91 en 2 verwijderingen
  1. 4 0
      include/ls-std/io/logging/Logger.hpp
  2. 24 2
      source/ls-std/io/logging/Logger.cpp
  3. 63 0
      test/cases/io/logging/LoggerTest.cpp

+ 4 - 0
include/ls-std/io/logging/Logger.hpp

@@ -32,15 +32,18 @@ namespace ls::std::io
       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{};
 
@@ -49,6 +52,7 @@ namespace ls::std::io
       [[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);
   };

+ 24 - 2
source/ls-std/io/logging/Logger.cpp

@@ -72,6 +72,11 @@ void Logger::hideLogLevel()
   this->displayLogLevel = false;
 }
 
+void Logger::hideTimestamp()
+{
+  this->displayTimestamp = false;
+}
+
 void Logger::info(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::INFO)
@@ -90,6 +95,11 @@ void Logger::showLogLevel()
   this->displayLogLevel = true;
 }
 
+void Logger::showTimestamp()
+{
+  this->displayTimestamp = true;
+}
+
 void Logger::trace(const byte_type *_data)
 {
   if (this->logLevel >= LogLevelValue::TRACE)
@@ -158,13 +168,25 @@ string Logger::_getLogLevelString(const LogLevel &_logLevel) const
   return logLevelString;
 }
 
-void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
+string Logger::_getTimestampString() const
 {
   time_t timestamp = ::time(nullptr);
   tm *localTime = localtime(&timestamp);
+  string timestampString{};
 
+  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 message = "[" + Logger::_generateTimeString(localTime) + "] " + logLevelString + string(_data) + NewLine::getUnixNewLine();
+  string timestampString = this->_getTimestampString();
+  string message = timestampString + logLevelString + string(_data) + NewLine::getUnixNewLine();
   this->writer->write(message);
 }
 

+ 63 - 0
test/cases/io/logging/LoggerTest.cpp

@@ -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;
@@ -87,6 +88,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::DEBUG);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -117,6 +119,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::ERR);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -147,6 +150,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::FATAL);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.debug("1. line!");
     logger.info("2. line!");
     logger.error("3. line!");
@@ -191,6 +195,44 @@ namespace
     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::getUnixNewLine()).c_str(), content.c_str());
+  }
+
   TEST_F(LoggerTest, info)
   {
     // write to log file
@@ -201,6 +243,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::INFO);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -247,6 +290,24 @@ namespace
     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
@@ -257,6 +318,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::TRACE);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");
@@ -287,6 +349,7 @@ namespace
     Logger logger{writer};
     logger.setLogLevel(LogLevelValue::WARN);
     logger.showLogLevel();
+    logger.showTimestamp();
     logger.fatal("1. line!");
     logger.error("2. line!");
     logger.warn("3. line!");