TLevelUnit.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 TLevel class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TLevelUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. SysUtils;
  26. {*-------------------------------------------------------------------------
  27. The minimum value that an integer can hold: -2147483648.
  28. -------------------------------------------------------------------------}
  29. const MIN_VALUE = Low(LongInt);
  30. {*-------------------------------------------------------------------------
  31. The mmaximum value that an integer can hold: 2147483647.
  32. -------------------------------------------------------------------------}
  33. const MAX_VALUE = High(LongInt);
  34. {*-------------------------------------------------------------------------
  35. Integer value for OFF: MAX_VALUE
  36. -------------------------------------------------------------------------}
  37. const OFF_INT = MAX_VALUE;
  38. {*-------------------------------------------------------------------------
  39. Integer value for FATAL: 50000
  40. -------------------------------------------------------------------------}
  41. const FATAL_INT = 60000;
  42. {*-------------------------------------------------------------------------
  43. Integer value for ERROR: 40000
  44. -------------------------------------------------------------------------}
  45. const ERROR_INT = 50000;
  46. {*-------------------------------------------------------------------------
  47. Integer value for WARN: 30000
  48. -------------------------------------------------------------------------}
  49. const WARN_INT = 40000;
  50. {*-------------------------------------------------------------------------
  51. Integer value for INFO: 20000
  52. -------------------------------------------------------------------------}
  53. const INFO_INT = 30000;
  54. {*-------------------------------------------------------------------------
  55. Integer value for DEBUG: 10000
  56. -------------------------------------------------------------------------}
  57. const DEBUG_INT = 20000;
  58. {*-------------------------------------------------------------------------
  59. Integer value for DEBUG: 10000
  60. -------------------------------------------------------------------------}
  61. const TRACE_INT = 10000;
  62. {*-------------------------------------------------------------------------
  63. Integer value for ALL: MIN_VALUE
  64. -------------------------------------------------------------------------}
  65. const ALL_INT = MIN_VALUE;
  66. type
  67. {*----------------------------------------------------------------------------
  68. Defines the minimum set of levels recognized by the system, that is
  69. <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>, <code>WARN</code>,
  70. <code>INFO</code>, <code>DEBUG</code> and <code>ALL</code>.
  71. The <code>Level</code> class may be subclassed to define a larger level set.
  72. ----------------------------------------------------------------------------}
  73. TLevel = class (TObject)
  74. private
  75. FLevelValue : Integer;
  76. FName : String;
  77. constructor Create(const ALevel : Integer; const AName : String);
  78. protected
  79. public
  80. function Equals(ALevel : TLevel) : Boolean;
  81. function IsGreaterOrEqual(ALevel : TLevel) : Boolean;
  82. function ToString() : String;
  83. function IntValue() : Integer;
  84. end;
  85. function toLevel(AString : String) : TLevel;
  86. var
  87. {*-------------------------------------------------------------------------
  88. OFF has the highest possible rank and is intended to turn off logging.
  89. -------------------------------------------------------------------------}
  90. OFF : TLevel;
  91. {*-------------------------------------------------------------------------
  92. FATAL designates very severe error events that will presumably lead
  93. the application to abort.
  94. -------------------------------------------------------------------------}
  95. FATAL : TLevel;
  96. {*-------------------------------------------------------------------------
  97. ERROR designates error events that usually still allow the application
  98. to continue running normally.
  99. -------------------------------------------------------------------------}
  100. ERROR : TLevel;
  101. {*-------------------------------------------------------------------------
  102. WARN designates potentially harmful situations.
  103. -------------------------------------------------------------------------}
  104. WARN : TLevel;
  105. {*-------------------------------------------------------------------------
  106. INFO designates informational messages that highlight the progress of
  107. the application at coarse-grained level.
  108. -------------------------------------------------------------------------}
  109. INFO : TLevel;
  110. {*-------------------------------------------------------------------------
  111. DEBUG designates fine-grained informational events that are most useful
  112. to debug an application.
  113. -------------------------------------------------------------------------}
  114. DEBUG : TLevel;
  115. {*-------------------------------------------------------------------------
  116. TRACE designates fine-grained informational events that are most useful
  117. to debug an application.
  118. -------------------------------------------------------------------------}
  119. TRACE : TLevel;
  120. {*-------------------------------------------------------------------------
  121. ALL has the lowest possible rank and is intended to turn on all logging.
  122. -------------------------------------------------------------------------}
  123. ALL : TLevel;
  124. implementation
  125. {*----------------------------------------------------------------------------
  126. Returns the level instance whose name matches the given string.
  127. @param AString The name of the level to look for
  128. @return The level matching the given name, Nil if not found
  129. ----------------------------------------------------------------------------}
  130. function ToLevel(AString : String) : TLevel;
  131. begin
  132. Result := Nil;
  133. if (CompareText(AString, 'OFF') = 0) then result := OFF;
  134. if (CompareText(AString, 'FATAL') = 0) then result := FATAL;
  135. if (CompareText(AString, 'ERROR') = 0) then result := ERROR;
  136. if (CompareText(AString, 'WARN') = 0) then result := WARN;
  137. if (CompareText(AString, 'INFO') = 0) then result := INFO;
  138. if (CompareText(AString, 'DEBUG') = 0) then result := DEBUG;
  139. if (CompareText(AString, 'TRACE') = 0) then result := TRACE;
  140. if (CompareText(AString, 'ALL') = 0) then result := ALL;
  141. end;
  142. {*----------------------------------------------------------------------------
  143. Instantiate a Level object.
  144. @param ALevel The integer value of the level
  145. @param AName The name of the level
  146. ----------------------------------------------------------------------------}
  147. constructor TLevel.Create(const ALevel : Integer; const AName : String);
  148. begin
  149. inherited Create;
  150. Self.FLevelValue := ALevel;
  151. Self.FName := AName;
  152. end;
  153. {*----------------------------------------------------------------------------
  154. Two levels are equal if their integer values are equal.
  155. @param ALevel The level to compare with
  156. @return True if the given level is equal to this one, false otherwise
  157. ----------------------------------------------------------------------------}
  158. function TLevel.Equals(ALevel : TLevel) : Boolean;
  159. begin
  160. Result := (Self.FLevelValue = ALevel.FLevelValue);
  161. end;
  162. {*----------------------------------------------------------------------------
  163. Returns true if this level has a higher or equal integer value than the
  164. level passed as argument, false otherwise.
  165. @param ALevel The level to compare with
  166. @return True if given level is greater than or equal to this one, false
  167. otherwise
  168. ----------------------------------------------------------------------------}
  169. function TLevel.IsGreaterOrEqual(ALevel : TLevel) : Boolean;
  170. begin
  171. if (ALevel = Nil) then
  172. raise Exception.Create('Level is Nil');
  173. Result := (Self.FLevelValue >= ALevel.FLevelValue);
  174. end;
  175. {*----------------------------------------------------------------------------
  176. Returns the string representation of this level.
  177. @return String representation
  178. ----------------------------------------------------------------------------}
  179. function TLevel.ToString() : String;
  180. begin
  181. Result := Self.FName;
  182. end;
  183. {*----------------------------------------------------------------------------
  184. Returns the integer representation of this level.
  185. @return Integer representation
  186. ----------------------------------------------------------------------------}
  187. function TLevel.IntValue() : Integer;
  188. begin
  189. Result := Self.FLevelValue;
  190. end;
  191. {*----------------------------------------------------------------------------
  192. Initialize the default level set.
  193. ----------------------------------------------------------------------------}
  194. initialization
  195. begin
  196. OFF := TLevel.Create(OFF_INT, 'OFF');
  197. FATAL := TLevel.Create(FATAL_INT, 'FATAL');
  198. ERROR := TLevel.Create(ERROR_INT, 'ERROR');
  199. WARN := TLevel.Create(WARN_INT, 'WARN');
  200. INFO := TLevel.Create(INFO_INT, 'INFO');
  201. DEBUG := TLevel.Create(DEBUG_INT, 'DEBUG');
  202. TRACE := TLevel.Create(TRACE_INT, 'TRACE');
  203. ALL := TLevel.Create(ALL_INT, 'ALL');
  204. end;
  205. {*----------------------------------------------------------------------------
  206. Free the default level set.
  207. ----------------------------------------------------------------------------}
  208. finalization
  209. begin
  210. OFF.Free;
  211. FATAL.Free;
  212. ERROR.Free;
  213. WARN.Free;
  214. INFO.Free;
  215. DEBUG.Free;
  216. TRACE.Free;
  217. ALL.Free;
  218. end;
  219. end.