LogLevel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Co-Author: Claude Sonnet 4.6 (LLM)
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-08-20
  7. * Changed: 2026-06-23
  8. *
  9. * */
  10. #include <algorithm>
  11. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  12. #include <ls-std/io/logging/LogLevel.hpp>
  13. using ls::standard::core::Class;
  14. using ls::standard::core::IllegalArgumentException;
  15. using ls::standard::io::LogLevel;
  16. using ls::standard::io::LogLevelValue;
  17. using std::find_if;
  18. using std::pair;
  19. using std::string;
  20. using std::string_view;
  21. LogLevel::LogLevel(const LogLevelValue &_value) : Class("LogLevel"), value(_value)
  22. {
  23. this->_init();
  24. }
  25. LogLevel::LogLevel() : Class("LogLevel")
  26. {
  27. this->_init();
  28. }
  29. LogLevel::~LogLevel() noexcept = default;
  30. LogLevel &LogLevel::operator=(const LogLevelValue &_value)
  31. {
  32. this->value = _value;
  33. return *this;
  34. }
  35. bool LogLevel::operator<(const LogLevelValue &_value) const
  36. {
  37. return this->value < _value;
  38. }
  39. bool LogLevel::operator<=(const LogLevelValue &_value) const
  40. {
  41. return this->value <= _value;
  42. }
  43. bool LogLevel::operator>(const LogLevelValue &_value) const
  44. {
  45. return this->value > _value;
  46. }
  47. bool LogLevel::operator>=(const LogLevelValue &_value) const
  48. {
  49. return this->value >= _value;
  50. }
  51. bool LogLevel::operator==(const LogLevelValue &_value) const
  52. {
  53. return this->value == _value;
  54. }
  55. LogLevelValue LogLevel::getValue() const
  56. {
  57. return this->value;
  58. }
  59. void LogLevel::setLogLevel(const LogLevelValue &_value)
  60. {
  61. this->value = _value;
  62. }
  63. void LogLevel::setLogLevel(const string &_value)
  64. {
  65. if (this->_isValidLogLevelString(string_view{_value}))
  66. {
  67. this->value = _getValueFromString(_value);
  68. }
  69. else
  70. {
  71. throw IllegalArgumentException{_value + " is not a valid log level string"};
  72. }
  73. }
  74. string LogLevel::toString() const
  75. {
  76. return this->level.at(this->value);
  77. }
  78. LogLevelValue LogLevel::_getValueFromString(const string &_value)
  79. {
  80. const auto &iterator = find_if(this->level.begin(), this->level.end(), [&_value](const pair<LogLevelValue, string> &_logLevelString) { return _logLevelString.second == _value; });
  81. const auto &[logLevelEnumValue, logLevelStringRepresentation] = *iterator;
  82. return iterator != level.end() ? logLevelEnumValue : LogLevelValue{};
  83. }
  84. void LogLevel::_init()
  85. {
  86. this->level.try_emplace(LogLevelValue::FATAL, "FATAL");
  87. this->level.try_emplace(LogLevelValue::ERR, "ERROR");
  88. this->level.try_emplace(LogLevelValue::WARN, "WARN");
  89. this->level.try_emplace(LogLevelValue::INFO, "INFO");
  90. this->level.try_emplace(LogLevelValue::DEBUG, "DEBUG");
  91. this->level.try_emplace(LogLevelValue::TRACE, "TRACE");
  92. }
  93. bool LogLevel::_isValidLogLevelString(const string_view _value) const
  94. {
  95. bool isValidString{};
  96. for (const auto &[logLevelEnumValue, logLevelStringRepresentation] : this->level)
  97. {
  98. isValidString = logLevelStringRepresentation == _value;
  99. if (isValidString)
  100. {
  101. break;
  102. }
  103. }
  104. return isValidString;
  105. }