Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Hive.Client.Console/3.2/LogFileReader.cs @ 3703

Last change on this file since 3703 was 2062, checked in by whackl, 15 years ago

refactoring hive client console #663

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.IO;
27using System.Diagnostics;
28
29namespace HeuristicLab.Hive.Client.Console {
30  class LogFileReader {
31    string filename = "";
32    FileSystemWatcher fileSystemWatcher = null;
33    long previousSeekPosition;
34
35    public delegate void MoreDataHandler(object sender, string newData);
36    public event MoreDataHandler MoreData;
37    private int maxBytes = 1024 * 16;
38    public int MaxBytes {
39      get { return this.maxBytes; }
40      set { this.maxBytes = value; }
41    }
42
43    public LogFileReader(string filename) {
44      this.filename = filename;
45    }
46
47    public void Start() {
48      FileInfo targetFile = new FileInfo(this.filename);
49
50      previousSeekPosition = 0;
51
52      fileSystemWatcher = new FileSystemWatcher();
53      fileSystemWatcher.IncludeSubdirectories = false;
54      fileSystemWatcher.Path = targetFile.DirectoryName;
55      fileSystemWatcher.Filter = targetFile.Name;
56
57      if (!targetFile.Exists) {
58        fileSystemWatcher.Created += new FileSystemEventHandler(TargetFile_Created);
59        fileSystemWatcher.EnableRaisingEvents = true;
60      } else {
61        TargetFile_Changed(null, null);
62        StartMonitoring();
63      }
64    }
65
66    public void Stop() {
67      fileSystemWatcher.EnableRaisingEvents = false;
68      fileSystemWatcher.Dispose();
69    }
70
71    public string ReadFullFile() {
72      using (StreamReader streamReader = new StreamReader(this.filename)) {
73        return streamReader.ReadToEnd();
74      }
75    }
76
77    public void StartMonitoring() {
78        fileSystemWatcher.Changed += new FileSystemEventHandler(TargetFile_Changed);
79      fileSystemWatcher.Renamed += new RenamedEventHandler(TargetFile_Renamed);
80      fileSystemWatcher.EnableRaisingEvents = true;
81    }
82
83    public void TargetFile_Renamed(object source, FileSystemEventArgs e)
84    {
85        Stop();
86        Start();
87    }
88
89    public void TargetFile_Created(object source, FileSystemEventArgs e) {
90      StartMonitoring();
91    }
92
93    public void TargetFile_Changed(object source, FileSystemEventArgs e) {
94      //lock (this)
95      {
96        //read from current seek position to end of file
97        byte[] bytesRead = new byte[maxBytes];
98        FileStream fs = new FileStream(this.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
99        if (fs.Length > maxBytes) {
100          this.previousSeekPosition = fs.Length - maxBytes;
101        }
102        this.previousSeekPosition = (int)fs.Seek(this.previousSeekPosition, SeekOrigin.Begin);
103        int numBytes = fs.Read(bytesRead, 0, maxBytes);
104        fs.Close();
105        this.previousSeekPosition += numBytes;
106
107        StringBuilder sb = new StringBuilder();
108        for (int i = 0; i < numBytes; i++) {
109          sb.Append((char)bytesRead[i]);
110        }
111
112        //call delegates with the string
113        this.MoreData(this, sb.ToString());
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.