/* * Author: Patrick-Christopher Mattulat * Company: Lynar Studios * E-Mail: webmaster@lynarstudios.com * Created: 2020-08-20 * Changed: 2023-05-19 * * */ #include #include #include 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 &_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; }