Logger.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-20
  6. * Changed: 2020-10-29
  7. *
  8. * */
  9. #include "Logger.hpp"
  10. #include "../../time/Date.hpp"
  11. #include "../NewLine.hpp"
  12. #include "../../boxing/String.hpp"
  13. ls_std::Logger::Logger(const std::string &_path) : Class("Logger"),
  14. file(ls_std::File{_path}),
  15. logLevel(ls_std::LogLevelValue::INFO)
  16. {
  17. this->_init();
  18. }
  19. void ls_std::Logger::close()
  20. {
  21. this->outputStream->close();
  22. }
  23. void ls_std::Logger::debug(const ls_std::byte *_data)
  24. {
  25. if(this->logLevel >= ls_std::LogLevelValue::DEBUG) {
  26. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::DEBUG));
  27. }
  28. }
  29. void ls_std::Logger::error(const ls_std::byte *_data)
  30. {
  31. if(this->logLevel >= ls_std::LogLevelValue::ERR) {
  32. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::ERR));
  33. }
  34. }
  35. void ls_std::Logger::fatal(const ls_std::byte *_data)
  36. {
  37. if(this->logLevel >= ls_std::LogLevelValue::FATAL) {
  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. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::INFO));
  49. }
  50. }
  51. void ls_std::Logger::setLogLevel(const ls_std::LogLevelValue &_logLevelValue)
  52. {
  53. this->logLevel = _logLevelValue;
  54. }
  55. void ls_std::Logger::trace(const ls_std::byte *_data)
  56. {
  57. if(this->logLevel >= ls_std::LogLevelValue::TRACE) {
  58. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::TRACE));
  59. }
  60. }
  61. void ls_std::Logger::warn(const ls_std::byte *_data)
  62. {
  63. if(this->logLevel >= ls_std::LogLevelValue::WARN) {
  64. this->_log(_data, ls_std::LogLevel(ls_std::LogLevelValue::WARN));
  65. }
  66. }
  67. void ls_std::Logger::_init()
  68. {
  69. if(!this->file.exists()) {
  70. this->file.createNewFile();
  71. }
  72. }
  73. void ls_std::Logger::_log(const ls_std::byte *_data, const ls_std::LogLevel& _logLevel)
  74. {
  75. if(this->outputStream == nullptr) {
  76. this->outputStream = std::make_shared<ls_std::FileOutputStream>(this->file, true);
  77. }
  78. ls_std::Date date {};
  79. std::string message = "[" +
  80. date.toString() + "] " +
  81. ls_std::String {_logLevel.toString() + ":"}.padRight(10, ' ') +
  82. std::string(_data) +
  83. ls_std::NewLine::getUnixNewLine();
  84. outputStream->write(message);
  85. }