Initial commit from DCSP - 2026/1/15 15:11:58
This commit is contained in:
89
lib/liblog.cpp
Normal file
89
lib/liblog.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "liblog.h"
|
||||
|
||||
#include "json.hpp"
|
||||
#include <time.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using namespace nlohmann;
|
||||
using namespace std;
|
||||
|
||||
void spdlog_initz(string path)
|
||||
{
|
||||
|
||||
json log_j;
|
||||
ifstream fin(path);
|
||||
|
||||
if(!fin)
|
||||
{
|
||||
cout<<"Log配置文件打开失败!"<<endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if(fin.good())
|
||||
{
|
||||
fin>>log_j;
|
||||
}
|
||||
cout<<"log配置文件读取完毕!"<<endl;
|
||||
|
||||
auto max_size = log_j["log_max_size"].get<int>(); //1024*1024*10 每个日志文件大小10M
|
||||
|
||||
auto max_files = log_j["log_max_files"].get<int>(); //三个日志文件
|
||||
|
||||
auto write_freq = log_j["log_write_freq"].get<int>(); //每write_freq秒写入一次log日志
|
||||
|
||||
auto level = log_j["log_level"].get<string>(); //日志级别
|
||||
|
||||
fin.close();
|
||||
|
||||
time_t time_utc = {0};
|
||||
char buf[128] = {0};
|
||||
tm *local;
|
||||
time_utc = time(NULL);
|
||||
local = localtime(&time_utc);
|
||||
strftime(buf,64,"%Y-%m-%d-%H-%M",local);
|
||||
|
||||
|
||||
auto logger = spdlog::rotating_logger_mt("log", "logs/CPU-"+(string)buf+".txt", max_size, max_files);
|
||||
|
||||
// auto console = spdlog::stdout_color_mt("console");
|
||||
// auto err_logger = spdlog::stderr_color_mt("stderr");
|
||||
// spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
|
||||
|
||||
spdlog::set_pattern("[%H:%M:%S] [%^---%L---%$] [thread %t] %v");
|
||||
|
||||
spdlog::flush_every(std::chrono::seconds(write_freq)); //每10s写入文件一次
|
||||
|
||||
|
||||
|
||||
if(level =="info")
|
||||
{
|
||||
logger->set_level(spdlog::level::info);
|
||||
}
|
||||
else if(level =="warn")
|
||||
{
|
||||
logger->set_level(spdlog::level::warn);
|
||||
}
|
||||
else if(level =="error")
|
||||
{
|
||||
logger->set_level(spdlog::level::err);
|
||||
}
|
||||
else if(level =="debug")
|
||||
{
|
||||
logger->set_level(spdlog::level::debug);
|
||||
}
|
||||
else if(level =="critical")
|
||||
{
|
||||
logger->set_level(spdlog::level::critical);
|
||||
}
|
||||
else if(level =="trace")
|
||||
{
|
||||
logger->set_level(spdlog::level::trace);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger->set_level(spdlog::level::info);
|
||||
}
|
||||
|
||||
spdlog::set_default_logger(logger);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user