16,748
社区成员
发帖
与我相关
我的任务
分享
//日志处理单元
unit uLog;
interface
uses
SysUtils, IniFiles, Windows, Classes;
type
TLogFunc = procedure(const S: string);
TLogType = (sslog, ssCommlog, ssSmsLog);
TLogThr = class(TThread)
private
FLogFunc: TLogFunc;
FText: string;
FlogType: TLogType;
protected
procedure Execute; override;
public
property Func: TLogFunc read FLogFunc write FLogFunc;
property Text: string read FText write FText;
property logType: TLogType read FlogType write FlogType;
end;
//TLog = class
//private
//class
procedure uLog_Init; //初始化
//public
//class
procedure uLog_log(const S: string);
//class
procedure uLog_Commlog(const S: string);
//class
procedure uLog_SmsLog(const S: string);
//end;
procedure uLog_logFunc(const S: string);
procedure uLog_CommlogFunc(const S: string);
procedure uLog_SmsLogFunc(const S: string);
function uLog_LogText(const S: string): string;
procedure uLog_SetNewLogPath(const logPath: string); //重新设置日志文件路径
var
gLogFile, gCommLogFile, gSmsLogFile: TextFile;
gOldLogFileName: string; //普通日志文件名
gOldCommLogFileName: string; //通讯日志文件名
gOldSmsLogFileName: string; //短信日志文件名
gRootPath,gLogPath: string;
logLock, CommLogLock, SmsLogLock: TRTLCriticalSection;
implementation
uses
uConst;
{ TLog }
//格式化日志
function uLog_LogText(const S: string): string;
begin
Result := StringReplace(StringReplace(S, #13#10, '\n' ,[rfReplaceAll]), #13, '\x0d', [rfReplaceALl]);
Result := StringReplace(Result, #10, '\x0a', [rfReplaceALl]);
Result := Format('%s: %s',[FormatDateTime(gConfig.DataFormat+' '+gConfig.TimeFormat, Now), Result]); //'dd/MM/yyyy hh:nn:ss'
end;
//重新设置日志文件路径
procedure uLog_SetNewLogPath(const logPath: string);
begin
gLogPath := logPath;
//造成文件名不一致,迫使日志函数重新定位路径------
gOldLogFileName := '1';
gOldCommLogFileName := '1';
gOldSmsLogFileName := '1';
//------------------------------------------------
end;
//class
procedure uLog_log(const S: string); //TLog.
begin
with TLogThr.Create(True) do
begin
Func := uLog_logFunc;
Text := S;
LogType := ssLog;
Resume;
end;
end;
//class
procedure uLog_Commlog(const S: string); //TLog.
begin
with TLogThr.Create(True) do
begin
Func := uLog_CommlogFunc;
Text := S;
LogType := ssCommLog;
Resume;
end;
end;
//class
procedure uLog_Smslog(const S: string); //TLog.
begin
with TLogThr.Create(True) do
begin
Func := uLog_SmslogFunc;
Text := S;
LogType := ssSmsLog;
Resume;
end;
end;
//自动找寻当日日志文件,新的一天开始会自动创建新文件(普通日志)
procedure uLog_logFunc(const S: string);
var
logFileName: string;
begin
try
logFileName := FormatDateTime('yyyyMMdd',Now)+'.log';
if logFileName <> gOldLogFileName then //程序启动 或 新的一天开始
begin
if gOldLogFileName<>'' then //存在的昨日文件
CloseFile(gLogFile); //关闭昨日文件
AssignFile(gLogFile, gLogPath+logFileName);
gOldLogFileName := logFileName;
if FileExists(gLogPath+logFileName) then
begin
Append(gLogFile); //打开准备追加
Writeln(gLogFile, ''); //另起一行
end else
ReWrite(gLogFile); //覆盖已存在的文件
end;
if S <> '' then
Writeln(gLogFile, uLog_LogText(S)); //格式化日志
except
//on e:exception do
// System.Writeln(e.Message);
end;
end;
//自动找寻当日日志文件,新的一天开始会自动创建新文件(通讯日志)
procedure uLog_CommlogFunc(const S: string);
var
logFileName: string;
begin
try
logFileName := FormatDateTime('yyyyMMdd',Now)+'.comm.log';
if logFileName <> gOldCommLogFileName then //程序启动 或 新的一天开始
begin
if gOldCommLogFileName<>'' then //存在的昨日文件
CloseFile(gCommLogFile); //关闭昨日文件
AssignFile(gCommLogFile, gLogPath+logFileName);
gOldCommLogFileName := logFileName;
if FileExists(gLogPath+logFileName) then
begin
Append(gCommLogFile); //打开准备追加
Writeln(gCommLogFile, ''); //另起一行
end else
ReWrite(gCommLogFile); //覆盖已存在的文件
end;
if S <> '' then
Writeln(gCommLogFile, uLog_LogText(S)); //格式化日志
except
//on e:exception do
// System.Writeln(e.Message);
end;
end;
//自动找寻当日日志文件,新的一天开始会自动创建新文件(短信日志)
procedure uLog_SmsLogFunc(const S: string);
var
logFileName: string;
begin
try
logFileName := FormatDateTime('yyyyMMdd',Now)+'.sms.log';
if logFileName <> gOldSmsLogFileName then //程序启动 或 新的一天开始
begin
if gOldSmsLogFileName<>'' then //存在的昨日文件
CloseFile(gSmsLogFile); //关闭昨日文件
AssignFile(gSmsLogFile, gLogPath+logFileName);
gOldSmsLogFileName := logFileName;
if FileExists(gLogPath+logFileName) then
begin
Append(gSmsLogFile); //打开准备追加
Writeln(gSmsLogFile, ''); //另起一行
end else
ReWrite(gSmsLogFile); //覆盖已存在的文件
end;
if S <> '' then
Writeln(gSmsLogFile, uLog_LogText(S)); //格式化日志
except
//on e:exception do
// System.Writeln(e.Message);
end;
end;
//从配置读取日志路径
//class
procedure uLog_Init; //TLog.
var
ini: TIniFile;
fileName: string;
h: Integer;
begin
gRootPath := ExtractFilePath(ParamStr(0));
gOldLogFileName := '';
gOldCommLogFileName := '';
fileName := gRootPath+FILE_CONFIG;
if not FileExists(fileName) then //创建配置文件
begin
h := FileCreate(fileName);
FileClose(h);
end;
ini := TIniFile.Create(fileName);
with ini do
try
gLogPath := ReadString('LOG', 'LogPath', gRootPath +'log\');
if not DirectoryExists(gLogPath) then
gLogPath := gRootPath +'log\';
finally
Free;
end;
//创建日志文件夹
if not DirectoryExists(gLogPath) then
if not CreateDir(gLogPath) then
raise Exception.Create('Cann''t create dierctory:'+gLogPath);
end;
{ TLogThr }
procedure TLogThr.Execute;
begin
FreeOnTerminate := True;
if Assigned(FLogFunc) then
begin
if FLogType = sslog then
EnterCriticalSection(logLock)
else if FLogType = ssCommlog then
EnterCriticalSection(CommlogLock)
else if FLogType = ssSmslog then
EnterCriticalSection(SmslogLock);
try
FLogFunc(FText);
finally
if FLogType = sslog then
LeaveCriticalSection(logLock)
else if FLogType = ssCommlog then
LeaveCriticalSection(CommlogLock)
else if FLogType = ssSmslog then
LeaveCriticalSection(SmslogLock);
end;
end;
end;
initialization
InitializeCriticalSection(logLock);
InitializeCriticalSection(CommLogLock);
InitializeCriticalSection(SmsLogLock);
//TLog.
uLog_Init;
//初始化---------------------
//TLog.
uLog_log('');
//TLog.
uLog_Commlog('');
//TLog.
uLog_SmsLog('');
////
finalization
if gOldLogFileName<>'' then
CloseFile(gLogFile);
if gOldCommLogFileName<>'' then
CloseFile(gCommLogFile);
if gOldSmsLogFileName<>'' then
CloseFile(gSmsLogFile);
DeleteCriticalSection(logLock);
DeleteCriticalSection(CommLogLock);
DeleteCriticalSection(SmsLogLock);
end.