Logger.cpp 4.1 KB

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