You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* @Author: your name
* @Date: 2022-04-20 15:49:50
* @LastEditTime: 2025-09-16 22:35:59
* @LastEditors: xiewenji 527774126@qq.com
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
* @FilePath: /ZCXD_MonitorPlatform/src/CoreLogicModule/include/CamDeal.h
*/
#ifndef DetLog_H_
#define DetLog_H_
#include <string>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <vector>
#include <mutex>
#include "ImgCheckConfig.h"
#include "CheckErrorCodeDefine.hpp"
using namespace std;
enum PrintLevel_
{
PrintLevel_0,
PrintLevel_1,
PrintLevel_2,
PrintLevel_3,
PrintLevel_4,
};
// 无参数重载:直接返回原字符串,避免 -Wformat-security 警告
inline std::string str_Format_1(const std::string &format)
{
return format;
}
template <typename... Args>
std::string str_Format_1(const std::string &format, Args... args)
{
// 先尝试用小缓冲
char buf[256];
int size = std::snprintf(buf, sizeof(buf), format.c_str(), args...);
if (size < 0)
return {};
if (size < sizeof(buf))
{
return std::string(buf, size); // 直接返回
}
else
{
// 超过栈缓存时才用堆分配
std::vector<char> dynamic_buf(size + 1);
std::snprintf(dynamic_buf.data(), dynamic_buf.size(), format.c_str(), args...);
return std::string(dynamic_buf.data(), size);
}
}
// 检测过程临结果
struct DetLog
{
std::vector<std::string> logList;
bool bPrintStr; // 是否打印日志
int addLogLevel;
// 保护 logList:同一个 DetLog(如图片结果的日志)会被实例主线程(Run/CheckRun)
// 与任务线程(AI 推理/分类) 并发写入,无锁 push_back 会造成 vector 堆破坏。
std::mutex mtx_log;
DetLog()
{
Init();
}
void Init()
{
std::lock_guard<std::mutex> lock(mtx_log);
logList.clear();
bPrintStr = false;
addLogLevel = DET_LOG_LEVEL_3;
}
// 线程安全地获取日志快照
std::vector<std::string> GetLogs()
{
std::lock_guard<std::mutex> lock(mtx_log);
return logList;
}
template <typename... Args>
void AddCheckstr(PrintLevel_ step, std::string stepStr, const std::string &format, Args... args)
{
std::string str = "";
if (step <= PrintLevel_0)
{
str += ".. ";
}
else if (step == PrintLevel_1)
{
str += "..... ";
}
else if (step == PrintLevel_2)
{
str += "........ ";
}
else if (step == PrintLevel_3)
{
str += "........... ";
}
else if (step >= PrintLevel_4)
{
str += ".............. ";
}
str += stepStr + ": ";
str += str_Format_1(format, args...);
if (bPrintStr)
{
printf("%s\n", str.c_str());
}
std::lock_guard<std::mutex> lock(mtx_log);
logList.push_back(str);
}
template <typename... Args>
void AddCheckstr(PrintLevel_ step, int LogLevel, std::string stepStr, const std::string &format, Args... args)
{
std::string str = "";
if (step <= PrintLevel_0)
{
str += ".. ";
}
else if (step == PrintLevel_1)
{
str += "..... ";
}
else if (step == PrintLevel_2)
{
str += "........ ";
}
else if (step == PrintLevel_3)
{
str += "........... ";
}
else if (step >= PrintLevel_4)
{
str += ".............. ";
}
str += stepStr + ": ";
str += str_Format_1(format, args...);
if (bPrintStr)
{
printf("%s\n", str.c_str());
}
if (LogLevel <= addLogLevel)
{
std::lock_guard<std::mutex> lock(mtx_log);
logList.push_back(str);
}
}
void printLog(std::string str)
{
printf("===========================%s==============================\n", str.c_str());
{
std::lock_guard<std::mutex> lock(mtx_log);
for (auto strl : logList)
{
printf("%s\n", strl.c_str());
}
}
printf("==========================================================\n");
}
};
#endif