LogLevel.cpp 2.9 KB

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