~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/Layout/Pattern/.svn/text-base/NamedPatternConverter.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
using System.Globalization;
21
 
using System.Text;
22
 
using System.IO;
23
 
 
24
 
using log4net.Core;
25
 
using log4net.Util;
26
 
 
27
 
namespace log4net.Layout.Pattern
28
 
{
29
 
        /// <summary>
30
 
        /// Converter to output and truncate <c>'.'</c> separated strings
31
 
        /// </summary>
32
 
        /// <remarks>
33
 
        /// <para>
34
 
        /// This abstract class supports truncating a <c>'.'</c> separated string
35
 
        /// to show a specified number of elements from the right hand side.
36
 
        /// This is used to truncate class names that are fully qualified.
37
 
        /// </para>
38
 
        /// <para>
39
 
        /// Subclasses should override the <see cref="GetFullyQualifiedName"/> method to
40
 
        /// return the fully qualified string.
41
 
        /// </para>
42
 
        /// </remarks>
43
 
        /// <author>Nicko Cadell</author>
44
 
        internal abstract class NamedPatternConverter : PatternLayoutConverter, IOptionHandler
45
 
        {
46
 
                protected int m_precision = 0;
47
 
 
48
 
                #region Implementation of IOptionHandler
49
 
 
50
 
                /// <summary>
51
 
                /// Initialize the converter 
52
 
                /// </summary>
53
 
                /// <remarks>
54
 
                /// <para>
55
 
                /// This is part of the <see cref="IOptionHandler"/> delayed object
56
 
                /// activation scheme. The <see cref="ActivateOptions"/> method must 
57
 
                /// be called on this object after the configuration properties have
58
 
                /// been set. Until <see cref="ActivateOptions"/> is called this
59
 
                /// object is in an undefined state and must not be used. 
60
 
                /// </para>
61
 
                /// <para>
62
 
                /// If any of the configuration properties are modified then 
63
 
                /// <see cref="ActivateOptions"/> must be called again.
64
 
                /// </para>
65
 
                /// </remarks>
66
 
                public void ActivateOptions()
67
 
                {
68
 
                        m_precision = 0;
69
 
 
70
 
                        if (Option != null) 
71
 
                        {
72
 
                                string optStr = Option.Trim();
73
 
                                if (optStr.Length > 0)
74
 
                                {
75
 
                                        int precisionVal;
76
 
                                        if (SystemInfo.TryParse(optStr, out precisionVal))
77
 
                                        {
78
 
                                                if (precisionVal <= 0) 
79
 
                                                {
80
 
                                                        LogLog.Error("NamedPatternConverter: Precision option (" + optStr + ") isn't a positive integer.");
81
 
                                                }
82
 
                                                else
83
 
                                                {
84
 
                                                        m_precision = precisionVal;
85
 
                                                }
86
 
                                        } 
87
 
                                        else
88
 
                                        {
89
 
                                                LogLog.Error("NamedPatternConverter: Precision option \"" + optStr + "\" not a decimal integer.");
90
 
                                        }
91
 
                                }
92
 
                        }
93
 
                }
94
 
 
95
 
                #endregion
96
 
 
97
 
                /// <summary>
98
 
                /// Get the fully qualified string data
99
 
                /// </summary>
100
 
                /// <param name="loggingEvent">the event being logged</param>
101
 
                /// <returns>the fully qualified name</returns>
102
 
                /// <remarks>
103
 
                /// <para>
104
 
                /// Overridden by subclasses to get the fully qualified name before the
105
 
                /// precision is applied to it.
106
 
                /// </para>
107
 
                /// <para>
108
 
                /// Return the fully qualified <c>'.'</c> (dot/period) separated string.
109
 
                /// </para>
110
 
                /// </remarks>
111
 
                abstract protected string GetFullyQualifiedName(LoggingEvent loggingEvent);
112
 
        
113
 
                /// <summary>
114
 
                /// Convert the pattern to the rendered message
115
 
                /// </summary>
116
 
                /// <param name="writer"><see cref="TextWriter" /> that will receive the formatted result.</param>
117
 
                /// <param name="loggingEvent">the event being logged</param>
118
 
                /// <remarks>
119
 
                /// Render the <see cref="GetFullyQualifiedName"/> to the precision
120
 
                /// specified by the <see cref="PatternConverter.Option"/> property.
121
 
                /// </remarks>
122
 
                override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
123
 
                {
124
 
                        string name = GetFullyQualifiedName(loggingEvent);
125
 
                        if (m_precision <= 0)
126
 
                        {
127
 
                                writer.Write(name);
128
 
                        }
129
 
                        else 
130
 
                        {
131
 
                                int len = name.Length;
132
 
 
133
 
                                // We subtract 1 from 'len' when assigning to 'end' to avoid out of
134
 
                                // bounds exception in return name.Substring(end+1, len). This can happen if
135
 
                                // precision is 1 and the logger name ends with a dot. 
136
 
                                int end = len - 1;
137
 
                                for(int i=m_precision; i>0; i--) 
138
 
                                {         
139
 
                                        end = name.LastIndexOf('.', end-1);
140
 
                                        if (end == -1)
141
 
                                        {
142
 
                                                writer.Write(name);
143
 
                                                return;
144
 
                                        }
145
 
                                }
146
 
                                writer.Write(name.Substring(end+1, len-end-1));
147
 
                        }         
148
 
                }
149
 
        }
150
 
}