Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Hive.Client.Console/3.2/LogFileReader.cs @ 2498

Last change on this file since 2498 was 1930, checked in by whackl, 16 years ago

#655 new logging mechanism added

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.IO;
6
7namespace HeuristicLab.Hive.Client.Console {
8  class LogFileReader {
9    string filename = "";
10    FileSystemWatcher fileSystemWatcher = null;
11    long previousSeekPosition;
12
13    public delegate void MoreDataHandler(object sender, string newData);
14    public event MoreDataHandler MoreData;
15    private int maxBytes = 1024 * 16;
16    public int MaxBytes {
17      get { return this.maxBytes; }
18      set { this.maxBytes = value; }
19    }
20
21    public LogFileReader(string filename) {
22      this.filename = filename;
23    }
24
25    public void Start() {
26      FileInfo targetFile = new FileInfo(this.filename);
27
28      previousSeekPosition = 0;
29
30      fileSystemWatcher = new FileSystemWatcher();
31      fileSystemWatcher.IncludeSubdirectories = false;
32      fileSystemWatcher.Path = targetFile.DirectoryName;
33      fileSystemWatcher.Filter = targetFile.Name;
34
35      if (!targetFile.Exists) {
36        fileSystemWatcher.Created += new FileSystemEventHandler(TargetFile_Created);
37        fileSystemWatcher.EnableRaisingEvents = true;
38      } else {
39        TargetFile_Changed(null, null);
40        StartMonitoring();
41      }
42    }
43
44    public void Stop() {
45      fileSystemWatcher.EnableRaisingEvents = false;
46      fileSystemWatcher.Dispose();
47    }
48
49    public string ReadFullFile() {
50      using (StreamReader streamReader = new StreamReader(this.filename)) {
51        return streamReader.ReadToEnd();
52      }
53    }
54
55    public void StartMonitoring() {
56      fileSystemWatcher.Changed += new FileSystemEventHandler(TargetFile_Changed);
57      fileSystemWatcher.EnableRaisingEvents = true;
58    }
59
60    public void TargetFile_Created(object source, FileSystemEventArgs e) {
61      StartMonitoring();
62    }
63
64    public void TargetFile_Changed(object source, FileSystemEventArgs e) {
65      //lock (this)
66      {
67        //read from current seek position to end of file
68        byte[] bytesRead = new byte[maxBytes];
69        FileStream fs = new FileStream(this.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
70        if (fs.Length > maxBytes) {
71          this.previousSeekPosition = fs.Length - maxBytes;
72        }
73        this.previousSeekPosition = (int)fs.Seek(this.previousSeekPosition, SeekOrigin.Begin);
74        int numBytes = fs.Read(bytesRead, 0, maxBytes);
75        fs.Close();
76        this.previousSeekPosition += numBytes;
77
78        StringBuilder sb = new StringBuilder();
79        for (int i = 0; i < numBytes; i++) {
80          sb.Append((char)bytesRead[i]);
81        }
82
83        //call delegates with the string
84        this.MoreData(this, sb.ToString());
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.