/*author carea*/#ifndef CPPLOG_H#define CPPLOG_H#include <string>#include <fstream>#include <sstream>#include <mutex>#include <vector>//extern size_t g_count;//extern size_t g_count_last;class LogRolling{public:bool is_file_size_overflow(void) const;void size_increased(size_t n);void init(size_t start_file_size, size_t rolling_file_size);void reset(void);size_t m_log_file_size_over_flow = 1024 * 1024 * 2/*2MB*/;private:size_t m_start_file_size = 0;size_t m_written_size = 0;};//void print_time(void);class Log{public:static Log& instance(void);~Log();enum class LOG_LEVEL { DEBUG_LEVEL, INFOR, ERROR_LEVEL, FATAL };enum class LOG_TARGET{ CONSOLE, FILE, ALL, NO_LOG};public:bool init(const std::string& logFile, Log::LOG_LEVEL level, Log::LOG_TARGET target, const std::string& format, size_t rolling_file_size);void open_log_file(const std::string& log_file);void log(const std::string& content, const std::string& file, int line, Log::LOG_LEVEL level, const std::string& func);public:int duration = 1;private:void init_format_order(const std::string & format);void write_log_to_target_locked(const std::string& time_clock, Log::LOG_LEVEL level, const std::string& func, const std::string& log, const std::string& file, int line);std::string get_current_time(void) const;enum class LOG_FORMAT_PART {LEVEL = 0, DATETIME, LOG, SOURCEFILE, LINE , FUNC, SIZE};void process_log_file_overflow(const std::string& log_record);void rolling(void);private:LogRolling m_log_rolling;std::ofstream m_fout;std::string m_log_file;static Log m_instance;Log::LOG_LEVEL m_level;Log::LOG_TARGET m_target;std::vector<int> m_format_order;bool m_inited = false;std::mutex m_mutex;int NO_USE;};void init_log_cpp(const std::string& logFile, Log::LOG_LEVEL level, Log::LOG_TARGET target, const std::string& format, size_t rolling_file_size);#define log_debug(content) \{ \std::string content_str; std::ostringstream oss; oss<<content; content_str = oss.str(); \Log::instance().log(content_str, __FILE__, __LINE__, Log::LOG_LEVEL::DEBUG_LEVEL, __FUNCTION__); \}#define log_infor(content) \{ \std::string content_str; std::ostringstream oss; oss<<content; content_str = oss.str(); \Log::instance().log(content_str, __FILE__, __LINE__, Log::LOG_LEVEL::INFOR, __FUNCTION__); \}#define log_error(content) \{ \std::string content_str; std::ostringstream oss; oss<<content; content_str = oss.str(); \Log::instance().log(content_str, __FILE__, __LINE__, Log::LOG_LEVEL::ERROR_LEVEL, __FUNCTION__); \}#define log_fatal(content) \{ \std::string content_str; std::ostringstream oss; oss<<content; content_str = oss.str(); \Log::instance().log(content_str, __FILE__, __LINE__, Log::LOG_LEVEL::FATAL, __FUNCTION__); \}/*int main(){init_log_cpp("log_sdk.txt", Log::LOG_LEVEL::DEBUG_LEVEL, Log::LOG_TARGET::FILE, "datetime_log", 1024 * 1024 * 2);//output date time and log onlyinit_log_cpp("main-log.txt", Log::LOG_LEVEL::DEBUG_LEVEL, Log::LOG_TARGET::FILE, "datetime_level_func_log_source_line", 1024 * 1024 * 2);int a = 1;log_debug("this is a message. a="<<a);return 0;}*/#endif // !CPPLOG_H
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。