PatternConverter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System.Text;
  20. using System.IO;
  21. using System.Collections;
  22. using log4net.Util;
  23. using log4net.Repository;
  24. namespace log4net.Util
  25. {
  26. /// <summary>
  27. /// Abstract class that provides the formatting functionality that
  28. /// derived classes need.
  29. /// </summary>
  30. /// <remarks>
  31. /// <para>
  32. /// Conversion specifiers in a conversion patterns are parsed to
  33. /// individual PatternConverters. Each of which is responsible for
  34. /// converting a logging event in a converter specific manner.
  35. /// </para>
  36. /// </remarks>
  37. /// <author>Nicko Cadell</author>
  38. /// <author>Gert Driesen</author>
  39. public abstract class PatternConverter
  40. {
  41. #region Protected Instance Constructors
  42. /// <summary>
  43. /// Protected constructor
  44. /// </summary>
  45. /// <remarks>
  46. /// <para>
  47. /// Initializes a new instance of the <see cref="PatternConverter" /> class.
  48. /// </para>
  49. /// </remarks>
  50. protected PatternConverter()
  51. {
  52. }
  53. #endregion Protected Instance Constructors
  54. #region Public Instance Properties
  55. /// <summary>
  56. /// Get the next pattern converter in the chain
  57. /// </summary>
  58. /// <value>
  59. /// the next pattern converter in the chain
  60. /// </value>
  61. /// <remarks>
  62. /// <para>
  63. /// Get the next pattern converter in the chain
  64. /// </para>
  65. /// </remarks>
  66. public virtual PatternConverter Next
  67. {
  68. get { return m_next; }
  69. }
  70. /// <summary>
  71. /// Gets or sets the formatting info for this converter
  72. /// </summary>
  73. /// <value>
  74. /// The formatting info for this converter
  75. /// </value>
  76. /// <remarks>
  77. /// <para>
  78. /// Gets or sets the formatting info for this converter
  79. /// </para>
  80. /// </remarks>
  81. public virtual FormattingInfo FormattingInfo
  82. {
  83. get { return new FormattingInfo(m_min, m_max, m_leftAlign); }
  84. set
  85. {
  86. m_min = value.Min;
  87. m_max = value.Max;
  88. m_leftAlign = value.LeftAlign;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the option value for this converter
  93. /// </summary>
  94. /// <summary>
  95. /// The option for this converter
  96. /// </summary>
  97. /// <remarks>
  98. /// <para>
  99. /// Gets or sets the option value for this converter
  100. /// </para>
  101. /// </remarks>
  102. public virtual string Option
  103. {
  104. get { return m_option; }
  105. set { m_option = value; }
  106. }
  107. #endregion Public Instance Properties
  108. #region Protected Abstract Methods
  109. /// <summary>
  110. /// Evaluate this pattern converter and write the output to a writer.
  111. /// </summary>
  112. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  113. /// <param name="state">The state object on which the pattern converter should be executed.</param>
  114. /// <remarks>
  115. /// <para>
  116. /// Derived pattern converters must override this method in order to
  117. /// convert conversion specifiers in the appropriate way.
  118. /// </para>
  119. /// </remarks>
  120. abstract protected void Convert(TextWriter writer, object state);
  121. #endregion Protected Abstract Methods
  122. #region Public Instance Methods
  123. /// <summary>
  124. /// Set the next pattern converter in the chains
  125. /// </summary>
  126. /// <param name="patternConverter">the pattern converter that should follow this converter in the chain</param>
  127. /// <returns>the next converter</returns>
  128. /// <remarks>
  129. /// <para>
  130. /// The PatternConverter can merge with its neighbor during this method (or a sub class).
  131. /// Therefore the return value may or may not be the value of the argument passed in.
  132. /// </para>
  133. /// </remarks>
  134. public virtual PatternConverter SetNext(PatternConverter patternConverter)
  135. {
  136. m_next = patternConverter;
  137. return m_next;
  138. }
  139. /// <summary>
  140. /// Write the pattern converter to the writer with appropriate formatting
  141. /// </summary>
  142. /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
  143. /// <param name="state">The state object on which the pattern converter should be executed.</param>
  144. /// <remarks>
  145. /// <para>
  146. /// This method calls <see cref="Convert"/> to allow the subclass to perform
  147. /// appropriate conversion of the pattern converter. If formatting options have
  148. /// been specified via the <see cref="FormattingInfo"/> then this method will
  149. /// apply those formattings before writing the output.
  150. /// </para>
  151. /// </remarks>
  152. virtual public void Format(TextWriter writer, object state)
  153. {
  154. if (m_min < 0 && m_max == int.MaxValue)
  155. {
  156. // Formatting options are not in use
  157. Convert(writer, state);
  158. }
  159. else
  160. {
  161. string msg = null;
  162. int len;
  163. lock (m_formatWriter)
  164. {
  165. m_formatWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
  166. Convert(m_formatWriter, state);
  167. StringBuilder buf = m_formatWriter.GetStringBuilder();
  168. len = buf.Length;
  169. if (len > m_max)
  170. {
  171. msg = buf.ToString(len - m_max, m_max);
  172. len = m_max;
  173. }
  174. else
  175. {
  176. msg = buf.ToString();
  177. }
  178. }
  179. if (len < m_min)
  180. {
  181. if (m_leftAlign)
  182. {
  183. writer.Write(msg);
  184. SpacePad(writer, m_min - len);
  185. }
  186. else
  187. {
  188. SpacePad(writer, m_min - len);
  189. writer.Write(msg);
  190. }
  191. }
  192. else
  193. {
  194. writer.Write(msg);
  195. }
  196. }
  197. }
  198. private static readonly string[] SPACES = { " ", " ", " ", " ", // 1,2,4,8 spaces
  199. " ", // 16 spaces
  200. " " }; // 32 spaces
  201. /// <summary>
  202. /// Fast space padding method.
  203. /// </summary>
  204. /// <param name="writer"><see cref="TextWriter" /> to which the spaces will be appended.</param>
  205. /// <param name="length">The number of spaces to be padded.</param>
  206. /// <remarks>
  207. /// <para>
  208. /// Fast space padding method.
  209. /// </para>
  210. /// </remarks>
  211. protected static void SpacePad(TextWriter writer, int length)
  212. {
  213. while(length >= 32)
  214. {
  215. writer.Write(SPACES[5]);
  216. length -= 32;
  217. }
  218. for(int i = 4; i >= 0; i--)
  219. {
  220. if ((length & (1<<i)) != 0)
  221. {
  222. writer.Write(SPACES[i]);
  223. }
  224. }
  225. }
  226. #endregion Public Instance Methods
  227. #region Private Instance Fields
  228. private PatternConverter m_next;
  229. private int m_min = -1;
  230. private int m_max = int.MaxValue;
  231. private bool m_leftAlign = false;
  232. /// <summary>
  233. /// The option string to the converter
  234. /// </summary>
  235. private string m_option = null;
  236. private ReusableStringWriter m_formatWriter = new ReusableStringWriter(System.Globalization.CultureInfo.InvariantCulture);
  237. #endregion Private Instance Fields
  238. #region Constants
  239. /// <summary>
  240. /// Initial buffer size
  241. /// </summary>
  242. private const int c_renderBufferSize = 256;
  243. /// <summary>
  244. /// Maximum buffer size before it is recycled
  245. /// </summary>
  246. private const int c_renderBufferMaxCapacity = 1024;
  247. #endregion
  248. #region Static Methods
  249. /// <summary>
  250. /// Write an dictionary to a <see cref="TextWriter"/>
  251. /// </summary>
  252. /// <param name="writer">the writer to write to</param>
  253. /// <param name="repository">a <see cref="ILoggerRepository"/> to use for object conversion</param>
  254. /// <param name="value">the value to write to the writer</param>
  255. /// <remarks>
  256. /// <para>
  257. /// Writes the <see cref="IDictionary"/> to a writer in the form:
  258. /// </para>
  259. /// <code>
  260. /// {key1=value1, key2=value2, key3=value3}
  261. /// </code>
  262. /// <para>
  263. /// If the <see cref="ILoggerRepository"/> specified
  264. /// is not null then it is used to render the key and value to text, otherwise
  265. /// the object's ToString method is called.
  266. /// </para>
  267. /// </remarks>
  268. protected static void WriteDictionary(TextWriter writer, ILoggerRepository repository, IDictionary value)
  269. {
  270. WriteDictionary(writer, repository, value.GetEnumerator());
  271. }
  272. /// <summary>
  273. /// Write an dictionary to a <see cref="TextWriter"/>
  274. /// </summary>
  275. /// <param name="writer">the writer to write to</param>
  276. /// <param name="repository">a <see cref="ILoggerRepository"/> to use for object conversion</param>
  277. /// <param name="value">the value to write to the writer</param>
  278. /// <remarks>
  279. /// <para>
  280. /// Writes the <see cref="IDictionaryEnumerator"/> to a writer in the form:
  281. /// </para>
  282. /// <code>
  283. /// {key1=value1, key2=value2, key3=value3}
  284. /// </code>
  285. /// <para>
  286. /// If the <see cref="ILoggerRepository"/> specified
  287. /// is not null then it is used to render the key and value to text, otherwise
  288. /// the object's ToString method is called.
  289. /// </para>
  290. /// </remarks>
  291. protected static void WriteDictionary(TextWriter writer, ILoggerRepository repository, IDictionaryEnumerator value)
  292. {
  293. writer.Write("{");
  294. bool first = true;
  295. // Write out all the dictionary key value pairs
  296. while (value.MoveNext())
  297. {
  298. if (first)
  299. {
  300. first = false;
  301. }
  302. else
  303. {
  304. writer.Write(", ");
  305. }
  306. WriteObject(writer, repository, value.Key);
  307. writer.Write("=");
  308. WriteObject(writer, repository, value.Value);
  309. }
  310. writer.Write("}");
  311. }
  312. /// <summary>
  313. /// Write an object to a <see cref="TextWriter"/>
  314. /// </summary>
  315. /// <param name="writer">the writer to write to</param>
  316. /// <param name="repository">a <see cref="ILoggerRepository"/> to use for object conversion</param>
  317. /// <param name="value">the value to write to the writer</param>
  318. /// <remarks>
  319. /// <para>
  320. /// Writes the Object to a writer. If the <see cref="ILoggerRepository"/> specified
  321. /// is not null then it is used to render the object to text, otherwise
  322. /// the object's ToString method is called.
  323. /// </para>
  324. /// </remarks>
  325. protected static void WriteObject(TextWriter writer, ILoggerRepository repository, object value)
  326. {
  327. if (repository != null)
  328. {
  329. repository.RendererMap.FindAndRender(value, writer);
  330. }
  331. else
  332. {
  333. // Don't have a repository to render with so just have to rely on ToString
  334. if (value == null)
  335. {
  336. writer.Write( SystemInfo.NullText );
  337. }
  338. else
  339. {
  340. writer.Write( value.ToString() );
  341. }
  342. }
  343. }
  344. #endregion
  345. private PropertiesDictionary properties;
  346. /// <summary>
  347. ///
  348. /// </summary>
  349. public PropertiesDictionary Properties
  350. {
  351. get { return properties; }
  352. set { properties = value; }
  353. }
  354. }
  355. }