ziji

本文详细介绍了C++中自定义日志框架的实现方式,包括如何使用宏定义简化日志输出,如何通过单例模式确保日志的唯一性和高效性,以及如何在不同场景下灵活地进行日志输出和管理。
#pragma once#include #include #include #include #include #include using namespace std;#define AGENTLOG(format,...) CLog::getInstance()->AgentLog(format,__VA_ARGS__);#define AGENTLOG2(format,...) CLog::getInstance()->test(format,__VA_ARGS__);#define AGENTLOG1(format,...) {\char theloggg[4096]; \memset(theloggg, 0, 4096); \char formatbuf[1024]; \memset(formatbuf, 0, 1024); \sprintf_s(formatbuf,"%s:","%s"); \strcat_s(formatbuf, sizeof(formatbuf), format); \sprintf_s(theloggg, formatbuf, /*__DATE__,*/ /*__TIME__*/ __FUNCTION__, ##__VA_ARGS__); \MessageBoxA(NULL,theloggg,NULL,NULL); \}class CLog{public:void test(TCHAR * format ,...){TCHAR destBuf[1024];va_list arg_ptr = NULL; va_start(arg_ptr, format); //wprintf(formatBuf,format);wvsprintf(destBuf,format, arg_ptr);wprintf(destBuf);va_end(arg_ptr);}void AgentLog(TCHAR* format,...){TCHAR theLoggg[2048];TCHAR formatBuf[1024];memset(formatBuf, 0, 1024*sizeof(wchar_t));wsprintf(formatBuf,_T("%s:"),__FUNCTIONW__);//wcscat_s(formatBuf,sizeof(formatBuf),format);_tcscat_s(formatBuf,1024,format);va_list arg_ptr = NULL; va_start(arg_ptr, format); wvsprintf(theLoggg, formatBuf, arg_ptr); CLog::outPut(theLoggg);va_end(arg_ptr);}private:CLog(void);//单例模式的唯一对象static CLog* g_Instance;//文件输出流wofstream* m_pFout;//msg计数int m_iMsgCount;//应用程序目录wstring m_strCurrentExePath;CRITICAL_SECTION m_cs;public:wstring m_strLogEdit;private://新建日志文件void createNewLog();void theOutPut(wstring msg);public:~CLog(void);//唯一实例生成函数static CLog* getInstance();//实例删除static void DeleteInstance();static void outPut(wstring msg);LPCTSTR getCurrentEXEPath();};#include "StdAfx.h"#include "Log.h"#define MAX_NEWFILE 2000#define MAX_FLUSHFILE 5CLog* CLog::g_Instance = 0;CLog::CLog(void){m_pFout=0;InitializeCriticalSection(&m_cs);CLog::getCurrentEXEPath();//创建Log目录wstring logPath=m_strCurrentExePath+_T("\\log");CreateDirectory(logPath.c_str(),NULL);createNewLog();}CLog::~CLog(void){if(m_pFout==0)return;m_pFout->flush();m_pFout->close();delete m_pFout;m_pFout = 0;}CLog* CLog::getInstance(){if(g_Instance==0)g_Instance = new CLog();return g_Instance;}void CLog::DeleteInstance(){if(g_Instance==0)return;//判断有没有打开的ostream指针if (g_Instance->m_pFout!=0){g_Instance->m_pFout->flush();g_Instance->m_pFout->close();delete g_Instance->m_pFout;g_Instance->m_pFout=0;}delete g_Instance;g_Instance = 0;}void CLog::createNewLog(){try{if (m_pFout!=0){m_pFout->flush();m_pFout->close();delete m_pFout;m_pFout=0;}m_iMsgCount=0;time_t tt;struct tm *p;time(&tt);p=localtime(&tt);TCHAR filename[260];memset(filename,0,260);wsprintf(filename,_T("%s\\log\\%d-%d-%d_%d-%d-%d_nbox.log\0"),m_strCurrentExePath.c_str(),p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);m_pFout=new wofstream();locale::global(locale(""));//将全局区域设为操作系统默认区域m_pFout->open(filename,ios::out);locale::global(locale("C"));//还原{return ;}}catch (exception e){}}LPCTSTR CLog::getCurrentEXEPath(){try{if (m_strCurrentExePath.length() > 0)return m_strCurrentExePath.c_str();TCHAR szEexFullpath[MAX_PATH];//得到EXE所有目录(包含"\")//得到程序的模块句柄HMODULE hMyModule = GetModuleHandle(NULL);//得到程序的路径DWORD dwLen = GetModuleFileName(hMyModule, szEexFullpath, MAX_PATH);//计算程序的路径(倒数查找最后一个"\")TCHAR * pPatch = &szEexFullpath[dwLen]; //szexepatch + dwLen;while (pPatch != NULL && *pPatch != '\\'){pPatch--;}*pPatch=0;m_strCurrentExePath = wstring(szEexFullpath);}catch (...){}return m_strCurrentExePath.c_str();}void CLog::outPut(wstring msg){//std::cout << msg;EnterCriticalSection(&getInstance()->m_cs);getInstance()->theOutPut(msg);LeaveCriticalSection(&getInstance()->m_cs);}void CLog::theOutPut(wstring msg){if(m_pFout==0)return;time_t timep;struct tm *p;time(&timep);p=localtime(&timep);TCHAR filename[260];memset(filename,0,260);wsprintf(filename,_T("%d:%d:%d -- "),p->tm_hour,p->tm_min , p->tm_sec);msg = wstring(filename) + msg;//add chenshuaigetInstance()->m_strLogEdit = getInstance()->m_strLogEdit + _T("\r\n") + msg;m_iMsgCount++;*m_pFout << msg << _T("\r\n");if(m_iMsgCount % MAX_FLUSHFILE ==0)m_pFout->flush();if(m_iMsgCount % MAX_NEWFILE ==0)createNewLog();}
### MATLAB 中 'ziji.m' 文件的语法错误分析 在 MATLAB 的 `ziji.m` 文件中,如果遇到 **第140行第32列** 报告的“无效表达式”或“圆括号不匹配”的错误,通常是因为函数调用、变量索引或者分隔符使用不当引起的。以下是可能的原因及其解决方案: #### 1. 函数调用中的参数问题 如果该行为函数调用语句,则可能是传递给函数的参数存在格式错误。例如: ```matlab result = myFunction(param1, param2); ``` - 如果 `param1` 或 `param2` 是数组或其他复杂数据结构,需确认其维度是否正确[^1]。 - 若涉及字符串参数,应确保使用单引号 `' '` 而不是双引号 `" "`。 #### 2. 数组索引错误 如果是数组操作引发的错误,比如: ```matlab value = data(index1:index2); ``` - 需要验证 `index1` 和 `index2` 是否为合法数值,并且它们应在数组的有效范围内[^2]。 - 如果 `data` 是多维数组,还需注意索引的数量与顺序是否一致。 #### 3. 圆括号配对问题 MATLAB 对于括号的要求非常严格,任何未闭合或错位的括号都会导致语法错误。例如: ```matlab if (condition == true && anotherCondition == false) disp('True'); end ``` - 上述代码中,条件判断部分的括号必须成对出现并保持逻辑清晰[^3]。 #### 4. 注释符号干扰 有时开发者会在同一行混合写注释和有效代码,这可能导致误解析情况发生。像下面这种情形就容易出现问题: ```matlab totalSum = sum(values); % 计算总和 totalSum 应当大于零 ``` - 正确做法是将说明性文字单独放在新行作为独立注解[^4]。 #### 示例修正后的代码片段 假设原始有问题的部分如下所示: ```matlab output = processSignal(inputData(start:end), thresholdValue; ``` 经过调整后可以改为: ```matlab % 确保 start/end 均定义良好且位于 inputData 边界之内 tempData = inputData(max(1,start):min(length(inputData), end)); output = processSignal(tempData, thresholdValue); ``` 以上修改考虑到了边界保护以及正确的括号运用方式。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值