LogLevel.cpp 2.7 KB

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