我的任务是创建一个类,它将围绕几个应用程序收集用户活动。
假设我有一个类TLogging和一个名为Logging的全局对象。
用户活动(屏幕打开等)应该收集在内存中(可能放在TLogging的(字符串)列表中),并在一段时间间隔后(每10分钟)保存到日志文件中,或者在应用程序关闭时。
最重要的是日志必须处于“静默模式”,它不能以任何方式影响用户工作流程:没有屏幕挂起,没有异常。
请告诉我这项任务的方向。
发布于 2011-11-15 18:20:08
这是一个涉及多个领域的非常广泛的问题。以下是一些提示:
发布于 2011-11-16 00:10:02
使用OmniThreadLibrary并假设日志记录对象是一个单例对象,这非常简单。我还会限制等待写入的消息的最大数量,这样内部队列就不会使用太多内存。
const
CMaxMsgCount = 1000;
CMaxLogTimeout_ms = 10{min}*60{sec/min}*1000{ms/sec};
type
TLogging = class
strict private
FLogMsgCount: IOmniResourceCount;
FLogQueue: IOmniBlockingCollection;
FWriter: IOmniTaskControl;
strict protected
procedure Logger(const task: IOmniTask);
public
constructor Create;
destructor Destroy;
procedure Log(const msg: string);
end;
var
Logging: TLogging;
constructor TLogging.Create;
begin
FLogMsgCount := CreateResourceCount(CMaxMsgCount);
FLogQueue := TOmniBlockingCollection.Create;
FWriter := CreateTask(Logger, 'Logger').Run;
end;
destructor TLogging.Destroy;
begin
FWriter.Terminate;
end;
procedure TLogging.Log(const msg: string);
begin
FLogQueue.Add(msg);
FLogMsgCount.Allocate;
end;
procedure TLogging.Logger(const task: IOmniTask);
procedure Flush;
var
logData: TOmniValue;
begin
// open file, possibly with retry
while FLogQueue.TryTake(logData) do begin
FLogMsgCount.Release;
// write logData.AsString
end;
// close file
end;
begin
while DSiWaitForTwoObjects(task.TerminateEvent, FLogMsgCount.Handle, false, CMaxLogTimeout_ms) <> WAIT_OBJECT_0 do
Flush;
Flush;
end;(免责声明:“它在我的机器上编译”,否则未经测试。)
https://stackoverflow.com/questions/8134526
复制相似问题