123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-20
- * Changed: 2023-05-19
- *
- * */
- #include <algorithm>
- #include <ls-std/core/exception/IllegalArgumentException.hpp>
- #include <ls-std/io/logging/LogLevel.hpp>
- using ls::std::core::Class;
- using ls::std::core::IllegalArgumentException;
- using ls::std::io::LogLevel;
- using ls::std::io::LogLevelValue;
- using std::find_if;
- using std::pair;
- using std::string;
- using std::string_view;
- LogLevel::LogLevel(const LogLevelValue &_value) : Class("LogLevel"), value(_value)
- {
- this->_init();
- }
- LogLevel::LogLevel() : Class("LogLevel")
- {
- this->_init();
- }
- LogLevel::~LogLevel() noexcept = default;
- LogLevel &LogLevel::operator=(const LogLevelValue &_value)
- {
- this->value = _value;
- return *this;
- }
- bool LogLevel::operator<(const LogLevelValue &_value) const
- {
- return this->value < _value;
- }
- bool LogLevel::operator<=(const LogLevelValue &_value) const
- {
- return this->value <= _value;
- }
- bool LogLevel::operator>(const LogLevelValue &_value) const
- {
- return this->value > _value;
- }
- bool LogLevel::operator>=(const LogLevelValue &_value) const
- {
- return this->value >= _value;
- }
- bool LogLevel::operator==(const LogLevelValue &_value) const
- {
- return this->value == _value;
- }
- LogLevelValue LogLevel::getValue() const
- {
- return this->value;
- }
- void LogLevel::setLogLevel(const LogLevelValue &_value)
- {
- this->value = _value;
- }
- void LogLevel::setLogLevel(const string &_value)
- {
- if (this->_isValidLogLevelString(string_view{_value}))
- {
- this->value = _getValueFromString(_value);
- }
- else
- {
- throw IllegalArgumentException{_value + " is not a valid log level string"};
- }
- }
- string LogLevel::toString() const
- {
- return this->level.at(this->value);
- }
- LogLevelValue LogLevel::_getValueFromString(const string &_value)
- {
- const auto &iterator = find_if(this->level.begin(), this->level.end(), [&_value](const pair<LogLevelValue, string> &_logLevelString) { return _logLevelString.second == _value; });
- const auto &[logLevelEnumValue, logLevelStringRepresentation] = *iterator;
- return iterator != level.end() ? logLevelEnumValue : LogLevelValue{};
- }
- void LogLevel::_init()
- {
- this->level.try_emplace(LogLevelValue::FATAL, "FATAL");
- this->level.try_emplace(LogLevelValue::ERR, "ERROR");
- this->level.try_emplace(LogLevelValue::WARN, "WARN");
- this->level.try_emplace(LogLevelValue::INFO, "INFO");
- this->level.try_emplace(LogLevelValue::DEBUG, "DEBUG");
- this->level.try_emplace(LogLevelValue::TRACE, "TRACE");
- }
- bool LogLevel::_isValidLogLevelString(string_view _value) const
- {
- bool isValidString{};
- for (const auto &[logLevelEnumValue, logLevelStringRepresentation] : this->level)
- {
- isValidString = logLevelStringRepresentation == _value;
- if (isValidString)
- {
- break;
- }
- }
- return isValidString;
- }
|