1
0

MyLog.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. {$I+}
  2. unit MyLog;
  3. {*******************************} interface {**********************************}
  4. var
  5. UseAppendMode :Boolean = true;//One file mode. OLog file opened only in writing.
  6. WriteTimeToLog :Boolean = true;
  7. WriteMillisecondsToLog :Boolean = False;
  8. LogElementsSeparator :String = ' ';
  9. Procedure WriteMyLog(const aStr :String);
  10. Procedure WriteMyLogEx(const aArgs :array of const; const aHeader :String = '');
  11. // WriteMyLogEx(['vMin',vMin,'vMax',vMax], 'Testing') -> Testing vMin=20 vMax=40
  12. {*****************************} implementation {*******************************}
  13. Uses
  14. System.SysUtils,
  15. System.Variants,
  16. System.IOUtils,
  17. System.SyncObjs,
  18. System.Diagnostics;
  19. var
  20. fInLog : TCriticalSection;
  21. fFS : TFormatSettings;
  22. fStopWatch : TStopwatch;
  23. fLogFile : TextFile;
  24. fLogOpened : Boolean;
  25. fFileBuf : array[1..4096] of Char;
  26. fLogFileName : string;
  27. procedure AddStr(var aStr :String;const aAddStr :String);inline;
  28. begin
  29. aStr := aStr + aAddStr;
  30. end;
  31. Procedure WriteMyLog(const aStr :String);
  32. begin
  33. if not fLogOpened then
  34. Exit;
  35. var vFullStr :String;
  36. if WriteTimeToLog or WriteMillisecondsToLog then begin
  37. vFullStr := '[';
  38. if WriteTimeToLog then
  39. AddStr(vFullStr, DateTimeToStr(Now, fFS));
  40. if WriteMillisecondsToLog then begin
  41. if WriteTimeToLog then
  42. AddStr(vFullStr, '-');
  43. AddStr(vFullStr, IntToStr(fStopWatch.ElapsedMilliseconds));
  44. end;
  45. AddStr(vFullStr, ']' + aStr);
  46. end else
  47. vFullStr := aStr;
  48. fInLog.Acquire;
  49. try
  50. try
  51. if UseAppendMode then begin
  52. AssignFile(fLogFile, fLogFileName);
  53. if FileExists(fLogFileName) then
  54. Append(fLogFile)
  55. else
  56. Rewrite(fLogFile);
  57. try
  58. Writeln(fLogFile, vFullStr);
  59. finally
  60. CloseFile(fLogFile);
  61. end;
  62. end else begin
  63. Writeln(fLogFile, vFullStr);
  64. Flush(fLogFile);
  65. end;
  66. except
  67. //Ignoring all exceptions
  68. end;{try}
  69. finally
  70. fInLog.Release;
  71. end;
  72. end;
  73. function PointerToStr(aPtr : Pointer):String;
  74. begin
  75. if aPtr=nil then
  76. Exit('nil')
  77. else
  78. Exit(IntToHex(UIntPtr(aPtr)));
  79. end;
  80. function ObjectToStr(aObj : TObject):String;
  81. begin
  82. if aObj=nil then
  83. Exit('[]nil')
  84. else
  85. Exit('['+aObj.ClassName+']'+PointerToStr(aObj));
  86. end;
  87. Procedure WriteMyLogEx(const aArgs :array of const; const aHeader :String = '');
  88. var
  89. vArgument :Boolean;
  90. vStr :String;
  91. procedure IncStr(const aAddStr :String);
  92. begin
  93. if vArgument then
  94. AddStr(vStr, aAddStr)
  95. else
  96. AddStr(vStr, '=' + aAddStr + LogElementsSeparator);
  97. end;
  98. begin
  99. if not fLogOpened then
  100. Exit;
  101. vArgument := True;
  102. vStr := aHeader;
  103. if vStr<>'' then
  104. AddStr(vStr, LogElementsSeparator);
  105. for var I := Low(aArgs) to High(aArgs) do begin
  106. with aArgs[I] do begin
  107. case VType of
  108. vtUnicodeString : IncStr(String(UnicodeString(VUnicodeString)));
  109. vtAnsiString : IncStr(String(AnsiString(VAnsiString)));
  110. vtWideString : IncStr(String(WideString(VWideString)));
  111. vtString : IncStr(String(VString^));
  112. vtInteger : IncStr(IntToStr(VInteger));
  113. vtInt64 : IncStr(IntToStr(VInt64^));
  114. vtExtended : IncStr(FloatToStr(VExtended^, fFS));
  115. vtBoolean : IncStr(BoolToStr(VBoolean, true));
  116. vtCurrency : IncStr(CurrToStr(VCurrency^, fFS));
  117. vtObject : IncStr(ObjectToStr(VObject));
  118. vtClass : IncStr(VClass.ClassName);
  119. vtPointer : IncStr(PointerToStr(vPointer));
  120. vtVariant : IncStr(VarToStr(VVariant^));
  121. vtInterface : IncStr(PointerToStr(vInterface));
  122. (*
  123. vtChar
  124. vtPChar
  125. vtWideChar
  126. vtPWideChar
  127. *)
  128. end;{case}
  129. end;{with}
  130. vArgument := not vArgument;
  131. end;{for i}
  132. WriteMyLog(vStr);
  133. end;
  134. procedure InitLogSubSystem;
  135. begin
  136. fLogOpened := True;
  137. try
  138. var vTmpPath := TPath.GetTempPath + PathDelim + 'MyLog';
  139. CreateDir(vTmpPath);
  140. AddStr(vTmpPath, PathDelim + ExtractFileName(ParamStr(0)));
  141. if UseAppendMode then
  142. fLogFileName := vTmpPath + '.log'
  143. else begin
  144. var vInd := 0;
  145. repeat
  146. fLogFileName := vTmpPath + '.' + IntToStr(vInd) + '.log';
  147. inc(vInd);
  148. until not FileExists(fLogFileName);
  149. AssignFile(fLogFile, fLogFileName);
  150. System.SetTextBuf(fLogFile, fFileBuf);
  151. ReWrite(fLogFile);
  152. end;
  153. fInLog := TCriticalSection.Create;
  154. except
  155. fLogOpened := False;
  156. end;{try}
  157. if fLogOpened then begin
  158. fFS := FormatSettings;
  159. fFS.DecimalSeparator := '.';
  160. fFS.DateSeparator := '/';
  161. fFS.ShortDateFormat := 'dd/mm/yy';
  162. fFS.LongDateFormat := fFS.ShortDateFormat;
  163. fFS.TimeSeparator := ':';
  164. fFS.ShortTimeFormat := 'hh:nn:ss';
  165. fFS.LongTimeFormat := 'hh:nn:ss';
  166. fStopWatch := TStopwatch.StartNew;
  167. end;
  168. end;
  169. procedure DoneLogSystem;
  170. begin
  171. try
  172. if fLogOpened then begin
  173. fLogOpened := False;
  174. if not UseAppendMode then
  175. CloseFile(fLogFile);
  176. fInLog.Free;
  177. end;
  178. except
  179. {Ignoring}
  180. end;
  181. end;
  182. initialization
  183. InitLogSubSystem;
  184. WriteMyLog('===================== Start of log ============================');
  185. finalization
  186. WriteMyLog('===================== End of log ==============================');
  187. DoneLogSystem;
  188. end.