📄 logfile.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "logfile.h"
#include "print.h"
#include "dido.h"
#include "option.h"
#include "datetool.h"
#include "vtl.h"
//设置文本
static void SetEditText(TLogForm *se);
//确认删除日志
static int ShowDeleteLog(void);
//确认打印日志
static int ShowPrintLog(void);
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TLogForm::TLogForm(TComponent* Owner)
: TForm(Owner)
{
}
//查看日志
int LookLog(TObject *Owner)
{
TLogForm *se;
int rc;
rc=FALSE;
se = new TLogForm((TComponent*)Owner);
if(se!=NULL){
se->m_nDone=0;
SetEditText(se);
se->ShowModal();
rc=se->m_nDone;
delete se;
}
return(rc);
}
//设置文本
void SetEditText(TLogForm *se)
{
char szFile[MAX_PATH];
int nCount;
nCount=0;
GetLogFilename(szFile,sizeof(szFile)-1);
try{
se->m_TextMemo->Lines->LoadFromFile(szFile);
nCount = se->m_TextMemo->Lines->Count;
}
catch(...){
se->m_TextMemo->Text="";
}
se->m_PrintBut->Enabled=nCount;
se->m_DelBut->Enabled=nCount;
}
void __fastcall TLogForm::m_PrintButClick(TObject *Sender)
{
static char pszTitle[]="巡更系统日志";
if(ShowPrintLog()) //确认打印日志
PrintEditText(pszTitle,m_TextMemo->Lines);
}
//---------------------------------------------------------------------------
void __fastcall TLogForm::m_DelButClick(TObject *Sender)
{
if(ShowDeleteLog()){//确认删除日志
m_TextMemo->Text="";
m_PrintBut->Enabled=FALSE;
m_DelBut->Enabled=FALSE;
DeleteLogFile();
}
}
//确认删除日志
int ShowDeleteLog(void)
{
static char pszText[]="是否真的删除所有日志?";
int rc;
//验证用户权限
rc= ValidateRight(RIGHT_MANAGE);
if(rc){
MessageBeep(MB_ICONHAND);
rc = Application->MessageBox(pszText,GetMainTitle(),
MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2|MB_APPLMODAL);
if(rc==IDYES)rc=TRUE;
else rc=FALSE;
}
return(rc);
}
//确认打印日志
int ShowPrintLog(void)
{
static char pszText[]="请准备好打印机!\n\n开始打印所有日志。";
int rc;
//验证用户权限
rc= ValidateRight(RIGHT_MANAGE);
if(rc){
MessageBeep(MB_ICONHAND);
rc = Application->MessageBox(pszText,GetMainTitle(),
MB_OKCANCEL|MB_ICONINFORMATION|MB_DEFBUTTON2|MB_APPLMODAL);
if(rc==IDOK)rc=TRUE;
else rc=FALSE;
}
return(rc);
}
void __fastcall TLogForm::m_CloseButClick(TObject *Sender)
{
ModalResult=mrOk;
}
//保存一个日志
int SaveOneLog(int nDone)
{
char *pszAcc;
pszAcc=GetCurAccName();
nDone=SaveAccountLog(pszAcc,nDone);//保存帐号日志
return(nDone);
}
//获取日志文件名
char *GetLogFilename(char *pszFile,int nSize)
{
static char pszDef[]="LOOKLOG";
char *p;
p = NULL; //获取文件名
GetFullPathName(Application->ExeName.c_str(),nSize,pszFile,&p);
if(p==NULL)return(NULL);
lstrcpy(p,pszDef);
return(pszFile);
}
//打开日志文件
int OpenLogFile(void)
{
char szFile[MAX_PATH];
int hFile;
GetLogFilename(szFile,sizeof(szFile)-1);
if(FileExists(szFile)){
hFile = FileOpen(szFile,fmOpenReadWrite|fmShareDenyNone);
FileSeek(hFile,0,2);
}
else hFile=FileCreate(szFile);
return(hFile);
}
//删除日志文件
int DeleteLogFile(void)
{
static char pszExt[]=".BAK";
char *pszFile,*pszBak,*p;
int rc;
rc=FALSE;
pszFile=new char[MAX_PATH*2+10];
if(pszFile!=NULL){
pszBak=pszFile+MAX_PATH;
GetLogFilename(pszFile,MAX_PATH);
lstrcpy(pszBak,pszFile);
p = strrchr(pszBak,'.');
if(p==NULL)lstrcat(pszBak,pszExt);
else lstrcpy(p,pszExt);
if(FileExists(pszBak)) //删除备份文件
DeleteFile(pszBak);
rc=RenameFile(pszFile,pszBak);//将文件改为备份文件
delete pszFile;
}
return(rc);
}
//关闭日志文件
void CloseLogFile(int hFile)
{
if(hFile!=-1) FileClose(hFile);
}
//读取日志文件中所有
char *ReadLogFileAll(void)
{
char *p;
int hFile,nLen;
p=NULL; nLen=0;
hFile=OpenLogFile();
if(hFile!=-1){
nLen=FileSeek(hFile,0,2);
if(nLen>0) p=(char *)GlobalAlloc(GPTR,nLen+1);
}
if(p!=NULL){
FileSeek(hFile,0,0);
FileRead(hFile,p,nLen);
p[nLen]=0;
}
else if(nLen>0) ShowNotMemory("读取日志文件");
CloseLogFile(hFile);
return(p);
}
//写日志文件(nReturn!=FALSE 加写换行符)
int WriteLogFile(int hFile,char *pszBuf,int nReturn)
{
int nLen;
if(hFile!=-1){
nLen=lstrlen(pszBuf);
nLen=FileWrite(hFile,pszBuf,nLen);
if(nReturn) nLen+=FileWrite(hFile,"\r\n",2);
}
else nLen=0;
return(nLen);
}
//保存帐号日志
int SaveAccountLog(char *pszAcc,int nDone)
{
int hFile;
int rc;
hFile = OpenLogFile();
rc=WriteAccountLog(hFile,pszAcc,nDone);
CloseLogFile(hFile);
return(rc);
}
//写入帐号日志
int WriteAccountLog(int hFile,char *pszAcc,int nDone)
{
static char pszText[]="%04d.%02d.%02d(%s)-%02d:%02d:%02d"
" 用户\"%s\" %s";
SYSTEMTIME t;
int nLen;
char *pszDone,szBuf[150];
//获取现在时间,日期
GetLocalTime(&t);
pszDone = GetDoneText(nDone);
wsprintf(szBuf,pszText,
t.wYear,t.wMonth,t.wDay,GetWeekName(t.wDayOfWeek),
t.wHour,t.wMinute,t.wSecond,
pszAcc,pszDone);
nLen=WriteLogFile(hFile,szBuf);
return(nLen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -