Przeglądaj źródła

Remove "time" dependency from "io" module

Patrick-Christopher Mattulat 2 lat temu
rodzic
commit
6da5fcff3c

+ 2 - 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:         2022-05-12
+ * Changed:         2022-05-13
  *
  * */
 
@@ -45,6 +45,7 @@ namespace ls
           ::std::shared_ptr<ls::std::core::IWriter> writer{};
 
           void _assignWriter(const ::std::shared_ptr<ls::std::core::IWriter> &_writer);
+          static ::std::string _generateTimeString(tm *_localTime);
           void _log(const ls::std::core::type::byte *_data, const ls::std::io::LogLevel &_logLevel);
       };
     }

+ 15 - 4
source/ls_std/io/logging/Logger.cpp

@@ -3,12 +3,13 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-20
- * Changed:         2022-05-12
+ * Changed:         2022-05-13
  *
  * */
 
 #include <ls_std/io/logging/Logger.hpp>
-#include <ls_std/time/Date.hpp> // FIXME: remove "time" dependency from "io" module
+#include <ctime>
+#include <iomanip>
 #include <ls_std/io/NewLine.hpp>
 #include <ls_std/boxing/String.hpp> // FIXME: remove "boxing" dependency from "io" module
 #include <ls_std/core/exception/IllegalArgumentException.hpp>
@@ -88,9 +89,19 @@ void ls::std::io::Logger::_assignWriter(const ::std::shared_ptr<ls::std::core::I
   this->writer = _writer;
 }
 
+::std::string ls::std::io::Logger::_generateTimeString(tm *_localTime)
+{
+  ::std::stringstream _stream{};
+  _stream << ::std::put_time(_localTime, "%Y-%m-%d %H:%M:%S");
+
+  return _stream.str();
+}
+
 void ls::std::io::Logger::_log(const ls::std::core::type::byte *_data, const ls::std::io::LogLevel &_logLevel)
 {
-  ls::std::time::Date date{};
-  ::std::string message = "[" + date.toString() + "] " + ls::std::boxing::String{_logLevel.toString() + ":"}.padRight(10, ' ') + ::std::string(_data) + ls::std::io::NewLine::getUnixNewLine();
+  time_t timestamp = ::std::time(nullptr);
+  tm *localTime = ::std::localtime(&timestamp);
+
+  ::std::string message = "[" + ls::std::io::Logger::_generateTimeString(localTime) + "] " + ls::std::boxing::String{_logLevel.toString() + ":"}.padRight(10, ' ') + ::std::string(_data) + ls::std::io::NewLine::getUnixNewLine();
   this->writer->write(message);
 }