Logger.cpp 3.7 KB

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