Logger.cpp 3.4 KB

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