Logger.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 != ErrorCode::NONE && e != ErrorCode::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))) {
  30. CORE_EXIT(1); // CoverageIgnore
  31. }
  32. s.printLine();
  33. }
  34. template<typename... Args>
  35. void log(const char* prefix, const char* format, Args&&... args) {
  36. Core::String32<2048> s;
  37. if(filterError(s.append(prefix)) || filterError(s.append(format)) ||
  38. filterError(s.format(Core::forward<Args>(args)...)) ||
  39. filterError(s.append(COLOR_RESET))) {
  40. CORE_EXIT(1); // CoverageIgnore
  41. }
  42. s.printLine();
  43. }
  44. }
  45. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 1
  46. #define CORE_LOG_ERROR(format, ...) \
  47. log(Core::Logger::Level::ERROR, __FILE__, __LINE__, \
  48. Core::Logger::COLOR_RED, "[ERROR] ", \
  49. format __VA_OPT__(, ) __VA_ARGS__);
  50. #else
  51. #define CORE_LOG_ERROR(format, ...)
  52. #endif
  53. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 2
  54. #define CORE_LOG_WARNING(format, ...) \
  55. log(Core::Logger::Level::WARNING, __FILE__, __LINE__, \
  56. Core::Logger::COLOR_YELLOW, "[WARNING] ", \
  57. format __VA_OPT__(, ) __VA_ARGS__);
  58. #else
  59. #define CORE_LOG_WARNING(format, ...)
  60. #endif
  61. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 3
  62. #define CORE_LOG_INFO(format, ...) \
  63. log(Core::Logger::Level::INFO, __FILE__, __LINE__, \
  64. Core::Logger::COLOR_GRAY, "[INFO] ", \
  65. format __VA_OPT__(, ) __VA_ARGS__);
  66. #else
  67. #define CORE_LOG_INFO(format, ...)
  68. #endif
  69. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 4
  70. #define CORE_LOG_DEBUG(format, ...) \
  71. log(Core::Logger::Level::DEBUG, __FILE__, __LINE__, \
  72. Core::Logger::COLOR_GREEN, "[DEBUG] ", \
  73. format __VA_OPT__(, ) __VA_ARGS__);
  74. #else
  75. #define CORE_LOG_DEBUG(format, ...)
  76. #endif
  77. #endif