Logger.cppm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) noexcept;
  14. void setReportHandler(ReportHandler h, void* data) noexcept;
  15. struct FormatLocation final {
  16. const char* format;
  17. std::source_location location;
  18. FormatLocation(const char* f, SOURCE) noexcept :
  19. format(f), location(sl) {
  20. }
  21. };
  22. template<typename... Args>
  23. void report(
  24. LogLevel l, const FormatLocation& format, Args&&... args) noexcept {
  25. String<512> s;
  26. s.addFormat(format.format, Core::forward<Args>(args)...);
  27. callReportHandler(l, s, format.location);
  28. }
  29. const char* getShortFileName(const char* s) noexcept;
  30. template<typename... Args>
  31. void log(
  32. LogLevel l, const char* prefix, const FormatLocation& format,
  33. Args&&... args) noexcept {
  34. if(logLevel < l) {
  35. return;
  36. }
  37. const char* file = getShortFileName(format.location.file_name());
  38. fputs(prefix, stdout);
  39. print("{}:{} | ", file, format.location.line());
  40. String<512> s;
  41. s.addFormat(format.format, Core::forward<Args>(args)...);
  42. s.print();
  43. puts(Terminal::RESET);
  44. }
  45. template<typename... Args>
  46. void logError(const FormatLocation& format, Args&&... args) noexcept {
  47. if constexpr(LOG_LEVEL >= 1) {
  48. String<32> s;
  49. s.addFormat("{}[ERROR] ", Terminal::FG_RED);
  50. log(LogLevel::ERROR, s, format, forward<Args>(args)...);
  51. }
  52. }
  53. template<typename... Args>
  54. void logWarning(const FormatLocation& format, Args&&... args) noexcept {
  55. if constexpr(LOG_LEVEL >= 2) {
  56. String<32> s;
  57. s.addFormat("{}[WARNING] ", Terminal::FG_BRIGHT_YELLOW);
  58. log(LogLevel::WARNING, s, format, forward<Args>(args)...);
  59. }
  60. }
  61. template<typename... Args>
  62. void logInfo(const FormatLocation& format, Args&&... args) noexcept {
  63. if constexpr(LOG_LEVEL >= 3) {
  64. String<32> s;
  65. s.addFormat("{}[INFO] ", Terminal::BOLD);
  66. log(LogLevel::INFO, s, format, forward<Args>(args)...);
  67. }
  68. }
  69. template<typename... Args>
  70. void logDebug(const FormatLocation& format, Args&&... args) noexcept {
  71. if constexpr(LOG_LEVEL >= 4) {
  72. String<32> s;
  73. s.addFormat(
  74. "{}{}[DEBUG] ", Terminal::BOLD, Terminal::FG_BRIGHT_BLACK);
  75. log(LogLevel::DEBUG, s, format, forward<Args>(args)...);
  76. }
  77. }
  78. }