BellLogger.h 3.5 KB

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