Logger.cpp 4.3 KB

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