Renamed project to ICD.Common.Utils

This commit is contained in:
jeff.thompson
2017-06-23 14:50:34 -04:00
parent 78eea65fc7
commit 2824f05180
93 changed files with 13 additions and 2 deletions

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using ICD.Common.Properties;
using ICD.Common.Utils;
namespace ICD.Common.Services.Logging
{
public enum eSeverity
{
Emergency = 0,
Alert = 1,
Critical = 2,
Error = 3,
Warning = 4,
Notice = 5,
Informational = 6,
Debug = 7
}
public interface ILoggerService
{
[PublicAPI]
event EventHandler<LogItemEventArgs> OnEntryAdded;
[PublicAPI]
event EventHandler<SeverityEventArgs> OnSeverityLevelChanged;
/// <summary>
/// Gets and sets the severity level.
/// </summary>
[PublicAPI]
eSeverity SeverityLevel { get; set; }
/// <summary>
/// Adds the log item.
/// </summary>
/// <param name="item">Log entry to add</param>
[PublicAPI]
void AddEntry(LogItem item);
/// <summary>
/// Gets the log history.
/// </summary>
/// <returns></returns>
[PublicAPI]
KeyValuePair<int, LogItem>[] GetHistory();
}
/// <summary>
/// Extension methods for ILoggerService.
/// </summary>
public static class LoggerServiceExtensions
{
/// <summary>
/// Adds the log item with string formatting.
/// </summary>
/// <param name="extends"></param>
/// <param name="severity">Severity Code, 0 - 7</param>
/// <param name="message">Message Text format string</param>
[PublicAPI]
public static void AddEntry(this ILoggerService extends, eSeverity severity, string message)
{
LogItem item = new LogItem(severity, message);
extends.AddEntry(item);
}
/// <summary>
/// Adds the log item with string formatting.
/// </summary>
/// <param name="extends"></param>
/// <param name="severity">Severity Code, 0 - 7</param>
/// <param name="message">Message Text format string</param>
/// <param name="args">objects to format into the string</param>
[PublicAPI]
public static void AddEntry(this ILoggerService extends, eSeverity severity, string message, params object[] args)
{
extends.AddEntry(severity, string.Format(message, args));
}
[PublicAPI]
public static void AddEntry(this ILoggerService extends, eSeverity severity, Exception e, string message)
{
extends.AddEntry(severity, string.Format("{0}: {1}{2}{3}{2}{4}", e.GetType().Name, message,
IcdEnvironment.NewLine, e.Message, e.StackTrace));
}
[PublicAPI]
public static void AddEntry(this ILoggerService extends, eSeverity severity, Exception e, string message,
params object[] args)
{
extends.AddEntry(severity, e, string.Format(message, args));
}
}
}

View File

@@ -0,0 +1,126 @@
using System;
using System.Text;
using ICD.Common.Properties;
using ICD.Common.Utils;
namespace ICD.Common.Services.Logging
{
/// <summary>
/// Log Entry Item
/// </summary>
public struct LogItem
{
private readonly string m_Message;
private readonly eSeverity m_Severity;
private readonly DateTime m_Timestamp;
#region Properties
/// <summary>
/// Accessor only for timestamp.
/// </summary>
[PublicAPI]
public DateTime Timestamp { get { return m_Timestamp; } }
/// <summary>
/// Get/Set for severity level.
/// </summary>
public eSeverity Severity { get { return m_Severity; } }
/// <summary>
/// Get/Set for message string.
/// </summary>
public string Message { get { return m_Message; } }
#endregion
#region Constructors
/// <summary>
/// Creates a new LogItem object with the specified values.
/// </summary>
/// <param name="severity">Severity Level, between 0 and 7</param>
/// <param name="message">Error message text</param>
public LogItem(eSeverity severity, string message)
{
m_Severity = severity;
m_Message = message;
m_Timestamp = IcdEnvironment.GetLocalTime();
}
#endregion
#region Methods
/// <summary>
/// Return the text format to send to Fusion
/// </summary>
/// <returns>text format for fusion, including timestamp, severity, and message</returns>
[PublicAPI]
public string GetFusionLogText()
{
StringBuilder s = new StringBuilder();
s.Append(Timestamp.ToString("yyyyMMddHHmmss"));
s.Append("||");
s.Append((int)Severity);
s.Append("||");
s.Append(Message);
return s.ToString();
}
/// <summary>
/// Implementing default equality.
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
public static bool operator ==(LogItem a1, LogItem a2)
{
return a1.Equals(a2);
}
/// <summary>
/// Implementing default inequality.
/// </summary>
/// <param name="a1"></param>
/// <param name="a2"></param>
/// <returns></returns>
public static bool operator !=(LogItem a1, LogItem a2)
{
return !(a1 == a2);
}
/// <summary>
/// Returns true if this instance is equal to the given object.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public override bool Equals(object other)
{
if (other == null || GetType() != other.GetType())
return false;
return GetHashCode() == ((LogItem)other).GetHashCode();
}
/// <summary>
/// Gets the hashcode for this instance.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + (m_Message == null ? 0 : m_Message.GetHashCode());
hash = hash * 23 + (int)m_Timestamp.Ticks;
hash = hash * 23 + (int)m_Severity;
return hash;
}
}
#endregion
}
}

View File

@@ -0,0 +1,16 @@
using ICD.Common.EventArguments;
namespace ICD.Common.Services.Logging
{
public sealed class LogItemEventArgs : GenericEventArgs<LogItem>
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="item"></param>
public LogItemEventArgs(LogItem item)
: base(item)
{
}
}
}

View File

@@ -0,0 +1,16 @@
using ICD.Common.EventArguments;
namespace ICD.Common.Services.Logging
{
public sealed class SeverityEventArgs : GenericEventArgs<eSeverity>
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="data"></param>
public SeverityEventArgs(eSeverity data)
: base(data)
{
}
}
}

View File

@@ -0,0 +1,28 @@
using System;
namespace ICD.Common.Services
{
public sealed class ServiceNotFoundException : Exception
{
private const string DEFAULT_MESSAGE = "The requested service {0} was not found in the service provider";
public Type ServiceType { get; set; }
public ServiceNotFoundException()
{
}
public ServiceNotFoundException(string message) : base(message)
{
}
public ServiceNotFoundException(string message, Exception inner) : base(message, inner)
{
}
public ServiceNotFoundException(Type type) : base(string.Format(DEFAULT_MESSAGE, type.Name))
{
ServiceType = type;
}
}
}

View File

@@ -0,0 +1,162 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ICD.Common.Properties;
using ICD.Common.Utils;
namespace ICD.Common.Services
{
public sealed class ServiceProvider : IDisposable
{
#region Static
private static ServiceProvider s_Instance;
private static ServiceProvider Instance { get { return s_Instance ?? (s_Instance = new ServiceProvider()); } }
/// <summary>
/// Retrieves the registered service of the given type. Use this for required dependencies.
/// </summary>
/// <typeparam name="TService">service type to retrieve</typeparam>
/// <returns></returns>
/// <exception cref="ServiceNotFoundException">Thrown if a service of the given type is not registered</exception>
[PublicAPI]
[NotNull]
public static TService GetService<TService>()
{
return (TService)GetService(typeof(TService));
}
/// <summary>
/// Retrieves the registered service of the given type. Use this for required dependencies.
/// </summary>
/// <param name="tService">service type to retrieve</param>
/// <returns></returns>
/// <exception cref="ServiceNotFoundException">Thrown if a service of the given type is not registered</exception>
[PublicAPI]
[NotNull]
public static object GetService(Type tService)
{
return Instance.GetServiceInstance(tService);
}
/// <summary>
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
/// Use this for optional dependencies.
/// </summary>
/// <typeparam name="TService">service type to retrieve</typeparam>
/// <returns>requested service or null if that service type is not registered</returns>
[PublicAPI]
[CanBeNull]
public static TService TryGetService<TService>()
{
return (TService)TryGetService(typeof(TService));
}
/// <summary>
/// Retrieves the registered service of the given type. Returns null if the service type was not found.
/// Use this for optional dependencies.
/// </summary>
/// <param name="tService">service type to retrieve</param>
/// <returns>requested service or null if that service type is not registered</returns>
[PublicAPI]
[CanBeNull]
public static object TryGetService(Type tService)
{
try
{
return Instance.GetServiceInstance(tService);
}
catch (ServiceNotFoundException)
{
return null;
}
}
/// <summary>
/// Registers a service instance to the type given.
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <param name="service"></param>
[PublicAPI]
public static void AddService<TService>(TService service)
{
AddService(typeof(TService), service);
}
/// <summary>
/// Registers a service instance to the type given.
/// </summary>
/// <param name="tService"></param>
/// <param name="service"></param>
[PublicAPI]
public static void AddService(Type tService, object service)
{
Instance.AddServiceInstance(tService, service);
}
#endregion
private readonly Dictionary<Type, object> m_Services = new Dictionary<Type, object>();
private readonly SafeCriticalSection m_ServicesSection = new SafeCriticalSection();
#region Methods
private object GetServiceInstance(Type tService)
{
try
{
m_ServicesSection.Enter();
object service;
if (m_Services.TryGetValue(tService, out service) && service != null)
return service;
throw new ServiceNotFoundException(tService);
}
finally
{
m_ServicesSection.Leave();
}
}
[PublicAPI]
private void AddServiceInstance(Type tService, object service)
{
m_ServicesSection.Enter();
m_Services[tService] = service;
m_ServicesSection.Leave();
}
#endregion
#region IDisposable
public void Dispose()
{
try
{
m_ServicesSection.Enter();
foreach (object service in m_Services.Values.Distinct())
{
if (!(service is IDisposable))
continue;
((IDisposable)service).Dispose();
}
m_Services.Clear();
}
finally
{
m_ServicesSection.Leave();
}
}
public static void DisposeStatic()
{
if (s_Instance != null)
s_Instance.Dispose();
s_Instance = null;
}
#endregion
}
}