fix journal file so that it only reports on brand new events

This commit is contained in:
Florian Stinglmayr 2021-08-27 19:33:01 +02:00
parent 1cab477db7
commit a64562cd48
3 changed files with 34 additions and 7 deletions

View File

@ -19,6 +19,8 @@ namespace EDJournal {
private static string iso8601 = "yyyyMMddTHHmmss"; private static string iso8601 = "yyyyMMddTHHmmss";
private static string iso8601_notime = "yyyyMMdd"; private static string iso8601_notime = "yyyyMMdd";
public string FullPath => fullpath;
public static bool VerifyFile(string path) { public static bool VerifyFile(string path) {
string filename = Path.GetFileName(path); string filename = Path.GetFileName(path);

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -26,11 +27,28 @@ namespace EDJournal {
Open(); Open();
} }
private void AddFileToStreams(string path, bool seekend = false) {
if (!streams.ContainsKey(path) && JournalFile.VerifyFile(path)) {
var filestream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
streams[path] = new StreamReader(filestream);
if (seekend) {
streams[path].BaseStream.Seek(0, SeekOrigin.End);
}
}
}
public void Open() { public void Open() {
if (watcher != null) { if (watcher != null) {
return; return;
} }
if (journal.Files.Count > 0) {
JournalFile lastfile = journal.GetLastFile();
/* add last file to stream */
AddFileToStreams(lastfile.FullPath, true);
}
watcher = new FileSystemWatcher(journal.Location); watcher = new FileSystemWatcher(journal.Location);
watcher.NotifyFilter = NotifyFilters.FileName | watcher.NotifyFilter = NotifyFilters.FileName |
@ -53,16 +71,11 @@ namespace EDJournal {
} }
private void Watcher_Created(object sender, FileSystemEventArgs e) { private void Watcher_Created(object sender, FileSystemEventArgs e) {
if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) { AddFileToStreams(e.FullPath);
streams[e.FullPath] = new StreamReader(e.FullPath);
}
} }
private void Watcher_Changed(object sender, FileSystemEventArgs e) { private void Watcher_Changed(object sender, FileSystemEventArgs e) {
if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) { AddFileToStreams(e.FullPath);
var filestream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
streams[e.FullPath] = new StreamReader(filestream);
}
var stream = streams[e.FullPath]; var stream = streams[e.FullPath];
if (stream == null) { if (stream == null) {

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -21,6 +22,7 @@ namespace EDJournal {
private void Initialise(string path) { private void Initialise(string path) {
basepath = Environment.ExpandEnvironmentVariables(path); basepath = Environment.ExpandEnvironmentVariables(path);
ScanFiles();
} }
public List<JournalFile> Files { public List<JournalFile> Files {
@ -34,6 +36,16 @@ namespace EDJournal {
this.ScanFiles(); this.ScanFiles();
} }
public JournalFile GetLastFile() {
var lastfile = Files
.GroupBy(x => x.Timestamp)
.Last()
.ToArray<JournalFile>()
.Last()
;
return lastfile;
}
public void ScanFiles() { public void ScanFiles() {
var files = Directory.EnumerateFiles(basepath); var files = Directory.EnumerateFiles(basepath);