Skip to content

Commit

Permalink
DFLogBuffer: add SplitLog
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Feb 25, 2024
1 parent ed086e6 commit 0e2a913
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion ExtLibs/Utilities/DFLogBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ public class DFLogBuffer : IEnumerable<String>, IDisposable

public DFLogBuffer(string filename) : this(File.Open(filename,FileMode.Open,FileAccess.Read,FileShare.Read))
{
_filename = filename;
}

public DFLogBuffer(Stream instream)
{
if (instream is FileStream)
_filename = ((FileStream)instream).Name;

dflog = new DFLog(this);
for (int a = 0; a < messageindex.Length; a++)
{
Expand Down Expand Up @@ -117,7 +121,7 @@ void setlinecount()
_count = lineCount;

// build fmt line database to pre seed the FMT message
messageindexline[128].ForEach(a => dflog.FMTLine(this[(int) a]));
messageindexline[128].ForEach(a => dflog.FMTLine(this[(int)a]));

try
{
Expand Down Expand Up @@ -303,6 +307,89 @@ void setlinecount()

indexcachelineno = -1;
}

public void SplitLog(int pieces = 0)
{
long length = basestream.Length;

if (pieces > 0)
{
long sizeofpiece = length / pieces;

for(int i = 0; i < pieces; i++)
{
long start = i * sizeofpiece;
long end = start + sizeofpiece;

using (var file = File.OpenWrite(_filename + "_split" + i + ".bin"))
{
var type = dflog.logformat["FMT"];

var buffer = new byte[1024*256];

// fmt from entire file
messageindex[type.Id].ForEach(a => {
basestream.Seek(a, SeekOrigin.Begin);
int read = basestream.Read(buffer, 0, type.Length);
file.Write(buffer, 0, read);
});

type = dflog.logformat["FMTU"];

messageindex[type.Id].ForEach(a => {
basestream.Seek(a, SeekOrigin.Begin);
int read = basestream.Read(buffer, 0, type.Length);
file.Write(buffer, 0, read);
});

type = dflog.logformat["UNIT"];

messageindex[type.Id].ForEach(a => {
basestream.Seek(a, SeekOrigin.Begin);
int read = basestream.Read(buffer, 0, type.Length);
file.Write(buffer, 0, read);
});

type = dflog.logformat["MULT"];

messageindex[type.Id].ForEach(a => {
basestream.Seek(a, SeekOrigin.Begin);
int read = basestream.Read(buffer, 0, type.Length);
file.Write(buffer, 0, read);
});



var min = long.MaxValue;
var max = long.MinValue;

// got min and max valid
linestartoffset.ForEach(a =>
{
if (a >= start && a < end)
{
min = Math.Min(min, a);
max = Math.Max(max, a);
}
});

basestream.Seek(min, SeekOrigin.Begin);

while (basestream.Position < max)
{
int readsize = (int)Math.Min((end - basestream.Position), buffer.Length);
int read = basestream.Read(buffer, 0, readsize);
file.Write(buffer, 0, read);
}
}
}
}
else
{
throw new Exception("Invalid pieces parameters");
}
}

private void BuildUnitMultiList()
{
foreach (var msgtype in FMT)
Expand Down Expand Up @@ -348,6 +435,7 @@ private void BuildUnitMultiList()

public List<Tuple<string,string,string,double>> UnitMultiList = new List<Tuple<string, string, string, double>>();
public Dictionary<int, (int index, List<string> value)> InstanceType = new Dictionary<int, (int index, List<string> value)>();
private string _filename;

public Dictionary<int, (int length, string name, string format, string columns)> FMT { get; set; } =
new Dictionary<int, (int, string, string, string)>();
Expand Down

0 comments on commit 0e2a913

Please sign in to comment.