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