OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 |
| 37 namespace Grpc.Core.Logging |
| 38 { |
| 39 /// <summary>Logger that logs to System.Console.</summary> |
| 40 public class ConsoleLogger : ILogger |
| 41 { |
| 42 readonly Type forType; |
| 43 readonly string forTypeString; |
| 44 |
| 45 /// <summary>Creates a console logger not associated to any specific typ
e.</summary> |
| 46 public ConsoleLogger() : this(null) |
| 47 { |
| 48 } |
| 49 |
| 50 /// <summary>Creates a console logger that logs messsage specific for gi
ven type.</summary> |
| 51 private ConsoleLogger(Type forType) |
| 52 { |
| 53 this.forType = forType; |
| 54 if (forType != null) |
| 55 { |
| 56 var namespaceStr = forType.Namespace ?? ""; |
| 57 if (namespaceStr.Length > 0) |
| 58 { |
| 59 namespaceStr += "."; |
| 60 } |
| 61 this.forTypeString = namespaceStr + forType.Name + " "; |
| 62 } |
| 63 else |
| 64 { |
| 65 this.forTypeString = ""; |
| 66 } |
| 67 } |
| 68 |
| 69 /// <summary> |
| 70 /// Returns a logger associated with the specified type. |
| 71 /// </summary> |
| 72 public ILogger ForType<T>() |
| 73 { |
| 74 if (typeof(T) == forType) |
| 75 { |
| 76 return this; |
| 77 } |
| 78 return new ConsoleLogger(typeof(T)); |
| 79 } |
| 80 |
| 81 /// <summary>Logs a message with severity Debug.</summary> |
| 82 public void Debug(string message) |
| 83 { |
| 84 Log("D", message); |
| 85 } |
| 86 |
| 87 /// <summary>Logs a formatted message with severity Debug.</summary> |
| 88 public void Debug(string format, params object[] formatArgs) |
| 89 { |
| 90 Debug(string.Format(format, formatArgs)); |
| 91 } |
| 92 |
| 93 /// <summary>Logs a message with severity Info.</summary> |
| 94 public void Info(string message) |
| 95 { |
| 96 Log("I", message); |
| 97 } |
| 98 |
| 99 /// <summary>Logs a formatted message with severity Info.</summary> |
| 100 public void Info(string format, params object[] formatArgs) |
| 101 { |
| 102 Info(string.Format(format, formatArgs)); |
| 103 } |
| 104 |
| 105 /// <summary>Logs a message with severity Warning.</summary> |
| 106 public void Warning(string message) |
| 107 { |
| 108 Log("W", message); |
| 109 } |
| 110 |
| 111 /// <summary>Logs a formatted message with severity Warning.</summary> |
| 112 public void Warning(string format, params object[] formatArgs) |
| 113 { |
| 114 Warning(string.Format(format, formatArgs)); |
| 115 } |
| 116 |
| 117 /// <summary>Logs a message and an associated exception with severity Wa
rning.</summary> |
| 118 public void Warning(Exception exception, string message) |
| 119 { |
| 120 Warning(message + " " + exception); |
| 121 } |
| 122 |
| 123 /// <summary>Logs a message with severity Error.</summary> |
| 124 public void Error(string message) |
| 125 { |
| 126 Log("E", message); |
| 127 } |
| 128 |
| 129 /// <summary>Logs a formatted message with severity Error.</summary> |
| 130 public void Error(string format, params object[] formatArgs) |
| 131 { |
| 132 Error(string.Format(format, formatArgs)); |
| 133 } |
| 134 |
| 135 /// <summary>Logs a message and an associated exception with severity Er
ror.</summary> |
| 136 public void Error(Exception exception, string message) |
| 137 { |
| 138 Error(message + " " + exception); |
| 139 } |
| 140 |
| 141 private void Log(string severityString, string message) |
| 142 { |
| 143 Console.Error.WriteLine("{0}{1} {2}{3}", |
| 144 severityString, |
| 145 DateTime.Now, |
| 146 forTypeString, |
| 147 message); |
| 148 } |
| 149 } |
| 150 } |
OLD | NEW |