WISNUWIDIARTA

TDLogger

Aug 10th, 2012
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit classDLogger;
  2.  
  3. interface
  4.  
  5. { Created by Wisnu Widiarta - 23rd April 2005 }
  6.  
  7. uses
  8.   Classes, ExtCtrls, IniFiles, SysUtils;
  9.  
  10. type
  11.   TLogLevel = (llAll = 4, llInfo = 3, llWarning = 2, llError = 1, llDebug = 0);
  12.  
  13.   TDLogger = class(TObject)
  14.   private
  15.     FTmrLogger: TTimer;
  16.     FListLogger: TStringList;
  17.     FlogLevel: TLogLevel;
  18.     FIniFile: TIniFile;
  19.     FActive: Boolean;
  20.     function getFormatDateTime: String;
  21.     function getDateTime: String;
  22.     procedure saveToFile;
  23.     procedure tmrSaveToFile(Sender: TObject);
  24.     procedure SetActive(const Value: Boolean);
  25.   protected
  26.     constructor Create(configFileName: String);
  27.   public
  28.     destructor Destroy; override;
  29.     procedure info(event: String);
  30.     procedure error(event: String);
  31.     procedure debug(event: String);
  32.     procedure setLogLevel(logLevel: TLogLevel);
  33.     procedure setWriteIntervalInMinute(minute: integer);
  34.     procedure setWriteIntervalInSecond(second: integer);
  35.     class function getInstance: TDLogger;
  36.     property Active: Boolean read FActive write SetActive;
  37.   end;
  38.  
  39.   TFileDumper = class(TThread)
  40.   private
  41.     FFileName: String;
  42.     FListLogger: TStringList;
  43.     procedure saveToFile;
  44.   protected
  45.     procedure Execute; override;
  46.   public
  47.     constructor Create(CreateSuspended: Boolean; fileName: String;
  48.       list: TStringList);
  49.   end;
  50.  
  51. implementation
  52.  
  53. uses Forms, SyncObjs;
  54.  
  55. { TDLogger }
  56. var
  57.   instance: TDLogger;
  58.  
  59. constructor TDLogger.Create(configFileName: String);
  60. begin
  61.   FTmrLogger := TTimer.Create(Nil);
  62.   FTmrLogger.Enabled := False;
  63.   FTmrLogger.OnTimer := Self.tmrSaveToFile;
  64.   Self.setWriteIntervalInMinute(1);
  65.   setLogLevel(llAll);
  66.  
  67.   FListLogger := TStringList.Create;
  68.   FIniFile := TIniFile.Create(configFileName);
  69.   FTmrLogger.Enabled := True;
  70.  
  71.   FActive := True;
  72. end;
  73.  
  74. procedure TDLogger.debug(event: String);
  75. begin
  76.   if FlogLevel in [llDebug, llAll] then
  77.     FListLogger.Add('DEBUG | ' + getDateTime + ' | ' + event);
  78. end;
  79.  
  80. destructor TDLogger.Destroy;
  81. begin
  82.   saveToFile;
  83.  
  84.   FListLogger.Free;
  85.   FTmrLogger.Enabled := False;
  86.   FTmrLogger.Free;
  87.   FIniFile.Free;
  88.  
  89.   instance := Nil;
  90.  
  91.   inherited;
  92. end;
  93.  
  94. procedure TDLogger.error(event: String);
  95. begin
  96.   if FlogLevel in [llError, llAll] then
  97.     FListLogger.Add('ERROR | ' + getDateTime + ' | ' + event);
  98. end;
  99.  
  100. function TDLogger.getDateTime: String;
  101. begin
  102.   Result := FormatDateTime(getFormatDateTime, Now);
  103. end;
  104.  
  105. function TDLogger.getFormatDateTime: String;
  106. begin
  107.   Result := FIniFile.ReadString('DATETIME', 'FORMAT', 'DD/MM/YYYY HH:MM:SS');
  108. end;
  109.  
  110. class function TDLogger.getInstance: TDLogger;
  111. begin
  112.   if instance = nil then
  113.     instance := TDLogger.Create(ExtractFileDir(ParamStr(0)) + 'DLOGGER.INI');
  114.  
  115.   Result := instance;
  116. end;
  117.  
  118. procedure TDLogger.info(event: String);
  119. begin
  120.   if FlogLevel in [llInfo, llAll] then
  121.     FListLogger.Add('INFO | ' + getDateTime + ' | ' + event);
  122. end;
  123.  
  124. procedure TDLogger.saveToFile;
  125. var
  126.   fileName, folder, fileSuffix: String;
  127.   listLogger: TStringList;
  128. begin
  129.   folder := FIniFile.ReadString('OUTPUT', 'FOLDER',
  130.     ExtractFileDir(ParamStr(0)));
  131.   fileSuffix := FIniFile.ReadString('OUTPUT', 'FILE', 'LOGFILE');
  132.   fileName := folder + '' + fileSuffix + FormatDateTime('-YYYY-MM-DD', Now)
  133.     + '.TXT';
  134.  
  135.   listLogger := TStringList.Create;
  136.   listLogger.Assign(FListLogger);
  137.   FListLogger.Clear;
  138.  
  139.   TFileDumper.Create(False, fileName, listLogger);
  140.   listLogger.Free;
  141. end;
  142.  
  143. procedure TDLogger.SetActive(const Value: Boolean);
  144. begin
  145.   FActive := Value;
  146. end;
  147.  
  148. procedure TDLogger.setLogLevel(logLevel: TLogLevel);
  149. begin
  150.   FlogLevel := logLevel;
  151. end;
  152.  
  153. procedure TDLogger.setWriteIntervalInMinute(minute: integer);
  154. begin
  155.   FTmrLogger.Enabled := False;
  156.   FTmrLogger.Interval := minute * 60 * 1000;
  157.   FTmrLogger.Enabled := True;
  158. end;
  159.  
  160. procedure TDLogger.setWriteIntervalInSecond(second: integer);
  161. begin
  162.   FTmrLogger.Enabled := False;
  163.   FTmrLogger.Interval := second * 1000;
  164.   FTmrLogger.Enabled := True;
  165. end;
  166.  
  167. procedure TDLogger.tmrSaveToFile(Sender: TObject);
  168. begin
  169.   if Active then
  170.     saveToFile;
  171. end;
  172.  
  173. { TFileDumper }
  174.  
  175. constructor TFileDumper.Create(CreateSuspended: Boolean; fileName: String;
  176.   list: TStringList);
  177. begin
  178.   inherited Create(Suspended);
  179.   FreeOnTerminate := True;
  180.  
  181.   FListLogger := TStringList.Create;
  182.   FListLogger.Assign(list);
  183.   FFileName := fileName;
  184. end;
  185.  
  186. procedure TFileDumper.Execute;
  187. begin
  188.   saveToFile;
  189. end;
  190.  
  191. procedure TFileDumper.saveToFile;
  192. var
  193.   fileName: String;
  194.   logFile: TextFile;
  195.   i: integer;
  196.   cs: TCriticalSection;
  197. begin
  198.   fileName := FFileName;
  199.  
  200.   cs := TCriticalSection.Create;
  201.   cs.Acquire;
  202.   try
  203.     try
  204.       AssignFile(logFile, fileName);
  205.       if FileExists(fileName) then
  206.         Append(logFile)
  207.       else
  208.         Rewrite(logFile);
  209.  
  210.       i := 0;
  211.       while (i < FListLogger.Count) do
  212.       begin
  213.         writeln(logFile, FListLogger[i]);
  214.         Inc(i);
  215.         if (i Mod 100 = 0) then
  216.           Application.ProcessMessages;
  217.       end;
  218.  
  219.       Flush(logFile);
  220.     finally
  221.       CloseFile(logFile);
  222.       FListLogger.Free;
  223.       cs.Release;
  224.       cs.Free;
  225.     end;
  226.   except
  227.     on E: Exception do
  228.     begin
  229.       // there is nothing to do
  230.     end;
  231.   end;
  232. end;
  233. end.
RAW Paste Data