logger.pas 7.8 KB

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