logger.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. unit logger;
  2. {
  3. $Id: logger.pas,v 1.2 2006/11/26 16:58:04 savage Exp $
  4. }
  5. {******************************************************************************}
  6. { }
  7. { Error Logging Unit }
  8. { }
  9. { The initial developer of this Pascal code was : }
  10. { Dominique Louis <[email protected]> }
  11. { }
  12. { Portions created by Dominique Louis are }
  13. { Copyright (C) 2000 - 2001 Dominique Louis. }
  14. { }
  15. { }
  16. { }
  17. { Contributor(s) }
  18. { -------------- }
  19. { }
  20. { }
  21. { Obtained through: }
  22. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  23. { }
  24. { You may retrieve the latest version of this file at the Project }
  25. { JEDI home page, located at http://delphi-jedi.org }
  26. { }
  27. { The contents of this file are used with permission, subject to }
  28. { the Mozilla Public License Version 1.1 (the "License"); you may }
  29. { not use this file except in compliance with the License. You may }
  30. { obtain a copy of the License at }
  31. { http://www.mozilla.org/MPL/MPL-1.1.html }
  32. { }
  33. { Software distributed under the License is distributed on an }
  34. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  35. { implied. See the License for the specific language governing }
  36. { rights and limitations under the License. }
  37. { }
  38. { Description }
  39. { ----------- }
  40. { Logging functions... }
  41. { }
  42. { }
  43. { Requires }
  44. { -------- }
  45. { SDL.dll on Windows platforms }
  46. { libSDL-1.1.so.0 on Linux platform }
  47. { }
  48. { Programming Notes }
  49. { ----------------- }
  50. { }
  51. { }
  52. { }
  53. { }
  54. { Revision History }
  55. { ---------------- }
  56. { 2001 - DL : Initial creation }
  57. { 25/10/2001 - DRE : Added $M+ directive to allow published }
  58. { in classes. Added a compile directive }
  59. { around fmShareExclusive as this does not }
  60. { exist in Free Pascal }
  61. { }
  62. {******************************************************************************}
  63. {
  64. $Log: logger.pas,v $
  65. Revision 1.2 2006/11/26 16:58:04 savage
  66. Modifed to create separate log files. Therefore each instance running from the same directory will have their own individual log file, prepended with a number.
  67. Revision 1.1 2004/02/05 00:08:20 savage
  68. Module 1.0 release
  69. }
  70. {$I jedi-sdl.inc}
  71. {$WEAKPACKAGEUNIT OFF}
  72. interface
  73. uses
  74. Classes,
  75. SysUtils;
  76. type
  77. TLogger = class
  78. private
  79. FFileHandle : TextFile;
  80. FApplicationName : string;
  81. FApplicationPath : string;
  82. protected
  83. public
  84. constructor Create;
  85. destructor Destroy; override;
  86. function GetApplicationName: string;
  87. function GetApplicationPath: string;
  88. procedure LogError( ErrorMessage : string; Location : string );
  89. procedure LogWarning( WarningMessage : string; Location : string );
  90. procedure LogStatus( StatusMessage : string; Location : string );
  91. published
  92. property ApplicationName : string read GetApplicationName;
  93. property ApplicationPath : string read GetApplicationPath;
  94. end;
  95. var
  96. Log : TLogger;
  97. implementation
  98. { TLogger }
  99. constructor TLogger.Create;
  100. var
  101. FileName : string;
  102. FileNo : integer;
  103. begin
  104. FApplicationName := ExtractFileName( ParamStr(0) );
  105. FApplicationPath := ExtractFilePath( ParamStr(0) );
  106. FileName := FApplicationPath + ChangeFileExt( FApplicationName, '.log' );
  107. FileNo := 0;
  108. while FileExists( FileName ) do
  109. begin
  110. inc( FileNo );
  111. FileName := FApplicationPath + IntToStr( FileNo ) + ChangeFileExt( FApplicationName, '.log' )
  112. end;
  113. AssignFile( FFileHandle, FileName );
  114. ReWrite( FFileHandle );
  115. (*inherited Create( FApplicationPath + ChangeFileExt( FApplicationName, '.log' ),
  116. fmCreate {$IFNDEF FPC}or fmShareExclusive{$ENDIF} );*)
  117. end;
  118. destructor TLogger.Destroy;
  119. begin
  120. CloseFile( FFileHandle );
  121. inherited;
  122. end;
  123. function TLogger.GetApplicationName: string;
  124. begin
  125. result := FApplicationName;
  126. end;
  127. function TLogger.GetApplicationPath: string;
  128. begin
  129. result := FApplicationPath;
  130. end;
  131. procedure TLogger.LogError(ErrorMessage, Location: string);
  132. var
  133. S : string;
  134. begin
  135. S := '*** ERROR *** : @ ' + TimeToStr(Time) + ' MSG : ' + ErrorMessage + ' IN : ' + Location + #13#10;
  136. WriteLn( FFileHandle, S );
  137. Flush( FFileHandle );
  138. end;
  139. procedure TLogger.LogStatus(StatusMessage, Location: string);
  140. var
  141. S : string;
  142. begin
  143. S := 'STATUS INFO : @ ' + TimeToStr(Time) + ' MSG : ' + StatusMessage + ' IN : ' + Location + #13#10;
  144. WriteLn( FFileHandle, S );
  145. Flush( FFileHandle );
  146. end;
  147. procedure TLogger.LogWarning(WarningMessage, Location: string);
  148. var
  149. S : string;
  150. begin
  151. S := '=== WARNING === : @ ' + TimeToStr(Time) + ' MSG : ' + WarningMessage + ' IN : ' + Location + #13#10;
  152. WriteLn( FFileHandle, S );
  153. Flush( FFileHandle );
  154. end;
  155. initialization
  156. begin
  157. Log := TLogger.Create;
  158. Log.LogStatus( 'Starting Application', 'Initialization' );
  159. end;
  160. finalization
  161. begin
  162. Log.LogStatus( 'Terminating Application', 'Finalization' );
  163. Log.Free;
  164. Log := nil;
  165. end;
  166. end.