99 lines
2.1 KiB
C++
99 lines
2.1 KiB
C++
#ifndef LXZL_LOGL_H
|
||
#define LXZL_LOGL_H
|
||
/**
|
||
* 日志生成模块
|
||
* 目标:支持同步、异步两种方式;支持多种输出方式(控制台、文件、udp、tcp)
|
||
*/
|
||
#include "UDP.h"
|
||
|
||
#include <string>
|
||
#include <thread>
|
||
#include <queue>
|
||
#include <mutex>
|
||
#include <condition_variable>
|
||
|
||
enum LogType{
|
||
LTStd,//日志输出到控制台
|
||
LTTxt,//日志输出到文本文件
|
||
LTUdp,//日志输出到UDP
|
||
LTTcp,//日志输出到TCP
|
||
};
|
||
enum LogLevel{
|
||
LLDebug,//调试
|
||
LLInfo,//一般
|
||
LLWarn,//警告
|
||
LLError,//错误
|
||
};
|
||
|
||
|
||
class LogL {
|
||
public:
|
||
/**
|
||
* 日志构造函数
|
||
* @param logType 日志输出类型
|
||
* @param isSync true:同步/false:异步
|
||
* @param logPath 输出地址(LTStd:无;LTTxt:填写路径;LTUdp:填写ip端口)
|
||
*/
|
||
explicit LogL(LogType logType, bool isSync = true, std::string logPath = "");
|
||
~LogL();
|
||
|
||
/**
|
||
* 调试日志
|
||
* @param str 日志内容
|
||
*/
|
||
void debug(const std::string &str);
|
||
void info(const std::string &str);
|
||
void warn(const std::string &str);
|
||
void error(const std::string &str);
|
||
|
||
private:
|
||
|
||
void logThread();
|
||
|
||
void log(const std::string &str, LogLevel level);
|
||
|
||
|
||
private:
|
||
std::thread *m_thread;
|
||
bool m_thread_run;
|
||
bool m_isSync;//是否同步
|
||
LogType m_logType;//输出类型
|
||
std::string m_logPath;//输出地址
|
||
std::queue<std::string> m_log_queue;//日志容器
|
||
std::mutex m_mtx;
|
||
std::condition_variable m_cond_empty;//空时停止
|
||
std::condition_variable m_cond_full;//满时停止
|
||
std::ofstream *m_file;
|
||
int32_t m_today;
|
||
UDPSender *m_sender;
|
||
};
|
||
|
||
|
||
/**
|
||
* 创建目录
|
||
* @param path 路径
|
||
* @return bool是否创建成功
|
||
*/
|
||
bool myMkdir(const char *path);
|
||
|
||
/**
|
||
* 获取当前日期时间
|
||
*/
|
||
class myDateTime {
|
||
public:
|
||
myDateTime();
|
||
|
||
int32_t year;
|
||
int32_t month;
|
||
int32_t day;
|
||
int32_t hour;
|
||
int32_t minute;
|
||
int32_t second;
|
||
|
||
std::string toDate() const;
|
||
std::string toTime() const;
|
||
std::string toDateTime() const;
|
||
};
|
||
|
||
#endif //LXZL_LOGL_H
|