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