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.
158 lines
3.8 KiB
158 lines
3.8 KiB
/*
|
|
* @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 "ImgCheckConfig.h"
|
|
#include "CheckErrorCodeDefine.hpp"
|
|
using namespace std;
|
|
enum PrintLevel_
|
|
{
|
|
PrintLevel_0,
|
|
PrintLevel_1,
|
|
PrintLevel_2,
|
|
PrintLevel_3,
|
|
PrintLevel_4,
|
|
};
|
|
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;
|
|
std::mutex mtx;
|
|
DetLog()
|
|
{
|
|
Init();
|
|
}
|
|
void Init()
|
|
{
|
|
logList.clear();
|
|
bPrintStr = false;
|
|
addLogLevel = DET_LOG_LEVEL_3;
|
|
}
|
|
|
|
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);
|
|
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);
|
|
logList.push_back(str);
|
|
}
|
|
}
|
|
void printLog(std::string str)
|
|
{
|
|
printf("===========================%s==============================\n", str.c_str());
|
|
{
|
|
std::lock_guard<std::mutex> lock(mtx);
|
|
for (auto strl : logList)
|
|
{
|
|
printf("%s\n", strl.c_str());
|
|
}
|
|
}
|
|
|
|
printf("==========================================================\n");
|
|
}
|
|
};
|
|
#endif |