//******************************************************************************************************
// LogEventPublisherDetails.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/eclipse-1.0.php
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 10/24/2016 - Steven E. Chisholm
// Generated original version of source code.
//
//
//******************************************************************************************************
using System;
using System.Data;
using System.IO;
using System.Text;
using GSF.IO;
namespace GSF.Diagnostics
{
///
/// Information about the that published this message.
///
///
/// Since it is likely that a log file will have this data repeated a bunch, this class allows
/// de-duplication of this data so it takes up less memory to store.
///
public sealed class LogEventPublisherDetails
: IEquatable
{
///
/// The associated with that generated the message.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public readonly PublisherTypeDefinition TypeData;
///
/// The event name of this log message.
///
public readonly string EventName;
///
/// A hashCode code of this owner.
///
private readonly int m_hashCode;
///
/// Loads a log messages from the supplied stream
///
/// the stream to load the log message from.
/// the helper to assist in loading/saving
internal LogEventPublisherDetails(Stream stream, LogMessageSaveHelper helper)
{
byte version = stream.ReadNextByte();
switch (version)
{
case 1:
string typeName = stream.ReadString();
string assemblyName = stream.ReadString();
TypeData = new PublisherTypeDefinition(typeName, assemblyName);
EventName = stream.ReadString();
break;
case 2:
EventName = stream.ReadString();
TypeData = helper.LoadPublisherTypeDefinition(stream);
break;
default:
throw new VersionNotFoundException();
}
m_hashCode = ComputeHashCode();
}
///
/// Represents a single owner of a log message.
///
public LogEventPublisherDetails(PublisherTypeDefinition typeData, string eventName)
{
if (typeData == null)
throw new ArgumentNullException(nameof(typeData));
if (eventName == null)
eventName = string.Empty;
TypeData = typeData;
EventName = eventName;
m_hashCode = ComputeHashCode();
}
private int ComputeHashCode()
{
return TypeData.GetHashCode() ^ EventName.GetHashCode();
}
///
/// Writes the log data to the stream
///
///
/// the helper to assist in loading/saving
internal void Save(Stream stream, LogMessageSaveHelper helper)
{
stream.Write((byte)2);
stream.Write(EventName);
helper.SavePublisherTypeDefinition(stream, TypeData);
}
///
/// Returns a string that represents the current object.
///
///
/// A string that represents the current object.
///
/// 2
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (TypeData.TypeName.Length > 0)
{
sb.AppendLine("Message Type: " + TypeData.TypeName);
}
if (TypeData.AssemblyName.Length > 0)
{
sb.AppendLine("Message Assembly: " + TypeData.AssemblyName);
}
if (EventName.Length > 0)
{
sb.AppendLine("Event: " + EventName);
}
sb.Length -= Environment.NewLine.Length;
return sb.ToString();
}
///
/// Serves as a hash function for a particular type.
///
///
/// A hash code for the current .
///
/// 2
public override int GetHashCode()
{
return (int)m_hashCode;
}
///
/// Determines whether the specified is equal to the current .
///
///
/// true if the specified object is equal to the current object; otherwise, false.
///
/// The object to compare with the current object. 2
public override bool Equals(object obj)
{
return Equals(obj as LogEventPublisherDetails);
}
///
/// Indicates whether the current object is equal to another object of the same type.
///
///
/// true if the current object is equal to the parameter; otherwise, false.
///
/// An object to compare with this object.
public bool Equals(LogEventPublisherDetails obj)
{
if (ReferenceEquals(obj, null))
return false;
if (ReferenceEquals(this, obj))
return true;
return m_hashCode == obj.m_hashCode &&
EventName == obj.EventName &&
TypeData.Equals(obj.TypeData);
}
}
}