Logger.cpp 2.3 KB

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