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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#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;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
 
using log4net.Layout;
using log4net.Core;
using log4net.DateFormatter;
using log4net.Layout.Pattern;
using log4net.Util;
 
namespace log4net.Util
{
    /// <summary>
    /// Most of the work of the <see cref="PatternLayout"/> class
    /// is delegated to the PatternParser class.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The <c>PatternParser</c> processes a pattern string and
    /// returns a chain of <see cref="PatternConverter"/> objects.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public sealed class PatternParser
    {
        #region Public Instance Constructors
 
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="pattern">The pattern to parse.</param>
        /// <remarks>
        /// <para>
        /// Initializes a new instance of the <see cref="PatternParser" /> class 
        /// with the specified pattern string.
        /// </para>
        /// </remarks>
        public PatternParser(string pattern) 
        {
            m_pattern = pattern;
        }
 
        #endregion Public Instance Constructors
 
        #region Public Instance Methods
 
        /// <summary>
        /// Parses the pattern into a chain of pattern converters.
        /// </summary>
        /// <returns>The head of a chain of pattern converters.</returns>
        /// <remarks>
        /// <para>
        /// Parses the pattern into a chain of pattern converters.
        /// </para>
        /// </remarks>
        public PatternConverter Parse()
        {
            string[] converterNamesCache = BuildCache();
 
            ParseInternal(m_pattern, converterNamesCache);
 
            return m_head;
        }
 
        #endregion Public Instance Methods
 
        #region Public Instance Properties
 
        /// <summary>
        /// Get the converter registry used by this parser
        /// </summary>
        /// <value>
        /// The converter registry used by this parser
        /// </value>
        /// <remarks>
        /// <para>
        /// Get the converter registry used by this parser
        /// </para>
        /// </remarks>
        public Hashtable PatternConverters
        {
            get { return m_patternConverters; }
        }
 
        #endregion Public Instance Properties
 
        #region Private Instance Methods
 
        /// <summary>
        /// Build the unified cache of converters from the static and instance maps
        /// </summary>
        /// <returns>the list of all the converter names</returns>
        /// <remarks>
        /// <para>
        /// Build the unified cache of converters from the static and instance maps
        /// </para>
        /// </remarks>
        private string[] BuildCache()
        {
            string[] converterNamesCache = new string[m_patternConverters.Keys.Count];
            m_patternConverters.Keys.CopyTo(converterNamesCache, 0);
 
            // sort array so that longer strings come first
            Array.Sort(converterNamesCache, 0, converterNamesCache.Length, StringLengthComparer.Instance);
 
            return converterNamesCache;
        }
 
        #region StringLengthComparer
 
        /// <summary>
        /// Sort strings by length
        /// </summary>
        /// <remarks>
        /// <para>
        /// <see cref="IComparer" /> that orders strings by string length.
        /// The longest strings are placed first
        /// </para>
        /// </remarks>
        private sealed class StringLengthComparer : IComparer
        {
            public static readonly StringLengthComparer Instance = new StringLengthComparer();
 
            private StringLengthComparer()
            {
            }
 
            #region Implementation of IComparer
 
            public int Compare(object x, object y)
            {
                string s1 = x as string;
                string s2 = y as string;
 
                if (s1 == null && s2 == null)
                {
                    return 0;
                }
                if (s1 == null)
                {
                    return 1;
                }
                if (s2 == null)
                {
                    return -1;
                }
 
                return s2.Length.CompareTo(s1.Length);
            }
        
            #endregion
        }
 
        #endregion // StringLengthComparer
 
        /// <summary>
        /// Internal method to parse the specified pattern to find specified matches
        /// </summary>
        /// <param name="pattern">the pattern to parse</param>
        /// <param name="matches">the converter names to match in the pattern</param>
        /// <remarks>
        /// <para>
        /// The matches param must be sorted such that longer strings come before shorter ones.
        /// </para>
        /// </remarks>
        private void ParseInternal(string pattern, string[] matches)
        {
            int offset = 0;
            while(offset < pattern.Length)
            {
                int i = pattern.IndexOf('%', offset);
                if (i < 0 || i == pattern.Length - 1)
                {
                    ProcessLiteral(pattern.Substring(offset));
                    offset = pattern.Length;
                }
                else
                {
                    if (pattern[i+1] == '%')
                    {
                        // Escaped
                        ProcessLiteral(pattern.Substring(offset, i - offset + 1));
                        offset = i + 2;
                    }
                    else
                    {
                        ProcessLiteral(pattern.Substring(offset, i - offset));
                        offset = i + 1;
 
                        FormattingInfo formattingInfo = new FormattingInfo();
 
                        // Process formatting options
 
                        // Look for the align flag
                        if (offset < pattern.Length)
                        {
                            if (pattern[offset] == '-')
                            {
                                // Seen align flag
                                formattingInfo.LeftAlign = true;
                                offset++;
                            }
                        }
                        // Look for the minimum length
                        while (offset < pattern.Length && char.IsDigit(pattern[offset]))
                        {
                            // Seen digit
                            if (formattingInfo.Min < 0)
                            {
                                formattingInfo.Min = 0;
                            }
                            formattingInfo.Min = (formattingInfo.Min * 10) + int.Parse(pattern[offset].ToString(CultureInfo.InvariantCulture), System.Globalization.NumberFormatInfo.InvariantInfo);
                            offset++;
                        }
                        // Look for the separator between min and max
                        if (offset < pattern.Length)
                        {
                            if (pattern[offset] == '.')
                            {
                                // Seen separator
                                offset++;
                            }
                        }
                        // Look for the maximum length
                        while (offset < pattern.Length && char.IsDigit(pattern[offset]))
                        {
                            // Seen digit
                            if (formattingInfo.Max == int.MaxValue)
                            {
                                formattingInfo.Max = 0;
                            }
                            formattingInfo.Max = (formattingInfo.Max * 10) + int.Parse(pattern[offset].ToString(CultureInfo.InvariantCulture), System.Globalization.NumberFormatInfo.InvariantInfo);
                            offset++;
                        }
 
                        int remainingStringLength = pattern.Length - offset;
 
                        // Look for pattern
                        for(int m=0; m<matches.Length; m++)
                        {
                            if (matches[m].Length <= remainingStringLength)
                            {
                                if (String.Compare(pattern, offset, matches[m], 0, matches[m].Length, false, System.Globalization.CultureInfo.InvariantCulture) == 0)
                                {
                                    // Found match
                                    offset = offset + matches[m].Length;
 
                                    string option = null;
 
                                    // Look for option
                                    if (offset < pattern.Length)
                                    {
                                        if (pattern[offset] == '{')
                                        {
                                            // Seen option start
                                            offset++;
                                            
                                            int optEnd = pattern.IndexOf('}', offset);
                                            if (optEnd < 0)
                                            {
                                                // error
                                            }
                                            else
                                            {
                                                option = pattern.Substring(offset, optEnd - offset);
                                                offset = optEnd + 1;
                                            }
                                        }
                                    }
 
                                    ProcessConverter(matches[m], option, formattingInfo);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// Process a parsed literal
        /// </summary>
        /// <param name="text">the literal text</param>
        private void ProcessLiteral(string text)
        {
            if (text.Length > 0)
            {
                // Convert into a pattern
                ProcessConverter("literal", text, new FormattingInfo());
            }
        }
 
        /// <summary>
        /// Process a parsed converter pattern
        /// </summary>
        /// <param name="converterName">the name of the converter</param>
        /// <param name="option">the optional option for the converter</param>
        /// <param name="formattingInfo">the formatting info for the converter</param>
        private void ProcessConverter(string converterName, string option, FormattingInfo formattingInfo)
        {
            LogLog.Debug(declaringType, "Converter ["+converterName+"] Option ["+option+"] Format [min="+formattingInfo.Min+",max="+formattingInfo.Max+",leftAlign="+formattingInfo.LeftAlign+"]");
 
            // Lookup the converter type
            ConverterInfo converterInfo = (ConverterInfo)m_patternConverters[converterName];
            if (converterInfo == null)
            {
                LogLog.Error(declaringType, "Unknown converter name ["+converterName+"] in conversion pattern.");
            }
            else
            {
                // Create the pattern converter
                PatternConverter pc = null;
                try
                {
                    pc = (PatternConverter)Activator.CreateInstance(converterInfo.Type);
                }
                catch(Exception createInstanceEx)
                {
                    LogLog.Error(declaringType, "Failed to create instance of Type [" + converterInfo.Type.FullName + "] using default constructor. Exception: " + createInstanceEx.ToString());
                }
 
                // formattingInfo variable is an instance variable, occasionally reset 
                // and used over and over again
                pc.FormattingInfo = formattingInfo;
                pc.Option = option;
                pc.Properties = converterInfo.Properties;
 
                IOptionHandler optionHandler = pc as IOptionHandler;
                if (optionHandler != null)
                {
                    optionHandler.ActivateOptions();
                }
 
                AddConverter(pc);
            }
        }
 
        /// <summary>
        /// Resets the internal state of the parser and adds the specified pattern converter 
        /// to the chain.
        /// </summary>
        /// <param name="pc">The pattern converter to add.</param>
        private void AddConverter(PatternConverter pc) 
        {
            // Add the pattern converter to the list.
 
            if (m_head == null) 
            {
                m_head = m_tail = pc;
            }
            else 
            {
                // Set the next converter on the tail
                // Update the tail reference
                // note that a converter may combine the 'next' into itself
                // and therefore the tail would not change!
                m_tail = m_tail.SetNext(pc);
            }
        }
 
        #endregion Protected Instance Methods
 
        #region Private Constants
 
        private const char ESCAPE_CHAR = '%';
  
        #endregion Private Constants
 
        #region Private Instance Fields
 
        /// <summary>
        /// The first pattern converter in the chain
        /// </summary>
        private PatternConverter m_head;
 
        /// <summary>
        ///  the last pattern converter in the chain
        /// </summary>
        private PatternConverter m_tail;
 
        /// <summary>
        /// The pattern
        /// </summary>
        private string m_pattern;
 
        /// <summary>
        /// Internal map of converter identifiers to converter types
        /// </summary>
        /// <remarks>
        /// <para>
        /// This map overrides the static s_globalRulesRegistry map.
        /// </para>
        /// </remarks>
        private Hashtable m_patternConverters = new Hashtable();
 
        #endregion Private Instance Fields
 
        #region Private Static Fields
 
        /// <summary>
        /// The fully qualified type of the PatternParser class.
        /// </summary>
        /// <remarks>
        /// Used by the internal logger to record the Type of the
        /// log message.
        /// </remarks>
        private readonly static Type declaringType = typeof(PatternParser);
 
        #endregion Private Static Fields
    }
}