Logger.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef CORE_LOGGER_HPP
  2. #define CORE_LOGGER_HPP
  3. #include "utils/ArrayString.hpp"
  4. namespace Core::Logger {
  5. enum class Level { ERROR, WARNING, INFO, DEBUG };
  6. extern Level level;
  7. const char* getFileName(const char* path);
  8. inline bool filterError(Error e) {
  9. return e != Error::NONE && e != Error::CAPACITY_REACHED;
  10. }
  11. // aborts on critical logging failure
  12. template<typename... Args>
  13. void log(Level l, const char* file, int line, const char* start,
  14. const char* format, Args&&... args) {
  15. if(Core::Logger::level < l) {
  16. return;
  17. }
  18. file = getFileName(file);
  19. Core::String32<2048> s;
  20. if(filterError(s.append(start)) || filterError(s.append("#:# | ")) ||
  21. filterError(s.format(file, line)) || filterError(s.append(format)) ||
  22. filterError(s.format(Core::forward<Args>(args)...)) ||
  23. filterError(s.append("\33[39;49m\n")) || filterError(s.print())) {
  24. CORE_EXIT(1);
  25. }
  26. }
  27. }
  28. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 1
  29. #define CORE_LOG_ERROR(format, ...) \
  30. log(Core::Logger::Level::ERROR, __FILE__, __LINE__, "\33[1;31m[ERROR] ", \
  31. format __VA_OPT__(, ) __VA_ARGS__);
  32. #else
  33. #define CORE_LOG_ERROR(format, ...)
  34. #endif
  35. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 2
  36. #define CORE_LOG_WARNING(format, ...) \
  37. log(Core::Logger::Level::WARNING, __FILE__, __LINE__, \
  38. "\33[1;33m[WARNING] ", format __VA_OPT__(, ) __VA_ARGS__);
  39. #else
  40. #define CORE_LOG_WARNING(format, ...)
  41. #endif
  42. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 3
  43. #define CORE_LOG_INFO(format, ...) \
  44. log(Core::Logger::Level::INFO, __FILE__, __LINE__, "\33[1;37m[INFO] ", \
  45. format __VA_OPT__(, ) __VA_ARGS__);
  46. #else
  47. #define CORE_LOG_INFO(format, ...)
  48. #endif
  49. #if defined(CORE_LOG_LEVEL) && CORE_LOG_LEVEL >= 4
  50. #define CORE_LOG_DEBUG(format, ...) \
  51. log(Core::Logger::Level::DEBUG, __FILE__, __LINE__, "\33[1;32m[DEBUG] ", \
  52. format __VA_OPT__(, ) __VA_ARGS__);
  53. #else
  54. #define CORE_LOG_DEBUG(format, ...)
  55. #endif
  56. #endif