Simplify log implementation (no need for interface abstractions).

This commit is contained in:
wo80
2022-02-14 13:29:19 +01:00
parent 1dbac98d67
commit d1a52c6c7c
9 changed files with 101 additions and 147 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ namespace MeshExplorer
private ListViewItem CreateListViewItem(LogItem item)
{
ListViewItem lvi = new ListViewItem(new string[] { item.Message, item.Info });
ListViewItem lvi = new ListViewItem(new string[] { item.Message, item.Details });
if (item.Level == LogLevel.Error)
{
+96 -26
View File
@@ -6,28 +6,87 @@
namespace TriangleNet
{
using System;
using System.Collections.Generic;
using TriangleNet.Logging;
public enum LogLevel { Info, Warning, Error }
/// <summary>
/// Represents an item stored in the log.
/// </summary>
public class LogItem
{
private readonly DateTime time;
private readonly LogLevel level;
private readonly string message;
private readonly string details;
/// <summary>
/// Gets the <see cref="DateTime"/> the item was logged.
/// </summary>
public DateTime Time => time;
/// <summary>
/// Gets the <see cref="LogLevel"/>.
/// </summary>
public LogLevel Level => level;
/// <summary>
/// Gets the log message.
/// </summary>
public string Message => message;
/// <summary>
/// Gets further details of the log message.
/// </summary>
public string Details => details;
/// <summary>
/// Creates a new instance of the <see cref="LogItem"/> class.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">The log message.</param>
public LogItem(LogLevel level, string message)
: this(level, message, "")
{ }
/// <summary>
/// Creates a new instance of the <see cref="LogItem"/> class.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="message">The log message.</param>
/// <param name="details">The message details.</param>
public LogItem(LogLevel level, string message, string details)
{
time = DateTime.Now;
this.level = level;
this.message = message;
this.details = details;
}
}
/// <summary>
/// A simple logger, which logs messages to a List.
/// </summary>
/// <remarks>Using singleton pattern as proposed by Jon Skeet.
/// http://csharpindepth.com/Articles/General/Singleton.aspx
/// </remarks>
public sealed class Log : ILog<LogItem>
public sealed class Log
{
/// <summary>
/// Log detailed information.
/// </summary>
public static bool Verbose { get; set; }
private List<LogItem> log = new List<LogItem>();
/// <summary>
/// Gets all log messages.
/// </summary>
public IList<LogItem> Data => data;
private LogLevel level = LogLevel.Info;
private readonly List<LogItem> data = new List<LogItem>();
#region Singleton pattern
// Singleton pattern as proposed by Jon Skeet:
// https://csharpindepth.com/Articles/Singleton
private static readonly Log instance = new Log();
// Explicit static constructor to tell C# compiler
@@ -36,7 +95,7 @@ namespace TriangleNet
private Log() { }
public static ILog<LogItem> Instance
public static Log Instance
{
get
{
@@ -46,39 +105,50 @@ namespace TriangleNet
#endregion
/// <summary>
/// Adds a <see cref="LogItem"/> to the log.
/// </summary>
/// <param name="item"></param>
public void Add(LogItem item)
{
log.Add(item);
data.Add(item);
}
/// <summary>
/// Clear all messages from the log.
/// </summary>
public void Clear()
{
log.Clear();
data.Clear();
}
/// <summary>
/// Log info message.
/// </summary>
/// <param name="message">The message.</param>
public void Info(string message)
{
log.Add(new LogItem(LogLevel.Info, message));
data.Add(new LogItem(LogLevel.Info, message));
}
public void Warning(string message, string location)
/// <summary>
/// Log warning message.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="details">Message details, for example the code location where the error occured (class, method).</param>
public void Warning(string message, string details)
{
log.Add(new LogItem(LogLevel.Warning, message, location));
data.Add(new LogItem(LogLevel.Warning, message, details));
}
public void Error(string message, string location)
/// <summary>
/// Log error message.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="details">Message details, for example the code location where the error occured (class, method).</param>
public void Error(string message, string details)
{
log.Add(new LogItem(LogLevel.Error, message, location));
}
public IList<LogItem> Data
{
get { return log; }
}
public LogLevel Level
{
get { return level; }
data.Add(new LogItem(LogLevel.Error, message, details));
}
}
}
-34
View File
@@ -1,34 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="ILog.cs" company="">
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
// </copyright>
// -----------------------------------------------------------------------
namespace TriangleNet.Logging
{
using System.Collections.Generic;
public enum LogLevel
{
Info = 0,
Warning = 1,
Error = 2
}
/// <summary>
/// A basic log interface.
/// </summary>
public interface ILog<T> where T : ILogItem
{
void Add(T item);
void Clear();
void Info(string message);
void Error(string message, string info);
void Warning(string message, string info);
IList<T> Data { get; }
LogLevel Level { get; }
}
}
-21
View File
@@ -1,21 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="ILogItem.cs" company="">
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
// </copyright>
// -----------------------------------------------------------------------
namespace TriangleNet.Logging
{
using System;
/// <summary>
/// A basic log item interface.
/// </summary>
public interface ILogItem
{
DateTime Time { get; }
LogLevel Level { get; }
string Message { get; }
string Info { get; }
}
}
-53
View File
@@ -1,53 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="SimpleLogItem.cs" company="">
// Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
// </copyright>
// -----------------------------------------------------------------------
namespace TriangleNet.Logging
{
using System;
/// <summary>
/// Represents an item stored in the log.
/// </summary>
public class LogItem : ILogItem
{
DateTime time;
LogLevel level;
string message;
string info;
public DateTime Time
{
get { return time; }
}
public LogLevel Level
{
get { return level; }
}
public string Message
{
get { return message; }
}
public string Info
{
get { return info; }
}
public LogItem(LogLevel level, string message)
: this(level, message, "")
{ }
public LogItem(LogLevel level, string message, string info)
{
this.time = DateTime.Now;
this.level = level;
this.message = message;
this.info = info;
}
}
}
+1 -4
View File
@@ -10,7 +10,6 @@ namespace TriangleNet
using System;
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Logging;
using TriangleNet.Meshing;
using TriangleNet.Meshing.Data;
using TriangleNet.Meshing.Iterators;
@@ -26,7 +25,7 @@ namespace TriangleNet
IPredicates predicates;
ILog<LogItem> logger;
Log logger = Log.Instance;
QualityMesher qualityMesher;
@@ -236,8 +235,6 @@ namespace TriangleNet
{
Initialize();
logger = Log.Instance;
behavior = new Behavior();
vertices = new Dictionary<int, Vertex>();
+1 -4
View File
@@ -10,7 +10,6 @@ namespace TriangleNet.Meshing
using System;
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Logging;
using TriangleNet.Meshing.Iterators;
using TriangleNet.Topology;
@@ -24,7 +23,7 @@ namespace TriangleNet.Meshing
List<Triangle> viri;
ILog<LogItem> logger;
Log logger = Log.Instance;
public ConstraintMesher(Mesh mesh, Configuration config)
{
@@ -35,8 +34,6 @@ namespace TriangleNet.Meshing
this.locator = mesh.locator;
this.viri = new List<Triangle>();
logger = Log.Instance;
}
+1 -4
View File
@@ -10,7 +10,6 @@ namespace TriangleNet.Meshing
using System;
using System.Collections.Generic;
using TriangleNet.Geometry;
using TriangleNet.Logging;
using TriangleNet.Meshing.Data;
using TriangleNet.Topology;
@@ -28,7 +27,7 @@ namespace TriangleNet.Meshing
NewLocation newLocation;
ILog<LogItem> logger;
Log logger = Log.Instance;
// Stores the vertices of the triangle that contains newvertex
// in SplitTriangle method.
@@ -36,8 +35,6 @@ namespace TriangleNet.Meshing
public QualityMesher(Mesh mesh, Configuration config)
{
logger = Log.Instance;
badsubsegs = new Queue<BadSubseg>();
queue = new BadTriQueue();
+1
View File
@@ -29,6 +29,7 @@ namespace TriangleNet.Topology.DCEL
internal int id;
internal int mark;
// If the face is a Voronio cell, this is the point that generates the cell.
internal Point generator;
internal HalfEdge edge;