Logger.hpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // aborts on critical logging failure
  14. template<typename... Args>
  15. void log(Level l, const char* file, int line, const char* prefix,
  16. const char* tag, const char* format, Args&&... args) {
  17. if(Core::Logger::level < l) {
  18. return;
  19. }
  20. file = getFileName(file);
  21. Core::ArrayString<2048> s;
  22. s.append(prefix).append(tag).append("#:# | ");
  23. s.format(file, line);
  24. s.append(format);
  25. s.format(Core::forward<Args>(args)...);
  26. s.append(COLOR_RESET);
  27. s.printLine();
  28. }
  29. template<typename... Args>
  30. void log(const char* prefix, const char* format, Args&&... args) {
  31. Core::ArrayString<2048> s;
  32. s.append(prefix).append(format);
  33. s.format(Core::forward<Args>(args)...);
  34. s.append(COLOR_RESET).printLine();
  35. }
  36. }
  37. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 1
  38. #define CORE_LOG_ERROR(format, ...) \
  39. log(Core::Logger::Level::ERROR, __FILE__, __LINE__, \
  40. Core::Logger::COLOR_RED, "[ERROR] ", \
  41. format __VA_OPT__(, ) __VA_ARGS__);
  42. #else
  43. #define CORE_LOG_ERROR(format, ...)
  44. #endif
  45. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 2
  46. #define CORE_LOG_WARNING(format, ...) \
  47. log(Core::Logger::Level::WARNING, __FILE__, __LINE__, \
  48. Core::Logger::COLOR_YELLOW, "[WARNING] ", \
  49. format __VA_OPT__(, ) __VA_ARGS__);
  50. #else
  51. #define CORE_LOG_WARNING(format, ...)
  52. #endif
  53. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 3
  54. #define CORE_LOG_INFO(format, ...) \
  55. log(Core::Logger::Level::INFO, __FILE__, __LINE__, \
  56. Core::Logger::COLOR_GRAY, "[INFO] ", \
  57. format __VA_OPT__(, ) __VA_ARGS__);
  58. #else
  59. #define CORE_LOG_INFO(format, ...)
  60. #endif
  61. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 4
  62. #define CORE_LOG_DEBUG(format, ...) \
  63. log(Core::Logger::Level::DEBUG, __FILE__, __LINE__, \
  64. Core::Logger::COLOR_GREEN, "[DEBUG] ", \
  65. format __VA_OPT__(, ) __VA_ARGS__);
  66. #else
  67. #define CORE_LOG_DEBUG(format, ...)
  68. #endif
  69. #endif