logger.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #include <string>
  3. #include <memory>
  4. #include <spdlog/spdlog.h>
  5. #include <spdlog/sinks/stdout_color_sinks.h>
  6. #include <spdlog/sinks/rotating_file_sink.h>
  7. namespace smartbotic::logging {
  8. enum class LogLevel {
  9. Trace = spdlog::level::trace,
  10. Debug = spdlog::level::debug,
  11. Info = spdlog::level::info,
  12. Warn = spdlog::level::warn,
  13. Error = spdlog::level::err,
  14. Critical = spdlog::level::critical,
  15. Off = spdlog::level::off
  16. };
  17. struct LogConfig {
  18. std::string name = "smartbotic";
  19. LogLevel level = LogLevel::Info;
  20. std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] [%t] %v";
  21. bool console = true;
  22. std::string file_path;
  23. size_t max_file_size = 10 * 1024 * 1024; // 10MB
  24. size_t max_files = 5;
  25. };
  26. class Logger {
  27. public:
  28. static void init(const LogConfig& config);
  29. static void shutdown();
  30. static std::shared_ptr<spdlog::logger> get(const std::string& name = "");
  31. // Convenience methods
  32. template<typename... Args>
  33. static void trace(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  34. get()->trace(fmt, std::forward<Args>(args)...);
  35. }
  36. template<typename... Args>
  37. static void debug(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  38. get()->debug(fmt, std::forward<Args>(args)...);
  39. }
  40. template<typename... Args>
  41. static void info(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  42. get()->info(fmt, std::forward<Args>(args)...);
  43. }
  44. template<typename... Args>
  45. static void warn(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  46. get()->warn(fmt, std::forward<Args>(args)...);
  47. }
  48. template<typename... Args>
  49. static void error(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  50. get()->error(fmt, std::forward<Args>(args)...);
  51. }
  52. template<typename... Args>
  53. static void critical(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  54. get()->critical(fmt, std::forward<Args>(args)...);
  55. }
  56. static void setLevel(LogLevel level);
  57. static void flush();
  58. private:
  59. static std::shared_ptr<spdlog::logger> defaultLogger_;
  60. static bool initialized_;
  61. };
  62. // Scoped logger for component-specific logging
  63. class ScopedLogger {
  64. public:
  65. explicit ScopedLogger(const std::string& component);
  66. template<typename... Args>
  67. void trace(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  68. logger_->trace(fmt, std::forward<Args>(args)...);
  69. }
  70. template<typename... Args>
  71. void debug(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  72. logger_->debug(fmt, std::forward<Args>(args)...);
  73. }
  74. template<typename... Args>
  75. void info(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  76. logger_->info(fmt, std::forward<Args>(args)...);
  77. }
  78. template<typename... Args>
  79. void warn(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  80. logger_->warn(fmt, std::forward<Args>(args)...);
  81. }
  82. template<typename... Args>
  83. void error(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  84. logger_->error(fmt, std::forward<Args>(args)...);
  85. }
  86. template<typename... Args>
  87. void critical(spdlog::format_string_t<Args...> fmt, Args&&... args) {
  88. logger_->critical(fmt, std::forward<Args>(args)...);
  89. }
  90. private:
  91. std::shared_ptr<spdlog::logger> logger_;
  92. };
  93. } // namespace smartbotic::logging
  94. // Convenience macros
  95. #define LOG_TRACE(...) smartbotic::logging::Logger::trace(__VA_ARGS__)
  96. #define LOG_DEBUG(...) smartbotic::logging::Logger::debug(__VA_ARGS__)
  97. #define LOG_INFO(...) smartbotic::logging::Logger::info(__VA_ARGS__)
  98. #define LOG_WARN(...) smartbotic::logging::Logger::warn(__VA_ARGS__)
  99. #define LOG_ERROR(...) smartbotic::logging::Logger::error(__VA_ARGS__)
  100. #define LOG_CRITICAL(...) smartbotic::logging::Logger::critical(__VA_ARGS__)