Logger.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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-04
  7. *
  8. * */
  9. #include <ctime>
  10. #include <iomanip>
  11. #include <ls-std/core/exception/IllegalArgumentException.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() = 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. if (_writer == nullptr)
  75. {
  76. throw ls::std::core::IllegalArgumentException{};
  77. }
  78. this->writer = _writer;
  79. }
  80. ::std::string ls::std::io::Logger::_buildCharacterChain(size_t _amount)
  81. {
  82. ::std::string fillContent{};
  83. for (size_t iteration{}; iteration < _amount; iteration++)
  84. {
  85. fillContent += ' ';
  86. }
  87. return fillContent;
  88. }
  89. ::std::string ls::std::io::Logger::_createFillContent(const ::std::string &_text)
  90. {
  91. size_t padSize = 10;
  92. size_t fillSize = _text.size() > padSize ? 0 : padSize - _text.size();
  93. ::std::string fillContent{};
  94. if (fillSize > 0)
  95. {
  96. fillContent = ls::std::io::Logger::_buildCharacterChain(fillSize);
  97. }
  98. return fillContent;
  99. }
  100. ::std::string ls::std::io::Logger::_generateTimeString(tm *_localTime)
  101. {
  102. ::std::stringstream _stream{};
  103. _stream << ::std::put_time(_localTime, "%Y-%m-%d %H:%M:%S");
  104. return _stream.str();
  105. }
  106. void ls::std::io::Logger::_log(const ls::std::core::type::byte *_data, const ls::std::io::LogLevel &_logLevel)
  107. {
  108. time_t timestamp = ::std::time(nullptr);
  109. tm *localTime = ::std::localtime(&timestamp);
  110. ::std::string logLevelString = ls::std::io::Logger::_padRight(::std::string{_logLevel.toString() + ":"});
  111. ::std::string message = "[" + ls::std::io::Logger::_generateTimeString(localTime) + "] " + logLevelString + ::std::string(_data) + ls::std::io::NewLine::getUnixNewLine();
  112. this->writer->write(message);
  113. }
  114. ::std::string ls::std::io::Logger::_padRight(const ::std::string &_text)
  115. {
  116. return _text + ls::std::io::Logger::_createFillContent(_text);
  117. }