BellLogger.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef BELL_LOGGER_H
  2. #define BELL_LOGGER_H
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <string>
  7. #include <memory>
  8. namespace bell
  9. {
  10. class AbstractLogger
  11. {
  12. public:
  13. bool enableSubmodule = false;
  14. virtual void debug(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
  15. virtual void error(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
  16. virtual void info(std::string filename, int line, std::string submodule, const char *format, ...) = 0;
  17. };
  18. extern bell::AbstractLogger* bellGlobalLogger;
  19. class BellLogger : public bell::AbstractLogger
  20. {
  21. public:
  22. // static bool enableColors = true;
  23. void debug(std::string filename, int line, std::string submodule, const char *format, ...)
  24. {
  25. printf(colorRed);
  26. printf("D ");
  27. if (enableSubmodule) {
  28. printf(colorReset);
  29. printf("[%s] ", submodule.c_str());
  30. }
  31. printFilename(filename);
  32. printf(":%d: ", line);
  33. va_list args;
  34. va_start(args, format);
  35. vprintf(format, args);
  36. va_end(args);
  37. printf("\n");
  38. };
  39. void error(std::string filename, int line, std::string submodule, const char *format, ...)
  40. {
  41. printf(colorRed);
  42. printf("E ");
  43. if (enableSubmodule) {
  44. printf(colorReset);
  45. printf("[%s] ", submodule.c_str());
  46. }
  47. printFilename(filename);
  48. printf(":%d: ", line);
  49. printf(colorRed);
  50. va_list args;
  51. va_start(args, format);
  52. vprintf(format, args);
  53. va_end(args);
  54. printf("\n");
  55. };
  56. void info(std::string filename, int line, std::string submodule, const char *format, ...)
  57. {
  58. printf(colorBlue);
  59. printf("I ");
  60. if (enableSubmodule) {
  61. printf(colorReset);
  62. printf("[%s] ", submodule.c_str());
  63. }
  64. printFilename(filename);
  65. printf(":%d: ", line);
  66. printf(colorReset);
  67. va_list args;
  68. va_start(args, format);
  69. vprintf(format, args);
  70. va_end(args);
  71. printf("\n");
  72. };
  73. void printFilename(std::string filename)
  74. {
  75. #ifdef _WIN32
  76. std::string basenameStr(filename.substr(filename.rfind("\\") + 1));
  77. #else
  78. std::string basenameStr(filename.substr(filename.rfind("/") + 1));
  79. #endif
  80. unsigned long hash = 5381;
  81. for (char const &c : basenameStr)
  82. {
  83. hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  84. }
  85. printf("\033[0;%dm", allColors[hash % NColors]);
  86. printf("%s", basenameStr.c_str());
  87. printf(colorReset);
  88. }
  89. private:
  90. static constexpr const char *colorReset = "\033[0m";
  91. static constexpr const char *colorRed = "\033[0;31m";
  92. static constexpr const char *colorBlue = "\033[0;34m";
  93. static constexpr const int NColors = 15;
  94. static constexpr int allColors[NColors] = {31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97};
  95. };
  96. void setDefaultLogger();
  97. void enableSubmoduleLogging();
  98. }
  99. #define BELL_LOG(type, ...) \
  100. do \
  101. { \
  102. bell::bellGlobalLogger->type(__FILE__, __LINE__, __VA_ARGS__); \
  103. } while (0)
  104. #endif