TLogLogUnit.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. {
  2. Copyright 2005-2006 Log4Delphi Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. }
  13. {*----------------------------------------------------------------------------
  14. Contains the TLogLog class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TLogLogUnit;
  19. {$IFDEF fpc}
  20. {$MODE objfpc}
  21. {$H+}
  22. {$ENDIF}
  23. interface
  24. type
  25. {*----------------------------------------------------------------------------
  26. This class is used internally to perfrom logging within the Log4Delphi
  27. package. It is not meant to be used outside the Log4Delphi package.
  28. Typically statements are logged to a file named 'log4delphi.log'.
  29. ----------------------------------------------------------------------------}
  30. TLogLog = class
  31. public
  32. class procedure Debug(const AMsg: string);
  33. class procedure Error(const AMsg: string);
  34. class procedure Warn(const AMsg: string);
  35. class procedure Info(const AMsg: string);
  36. class procedure Fatal(const AMsg: string);
  37. class procedure SetQuietMode(const AMode: Boolean);
  38. end;
  39. procedure initialize(const fileName: string);
  40. procedure finalize();
  41. var
  42. isInit: Boolean = false;
  43. implementation
  44. var
  45. outFile: TextFile;
  46. quietMode: Boolean = false;
  47. {*----------------------------------------------------------------------------
  48. Initialize the internal logging of Log4Delphi. This method should not be
  49. called by application developers.
  50. @param fileName The name of the file to send output to
  51. ----------------------------------------------------------------------------}
  52. procedure initialize(const fileName: string);
  53. begin
  54. if isInit then finalize;
  55. if (not quietMode) then
  56. begin
  57. AssignFile(outFile, fileName);
  58. Rewrite(outFile);
  59. end;
  60. isInit := true;
  61. end;
  62. {*----------------------------------------------------------------------------
  63. Finalize the internal logging by releasing resources. This method should
  64. not be called by application developers.
  65. ----------------------------------------------------------------------------}
  66. procedure finalize();
  67. begin
  68. if ((isInit) and not (quietMode)) then
  69. CloseFile(outFile);
  70. isInit := false;
  71. end;
  72. {*----------------------------------------------------------------------------
  73. Send a debug message.
  74. @param AMsg The message to log
  75. ----------------------------------------------------------------------------}
  76. class procedure TLogLog.Debug(const AMsg: string);
  77. begin
  78. if ((isInit) and not (quietMode)) then
  79. writeln(outFile, 'DEBUG: ' + AMsg);
  80. end;
  81. {*----------------------------------------------------------------------------
  82. Send an error message.
  83. @param AMsg The message to log
  84. ----------------------------------------------------------------------------}
  85. class procedure TLogLog.Error(const AMsg: string);
  86. begin
  87. if ((isInit) and not (quietMode)) then
  88. writeln(outFile, 'ERROR: ' + AMsg);
  89. end;
  90. {*----------------------------------------------------------------------------
  91. Send a warn message.
  92. @param AMsg The message to log
  93. ----------------------------------------------------------------------------}
  94. class procedure TLogLog.Warn(const AMsg: string);
  95. begin
  96. if ((isInit) and not (quietMode)) then
  97. writeln(outFile, 'WARN: ' + AMsg);
  98. end;
  99. {*----------------------------------------------------------------------------
  100. Send an info message.
  101. @param AMsg The message to log
  102. ----------------------------------------------------------------------------}
  103. class procedure TLogLog.Info(const AMsg: string);
  104. begin
  105. if ((isInit) and not (quietMode)) then
  106. writeln(outFile, 'INFO: ' + AMsg);
  107. end;
  108. {*----------------------------------------------------------------------------
  109. Send a fatal message.
  110. @param AMsg The message to log
  111. ----------------------------------------------------------------------------}
  112. class procedure TLogLog.Fatal(const AMsg: string);
  113. begin
  114. if ((isInit) and not (quietMode)) then
  115. writeln(outFile, 'FATAL: ' + AMsg);
  116. end;
  117. {*----------------------------------------------------------------------------
  118. This method is used to turn internal logging off. It may be used later in
  119. a configuration setting to prevent internal logging in shipped code.
  120. @param AMode True if quiet and false otherwise
  121. ----------------------------------------------------------------------------}
  122. class procedure TLogLog.SetQuietMode(const AMode: Boolean);
  123. begin
  124. quietMode := AMode;
  125. end;
  126. end.