Jan Tattermusch | 0526161 | 2015-07-24 00:28:16 -0700 | [diff] [blame] | 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 | public ConsoleLogger() : this(null) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | private ConsoleLogger(Type forType) |
| 50 | { |
| 51 | this.forType = forType; |
| 52 | this.forTypeString = forType != null ? forType.FullName + " " : ""; |
| 53 | } |
| 54 | |
| 55 | public ILogger ForType<T>() |
| 56 | { |
| 57 | if (typeof(T) == forType) |
| 58 | { |
| 59 | return this; |
| 60 | } |
| 61 | return new ConsoleLogger(typeof(T)); |
| 62 | } |
| 63 | |
| 64 | public void Debug(string message, params object[] formatArgs) |
| 65 | { |
| 66 | Log("D", message, formatArgs); |
| 67 | } |
| 68 | |
| 69 | public void Info(string message, params object[] formatArgs) |
| 70 | { |
| 71 | Log("I", message, formatArgs); |
| 72 | } |
| 73 | |
| 74 | public void Warning(string message, params object[] formatArgs) |
| 75 | { |
| 76 | Log("W", message, formatArgs); |
| 77 | } |
| 78 | |
| 79 | public void Warning(Exception exception, string message, params object[] formatArgs) |
| 80 | { |
| 81 | Log("W", message + " " + exception, formatArgs); |
| 82 | } |
| 83 | |
| 84 | public void Error(string message, params object[] formatArgs) |
| 85 | { |
| 86 | Log("E", message, formatArgs); |
| 87 | } |
| 88 | |
| 89 | public void Error(Exception exception, string message, params object[] formatArgs) |
| 90 | { |
| 91 | Log("E", message + " " + exception, formatArgs); |
| 92 | } |
| 93 | |
| 94 | private void Log(string severityString, string message, object[] formatArgs) |
| 95 | { |
| 96 | Console.Error.WriteLine("{0}{1} {2}{3}", |
| 97 | severityString, |
| 98 | DateTime.Now, |
| 99 | forTypeString, |
| 100 | string.Format(message, formatArgs)); |
| 101 | } |
| 102 | } |
| 103 | } |