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