This commit is contained in:
2025-06-16 08:52:40 +08:00
commit e53dc4a080
23 changed files with 1725 additions and 0 deletions

70
Log/ConfigParser.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "ConfigParser.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
/** 清理字符串前后空格 */
void trim(std::string &s) {
auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) {
return std::isspace(c);
});
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) {
return std::isspace(c);
}).base();
s = (wsback <= wsfront) ? std::string() : std::string(wsfront, wsback);
}
CPStatus configFromIni(const std::string &path, std::unordered_map<std::string, std::string> &config) {
CPStatus result = CPSUCCESS;
//打开文件
ifstream file(path);
if (!file.is_open()) {
result = CPFILE;
return result;
}
// 一次性读取整个文件减少I/O操作
file.seekg(0, std::ios::end);
size_t size = file.tellg();
file.seekg(0, std::ios::beg);
string m_buffer;
m_buffer.resize(size);
file.read(&m_buffer[0], static_cast<long long>(size));
//处理全部数据
config.clear();
std::string line;
line.reserve(256); // 预分配行缓存
for (auto it = m_buffer.cbegin(); it != m_buffer.cend();) {
// 快速跳过空白行
while (it != m_buffer.cend() && (*it == '\r' || *it == '\n')) { ++it; }
if (it == m_buffer.cend()) break;
// 获取行内容
line.clear();
while (it != m_buffer.cend() && *it != '\r' && *it != '\n' && *it != '\0') {
line += *it++;
}
//处理行内容
// 跳过空行和注释
if (line.empty() || line[0] == ';' || line[0] == '#') {
++it;
continue;
}
// 分割键值对
size_t eq_pos = line.find('=');
if (eq_pos != std::string::npos) {
std::string key = line.substr(0, eq_pos);
std::string value = line.substr(eq_pos + 1);
trim(key);
trim(value);
if (!key.empty()) {
config[key] = value; // 移动语义自动优化
}
}
}
return result;
}

29
Log/ConfigParser.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef CONFIGPARSER_H
#define CONFIGPARSER_H
/**
* 配置文件解析模块
* 目标:实现对多种文件格式的解析功能
* 1、ini格式文件 √
* 2、json格式文件 未实现
* 3、YAML格式文件 未实现
* 4、Protobuf格式文件 未实现
*/
#include <string>
#include <unordered_map>
/** 状态码 */
enum CPStatus {
CPSUCCESS = 0,//成功
CPFILE = -1//文件错误
};
/**
* 读取ini类型的配置文件
* @param path 配置文件路径
* @param config 存储配置文件的unordered_map容器
* @return 状态码
*/
CPStatus configFromIni(const std::string &path, std::unordered_map<std::string, std::string> &config);
#endif //CONFIGPARSER_H

331
Log/LogL.cpp Normal file
View File

@ -0,0 +1,331 @@
#include "LogL.h"
#include <iostream>
#include <fstream>
#include <utility>
#include <sys/stat.h>
#include <ctime>
#include <sstream>
#ifdef __linux__
#include <cstring>
#endif
using namespace std;
LogL::LogL(LogType logType, bool isSync, std::string logPath) : m_isSync(isSync), m_logType(logType),
m_logPath(std::move(logPath)), m_file(nullptr),
m_thread(nullptr), m_sender(nullptr) {
switch (m_logType) {
case LTStd: {
break;
}
case LTTxt: {
//创建文件
myDateTime now;
std::string dirName;
if (m_logPath.empty()) {
dirName = "logs";
}
else {
dirName = m_logPath;
}
myMkdir(dirName.c_str());
std::string fileName = dirName + "/" + now.toDate() + ".txt";
m_file = new std::ofstream(fileName, std::ios::app);
m_today = now.day;
break;
}
case LTUdp: {
m_sender = new UDPSender;
std::vector<std::string> parts;
std::stringstream ss(m_logPath);
std::string part;
// 按逗号分割字符串
while (std::getline(ss, part, ',')) {
parts.push_back(part);
}
if (parts.size() == 3) {
m_sender->set_destination(parts[0], stoi(parts[1]));
m_sender->set_source(parts[0], stoi(parts[2]));
}
else if (parts.size() == 2) {
m_sender->set_destination(parts[0], stoi(parts[1]));
}
else {
throw std::runtime_error("UDP Path Error");
}
break;
}
case LTTcp: {
break;
}
}
if (!m_isSync) {
//运行日志线程
m_thread_run = true;
m_thread = new std::thread(&LogL::logThread, this);
}
}
LogL::~LogL() {
//停止线程
while (m_thread) {
if (m_log_queue.empty()) {
cout << "已清空日志队列" << endl;
break;
}
cout << "正在写入剩余日志:" << m_log_queue.size() << endl;
this_thread::sleep_for(chrono::milliseconds(300));
}
if (m_thread) {
//设置结束并等待线程结束
m_thread_run = false;
m_cond_empty.notify_all();
if (m_thread->joinable()) {
m_thread->join();
}
delete m_thread;
m_thread = nullptr;
}
//关闭文件
m_mtx.lock();
if (m_file) {
m_file->flush();
m_file->close();
delete m_file;
m_file = nullptr;
}
m_mtx.unlock();
cout << "日志析构函数完成" << endl;
}
void LogL::logThread() {
std::string str;
while (m_thread_run) {
//如果队列为空,等待
if (m_log_queue.empty()) {
std::unique_lock<std::mutex> lock(m_mtx);
m_cond_empty.wait(lock);
continue;
}
//取出队列头并出栈
str = m_log_queue.front();
m_log_queue.pop();
m_cond_full.notify_one();
switch (m_logType) {
case LTStd:
cout << str << endl;
break;
case LTTxt: {
if (m_file == nullptr) {
std::cerr << "logThread():日志文件打开失败" << endl;
break;
}
*m_file << str << endl;
break;
}
case LTUdp:
try {
m_sender->send(str);
}
catch (const std::runtime_error &e) {
std::cerr << e.what() << endl;
}
break;
case LTTcp:
break;
}
}
}
void LogL::debug(const std::string &str) {
log(str, LLDebug);
}
void LogL::info(const std::string &str) {
log(str, LLInfo);
}
void LogL::warn(const std::string &str) {
log(str, LLWarn);
}
void LogL::error(const std::string &str) {
log(str, LLError);
}
void LogL::log(const std::string &str, LogLevel level) {
string tmpLever;
switch (level) {
case LLDebug:
tmpLever = " [debug]:";
break;
case LLInfo:
tmpLever = " [info]:";
break;
case LLWarn:
tmpLever = " [warn]:";
break;
case LLError:
tmpLever = " [error]:";
break;
}
myDateTime now;
if (m_logType == LTTxt) {
//逾期更新日志文件
m_mtx.lock();
if (now.day != m_today) {
if (m_file) {
m_file->flush();
m_file->close();
delete m_file;
m_file = nullptr;
}
std::string dirName;
if (m_logPath.empty()) {
dirName = "log";
}
else {
dirName = m_logPath;
}
myMkdir(dirName.c_str());
std::string fileName =
dirName + "/" + now.toDate() + ".txt";
m_file = new std::ofstream(fileName, std::ios::app);
}
m_mtx.unlock();
}
std::string logInfo = now.toDateTime() + tmpLever + str;
if (m_isSync) {
switch (m_logType) {
case LTStd:
cout << logInfo << endl;
break;
case LTTxt: {
if (m_file == nullptr) {
std::cerr << "日志文件打开失败" << endl;
break;
}
*m_file << logInfo << endl;
break;
}
case LTUdp:
try {
m_sender->send(logInfo);
}
catch (const std::runtime_error &e) {
std::cerr << e.what() << endl;
}
break;
case LTTcp:
break;
}
}
else {
if (m_log_queue.size() >= 4096) {
std::unique_lock<std::mutex> lock(m_mtx);
m_cond_full.wait(lock);
}
m_mtx.lock();
m_log_queue.push(logInfo);
m_mtx.unlock();
m_cond_empty.notify_all();
}
}
bool createPath(const char *path) {
struct stat st = {0};
if (stat(path, &st) == -1) {
#if WIN32
if (mkdir(path) == 0) {
#else
if (mkdir(path,0777) == 0) {
#endif
return true;
}
else {
std::cerr << "Failed to create directory: " << path << std::endl;
return false;
}
}
return true; // Directory already exists
}
bool myMkdir(const char *path) {
char buffer[1024];
char *p = buffer;
const char *sep = "/";
strcpy(buffer, path);
while ((p = strchr(p, *sep)) != nullptr) {
*p = '\0';
if (!createPath(buffer)) {
return false;
}
*p = *sep;
p++;
}
createPath(buffer);
return true;
}
myDateTime::myDateTime() {
auto now = std::chrono::system_clock::now();
std::time_t time1 = std::chrono::system_clock::to_time_t(now);
std::tm *tm1 = std::localtime(&time1);
year = tm1->tm_year + 1900;
month = tm1->tm_mon + 1;
day = tm1->tm_mday;
hour = tm1->tm_hour;
minute = tm1->tm_min;
second = tm1->tm_sec;
}
std::string myDateTime::toDate() const {
string date = to_string(year) + "-";
if (month < 10) {
date += "0" + to_string(month) + "-";
}
else {
date += to_string(month) + "-";
}
if (day < 10) {
date += "0" + to_string(day);
}
else {
date += to_string(day);
}
return date;
}
std::string myDateTime::toTime() const {
string time;
if (hour < 10) {
time += "0" + to_string(hour) + ":";
}
else {
time += to_string(hour) + ":";
}
if (minute < 10) {
time += "0" + to_string(minute) + ":";
}
else {
time += to_string(minute) + ":";
}
if (second < 10) {
time += "0" + to_string(second);
}
else {
time += to_string(second);
}
return time;
}
std::string myDateTime::toDateTime() const {
string dateTime = toDate() + " " + toTime();
return dateTime;
}

98
Log/LogL.h Normal file
View File

@ -0,0 +1,98 @@
#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