• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
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.Collections.Generic;
21 using System.Globalization;
22 using System.IO;
23 using Grpc.Core.Utils;
24 
25 namespace Grpc.Core.Logging
26 {
27     /// <summary>Logger that logs to an arbitrary <c>System.IO.TextWriter</c>.</summary>
28     public class TextWriterLogger : ILogger
29     {
30         // Format similar enough to C core log format except nanosecond precision is not supported.
31         const string DateTimeFormatString = "MMdd HH:mm:ss.ffffff";
32 
33         readonly Func<TextWriter> textWriterProvider;
34         readonly Type forType;
35         readonly string forTypeString;
36 
37         /// <summary>
38         /// Creates a console logger not associated to any specific type and writes to given <c>System.IO.TextWriter</c>.
39         /// User is responsible for providing an instance of TextWriter that is thread-safe.
40         /// </summary>
TextWriterLogger(TextWriter textWriter)41         public TextWriterLogger(TextWriter textWriter) : this(() => textWriter)
42         {
43             GrpcPreconditions.CheckNotNull(textWriter);
44         }
45 
46         /// <summary>
47         /// Creates a console logger not associated to any specific type and writes to a <c>System.IO.TextWriter</c> obtained from given provider.
48         /// User is responsible for providing an instance of TextWriter that is thread-safe.
49         /// </summary>
TextWriterLogger(Func<TextWriter> textWriterProvider)50         public TextWriterLogger(Func<TextWriter> textWriterProvider) : this(textWriterProvider, null)
51         {
52         }
53 
54         /// <summary>Creates a console logger that logs messsage specific for given type.</summary>
TextWriterLogger(Func<TextWriter> textWriterProvider, Type forType)55         protected TextWriterLogger(Func<TextWriter> textWriterProvider, Type forType)
56         {
57             this.textWriterProvider = GrpcPreconditions.CheckNotNull(textWriterProvider);
58             this.forType = forType;
59             if (forType != null)
60             {
61                 var namespaceStr = forType.Namespace ?? "";
62                 if (namespaceStr.Length > 0)
63                 {
64                      namespaceStr += ".";
65                 }
66                 this.forTypeString = namespaceStr + forType.Name + " ";
67             }
68             else
69             {
70                 this.forTypeString = "";
71             }
72         }
73 
74         /// <summary>
75         /// Returns a logger associated with the specified type.
76         /// </summary>
ForType()77         public virtual ILogger ForType<T>()
78         {
79             if (typeof(T) == forType)
80             {
81                 return this;
82             }
83             return new TextWriterLogger(this.textWriterProvider, typeof(T));
84         }
85 
86         /// <summary>Logs a message with severity Debug.</summary>
Debug(string message)87         public void Debug(string message)
88         {
89             Log("D", message);
90         }
91 
92         /// <summary>Logs a formatted message with severity Debug.</summary>
Debug(string format, params object[] formatArgs)93         public void Debug(string format, params object[] formatArgs)
94         {
95             Debug(string.Format(format, formatArgs));
96         }
97 
98         /// <summary>Logs a message with severity Info.</summary>
Info(string message)99         public void Info(string message)
100         {
101             Log("I", message);
102         }
103 
104         /// <summary>Logs a formatted message with severity Info.</summary>
Info(string format, params object[] formatArgs)105         public void Info(string format, params object[] formatArgs)
106         {
107             Info(string.Format(format, formatArgs));
108         }
109 
110         /// <summary>Logs a message with severity Warning.</summary>
Warning(string message)111         public void Warning(string message)
112         {
113             Log("W", message);
114         }
115 
116         /// <summary>Logs a formatted message with severity Warning.</summary>
Warning(string format, params object[] formatArgs)117         public void Warning(string format, params object[] formatArgs)
118         {
119             Warning(string.Format(format, formatArgs));
120         }
121 
122         /// <summary>Logs a message and an associated exception with severity Warning.</summary>
Warning(Exception exception, string message)123         public void Warning(Exception exception, string message)
124         {
125             Warning(message + " " + exception);
126         }
127 
128         /// <summary>Logs a message with severity Error.</summary>
Error(string message)129         public void Error(string message)
130         {
131             Log("E", message);
132         }
133 
134         /// <summary>Logs a formatted message with severity Error.</summary>
Error(string format, params object[] formatArgs)135         public void Error(string format, params object[] formatArgs)
136         {
137             Error(string.Format(format, formatArgs));
138         }
139 
140         /// <summary>Logs a message and an associated exception with severity Error.</summary>
Error(Exception exception, string message)141         public void Error(Exception exception, string message)
142         {
143             Error(message + " " + exception);
144         }
145 
146         /// <summary>Gets the type associated with this logger.</summary>
147         protected Type AssociatedType
148         {
149             get { return forType; }
150         }
151 
Log(string severityString, string message)152         private void Log(string severityString, string message)
153         {
154             textWriterProvider().WriteLine("{0}{1} {2}{3}",
155                 severityString,
156                 DateTime.Now.ToString(DateTimeFormatString, CultureInfo.InvariantCulture),
157                 forTypeString,
158                 message);
159         }
160     }
161 }
162