| | 1 | | /* |
| | 2 | | * Licensed to SharpSoftware under one or more contributor |
| | 3 | | * license agreements. See the NOTICE file distributed with this work for |
| | 4 | | * additional information regarding copyright ownership. |
| | 5 | | * |
| | 6 | | * SharpSoftware licenses this file to you under the Apache License, |
| | 7 | | * Version 2.0 (the "License"); you may not use this file except in |
| | 8 | | * compliance with the License. You may obtain a copy of the License at |
| | 9 | | * |
| | 10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 11 | | * |
| | 12 | | * Unless required by applicable law or agreed to in writing, software |
| | 13 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 14 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | |
|
| | 19 | | using System.Collections.Generic; |
| | 20 | |
|
| | 21 | | namespace Itinero.Logging; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// A logger. |
| | 25 | | /// </summary> |
| | 26 | | public class Logger |
| | 27 | | { |
| | 28 | | private readonly string _name; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Creates a new logger. |
| | 32 | | /// </summary> |
| 0 | 33 | | public Logger(string name) |
| 0 | 34 | | { |
| 0 | 35 | | _name = name; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Creates a new logger. |
| | 40 | | /// </summary> |
| | 41 | | internal static Logger Create(string name) |
| 0 | 42 | | { |
| 0 | 43 | | return new(name); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Logs a message. |
| | 48 | | /// </summary> |
| | 49 | | public void Log(TraceEventType type, string message, params object[] args) |
| 0 | 50 | | { |
| 0 | 51 | | if (LogAction == null) |
| 0 | 52 | | { |
| 0 | 53 | | LogAction = (o, level, localmessage, parameters) => |
| 0 | 54 | | { |
| 0 | 55 | | System.Diagnostics.Debug.WriteLine($"[{o}] {level} - {localmessage}"); |
| 0 | 56 | | }; |
| 0 | 57 | | } |
| | 58 | |
|
| 0 | 59 | | LogAction(_name, type.ToString().ToLower(), string.Format(message, args), null); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Logs a message. |
| | 64 | | /// </summary> |
| | 65 | | public static void Log(string name, TraceEventType type, string message, params object[] args) |
| 1 | 66 | | { |
| 1 | 67 | | if (LogAction == null) |
| 1 | 68 | | { |
| 1 | 69 | | LogAction = (o, level, localmessage, parameters) => |
| 1 | 70 | | { |
| 1 | 71 | | System.Diagnostics.Debug.WriteLine($"[{o}] {level} - {localmessage}"); |
| 2 | 72 | | }; |
| 1 | 73 | | } |
| | 74 | |
|
| 1 | 75 | | LogAction(name, type.ToString().ToLower(), string.Format(message, args), null); |
| 1 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Defines the log action function. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="origin">The origin of the message, a class or module name.</param> |
| | 82 | | /// <param name="level">The level of the message, 'critical', 'error', 'warning', 'verbose' or 'information'.</param |
| | 83 | | /// <param name="message">The message content.</param> |
| | 84 | | /// <param name="parameters">Any parameters that may be useful.</param> |
| | 85 | | public delegate void LogActionFunction(string origin, string level, string message, |
| | 86 | | Dictionary<string, object>? parameters); |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Gets or sets the action to actually log a message. |
| | 90 | | /// </summary> |
| 3 | 91 | | public static LogActionFunction? LogAction { get; set; } |
| | 92 | | } |