Utilities/Logger.cs
changeset 421 055a9ec117d2
parent 420 46c056e76130
child 422 0fe7d6e91094
     1.1 --- a/Utilities/Logger.cs	Sun Aug 11 21:26:56 2013 +0000
     1.2 +++ b/Utilities/Logger.cs	Sun Aug 18 21:44:08 2013 +0000
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5      private DateTime day = DateTime.MinValue;
     1.6      private string fileName;
     1.7 +    private string[] identifiers;
     1.8      private ISensor[] sensors;
     1.9  
    1.10      public Logger(IComputer computer) {
    1.11 @@ -34,6 +35,7 @@
    1.12      }
    1.13  
    1.14      private void HardwareRemoved(IHardware hardware) {
    1.15 +      hardware.SensorAdded -= SensorAdded;
    1.16        hardware.SensorRemoved -= SensorRemoved;
    1.17        foreach (ISensor sensor in hardware.Sensors)
    1.18          SensorRemoved(sensor);
    1.19 @@ -42,12 +44,28 @@
    1.20      }
    1.21  
    1.22      private void HardwareAdded(IHardware hardware) {
    1.23 +      foreach (ISensor sensor in hardware.Sensors)
    1.24 +        SensorAdded(sensor);
    1.25 +      hardware.SensorAdded += SensorAdded;
    1.26        hardware.SensorRemoved += SensorRemoved;
    1.27        foreach (IHardware subHardware in hardware.SubHardware)
    1.28          HardwareAdded(subHardware);
    1.29      }
    1.30  
    1.31 +    private void SensorAdded(ISensor sensor) {
    1.32 +      if (sensors == null)
    1.33 +        return;
    1.34 +
    1.35 +      for (int i = 0; i < sensors.Length; i++) {
    1.36 +        if (sensor.Identifier.ToString() == identifiers[i])
    1.37 +          sensors[i] = sensor;
    1.38 +      }
    1.39 +    }
    1.40 +
    1.41      private void SensorRemoved(ISensor sensor) {
    1.42 +      if (sensors == null)
    1.43 +        return;
    1.44 +
    1.45        for (int i = 0; i < sensors.Length; i++) {
    1.46          if (sensor == sensors[i])
    1.47            sensors[i] = null;
    1.48 @@ -59,27 +77,10 @@
    1.49          Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
    1.50      }
    1.51  
    1.52 -    private void AddSensorRemovedHandler() {
    1.53 -      for (int i = 0; i < sensors.Length; i++) {
    1.54 -        ISensor sensor = sensors[i];
    1.55 -        int index = i;
    1.56 -        SensorEventHandler handler = null;        
    1.57 -        handler = s => {
    1.58 -          if (s != sensor) 
    1.59 -            return;
    1.60 -
    1.61 -          sensors[index] = null;
    1.62 -          sensor.Hardware.SensorRemoved -= handler;          
    1.63 -        };
    1.64 -        sensor.Hardware.SensorRemoved += handler;
    1.65 -      }
    1.66 -    }
    1.67 -
    1.68      private bool OpenExistingLogFile() {
    1.69        if (!File.Exists(fileName))
    1.70          return false;
    1.71  
    1.72 -      string[] identifiers;
    1.73        try {
    1.74          String line;
    1.75          using (StreamReader reader = new StreamReader(fileName)) 
    1.76 @@ -90,11 +91,14 @@
    1.77          
    1.78          identifiers = line.Split(',').Skip(1).ToArray();
    1.79        } catch {
    1.80 +        identifiers = null;
    1.81          return false;
    1.82        }
    1.83  
    1.84 -      if (identifiers.Length == 0)
    1.85 +      if (identifiers.Length == 0) {
    1.86 +        identifiers = null;
    1.87          return false;
    1.88 +      }
    1.89  
    1.90        sensors = new ISensor[identifiers.Length];
    1.91        SensorVisitor visitor = new SensorVisitor(sensor => {
    1.92 @@ -112,7 +116,8 @@
    1.93          list.Add(sensor);
    1.94        });
    1.95        visitor.VisitComputer(computer);
    1.96 -      sensors = list.ToArray();      
    1.97 +      sensors = list.ToArray();
    1.98 +      identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
    1.99  
   1.100        using (StreamWriter writer = new StreamWriter(fileName, false)) {
   1.101          writer.Write(",");
   1.102 @@ -139,7 +144,7 @@
   1.103  
   1.104      public void Log() {
   1.105        var now = DateTime.Now;
   1.106 -      if (day != now.Date) {
   1.107 +      if (day != now.Date || !File.Exists(fileName)) {
   1.108          day = now.Date;
   1.109          fileName = GetFileName(day);
   1.110  
   1.111 @@ -147,23 +152,25 @@
   1.112            CreateNewLogFile();
   1.113        }
   1.114  
   1.115 -      using (StreamWriter writer = new StreamWriter(fileName, true)) {
   1.116 -        writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
   1.117 -        writer.Write(",");
   1.118 -        for (int i = 0; i < sensors.Length; i++) {
   1.119 -          if (sensors[i] != null) {
   1.120 -            float? value = sensors[i].Value;
   1.121 -            if (value.HasValue)
   1.122 -              writer.Write(
   1.123 -                value.Value.ToString("R", CultureInfo.InvariantCulture));
   1.124 +      try {
   1.125 +        using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
   1.126 +          FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
   1.127 +          writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
   1.128 +          writer.Write(",");
   1.129 +          for (int i = 0; i < sensors.Length; i++) {
   1.130 +            if (sensors[i] != null) {
   1.131 +              float? value = sensors[i].Value;
   1.132 +              if (value.HasValue)
   1.133 +                writer.Write(
   1.134 +                  value.Value.ToString("R", CultureInfo.InvariantCulture));
   1.135 +            }
   1.136 +            if (i < sensors.Length - 1)
   1.137 +              writer.Write(",");
   1.138 +            else
   1.139 +              writer.WriteLine();
   1.140            }
   1.141 -          if (i < sensors.Length - 1)
   1.142 -            writer.Write(",");
   1.143 -          else
   1.144 -            writer.WriteLine();
   1.145          }
   1.146 -      }
   1.147 -
   1.148 +      } catch (IOException) { }
   1.149      }
   1.150    }
   1.151  }