123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-20
- * Changed: 2023-04-12
- *
- * */
- #include <ctime>
- #include <iomanip>
- #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
- #include <ls-std/io/NewLine.hpp>
- #include <ls-std/io/logging/Logger.hpp>
- #if defined(_MSC_VER) || defined(__APPLE__)
- #include <sstream>
- #endif
- using ls::std::core::Class;
- using ls::std::core::NullPointerArgumentEvaluator;
- using ls::std::core::interface_type::IWriter;
- using ls::std::core::type::byte_type;
- using ls::std::io::Logger;
- using ls::std::io::LogLevel;
- using ls::std::io::LogLevelValue;
- using ls::std::io::NewLine;
- using std::localtime;
- using std::put_time;
- using std::shared_ptr;
- using std::string;
- using std::stringstream;
- using std::time;
- Logger::Logger(const shared_ptr<IWriter> &_writer) : Class("Logger"), logLevel(LogLevelValue::INFO)
- {
- this->_assignWriter(_writer);
- }
- Logger::~Logger() noexcept = default;
- void Logger::debug(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::DEBUG)
- {
- this->_log(_data, LogLevel(LogLevelValue::DEBUG));
- }
- }
- void Logger::error(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::ERR)
- {
- this->_log(_data, LogLevel(LogLevelValue::ERR));
- }
- }
- void Logger::fatal(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::FATAL)
- {
- this->_log(_data, LogLevel(LogLevelValue::FATAL));
- }
- }
- LogLevel Logger::getLogLevel()
- {
- return this->logLevel;
- }
- void Logger::hideLogLevel()
- {
- this->displayLogLevel = false;
- }
- void Logger::hideTimestamp()
- {
- this->displayTimestamp = false;
- }
- void Logger::info(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::INFO)
- {
- this->_log(_data, LogLevel(LogLevelValue::INFO));
- }
- }
- void Logger::setLogLevel(const LogLevelValue &_logLevelValue)
- {
- this->logLevel = _logLevelValue;
- }
- void Logger::showLogLevel()
- {
- this->displayLogLevel = true;
- }
- void Logger::showTimestamp()
- {
- this->displayTimestamp = true;
- }
- void Logger::trace(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::TRACE)
- {
- this->_log(_data, LogLevel(LogLevelValue::TRACE));
- }
- }
- void Logger::warn(const byte_type *_data)
- {
- if (this->logLevel >= LogLevelValue::WARN)
- {
- this->_log(_data, LogLevel(LogLevelValue::WARN));
- }
- }
- void Logger::_assignWriter(const shared_ptr<IWriter> &_writer)
- {
- NullPointerArgumentEvaluator{_writer, "writer reference is null!"}.evaluate();
- this->writer = _writer;
- }
- string Logger::_buildCharacterChain(size_t _amount)
- {
- string fillContent{};
- for (size_t iteration{}; iteration < _amount; iteration++)
- {
- fillContent += ' ';
- }
- return fillContent;
- }
- string Logger::_createFillContent(const string &_text)
- {
- size_t padSize = 10;
- size_t fillSize = _text.size() > padSize ? 0 : padSize - _text.size();
- string fillContent{};
- if (fillSize > 0)
- {
- fillContent = Logger::_buildCharacterChain(fillSize);
- }
- return fillContent;
- }
- string Logger::_generateTimeString(tm *_localTime)
- {
- stringstream _stream{};
- _stream << put_time(_localTime, "%Y-%m-%d %H:%M:%S");
- return _stream.str();
- }
- string Logger::_getLogLevelString(const LogLevel &_logLevel) const
- {
- string logLevelString{};
- if (this->displayLogLevel)
- {
- logLevelString = Logger::_padRight(string{_logLevel.toString() + ":"});
- }
- return logLevelString;
- }
- string Logger::_getTimestampString() const
- {
- time_t timestamp = ::time(nullptr);
- tm *localTime = localtime(×tamp);
- string timestampString{};
- if (this->displayTimestamp)
- {
- timestampString = "[" + Logger::_generateTimeString(localTime) + "] ";
- }
- return timestampString;
- }
- void Logger::_log(const byte_type *_data, const LogLevel &_logLevel)
- {
- string logLevelString = this->_getLogLevelString(_logLevel);
- string timestampString = this->_getTimestampString();
- string message = timestampString + logLevelString + string(_data) + NewLine::getUnixNewLine();
- this->writer->write(message);
- }
- string Logger::_padRight(const string &_text)
- {
- return _text + Logger::_createFillContent(_text);
- }
|