#include "vslam/log.h" #include #include #include #include #include #include #include namespace vslam { namespace { std::atomic g_level{static_cast(LogLevel::kInfo)}; std::atomic g_color{false}; std::atomic g_color_init{false}; std::mutex& SinkMutex() { static std::mutex m; return m; } LogSink& SinkRef() { static LogSink sink; // rong = dung sink mac dinh ra stderr return sink; } const char* ColorFor(LogLevel l) { switch (l) { case LogLevel::kDebug: return "\033[2m"; // mo case LogLevel::kInfo: return ""; case LogLevel::kWarn: return "\033[33m"; // vang case LogLevel::kError: return "\033[31m"; // do default: return ""; } } // Chi lay ten file, bo duong dan. __FILE__ trong build tree rat dai. const char* BaseName(const char* path) { const char* slash = std::strrchr(path, '/'); return slash != nullptr ? slash + 1 : path; } bool ColorEnabled() { if (!g_color_init.load(std::memory_order_relaxed)) { g_color.store(::isatty(STDERR_FILENO) != 0, std::memory_order_relaxed); g_color_init.store(true, std::memory_order_relaxed); } return g_color.load(std::memory_order_relaxed); } } // namespace const char* LogLevelName(LogLevel l) { switch (l) { case LogLevel::kDebug: return "DEBUG"; case LogLevel::kInfo: return "INFO"; case LogLevel::kWarn: return "WARN"; case LogLevel::kError: return "ERROR"; case LogLevel::kOff: return "OFF"; } return "?"; } bool ParseLogLevel(const std::string& s, LogLevel* out) { if (s == "debug" || s == "DEBUG") { *out = LogLevel::kDebug; return true; } if (s == "info" || s == "INFO") { *out = LogLevel::kInfo; return true; } if (s == "warn" || s == "WARN") { *out = LogLevel::kWarn; return true; } if (s == "error" || s == "ERROR") { *out = LogLevel::kError; return true; } if (s == "off" || s == "OFF") { *out = LogLevel::kOff; return true; } return false; } void SetLogLevel(LogLevel l) { g_level.store(static_cast(l), std::memory_order_relaxed); } LogLevel GetLogLevel() { return static_cast(g_level.load(std::memory_order_relaxed)); } void SetLogSink(LogSink sink) { std::lock_guard lk(SinkMutex()); SinkRef() = std::move(sink); } void SetLogColor(bool on) { g_color.store(on, std::memory_order_relaxed); g_color_init.store(true, std::memory_order_relaxed); } namespace detail { void EmitLog(LogLevel level, const char* file, int line, const std::string& msg) { { std::lock_guard lk(SinkMutex()); if (SinkRef()) { SinkRef()(level, file, line, msg); return; } } // Sink mac dinh: stderr, mot lan fwrite co khoa. Dinh dang: // HH:MM:SS.mmm WARN scan_sanitizer.cc:88 noi dung using namespace std::chrono; const auto now = system_clock::now(); const auto t = system_clock::to_time_t(now); const auto ms = duration_cast(now.time_since_epoch()).count() % 1000; std::tm tm_buf{}; ::localtime_r(&t, &tm_buf); char stamp[32]; std::snprintf(stamp, sizeof(stamp), "%02d:%02d:%02d.%03d", tm_buf.tm_hour, tm_buf.tm_min, tm_buf.tm_sec, static_cast(ms)); const bool color = ColorEnabled(); const char* c_on = color ? ColorFor(level) : ""; const char* c_off = (color && c_on[0] != '\0') ? "\033[0m" : ""; static std::mutex out_mu; std::lock_guard lk(out_mu); std::fprintf(stderr, "%s%s %-5s %s:%d %s%s\n", c_on, stamp, LogLevelName(level), BaseName(file), line, msg.c_str(), c_off); } bool ThrottlePass(std::atomic* state, double period_sec) { // steady_clock, KHONG phai stamp du lieu. Neu dung stamp thi khi replay // nhanh (speed=0) moi dong se bi bop nham. const auto now = std::chrono::steady_clock::now().time_since_epoch(); const int64_t now_ns = std::chrono::duration_cast(now).count(); const int64_t period_ns = static_cast(period_sec * 1e9); int64_t last = state->load(std::memory_order_relaxed); if (last != 0 && now_ns - last < period_ns) return false; // CAS: neu hai luong cung toi day thi chi mot luong duoc in. return state->compare_exchange_strong(last, now_ns, std::memory_order_relaxed); } } // namespace detail } // namespace vslam