You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TwoLaserHead-PushJig/LaiPuLaser/LogMgr.cpp

319 lines
9.4 KiB
C++

#include "StdAfx.h"
#include "LogMgr.h"
#include "MainFrm.h"
#include "FileMgr.h"
#include "Propertie.h"
#include "PropertieMgr.h"
#include "BitOperation.h"
#include "AuthorityMgr.h"
#include "WorkTime.h"
CLogMgr* gLogMgr = new CLogMgr;//日志管理对象
CLogMgr::CLogMgr(void)
{
m_bWriteDebugLog = true;
m_bDebug = false;//是否正在调试
m_DebugCmdDelay = 100;//调试指令延时时间
m_LogListMaxLine = 1000;//日志列表最大的行数,超过时清空
m_bWriteToLogList = true;//输出到日志列表
//创建记录文本日志的文件夹
CFileMgr fg;
fg.GetFullFilePath(PlcLogDir, "\\PlcLog");
if (!PathIsDirectory(PlcLogDir))
{
CreateDirectory(PlcLogDir,NULL);
}
}
CLogMgr::~CLogMgr(void)
{
}
#if 1
void CLogMgr::Ini()
{
//绑定pFrame
CMainFrame* pFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
#ifdef __DLG_PANE_CMD__
m_pOutputCmd = &pFrame->m_OutputCmd;
#endif
m_pOutputLog = &pFrame->m_OutputLog;
{
CString FileName = "\\Debug.txt";
CFileMgr FileMgr;
char filepath[1024];
FileMgr.GetFullFilePath(filepath,FileName);
m_DebugLogFile.open(filepath);
}
{
CString FileName = "\\Result.txt";
CFileMgr FileMgr;
char filepath[1024];
FileMgr.GetFullFilePath(filepath,FileName);
m_ResultFile.open(filepath);
}
}
CMFCPropertyGridProperty *CLogMgr::CreatGridProperty()
{
CString PropertyName;//属性名称
CString Description;//描述
CString Path = _T("LogMgr");;//存储路径
CString Name;
//-------------------------------------------------------------------------------//
PropertyName = _T("日志");
CMFCPropertyGridProperty* pGroup = new CMFCPropertyGridProperty(PropertyName);
//-------------------------------------------------------------------------------//
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
//添加属性变量映射
Name = _T("bWriteDebugLog");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_bWriteDebugLog);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("日志文件");
Description = _T("是否输出调试日志文件");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bWriteDebugLog, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
//添加属性变量映射
Name = _T("m_bWriteToLogList");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_bWriteToLogList);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("日志列表");
Description = _T("是否输出日志列表");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bWriteToLogList, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
//添加属性变量映射
Name = _T("m_LogListMaxLine");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_LogListMaxLine);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("最大行数");
Description = _T("日志列表最大的行数,超过时清空日志列表");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_LogListMaxLine, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
//添加属性变量映射
Name = _T("m_bDebug");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_bDebug);
pPropertie->SetType(_PROP_TYPE_BOOL);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("是否调试");
Description = _T("是否正在调试状态");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_bDebug, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
if(gAuthorityMgr->CheckAuthority(_FACTORY))
{
//添加属性变量映射
Name = _T("m_DebugCmdDelay");//变量名字
CPropertie *pPropertie = new CPropertie;
pPropertie->SetpVal((void*)&m_DebugCmdDelay);
pPropertie->SetType(_PROP_TYPE_INT);
pPropertie->SetpModule(this);
pPropertie->SetPath(Path);
pPropertie->SetName(Name);
pPropertie->WriteRead(true);//读取保存的属性
//添加属性显示
PropertyName = _T("调试延时");
Description = _T("调试指令延时时间(ms)");
CMFCPropertyGridProperty* p1 = new CMFCPropertyGridProperty(PropertyName, (_variant_t)m_DebugCmdDelay, Description);
pGroup->AddSubItem(p1);
gDevicePropertieMgr.Insert(p1, pPropertie);
}
//-------------------------------------------------------------------------------//
return pGroup;
}
void CLogMgr::ExportPar(ofstream *pFile)
{
(*pFile)<<"[模块] [CLogMgr]------------------------------------------------"<<endl;
(*pFile)<<"[输出调试日志][m_bWriteDebugLog] = "<<m_bWriteDebugLog<<endl;
}
#endif
#if 1
//写入日志
void CLogMgr::WriteLog(CLog &Log)
{
if(gExitApp)
return;
CString strTime; //获取系统时间
CTime tm=CTime::GetCurrentTime();
strTime=tm.Format("[%m月%d日 %X] ");
//日志列表
if(m_bWriteToLogList)
{
if(Log.bCmdLog)
{
#ifdef __DLG_PANE_CMD__
CString strCmd = " 命令 : ";
m_pOutputCmd->AddToList(strTime+strCmd+Log.str,m_LogListMaxLine);
#endif
}
else
{
m_pOutputLog->AddToList(strTime+Log.str,m_LogListMaxLine);
}
}
//写入文件
if(m_bWriteDebugLog)
{
m_DebugLogFile<<Log.str<<endl;
}
}
void CLogMgr::WriteCmd(CString Cmd,CString val1,CString val2)
{
if(gExitApp)
return;
if(val1 != STR_NULL || val2 != STR_NULL)
{
Cmd += " : ";
}
if(val1 != STR_NULL)
{
val1 = "<"+val1+">";
}
if(val2 != STR_NULL)
{
val2 = "<"+val2+">";
}
CLog log;
log.AddCmdLog();
log.str = Cmd+val1+val2;
WriteLog(log);
}
#endif
#if 1//一些方便的写log 方式
void CLogMgr::WriteDebugLog(CString Str,LOG_TYPE type)
{
if(gExitApp)
return;
if(m_bWriteDebugLog || m_bWriteToLogList)
{
CLog log;
log.AddDebugLog();
switch(type)
{
case _LOG_ERROR:
Str = "[Error]"+Str;
break;
case _LOG_FUNC:
Str = "[Function]"+Str;
break;
default:
break;
}
CString str1 = Str;
log.str = str1;
WriteLog(log);
}
}
//两个变量
void CLogMgr::WriteDebugLog(CString Str,CString ValName1,CString ValName2,double val1,double val2)
{
if(gExitApp)
return;
CString sVal1;
sVal1.Format("%lf",val1);
CString sVal2;
sVal2.Format("%lf",val2);
CString str = Str+"["+ValName1+"] = "+sVal1+" ["+ValName2+"] = "+sVal2;
WriteDebugLog(str);
}
//一个变量
void CLogMgr::WriteDebugLog(CString Str,CString ValName,double val)
{
if(gExitApp)
return;
CString sVal1;
sVal1.Format("%lf",val);
CString str = Str+"["+ValName+"] = "+sVal1;
WriteDebugLog(str);
}
void CLogMgr::WriteByte(BYTE bit)
{
if(gExitApp)
return;
CString str;
for(int i=0;i<8;i++)
{
if(IsBitOn(bit,i))
str += "1";
else
str += "0";
}
WriteDebugLog(str);
}
void CLogMgr::WriteDebugLogFile(CString str)
{
if(gExitApp)
return;
if(m_bWriteDebugLog)
{
m_DebugLogFile<<str<<endl;
}
}
void CLogMgr::WritePlcTimeLog(CString str)
{
CWorkTime wm;
auto fileName=wm.GetCurDate("-");
auto path = PlcLogDir + "\\"+fileName + ".txt";
auto LogHead = wm.GetCurTime(":")+" >";
str = LogHead + str + "\n";
CStdioFile file;
if (file.Open(path,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))
{
file.SeekToEnd();
file.WriteString(str);
file.Close();
}
}
#endif