| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #pragma once
- #include <string>
- #include <memory>
- #include <spdlog/spdlog.h>
- #include <spdlog/sinks/stdout_color_sinks.h>
- #include <spdlog/sinks/rotating_file_sink.h>
- namespace smartbotic::logging {
- enum class LogLevel {
- Trace = spdlog::level::trace,
- Debug = spdlog::level::debug,
- Info = spdlog::level::info,
- Warn = spdlog::level::warn,
- Error = spdlog::level::err,
- Critical = spdlog::level::critical,
- Off = spdlog::level::off
- };
- struct LogConfig {
- std::string name = "smartbotic";
- LogLevel level = LogLevel::Info;
- std::string pattern = "[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] [%t] %v";
- bool console = true;
- std::string file_path;
- size_t max_file_size = 10 * 1024 * 1024; // 10MB
- size_t max_files = 5;
- };
- class Logger {
- public:
- static void init(const LogConfig& config);
- static void shutdown();
- static std::shared_ptr<spdlog::logger> get(const std::string& name = "");
- // Convenience methods
- template<typename... Args>
- static void trace(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->trace(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- static void debug(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->debug(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- static void info(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->info(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- static void warn(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->warn(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- static void error(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->error(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- static void critical(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- get()->critical(fmt, std::forward<Args>(args)...);
- }
- static void setLevel(LogLevel level);
- static void flush();
- private:
- static std::shared_ptr<spdlog::logger> defaultLogger_;
- static bool initialized_;
- };
- // Scoped logger for component-specific logging
- class ScopedLogger {
- public:
- explicit ScopedLogger(const std::string& component);
- template<typename... Args>
- void trace(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->trace(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- void debug(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->debug(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- void info(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->info(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- void warn(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->warn(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- void error(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->error(fmt, std::forward<Args>(args)...);
- }
- template<typename... Args>
- void critical(spdlog::format_string_t<Args...> fmt, Args&&... args) {
- logger_->critical(fmt, std::forward<Args>(args)...);
- }
- private:
- std::shared_ptr<spdlog::logger> logger_;
- };
- } // namespace smartbotic::logging
- // Convenience macros
- #define LOG_TRACE(...) smartbotic::logging::Logger::trace(__VA_ARGS__)
- #define LOG_DEBUG(...) smartbotic::logging::Logger::debug(__VA_ARGS__)
- #define LOG_INFO(...) smartbotic::logging::Logger::info(__VA_ARGS__)
- #define LOG_WARN(...) smartbotic::logging::Logger::warn(__VA_ARGS__)
- #define LOG_ERROR(...) smartbotic::logging::Logger::error(__VA_ARGS__)
- #define LOG_CRITICAL(...) smartbotic::logging::Logger::critical(__VA_ARGS__)
|