Logger.cppm 3.2 KB

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