LogLevel.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <ls_std/io/logging/LogLevel.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. ls_std::LogLevel::LogLevel(const ls_std::LogLevelValue &_value)
  12. : ls_std::Class("LogLevel"),
  13. value(_value)
  14. {
  15. this->_init();
  16. }
  17. ls_std::LogLevel::LogLevel() : ls_std::Class("LogLevel")
  18. {
  19. this->_init();
  20. }
  21. ls_std::LogLevel::operator unsigned char() const
  22. {
  23. return this->value;
  24. }
  25. ls_std::LogLevel &ls_std::LogLevel::operator=(const ls_std::LogLevelValue &_value)
  26. {
  27. this->value = _value;
  28. return *this;
  29. }
  30. bool ls_std::LogLevel::operator<(const ls_std::LogLevelValue &_value)
  31. {
  32. return this->value < _value;
  33. }
  34. bool ls_std::LogLevel::operator<=(const ls_std::LogLevelValue &_value)
  35. {
  36. return this->value <= _value;
  37. }
  38. bool ls_std::LogLevel::operator>(const ls_std::LogLevelValue &_value)
  39. {
  40. return this->value > _value;
  41. }
  42. bool ls_std::LogLevel::operator>=(const ls_std::LogLevelValue &_value)
  43. {
  44. return this->value >= _value;
  45. }
  46. bool ls_std::LogLevel::operator==(const ls_std::LogLevelValue &_value)
  47. {
  48. return this->value == _value;
  49. }
  50. void ls_std::LogLevel::setLogLevel(const ls_std::LogLevelValue &_value)
  51. {
  52. this->value = _value;
  53. }
  54. void ls_std::LogLevel::setLogLevel(const std::string &_value)
  55. {
  56. if (this->_isValidLogLevelString(_value))
  57. {
  58. this->value = _getValueFromString(_value);
  59. }
  60. else
  61. {
  62. throw ls_std::IllegalArgumentException{};
  63. }
  64. }
  65. std::string ls_std::LogLevel::toString() const
  66. {
  67. return this->level.at(this->value);
  68. }
  69. ls_std::LogLevelValue ls_std::LogLevel::_getValueFromString(const std::string &_value)
  70. {
  71. ls_std::LogLevelValue logLevelValue{};
  72. for (const auto &logLevelString : this->level)
  73. {
  74. if (logLevelString.second == _value)
  75. {
  76. logLevelValue = (ls_std::LogLevelValue) logLevelString.first;
  77. break;
  78. }
  79. }
  80. return logLevelValue;
  81. }
  82. void ls_std::LogLevel::_init()
  83. {
  84. this->level.insert({ls_std::LogLevelValue::FATAL, "FATAL"});
  85. this->level.insert({ls_std::LogLevelValue::ERR, "ERROR"});
  86. this->level.insert({ls_std::LogLevelValue::WARN, "WARN"});
  87. this->level.insert({ls_std::LogLevelValue::INFO, "INFO"});
  88. this->level.insert({ls_std::LogLevelValue::DEBUG, "DEBUG"});
  89. this->level.insert({ls_std::LogLevelValue::TRACE, "TRACE"});
  90. }
  91. bool ls_std::LogLevel::_isValidLogLevelString(const std::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. }