Logger.cpp 2.3 KB

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