Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive.ExperimentManager/HeuristicLab.Hive.Slave.Console/3.3/LogFileReader.cs @ 4792

Last change on this file since 4792 was 4772, checked in by cneumuel, 14 years ago

#1260

  • added LogServiceReader to display log for slave without writing to local files
  • aborted jobs with childjobs now got back to state WaitForChildJob (instead of Offline)
  • lifecyclemanager now knows about available plugins (does not yet work perfectly)
File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.IO;
23using System.Text;
24
25namespace HeuristicLab.Hive.Slave.Console {
26  internal class LogFileReader : HeuristicLab.Hive.Slave.Console.ILogReader {
27    string filename = "";
28    FileSystemWatcher fileSystemWatcher = null;
29    long previousSeekPosition;
30
31    public event MoreDataHandler MoreData;
32    private int maxBytes = 1024 * 16;
33    public int MaxBytes {
34      get { return this.maxBytes; }
35      set { this.maxBytes = value; }
36    }
37
38    public LogFileReader(string filename) {
39      this.filename = filename;
40    }
41
42    public void Start() {
43      FileInfo targetFile = new FileInfo(this.filename);
44
45      previousSeekPosition = 0;
46
47      fileSystemWatcher = new FileSystemWatcher();
48      fileSystemWatcher.IncludeSubdirectories = false;
49      fileSystemWatcher.Path = targetFile.DirectoryName;
50      fileSystemWatcher.Filter = targetFile.Name;
51
52      if (!targetFile.Exists) {
53        fileSystemWatcher.Created += new FileSystemEventHandler(TargetFile_Created);
54        fileSystemWatcher.EnableRaisingEvents = true;
55      } else {
56        TargetFile_Changed(null, null);
57        StartMonitoring();
58      }
59    }
60
61    public void Stop() {
62      fileSystemWatcher.EnableRaisingEvents = false;
63      fileSystemWatcher.Dispose();
64    }
65
66    public string ReadFullFile() {
67      using (StreamReader streamReader = new StreamReader(this.filename)) {
68        return streamReader.ReadToEnd();
69      }
70    }
71
72    public void StartMonitoring() {
73      fileSystemWatcher.Changed += new FileSystemEventHandler(TargetFile_Changed);
74      fileSystemWatcher.Renamed += new RenamedEventHandler(TargetFile_Renamed);
75      fileSystemWatcher.EnableRaisingEvents = true;
76    }
77
78    public void TargetFile_Renamed(object source, FileSystemEventArgs e)
79    {
80        Stop();
81        Start();
82    }
83
84    public void TargetFile_Created(object source, FileSystemEventArgs e) {
85      StartMonitoring();
86    }
87
88    public void TargetFile_Changed(object source, FileSystemEventArgs e) {
89      //lock (this)
90      {
91        //read from current seek position to end of file
92        byte[] bytesRead = new byte[maxBytes];
93        FileStream fs = new FileStream(this.filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
94        if (fs.Length > maxBytes) {
95          this.previousSeekPosition = fs.Length - maxBytes;
96        }
97        this.previousSeekPosition = (int)fs.Seek(this.previousSeekPosition, SeekOrigin.Begin);
98        int numBytes = fs.Read(bytesRead, 0, maxBytes);
99        fs.Close();
100        this.previousSeekPosition += numBytes;
101
102        StringBuilder sb = new StringBuilder();
103        for (int i = 0; i < numBytes; i++) {
104          sb.Append((char)bytesRead[i]);
105        }
106
107        //call delegates with the string
108        this.MoreData(this, sb.ToString());
109      }
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.