Logger.cppm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. export module Core.Logger;
  2. import Core.TerminalConstants;
  3. import Core.Meta;
  4. import Core.ToString;
  5. import Core.Std;
  6. #define SOURCE const std::source_location& sl = std::source_location::current()
  7. export namespace Core {
  8. enum class LogLevel { NONE, ERROR, WARNING, INFO, DEBUG };
  9. extern LogLevel logLevel;
  10. using ReportHandler = void (*)(
  11. LogLevel l, const std::source_location& sl, void* data,
  12. const char* message);
  13. void callReportHandler(LogLevel l, const char* report, SOURCE);
  14. void setReportHandler(ReportHandler h, void* data);
  15. struct FormatLocation final {
  16. const char* format;
  17. std::source_location location;
  18. FormatLocation(
  19. const char* f,
  20. const std::source_location& l = std::source_location::current()) :
  21. format(f), location(l) {
  22. }
  23. };
  24. template<typename... Args>
  25. void report(LogLevel l, const FormatLocation& format, Args&&... args) {
  26. char buffer[512];
  27. formatBuffer(
  28. buffer, sizeof(buffer), format.format,
  29. Core::forward<Args>(args)...);
  30. callReportHandler(l, buffer, format.location);
  31. }
  32. const char* getShortFileName(const char* s);
  33. template<typename... Args>
  34. void log(
  35. LogLevel l, const char* prefix, const FormatLocation& format,
  36. Args&&... args) {
  37. if(logLevel < l) {
  38. return;
  39. }
  40. const char* file = getShortFileName(format.location.file_name());
  41. fputs(prefix, stdout);
  42. print("{}:{} | ", file, format.location.line());
  43. char buffer[512];
  44. formatBuffer(
  45. buffer, sizeof(buffer), format.format,
  46. Core::forward<Args>(args)...);
  47. fputs(buffer, stdout);
  48. puts(Terminal::RESET);
  49. }
  50. template<typename... Args>
  51. void logError(const FormatLocation& format, Args&&... args) {
  52. if constexpr(LOG_LEVEL >= 1) {
  53. char buffer[32];
  54. formatBuffer(
  55. buffer, sizeof(buffer), "{}[ERROR] ", Terminal::FG_RED);
  56. log(LogLevel::ERROR, buffer, format, forward<Args>(args)...);
  57. }
  58. }
  59. template<typename... Args>
  60. void logWarning(const FormatLocation& format, Args&&... args) {
  61. if constexpr(LOG_LEVEL >= 2) {
  62. char buffer[32];
  63. formatBuffer(
  64. buffer, sizeof(buffer), "{}[WARNING] ",
  65. Terminal::FG_BRIGHT_YELLOW);
  66. log(LogLevel::WARNING, buffer, format, forward<Args>(args)...);
  67. }
  68. }
  69. template<typename... Args>
  70. void logInfo(const FormatLocation& format, Args&&... args) {
  71. if constexpr(LOG_LEVEL >= 3) {
  72. char buffer[32];
  73. formatBuffer(buffer, sizeof(buffer), "{}[INFO] ", Terminal::BOLD);
  74. log(LogLevel::INFO, buffer, format, forward<Args>(args)...);
  75. }
  76. }
  77. template<typename... Args>
  78. void logDebug(const FormatLocation& format, Args&&... args) {
  79. if constexpr(LOG_LEVEL >= 4) {
  80. char buffer[32];
  81. formatBuffer(
  82. buffer, sizeof(buffer), "{}{}[DEBUG] ", Terminal::BOLD,
  83. Terminal::FG_BRIGHT_BLACK);
  84. log(LogLevel::DEBUG, buffer, format, forward<Args>(args)...);
  85. }
  86. }
  87. }