TPatternLayoutUnit.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 TPatternLayout class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TPatternLayoutUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. TLayoutUnit, TLoggingEventUnit;
  26. type
  27. {*----------------------------------------------------------------------------
  28. The goal of this class is to format a LoggingEvent and return the results
  29. as a String. The results depend on the conversion pattern. See the
  30. Log4Delphi User Guide for more information on using this class.
  31. ----------------------------------------------------------------------------}
  32. TPatternLayout = class (TLayout)
  33. private
  34. FFormatString : String;
  35. FDateFormatString : String;
  36. public
  37. constructor Create(); Overload;
  38. constructor Create(const APattern : String); Overload;
  39. function Format(AEvent : TLoggingEvent) : String; Override;
  40. function IgnoresException() : Boolean; Override;
  41. end;
  42. implementation
  43. uses
  44. SysUtils, TStringUnit, TLogLogUnit;
  45. {*----------------------------------------------------------------------------
  46. Find the first alpha character (a..z,A..Z) in the given string from the
  47. specified offset.
  48. @param str The string to search
  49. @param index The offset to search from
  50. ----------------------------------------------------------------------------}
  51. function FirstAlphaCharacter(str : String; index : Integer) : Integer;
  52. var
  53. i : Integer;
  54. var tmp : STring;
  55. begin
  56. tmp := Copy(Str, Index+1, (System.Length(str) - Index + 1));
  57. for i := 1 to Length(tmp) do
  58. if (tmp[i] in ['a'..'z','A'..'Z']) then begin
  59. Result := i + index;
  60. exit;
  61. end;
  62. Result := -1;
  63. end;
  64. {*----------------------------------------------------------------------------
  65. Instantiate a Pattern Layout using the default pattern: "%m%n".
  66. ----------------------------------------------------------------------------}
  67. constructor TPatternLayout.Create();
  68. begin
  69. Self.Create('%m%n');
  70. end;
  71. {*----------------------------------------------------------------------------
  72. Instantiate a Pattern Layout using the given pattern string.
  73. @param APattern The pattern to use
  74. ----------------------------------------------------------------------------}
  75. constructor TPatternLayout.Create(const APattern : String);
  76. var
  77. index : Integer;
  78. charIndex : Integer;
  79. pIndex : Integer;
  80. oldtok, newtok : String;
  81. begin
  82. inherited Create;
  83. if (APattern <> '') then
  84. FFormatString := APattern
  85. else
  86. FFormatString := '%m%n';
  87. // loop through all conversion specifiers
  88. index := TStringUnit.IndexOf(FFormatString, '%', 0);
  89. while (index >= 0) do begin
  90. // find end of specifier
  91. charIndex := FirstAlphaCharacter(FFormatString, index);
  92. case FFormatString[charIndex] of
  93. 'n' : begin
  94. FFormatString :=
  95. StringReplace(FFormatString,'%n',#13#10, [rfReplaceAll]);
  96. end;
  97. 'm' : begin
  98. oldtok := Copy(FFormatString, index+1, charindex-index+1);
  99. newtok := StringReplace(oldtok,'m','s',[rfReplaceAll]);
  100. insert('0:',newtok,2);
  101. FFormatString :=
  102. StringReplace(FFormatString, oldtok, newtok, [rfReplaceAll]);
  103. end;
  104. 'p' : begin
  105. oldtok := Copy(FFormatString, index+1, charindex-index+1);
  106. newtok := StringReplace(oldtok,'p','s',[rfReplaceAll]);
  107. insert('1:',newtok,2);
  108. FFormatString :=
  109. StringReplace(FFormatString, oldtok, newtok, [rfReplaceAll]);
  110. end;
  111. 'e' : begin
  112. oldtok := Copy(FFormatString, index+1, charindex-index+1);
  113. newtok := StringReplace(oldtok,'e','s',[rfReplaceAll]);
  114. insert('2:',newtok,2);
  115. FFormatString :=
  116. StringReplace(FFormatString, oldtok, newtok, [rfReplaceAll]);
  117. end;
  118. 'L' : begin
  119. oldtok := Copy(FFormatString, index+1, charindex-index+1);
  120. newtok := StringReplace(oldtok,'L','s',[rfReplaceAll]);
  121. insert('3:',newtok,2);
  122. FFormatString :=
  123. StringReplace(FFormatString, oldtok, newtok, [rfReplaceAll]);
  124. end;
  125. 'd' : begin
  126. FDateFormatString := '';
  127. if (FFormatString[charIndex+1] = '{') then begin
  128. pIndex := IndexOf(FFormatString,'}',charIndex+1);
  129. if (pIndex >= 0) then begin
  130. FDateFormatString :=
  131. Copy(FFormatString,charIndex+2,pIndex-charIndex-1);
  132. FFormatString := StringReplace(FFormatString, '%d{'
  133. + FDateFormatString+'}', '%d', [rfReplaceAll]);
  134. end;
  135. end;
  136. end;
  137. end;
  138. index := IndexOf(FFormatString, '%', index+1);
  139. end;
  140. TLogLog.debug('TPatternLayout#Create');
  141. end;
  142. {*----------------------------------------------------------------------------
  143. Returns the log statement formatted using the pattern string.
  144. @param AEvent The event to format
  145. @return The event formatted using the pattern
  146. ----------------------------------------------------------------------------}
  147. function TPatternLayout.Format(AEvent : TLoggingEvent) : String;
  148. var
  149. ex : String;
  150. begin
  151. if (AEvent.GetException <> Nil) then
  152. ex := AEVEnt.GetException.ClassName + ': ' + AEvent.GetException.Message
  153. else
  154. ex := '';
  155. Result := StringReplace(FFormatString, '%d',
  156. FormatDateTime(FDateFormatString, AEvent.GetStartTime), [rfReplaceAll]);
  157. Result := SysUtils.Format(Result, [AEvent.GetMessage,
  158. AEvent.GetLevel.ToString, ex, AEvent.GetLogger]);
  159. end;
  160. {*----------------------------------------------------------------------------
  161. Determines if this layout ignores exceptions. This layout does not
  162. ignore exceptions.
  163. @return False
  164. ----------------------------------------------------------------------------}
  165. function TPatternLayout.IgnoresException() : Boolean;
  166. begin
  167. Result := false;
  168. end;
  169. end.