Logger.cppm 2.8 KB

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