// Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace OpenTap.Diagnostic
{
///
/// A simple log listener that writes events to a binary file.
///
public class LogFile : ILogListener, IDisposable
{
private readonly StreamWriter _sw;
///
/// Initializes a new instance of the class.
/// Logs events to a given stream.
///
/// The stream to use.
public LogFile(Stream st)
{
if (st == null)
throw new ArgumentNullException("st");
_sw = new StreamWriter(st, Encoding.UTF8, 1024 * 1024);
}
///
/// Implements the ILogListener method.
///
/// Events array.
public void EventsLogged(IEnumerable logEvents)
{
if (logEvents == null)
throw new ArgumentNullException("logEvent");
foreach (var evt in logEvents)
WriteLog(evt);
}
///
/// This method must be called to close the stream.
///
public void Close()
{
_sw.Flush();
_sw.Close();
}
internal void WriteLog(Event evt)
{
_sw.WriteLine("{3} ; {0,-13} ; {1,-11} ; {2}", evt.Source, evt.EventType, evt.Message.Replace("\n", " ").Replace("\r", string.Empty), evt.Timestamp);
}
///
/// Flush the current stream.
///
public void Flush()
{
_sw.Flush();
}
///
/// Dispose method since BufferedStream implements IDisposable.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose method since BufferedStream implements IDisposable.
///
/// If false, cleanup native resources, if true, clean up native and managed.
protected virtual void Dispose(bool cleanupScope)
{
if (_sw != null)
{
_sw.Dispose();
}
}
}
///
/// A simple log listener that writes events to a binary file.
///
public class BinaryLog : ILogListener, IDisposable
{
private Stream str;
///
/// Initializes a new instance of the class.
/// Logs events to a given stream.
///
/// The stream to use.
public BinaryLog(Stream st)
{
if (st == null)
throw new ArgumentNullException("st");
str = new BufferedStream(st, 1024 * 1024);
}
///
/// Method that must be called to properly close the stream.
///
public void Close()
{
str.Flush();
str.Close();
}
///
/// Flushes the messages from the listener.
///
public void Flush()
{
str.Flush();
}
///
/// Logs the events.
///
/// List of events to log.
public void EventsLogged(IEnumerable logEvents)
{
if (logEvents == null)
throw new ArgumentNullException("logEvents");
foreach (var evt in logEvents)
{
byte[] source = Encoding.UTF8.GetBytes(evt.Source);
byte[] message = Encoding.UTF8.GetBytes(evt.Message);
Int32 len =
4 +
8 +
8 +
4 + message.Length +
4 + source.Length;
str.Write(BitConverter.GetBytes(len), 0, 4);
str.Write(BitConverter.GetBytes((Int32)evt.EventType), 0, 4);
str.Write(BitConverter.GetBytes(evt.Timestamp), 0, 8);
str.Write(BitConverter.GetBytes(evt.DurationNS), 0, 8);
str.Write(BitConverter.GetBytes((Int32)message.Length), 0, 4);
str.Write(message, 0, message.Length);
str.Write(BitConverter.GetBytes((Int32)source.Length), 0, 4);
str.Write(source, 0, source.Length);
}
}
///
/// Resets and releases resources for this unmanaged class
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Dispose method since BufferedStream implements IDisposable.
///
/// If false, cleanup native resources, if true, clean up native and managed.
protected virtual void Dispose(bool cleanupScope)
{
if (str != null)
{
str.Dispose();
}
}
}
}