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: 2022-05-11
  7. *
  8. * */
  9. #include <ls_std/io/logging/LogLevel.hpp>
  10. #include <ls_std/core/exception/IllegalArgumentException.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::operator unsigned char() const
  22. {
  23. return this->value;
  24. }
  25. ls::std::io::LogLevel &ls::std::io::LogLevel::operator=(const ls::std::io::LogLevelValue &_value)
  26. {
  27. this->value = _value;
  28. return *this;
  29. }
  30. bool ls::std::io::LogLevel::operator<(const ls::std::io::LogLevelValue &_value)
  31. {
  32. return this->value < _value;
  33. }
  34. bool ls::std::io::LogLevel::operator<=(const ls::std::io::LogLevelValue &_value)
  35. {
  36. return this->value <= _value;
  37. }
  38. bool ls::std::io::LogLevel::operator>(const ls::std::io::LogLevelValue &_value)
  39. {
  40. return this->value > _value;
  41. }
  42. bool ls::std::io::LogLevel::operator>=(const ls::std::io::LogLevelValue &_value)
  43. {
  44. return this->value >= _value;
  45. }
  46. bool ls::std::io::LogLevel::operator==(const ls::std::io::LogLevelValue &_value)
  47. {
  48. return this->value == _value;
  49. }
  50. void ls::std::io::LogLevel::setLogLevel(const ls::std::io::LogLevelValue &_value)
  51. {
  52. this->value = _value;
  53. }
  54. void ls::std::io::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::core::IllegalArgumentException{};
  63. }
  64. }
  65. std::string ls::std::io::LogLevel::toString() const
  66. {
  67. return this->level.at(this->value);
  68. }
  69. ls::std::io::LogLevelValue ls::std::io::LogLevel::_getValueFromString(const ::std::string &_value)
  70. {
  71. ls::std::io::LogLevelValue logLevelValue{};
  72. for (const auto &logLevelString : this->level)
  73. {
  74. if (logLevelString.second == _value)
  75. {
  76. logLevelValue = (ls::std::io::LogLevelValue) logLevelString.first;
  77. break;
  78. }
  79. }
  80. return logLevelValue;
  81. }
  82. void ls::std::io::LogLevel::_init()
  83. {
  84. this->level.insert({ls::std::io::LogLevelValue::FATAL, "FATAL"});
  85. this->level.insert({ls::std::io::LogLevelValue::ERR, "ERROR"});
  86. this->level.insert({ls::std::io::LogLevelValue::WARN, "WARN"});
  87. this->level.insert({ls::std::io::LogLevelValue::INFO, "INFO"});
  88. this->level.insert({ls::std::io::LogLevelValue::DEBUG, "DEBUG"});
  89. this->level.insert({ls::std::io::LogLevelValue::TRACE, "TRACE"});
  90. }
  91. bool ls::std::io::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. }