LogLevel.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-23
  7. *
  8. * */
  9. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  10. #include <ls-std/io/logging/LogLevel.hpp>
  11. using ls::std::core::Class;
  12. using ls::std::core::IllegalArgumentException;
  13. using ls::std::io::LogLevel;
  14. using ls::std::io::LogLevelValue;
  15. using std::string;
  16. LogLevel::LogLevel(const LogLevelValue &_value) : Class("LogLevel"), value(_value)
  17. {
  18. this->_init();
  19. }
  20. LogLevel::LogLevel() : Class("LogLevel")
  21. {
  22. this->_init();
  23. }
  24. LogLevel::~LogLevel() noexcept = default;
  25. LogLevel &LogLevel::operator=(const LogLevelValue &_value)
  26. {
  27. this->value = _value;
  28. return *this;
  29. }
  30. bool LogLevel::operator<(const LogLevelValue &_value)
  31. {
  32. return this->value < _value;
  33. }
  34. bool LogLevel::operator<=(const LogLevelValue &_value)
  35. {
  36. return this->value <= _value;
  37. }
  38. bool LogLevel::operator>(const LogLevelValue &_value)
  39. {
  40. return this->value > _value;
  41. }
  42. bool LogLevel::operator>=(const LogLevelValue &_value)
  43. {
  44. return this->value >= _value;
  45. }
  46. bool LogLevel::operator==(const LogLevelValue &_value)
  47. {
  48. return this->value == _value;
  49. }
  50. LogLevelValue LogLevel::getValue()
  51. {
  52. return this->value;
  53. }
  54. void LogLevel::setLogLevel(const LogLevelValue &_value)
  55. {
  56. this->value = _value;
  57. }
  58. void LogLevel::setLogLevel(const string &_value)
  59. {
  60. if (this->_isValidLogLevelString(_value))
  61. {
  62. this->value = _getValueFromString(_value);
  63. }
  64. else
  65. {
  66. throw IllegalArgumentException{_value + " is not a valid log level string"};
  67. }
  68. }
  69. string LogLevel::toString() const
  70. {
  71. return this->level.at(this->value);
  72. }
  73. LogLevelValue LogLevel::_getValueFromString(const string &_value)
  74. {
  75. LogLevelValue logLevelValue{};
  76. for (const auto &logLevelString : this->level)
  77. {
  78. if (logLevelString.second == _value)
  79. {
  80. logLevelValue = (LogLevelValue) logLevelString.first;
  81. break;
  82. }
  83. }
  84. return logLevelValue;
  85. }
  86. void LogLevel::_init()
  87. {
  88. this->level.insert({LogLevelValue::FATAL, "FATAL"});
  89. this->level.insert({LogLevelValue::ERR, "ERROR"});
  90. this->level.insert({LogLevelValue::WARN, "WARN"});
  91. this->level.insert({LogLevelValue::INFO, "INFO"});
  92. this->level.insert({LogLevelValue::DEBUG, "DEBUG"});
  93. this->level.insert({LogLevelValue::TRACE, "TRACE"});
  94. }
  95. bool LogLevel::_isValidLogLevelString(const string &_value)
  96. {
  97. bool isValidString{};
  98. for (const auto &logLevelString : this->level)
  99. {
  100. isValidString = logLevelString.second == _value;
  101. if (isValidString)
  102. {
  103. break;
  104. }
  105. }
  106. return isValidString;
  107. }