Logger.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2021-07-16
  7. *
  8. * */
  9. #include <ls_std/io/logging/Logger.hpp>
  10. #include <ls_std/time/Date.hpp>
  11. #include <ls_std/io/NewLine.hpp>
  12. #include <ls_std/boxing/String.hpp>
  13. #include <ls_std/exception/IllegalArgumentException.hpp>
  14. ls_std::Logger::Logger(const std::shared_ptr<ls_std::IWriter> &_writer)
  15. : ls_std::Class("Logger"),
  16. logLevel(ls_std::LogLevelValue::INFO)
  17. {
  18. this->_assignWriter(_writer);
  19. }
  20. void ls_std::Logger::debug(const ls_std::byte *_data)
  21. {
  22. if (this->logLevel >= ls_std::LogLevelValue::DEBUG)
  23. {
  24. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::DEBUG));
  25. }
  26. }
  27. void ls_std::Logger::error(const ls_std::byte *_data)
  28. {
  29. if (this->logLevel >= ls_std::LogLevelValue::ERR)
  30. {
  31. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::ERR));
  32. }
  33. }
  34. void ls_std::Logger::fatal(const ls_std::byte *_data)
  35. {
  36. if (this->logLevel >= ls_std::LogLevelValue::FATAL)
  37. {
  38. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::FATAL));
  39. }
  40. }
  41. ls_std::LogLevel ls_std::Logger::getLogLevel()
  42. {
  43. return this->logLevel;
  44. }
  45. void ls_std::Logger::info(const ls_std::byte *_data)
  46. {
  47. if (this->logLevel >= ls_std::LogLevelValue::INFO)
  48. {
  49. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::INFO));
  50. }
  51. }
  52. void ls_std::Logger::setLogLevel(const ls_std::LogLevelValue &_logLevelValue)
  53. {
  54. this->logLevel = _logLevelValue;
  55. }
  56. void ls_std::Logger::trace(const ls_std::byte *_data)
  57. {
  58. if (this->logLevel >= ls_std::LogLevelValue::TRACE)
  59. {
  60. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::TRACE));
  61. }
  62. }
  63. void ls_std::Logger::warn(const ls_std::byte *_data)
  64. {
  65. if (this->logLevel >= ls_std::LogLevelValue::WARN)
  66. {
  67. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::WARN));
  68. }
  69. }
  70. void ls_std::Logger::_assignWriter(const std::shared_ptr<ls_std::IWriter> &_writer)
  71. {
  72. if (_writer == nullptr)
  73. {
  74. throw ls_std::IllegalArgumentException{};
  75. }
  76. this->writer = _writer;
  77. }
  78. void ls_std::Logger::_log(const ls_std::byte *_data, const ls_std::LogLevel &_logLevel)
  79. {
  80. ls_std::Date date{};
  81. std::string message = "[" + date.toString() + "] " + ls_std::String{_logLevel.toString() + ":"}.padRight(10, ' ') + std::string(_data) + ls_std::NewLine::getUnixNewLine();
  82. this->writer->write(message);
  83. }