99 lines
4.2 KiB
C++
99 lines
4.2 KiB
C++
#ifndef VSLAM_LOG_H
|
|
#define VSLAM_LOG_H
|
|
|
|
// Thay ROS_DEBUG / ROS_INFO / ROS_WARN / ROS_ERROR va cac bien the _THROTTLE.
|
|
//
|
|
// Yeu cau tu hien trang:
|
|
// * _THROTTLE la bat buoc, khong phai tien nghi. Nhieu cho trong repo hien
|
|
// tai log o tan so cam bien (200 Hz IMU, 30 Hz camera); khong bop lai thi
|
|
// journald tren Jetson day o va I/O tro thanh nut co chai that.
|
|
// * Throttle phai dung steady_clock, khong phai stamp du lieu. Neu dung
|
|
// stamp thi khi replay nhanh (speed=0) moi dong se bi bop nham.
|
|
// * Ghi log tuyet doi khong duoc chan vong an toan. Sink mac dinh ghi
|
|
// thang ra stderr co khoa; neu sau nay can sink ton kem (file, mang) thi
|
|
// phai la sink bat dong bo co hang doi va DROP chu khong block.
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace vslam {
|
|
|
|
enum class LogLevel : int { kDebug = 0, kInfo = 1, kWarn = 2, kError = 3, kOff = 4 };
|
|
|
|
const char* LogLevelName(LogLevel l);
|
|
bool ParseLogLevel(const std::string& s, LogLevel* out);
|
|
|
|
// Sink nhan dong da dinh dang san (khong co newline).
|
|
using LogSink = std::function<void(LogLevel, const char* file, int line,
|
|
const std::string& msg)>;
|
|
|
|
void SetLogLevel(LogLevel l);
|
|
LogLevel GetLogLevel();
|
|
void SetLogSink(LogSink sink);
|
|
|
|
// Bat/tat mau ANSI. Mac dinh: tu dong theo isatty(stderr).
|
|
void SetLogColor(bool on);
|
|
|
|
namespace detail {
|
|
|
|
void EmitLog(LogLevel level, const char* file, int line, const std::string& msg);
|
|
|
|
// Tra ve true neu da du `period_sec` ke tu lan cuoi cho diem goi nay.
|
|
// `state` la bien tinh rieng cua tung diem goi.
|
|
bool ThrottlePass(std::atomic<int64_t>* state, double period_sec);
|
|
|
|
} // namespace detail
|
|
|
|
#define VSLAM_LOG_AT(level, ...) \
|
|
do { \
|
|
if (static_cast<int>(level) >= static_cast<int>(::vslam::GetLogLevel())) { \
|
|
std::ostringstream _vslam_oss; \
|
|
_vslam_oss << __VA_ARGS__; \
|
|
::vslam::detail::EmitLog((level), __FILE__, __LINE__, _vslam_oss.str()); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define VSLAM_DEBUG(...) VSLAM_LOG_AT(::vslam::LogLevel::kDebug, __VA_ARGS__)
|
|
#define VSLAM_INFO(...) VSLAM_LOG_AT(::vslam::LogLevel::kInfo, __VA_ARGS__)
|
|
#define VSLAM_WARN(...) VSLAM_LOG_AT(::vslam::LogLevel::kWarn, __VA_ARGS__)
|
|
#define VSLAM_ERROR(...) VSLAM_LOG_AT(::vslam::LogLevel::kError, __VA_ARGS__)
|
|
|
|
// Bien the throttle. `period` tinh bang giay, theo steady_clock.
|
|
#define VSLAM_LOG_THROTTLE_AT(level, period, ...) \
|
|
do { \
|
|
static std::atomic<int64_t> _vslam_throttle_state{0}; \
|
|
if (::vslam::detail::ThrottlePass(&_vslam_throttle_state, (period))) { \
|
|
VSLAM_LOG_AT(level, __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define VSLAM_DEBUG_THROTTLE(period, ...) \
|
|
VSLAM_LOG_THROTTLE_AT(::vslam::LogLevel::kDebug, period, __VA_ARGS__)
|
|
#define VSLAM_INFO_THROTTLE(period, ...) \
|
|
VSLAM_LOG_THROTTLE_AT(::vslam::LogLevel::kInfo, period, __VA_ARGS__)
|
|
#define VSLAM_WARN_THROTTLE(period, ...) \
|
|
VSLAM_LOG_THROTTLE_AT(::vslam::LogLevel::kWarn, period, __VA_ARGS__)
|
|
#define VSLAM_ERROR_THROTTLE(period, ...) \
|
|
VSLAM_LOG_THROTTLE_AT(::vslam::LogLevel::kError, period, __VA_ARGS__)
|
|
|
|
// Chi log mot lan duy nhat cho diem goi nay.
|
|
#define VSLAM_LOG_ONCE_AT(level, ...) \
|
|
do { \
|
|
static std::atomic<bool> _vslam_once{false}; \
|
|
bool _vslam_expected = false; \
|
|
if (_vslam_once.compare_exchange_strong(_vslam_expected, true)) { \
|
|
VSLAM_LOG_AT(level, __VA_ARGS__); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define VSLAM_INFO_ONCE(...) VSLAM_LOG_ONCE_AT(::vslam::LogLevel::kInfo, __VA_ARGS__)
|
|
#define VSLAM_WARN_ONCE(...) VSLAM_LOG_ONCE_AT(::vslam::LogLevel::kWarn, __VA_ARGS__)
|
|
|
|
} // namespace vslam
|
|
|
|
#endif // VSLAM_LOG_H
|