#pragma once // log utility // // created on 2022-11-30 // #include "referer.h" #include #include //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // object event // enum log_level { LOG_LEVEL_ALL = 0, LOG_LEVEL_DEBUG, LOG_LEVEL_WARNING, LOG_LEVEL_FATAL, }; class log_cls { std::string file_; int32_t max_size_; log_level level_; MUTEX lock_; FILE *dst_; static log_cls* inst_; void create_log_file(void); protected: log_cls(const char* path_file, log_level level, int32_t max_size); ~log_cls(); void log_internal(const char* txt); public: static void initialize(const char* path_file, log_level level = LOG_LEVEL_ALL, int32_t max_size = SIZE_MB(10)); template static void log(log_level level, const char* fmt, Args ... args) { if (level >= log_cls::get_log_level() && log_cls::inst_) { size_t size = snprintf(nullptr, 0, fmt, args ...) + 1; std::unique_ptr buf(new char[size]); snprintf(buf.get(), size, fmt, args ...); log_cls::inst_->log_internal(buf.get()); } } static int32_t log_when_err(int32_t err, const char* oper_desc, log_level level = LOG_LEVEL_WARNING); // log as: oper_desc = strerror(errno)\n. return real error number errno static log_level get_log_level(void); static std::string get_log_file(void); };