< Summary

Class:Itinero.Logging.Logger
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Logging/Logger.cs
Covered lines:11
Uncovered lines:17
Coverable lines:28
Total lines:92
Line coverage:39.2% (11 of 28)
Covered branches:2
Total branches:4
Branch coverage:50% (2 of 4)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%10%
Create(...)100%10%
Log(...)0%20%
Log(...)100%2100%
get_LogAction()100%1100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Logging/Logger.cs

#LineLine coverage
 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
 19using System.Collections.Generic;
 20
 21namespace Itinero.Logging;
 22
 23/// <summary>
 24/// A logger.
 25/// </summary>
 26public class Logger
 27{
 28    private readonly string _name;
 29
 30    /// <summary>
 31    /// Creates a new logger.
 32    /// </summary>
 033    public Logger(string name)
 034    {
 035        _name = name;
 036    }
 37
 38    /// <summary>
 39    /// Creates a new logger.
 40    /// </summary>
 41    internal static Logger Create(string name)
 042    {
 043        return new(name);
 044    }
 45
 46    /// <summary>
 47    /// Logs a message.
 48    /// </summary>
 49    public void Log(TraceEventType type, string message, params object[] args)
 050    {
 051        if (LogAction == null)
 052        {
 053            LogAction = (o, level, localmessage, parameters) =>
 054            {
 055                System.Diagnostics.Debug.WriteLine($"[{o}] {level} - {localmessage}");
 056            };
 057        }
 58
 059        LogAction(_name, type.ToString().ToLower(), string.Format(message, args), null);
 060    }
 61
 62    /// <summary>
 63    /// Logs a message.
 64    /// </summary>
 65    public static void Log(string name, TraceEventType type, string message, params object[] args)
 166    {
 167        if (LogAction == null)
 168        {
 169            LogAction = (o, level, localmessage, parameters) =>
 170            {
 171                System.Diagnostics.Debug.WriteLine($"[{o}] {level} - {localmessage}");
 272            };
 173        }
 74
 175        LogAction(name, type.ToString().ToLower(), string.Format(message, args), null);
 176    }
 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>
 391    public static LogActionFunction? LogAction { get; set; }
 92}