logger.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. unit logger;
  2. {
  3. $Id: logger.pas,v 1.1 2004/02/05 00:08:20 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.1 2004/02/05 00:08:20 savage
  66. Module 1.0 release
  67. }
  68. {$I jedi-sdl.inc}
  69. {$WEAKPACKAGEUNIT OFF}
  70. interface
  71. uses
  72. Classes,
  73. SysUtils;
  74. type
  75. TLogger = class
  76. private
  77. FFileHandle : TextFile;
  78. FApplicationName : string;
  79. FApplicationPath : string;
  80. protected
  81. public
  82. constructor Create;
  83. destructor Destroy; override;
  84. function GetApplicationName: string;
  85. function GetApplicationPath: string;
  86. procedure LogError( ErrorMessage : string; Location : string );
  87. procedure LogWarning( WarningMessage : string; Location : string );
  88. procedure LogStatus( StatusMessage : string; Location : string );
  89. published
  90. property ApplicationName : string read GetApplicationName;
  91. property ApplicationPath : string read GetApplicationPath;
  92. end;
  93. var
  94. Log : TLogger;
  95. implementation
  96. { TLogger }
  97. constructor TLogger.Create;
  98. begin
  99. FApplicationName := ExtractFileName( ParamStr(0) );
  100. FApplicationPath := ExtractFilePath( ParamStr(0) );
  101. AssignFile( FFileHandle, FApplicationPath + ChangeFileExt( FApplicationName, '.log' ) );
  102. ReWrite( FFileHandle );
  103. (*inherited Create( FApplicationPath + ChangeFileExt( FApplicationName, '.log' ),
  104. fmCreate {$IFNDEF FPC}or fmShareExclusive{$ENDIF} );*)
  105. end;
  106. destructor TLogger.Destroy;
  107. begin
  108. CloseFile( FFileHandle );
  109. inherited;
  110. end;
  111. function TLogger.GetApplicationName: string;
  112. begin
  113. result := FApplicationName;
  114. end;
  115. function TLogger.GetApplicationPath: string;
  116. begin
  117. result := FApplicationPath;
  118. end;
  119. procedure TLogger.LogError(ErrorMessage, Location: string);
  120. var
  121. S : string;
  122. begin
  123. S := '*** ERROR *** : @ ' + TimeToStr(Time) + ' MSG : ' + ErrorMessage + ' IN : ' + Location + #13#10;
  124. WriteLn( FFileHandle, S );
  125. Flush( FFileHandle );
  126. end;
  127. procedure TLogger.LogStatus(StatusMessage, Location: string);
  128. var
  129. S : string;
  130. begin
  131. S := 'STATUS INFO : @ ' + TimeToStr(Time) + ' MSG : ' + StatusMessage + ' IN : ' + Location + #13#10;
  132. WriteLn( FFileHandle, S );
  133. Flush( FFileHandle );
  134. end;
  135. procedure TLogger.LogWarning(WarningMessage, Location: string);
  136. var
  137. S : string;
  138. begin
  139. S := '=== WARNING === : @ ' + TimeToStr(Time) + ' MSG : ' + WarningMessage + ' IN : ' + Location + #13#10;
  140. WriteLn( FFileHandle, S );
  141. Flush( FFileHandle );
  142. end;
  143. initialization
  144. begin
  145. Log := TLogger.Create;
  146. Log.LogStatus( 'Starting Application', 'Initialization' );
  147. end;
  148. finalization
  149. begin
  150. Log.LogStatus( 'Terminating Application', 'Finalization' );
  151. Log.Free;
  152. Log := nil;
  153. end;
  154. end.