Skip to content

Guide: Building a custom logfile parser

wolkesson edited this page Jan 11, 2018 · 1 revision

Parsers are used to process and extract data from log files. The goal of this guide is to implemtent a custom logfile parser that can provide data to the plotting and analysis components.

Create a new project

Open visual studio 2015 or newer and create a new C# library project

Add references

Add the following references from the SampleCrunch installation folder:

  • PluginFramework.dll

Implement the IParserFactory and IParser interfaces

The system uses a factory to provide settings before creating the actual log file parser. These settings are stored and retained in the project file.

Implement IParserFactory

Create a class that implement IParserFactory and uses the PluginAttribute. Here is the simplest possible implementation:

[ParserPlugin("MyPlugin", "My File Extension | *.mfe")]
public class MyPluginFactory : IParserFactory 
{
    public bool CanOpen(string filename)
    {
        return true;
    }

    public IParser Open(string filename, ParserSettings settings = null)
    {
        return new MyPlugin(filename);
    }

    public bool ShowSettingsDialog(string filename, ref ParserSettings settings)
    {
        return true;
    }
}

Implement IParser

Create a class that implement IParser interface. Here's simple example that generate a random signal:

public class MyPlugin : IParser
{
    public MyPlugin(string fileName)
    {
        // Create the list of signals and add one signal
        this.Signals = new SignalList();
        this.Signals.Add(new Signal() { Name = "Signal 1" });
    }

    public DateTime Origo
    {
        get { return new DateTime(2015, 08, 10, 13, 11, 3); }
    }

    public TimeSpan Length
    {
        get { return TimeSpan.FromHours(3); }
    }

    public SignalList Signals
    {
        get;
        private set;
    }

    public Sample[] ReadSignal(Signal signal)
    {
        double sampleInterval = 40; // Time in milliseconds between samples
        DateTime t = Origo; // Time of first sample

        // Create empty array of samples
        Sample[] returnSample = new Sample[(int)(this.Length.TotalMilliseconds / sampleInterval)];

        // Generate random samples at fixed time intervals
        Random rnd = new Random();
        for (long i = 0; i < returnSample.LongLength; i++)
        {
            returnSample[i] = new Sample() { Time = t, Value = rnd.NextDouble() };
            t = t.AddMilliseconds(sampleInterval);
        }

        return returnSample;
    }
}

Import Plugin

Build the project. Open plugin manager and select the dll-file created by the build. Restart sample crunch.