Logger.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2023-05-18
  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::string_view;
  30. using std::stringstream;
  31. using std::time;
  32. Logger::Logger(const shared_ptr<IWriter> &_writer) : Class("Logger"), logLevel(LogLevelValue::INFO)
  33. {
  34. this->_assignWriter(_writer);
  35. }
  36. Logger::~Logger() noexcept = default;
  37. void Logger::debug(const byte_type *_data) const
  38. {
  39. if (this->logLevel >= LogLevelValue::DEBUG)
  40. {
  41. this->_log(_data, LogLevel(LogLevelValue::DEBUG));
  42. }
  43. }
  44. void Logger::error(const byte_type *_data) const
  45. {
  46. if (this->logLevel >= LogLevelValue::ERR)
  47. {
  48. this->_log(_data, LogLevel(LogLevelValue::ERR));
  49. }
  50. }
  51. void Logger::fatal(const byte_type *_data) const
  52. {
  53. if (this->logLevel >= LogLevelValue::FATAL)
  54. {
  55. this->_log(_data, LogLevel(LogLevelValue::FATAL));
  56. }
  57. }
  58. LogLevel Logger::getLogLevel() const
  59. {
  60. return this->logLevel;
  61. }
  62. void Logger::hideLogLevel()
  63. {
  64. this->displayLogLevel = false;
  65. }
  66. void Logger::hideTimestamp()
  67. {
  68. this->displayTimestamp = false;
  69. }
  70. void Logger::info(const byte_type *_data) const
  71. {
  72. if (this->logLevel >= LogLevelValue::INFO)
  73. {
  74. this->_log(_data, LogLevel(LogLevelValue::INFO));
  75. }
  76. }
  77. void Logger::setLogLevel(const LogLevelValue &_logLevelValue)
  78. {
  79. this->logLevel = _logLevelValue;
  80. }
  81. void Logger::showLogLevel()
  82. {
  83. this->displayLogLevel = true;
  84. }
  85. void Logger::showTimestamp()
  86. {
  87. this->displayTimestamp = true;
  88. }
  89. void Logger::trace(const byte_type *_data) const
  90. {
  91. if (this->logLevel >= LogLevelValue::TRACE)
  92. {
  93. this->_log(_data, LogLevel(LogLevelValue::TRACE));
  94. }
  95. }
  96. void Logger::warn(const byte_type *_data) const
  97. {
  98. if (this->logLevel >= LogLevelValue::WARN)
  99. {
  100. this->_log(_data, LogLevel(LogLevelValue::WARN));
  101. }
  102. }
  103. void Logger::_assignWriter(const shared_ptr<IWriter> &_writer)
  104. {
  105. NullPointerArgumentEvaluator{_writer, "writer reference is null!"}.evaluate();
  106. this->writer = _writer;
  107. }
  108. string Logger::_buildCharacterChain(size_t _amount)
  109. {
  110. string fillContent{};
  111. for (size_t iteration{}; iteration < _amount; iteration++)
  112. {
  113. fillContent += ' ';
  114. }
  115. return fillContent;
  116. }
  117. string Logger::_createFillContent(string_view _text)
  118. {
  119. size_t padSize = 10;
  120. size_t fillSize = _text.size() > padSize ? 0 : padSize - _text.size();
  121. string fillContent{};
  122. if (fillSize > 0)
  123. {
  124. fillContent = Logger::_buildCharacterChain(fillSize);
  125. }
  126. return fillContent;
  127. }
  128. string Logger::_generateTimeString(const tm *_localTime)
  129. {
  130. stringstream _stream{};
  131. _stream << put_time(_localTime, "%Y-%m-%d %H:%M:%S");
  132. return _stream.str();
  133. }
  134. string Logger::_getLogLevelString(const LogLevel &_logLevel) const
  135. {
  136. string logLevelString{};
  137. if (this->displayLogLevel)
  138. {
  139. logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
  140. }
  141. return logLevelString;
  142. }
  143. string Logger::_getTimestampString() const
  144. {
  145. time_t timestamp = ::time(nullptr);
  146. struct tm localTime
  147. {
  148. };
  149. localtime_r(&timestamp, &localTime);
  150. string timestampString{};
  151. if (this->displayTimestamp)
  152. {
  153. timestampString = "[" + Logger::_generateTimeString(&localTime) + "] ";
  154. }
  155. return timestampString;
  156. }
  157. void Logger::_log(const byte_type *_data, const LogLevel &_logLevel) const
  158. {
  159. string logLevelString = this->_getLogLevelString(_logLevel);
  160. string timestampString = this->_getTimestampString();
  161. string message = timestampString + logLevelString + string(_data) + NewLine::getUnixNewLine();
  162. this->writer->write(message);
  163. }
  164. string Logger::_padRight(const string &_text)
  165. {
  166. return _text + Logger::_createFillContent(_text);
  167. }