fix journal file so that it only reports on brand new events
This commit is contained in:
		
							parent
							
								
									1cab477db7
								
							
						
					
					
						commit
						a64562cd48
					
				@ -19,6 +19,8 @@ namespace EDJournal {
 | 
			
		||||
        private static string iso8601 = "yyyyMMddTHHmmss";
 | 
			
		||||
        private static string iso8601_notime = "yyyyMMdd";
 | 
			
		||||
 | 
			
		||||
        public string FullPath => fullpath;
 | 
			
		||||
 | 
			
		||||
        public static bool VerifyFile(string path) {
 | 
			
		||||
            string filename = Path.GetFileName(path);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
 | 
			
		||||
@ -26,11 +27,28 @@ namespace EDJournal {
 | 
			
		||||
            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() {
 | 
			
		||||
            if (watcher != null) {
 | 
			
		||||
                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.NotifyFilter = NotifyFilters.FileName |
 | 
			
		||||
@ -53,16 +71,11 @@ namespace EDJournal {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void Watcher_Created(object sender, FileSystemEventArgs e) {
 | 
			
		||||
            if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) {
 | 
			
		||||
                streams[e.FullPath] = new StreamReader(e.FullPath);
 | 
			
		||||
            }
 | 
			
		||||
            AddFileToStreams(e.FullPath);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void Watcher_Changed(object sender, FileSystemEventArgs e) {
 | 
			
		||||
            if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) {
 | 
			
		||||
                var filestream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
 | 
			
		||||
                streams[e.FullPath] = new StreamReader(filestream);
 | 
			
		||||
            }
 | 
			
		||||
            AddFileToStreams(e.FullPath);
 | 
			
		||||
 | 
			
		||||
            var stream = streams[e.FullPath];
 | 
			
		||||
            if (stream == null) {
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.IO;
 | 
			
		||||
 | 
			
		||||
@ -21,6 +22,7 @@ namespace EDJournal {
 | 
			
		||||
 | 
			
		||||
        private void Initialise(string path) {
 | 
			
		||||
            basepath = Environment.ExpandEnvironmentVariables(path);
 | 
			
		||||
            ScanFiles();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public List<JournalFile> Files {
 | 
			
		||||
@ -34,6 +36,16 @@ namespace EDJournal {
 | 
			
		||||
            this.ScanFiles();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public JournalFile GetLastFile() {
 | 
			
		||||
            var lastfile = Files
 | 
			
		||||
                .GroupBy(x => x.Timestamp)
 | 
			
		||||
                .Last()
 | 
			
		||||
                .ToArray<JournalFile>()
 | 
			
		||||
                .Last()
 | 
			
		||||
                ;
 | 
			
		||||
            return lastfile;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void ScanFiles() { 
 | 
			
		||||
            var files = Directory.EnumerateFiles(basepath);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user