#include #include #include #include "c_wrapper.h" #include "logger_impl.h" LoggerImpl* logger_ptr = nullptr; std::unordered_map appender_map_; static int app_id_ = 1; std::atomic_int logger_ref_ = 0; std::mutex logger_mutex_; int new_appender(const char* name, const char* attributes) { std::lock_guard lock(logger_mutex_); if (!logger_ptr) { logger_ptr = new LoggerImpl(); } auto app_id = app_id_++; try { appender_map_[app_id] = logger_ptr->newAppender(name, attributes); } catch (...) { app_id_ -= 1; return -1; } logger_ref_ += 1; return app_id; } void delete_appender(int app_id) { std::lock_guard lock(logger_mutex_); auto it = appender_map_.find(app_id); if (it == appender_map_.end()) { return; } it->second->destroy(); appender_map_.erase(it); logger_ref_ -= 1; if (logger_ref_ <= 0) { delete logger_ptr; logger_ptr = nullptr; } } void write(int app_id, int level, const char* log) { if (!log || (level <= 0) || (app_id <= 0)) { return; } std::lock_guard lock(logger_mutex_); auto it = appender_map_.find(app_id); if (it == appender_map_.end()) { return; } it->second->write(level, "%s", log); }