Logger.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef CORE_LOGGER_HPP
  2. #define CORE_LOGGER_HPP
  3. #include "core/utils/ArrayString.hpp"
  4. namespace Core::Logger {
  5. enum class Level { ERROR, WARNING, INFO, DEBUG };
  6. extern Level level;
  7. [[maybe_unused]] static constexpr const char* COLOR_RED = "\33[1;31m";
  8. [[maybe_unused]] static constexpr const char* COLOR_YELLOW = "\33[1;33m";
  9. [[maybe_unused]] static constexpr const char* COLOR_GRAY = "\33[1;37m";
  10. [[maybe_unused]] static constexpr const char* COLOR_GREEN = "\33[1;32m";
  11. [[maybe_unused]] static constexpr const char* COLOR_RESET = "\33[0m";
  12. const char* getFileName(const char* path);
  13. inline bool filterError(Error e) {
  14. return e != Error::NONE && e != Error::CAPACITY_REACHED;
  15. }
  16. // aborts on critical logging failure
  17. template<typename... Args>
  18. void log(Level l, const char* file, int line, const char* prefix,
  19. const char* tag, const char* format, Args&&... args) {
  20. if(Core::Logger::level < l) {
  21. return;
  22. }
  23. file = getFileName(file);
  24. Core::String32<2048> s;
  25. if(filterError(s.append(prefix)) || filterError(s.append(tag)) ||
  26. filterError(s.append("#:# | ")) ||
  27. filterError(s.format(file, line)) || filterError(s.append(format)) ||
  28. filterError(s.format(Core::forward<Args>(args)...)) ||
  29. filterError(s.append(COLOR_RESET)) || filterError(s.printLine())) {
  30. CORE_EXIT(1); // CoverageIgnore
  31. }
  32. }
  33. template<typename... Args>
  34. void log(const char* prefix, const char* format, Args&&... args) {
  35. Core::String32<2048> s;
  36. if(filterError(s.append(prefix)) || filterError(s.append(format)) ||
  37. filterError(s.format(Core::forward<Args>(args)...)) ||
  38. filterError(s.append(COLOR_RESET)) || filterError(s.printLine())) {
  39. CORE_EXIT(1); // CoverageIgnore
  40. }
  41. }
  42. }
  43. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 1
  44. #define CORE_LOG_ERROR(format, ...) \
  45. log(Core::Logger::Level::ERROR, __FILE__, __LINE__, \
  46. Core::Logger::COLOR_RED, "[ERROR] ", \
  47. format __VA_OPT__(, ) __VA_ARGS__);
  48. #else
  49. #define CORE_LOG_ERROR(format, ...)
  50. #endif
  51. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 2
  52. #define CORE_LOG_WARNING(format, ...) \
  53. log(Core::Logger::Level::WARNING, __FILE__, __LINE__, \
  54. Core::Logger::COLOR_YELLOW, "[WARNING] ", \
  55. format __VA_OPT__(, ) __VA_ARGS__);
  56. #else
  57. #define CORE_LOG_WARNING(format, ...)
  58. #endif
  59. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 3
  60. #define CORE_LOG_INFO(format, ...) \
  61. log(Core::Logger::Level::INFO, __FILE__, __LINE__, \
  62. Core::Logger::COLOR_GRAY, "[INFO] ", \
  63. format __VA_OPT__(, ) __VA_ARGS__);
  64. #else
  65. #define CORE_LOG_INFO(format, ...)
  66. #endif
  67. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 4
  68. #define CORE_LOG_DEBUG(format, ...) \
  69. log(Core::Logger::Level::DEBUG, __FILE__, __LINE__, \
  70. Core::Logger::COLOR_GREEN, "[DEBUG] ", \
  71. format __VA_OPT__(, ) __VA_ARGS__);
  72. #else
  73. #define CORE_LOG_DEBUG(format, ...)
  74. #endif
  75. #endif