加入操作日志
本人是菜鸟,有个问题想请教各位大侠。
最近正在用c++builder做一个软件,我想在其中加入一个记录操作的日志用来记录用户的所有操作
恳请各位帮忙,先谢谢各位了!
[解决办法]
我自己用的一个写日志文件,reLog是个RichEdit控件。要#include <stdio.h>
void TMainForm::WriteLog(AnsiString logString,int LogLevel)
{
//TODO: Add your source code here
AnsiString logDate,logTime,logFileName;
TColor logColor[5]={clGreen,clBlue,clPurple,clRed,clRed};
AnsiString logDesc[5] = {"提示","信息","警告","错误","异常"};
logDate = Now().FormatString("YYYY-MM-DD ");
logTime = Now().FormatString("HH:NN:SS ");
FILE *stream;
if(LogLevel >= ScreenLogLevel)
{
if(reLog->Lines->Count > 300)
reLog->Lines->Delete(0);
reLog->SelStart = reLog->Text.Length() ;
reLog->SelAttributes->Color = logColor[LogLevel-1];
reLog->Lines->Add(logTime+logString);
reLog->Perform(EM_SCROLLCARET,0,0);
}
logFileName = ExtractFilePath(Application->ExeName)+ "\\log\\" + Now().FormatString("YYMMDD") + ".LOG";
if(LogLevel >= FileLogLevel)
{
try
{
stream = fopen(logFileName.c_str(), "a+");
fprintf(stream,"%s%s事件级别:%s 事件描述:%s\n",logDate.c_str(),logTime.c_str(),logDesc[LogLevel-1],logString.c_str());
fclose(stream);
}
catch(Exception &e)
{
reLog->SelAttributes->Color = clRed;
reLog->Lines->Add("写日志文件失败!原因为:" + e.Message);
}
}
}