Logger.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <ctime>
  10. #include <iomanip>
  11. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  12. #include <ls-std/io/NewLine.hpp>
  13. #include <ls-std/io/logging/Logger.hpp>
  14. #if defined(_MSC_VER) || defined(__APPLE__)
  15. #include <sstream>
  16. #endif
  17. ls::std::io::Logger::Logger(const ::std::shared_ptr<ls::std::core::interface_type::IWriter> &_writer) : ls::std::core::Class("Logger"), logLevel(ls::std::io::LogLevelValue::INFO)
  18. {
  19. this->_assignWriter(_writer);
  20. }
  21. ls::std::io::Logger::~Logger() noexcept = default;
  22. void ls::std::io::Logger::debug(const ls::std::core::type::byte *_data)
  23. {
  24. if (this->logLevel >= ls::std::io::LogLevelValue::DEBUG)
  25. {
  26. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::DEBUG));
  27. }
  28. }
  29. void ls::std::io::Logger::error(const ls::std::core::type::byte *_data)
  30. {
  31. if (this->logLevel >= ls::std::io::LogLevelValue::ERR)
  32. {
  33. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::ERR));
  34. }
  35. }
  36. void ls::std::io::Logger::fatal(const ls::std::core::type::byte *_data)
  37. {
  38. if (this->logLevel >= ls::std::io::LogLevelValue::FATAL)
  39. {
  40. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::FATAL));
  41. }
  42. }
  43. ls::std::io::LogLevel ls::std::io::Logger::getLogLevel()
  44. {
  45. return this->logLevel;
  46. }
  47. void ls::std::io::Logger::info(const ls::std::core::type::byte *_data)
  48. {
  49. if (this->logLevel >= ls::std::io::LogLevelValue::INFO)
  50. {
  51. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::INFO));
  52. }
  53. }
  54. void ls::std::io::Logger::setLogLevel(const ls::std::io::LogLevelValue &_logLevelValue)
  55. {
  56. this->logLevel = _logLevelValue;
  57. }
  58. void ls::std::io::Logger::trace(const ls::std::core::type::byte *_data)
  59. {
  60. if (this->logLevel >= ls::std::io::LogLevelValue::TRACE)
  61. {
  62. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::TRACE));
  63. }
  64. }
  65. void ls::std::io::Logger::warn(const ls::std::core::type::byte *_data)
  66. {
  67. if (this->logLevel >= ls::std::io::LogLevelValue::WARN)
  68. {
  69. this->_log(_data, ls::std::io::LogLevel(ls::std::io::LogLevelValue::WARN));
  70. }
  71. }
  72. void ls::std::io::Logger::_assignWriter(const ::std::shared_ptr<ls::std::core::interface_type::IWriter> &_writer)
  73. {
  74. ls::std::core::NullPointerArgumentEvaluator{_writer, "writer reference is null!"}.evaluate();
  75. this->writer = _writer;
  76. }
  77. ::std::string ls::std::io::Logger::_buildCharacterChain(size_t _amount)
  78. {
  79. ::std::string fillContent{};
  80. for (size_t iteration{}; iteration < _amount; iteration++)
  81. {
  82. fillContent += ' ';
  83. }
  84. return fillContent;
  85. }
  86. ::std::string ls::std::io::Logger::_createFillContent(const ::std::string &_text)
  87. {
  88. size_t padSize = 10;
  89. size_t fillSize = _text.size() > padSize ? 0 : padSize - _text.size();
  90. ::std::string fillContent{};
  91. if (fillSize > 0)
  92. {
  93. fillContent = ls::std::io::Logger::_buildCharacterChain(fillSize);
  94. }
  95. return fillContent;
  96. }
  97. ::std::string ls::std::io::Logger::_generateTimeString(tm *_localTime)
  98. {
  99. ::std::stringstream _stream{};
  100. _stream << ::std::put_time(_localTime, "%Y-%m-%d %H:%M:%S");
  101. return _stream.str();
  102. }
  103. void ls::std::io::Logger::_log(const ls::std::core::type::byte *_data, const ls::std::io::LogLevel &_logLevel)
  104. {
  105. time_t timestamp = ::std::time(nullptr);
  106. tm *localTime = ::std::localtime(&timestamp);
  107. ::std::string logLevelString = ls::std::io::Logger::_padRight(::std::string{_logLevel.toString() + ":"});
  108. ::std::string message = "[" + ls::std::io::Logger::_generateTimeString(localTime) + "] " + logLevelString + ::std::string(_data) + ls::std::io::NewLine::getUnixNewLine();
  109. this->writer->write(message);
  110. }
  111. ::std::string ls::std::io::Logger::_padRight(const ::std::string &_text)
  112. {
  113. return _text + ls::std::io::Logger::_createFillContent(_text);
  114. }