方法1:抓取发送的数据包和接收的数据,获取时间
方法2:由发送者将自己的时间作为一项数据发送给接收者
方法3:采用日志,记录发送时间和接收时间
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include<string>
#include<fstream>
#include<time.h>
using namespace std;
class LogHelper
{
public:
LogHelper(void);
void Init(BLL* pBLL);
~LogHelper(void);
//string actorstr;发送、接收;string opstr操作类型:删除、拖动、分割删除、效果、编辑
void WriteLog(char* actor_str,char* op_str);
private:
CRITICAL_SECTION csLogWrite; //多线程
BLL* pBLL;
};
#include "LogHelper.h"
LogHelper::LogHelper(void)
{
}
void LogHelper::Init(BLL* pBLL)
{
InitializeCriticalSection(&csLogWrite);
this->pBLL = pBLL;
}
LogHelper::~LogHelper(void)
{
DeleteCriticalSection(&csLogWrite);
}
void LogHelper::WriteLog(char* actor_str,char* op_str)
{
EnterCriticalSection(&csLogWrite);
try
{
//获取当前日期
time_t t = time(0);
char tmp[64];
strftime(tmp,sizeof(tmp),"%Y%m%d",localtime(&t));
ofstream fout;
char fileName[100];
if(strcmp(pBLL->dirName,"")!=0)
sprintf(fileName,"%s\\%s_%s.Log",pBLL->dirName,pBLL->myName,tmp);
else
sprintf(fileName,"%s_%s.Log",pBLL->myName,tmp);
fout.open(fileName,ios::app);
if(fout.is_open())
{
SYSTEMTIME sys_time;
GetLocalTime(&sys_time);
char msg[100];
sprintf(msg,"%4d/%02d/%02d %02d:%02d:%02d.%03d %s%s",sys_time.wYear,sys_time.wMonth,sys_time.wDay,sys_time.wHour,sys_time.wMinute,sys_time.wSecond,sys_time.wMilliseconds,actor_str,op_str);
fout<<msg<<endl;
fout.close();
}
}
catch (exception &e)
{
}
finally
{
LeaveCriticalSection(&csLogWrite);
}
}