Logger.cpp 3.8 KB

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