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