• R/O
  • SSH
  • HTTPS

ewatch: Commit


Commit MetaInfo

Revision88 (tree)
Time2018-02-05 18:53:18
Authorbananajinn

Log Message

ファイルの先読みが無限ループしたりするのを修正

Change Summary

Incremental Difference

--- trunk/EWatch/EWatch/Watchers/FileWatcher.cs (revision 87)
+++ trunk/EWatch/EWatch/Watchers/FileWatcher.cs (revision 88)
@@ -48,8 +48,10 @@
4848 }
4949 fs.Seek(position, SeekOrigin.Begin);
5050 using(StreamReader sr = new StreamReader(fs, encoding)){
51- while(!sr.EndOfStream){
52- string line = sr.ReadLine();
51+ string preReadLine = null;
52+ while(!sr.EndOfStream || preReadLine != null){
53+ string line = preReadLine ?? sr.ReadLine();
54+ preReadLine = null;
5355 var data = new LogData {
5456 TimeStamp = DateTime.Now,
5557 HostName = Environment.MachineName,
@@ -59,7 +61,8 @@
5961 var table = LogMatcher.Match(actionTable, data);
6062 if(table != null) {
6163 if(!string.IsNullOrEmpty(table.WhilePattern)){
62- var lines = PreRead(sr, table.WhilePattern, table.WhileMax, 500);
64+ var lines = new List<string>();
65+ preReadLine = PreRead(sr, lines, table.WhilePattern, table.WhileMax, 500);
6366 if(!Util.IsNullOrEmpty(lines)){
6467 data.Message = string.Join("\n", data.Message, string.Join("\n", lines));
6568 }
@@ -81,28 +84,24 @@
8184 }
8285 }
8386
84- private string[] PreRead(StreamReader sr, string pattern, int? max, int timeoutMilliSec)
87+ private string PreRead(StreamReader sr, List<string> buffer, string pattern, int? max, int timeoutMilliSec)
8588 {
86- var result = new List<string>();
8789 var regex = new Regex(pattern);
8890 var timeout = DateTime.Now.AddMilliseconds(timeoutMilliSec);
89- var done = false;
91+ var count = 0;
9092
91- while(!done && DateTime.Now.CompareTo(timeout) < 0){
92- while(!sr.EndOfStream && result.Count < max){
93- var pos = sr.BaseStream.Position;
93+ while(DateTime.Now.CompareTo(timeout) < 0){
94+ while(!sr.EndOfStream && (max == null || count < max)){
9495 var line = sr.ReadLine();
9596 if(!regex.IsMatch(line)){
96- sr.BaseStream.Seek(pos, SeekOrigin.Begin);
97- done = true;
98- break;
97+ return line;
9998 }
100- result.Add(line);
99+ buffer.Add(line);
100+ count++;
101101 }
102102 Thread.Sleep(100);
103103 }
104-
105- return result.ToArray();
104+ return null;
106105 }
107106
108107 public override void Dispose()
Show on old repository browser