Fixed a few stability issues in the logging implementation. Added support for logging sensors once the reappear.
authormoel.mich
Sun, 18 Aug 2013 21:44:08 +0000
changeset 421055a9ec117d2
parent 420 46c056e76130
child 422 0fe7d6e91094
Fixed a few stability issues in the logging implementation. Added support for logging sensors once the reappear.
GUI/MainForm.cs
Properties/AssemblyVersion.cs
Utilities/Logger.cs
     1.1 --- a/GUI/MainForm.cs	Sun Aug 11 21:26:56 2013 +0000
     1.2 +++ b/GUI/MainForm.cs	Sun Aug 18 21:44:08 2013 +0000
     1.3 @@ -150,6 +150,8 @@
     1.4          wmiProvider = new WmiProvider(computer);
     1.5        }
     1.6  
     1.7 +      logger = new Logger(computer);
     1.8 +
     1.9        plotColorPalette = new Color[13];
    1.10        plotColorPalette[0] = Color.Blue;
    1.11        plotColorPalette[1] = Color.OrangeRed;
    1.12 @@ -281,8 +283,7 @@
    1.13        };
    1.14  
    1.15        logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
    1.16 -        settings);
    1.17 -      logger = new Logger(computer);
    1.18 +        settings);      
    1.19  
    1.20        InitializePlotForm();
    1.21  
    1.22 @@ -497,6 +498,7 @@
    1.23        Close();
    1.24      }
    1.25  
    1.26 +    private int delayCount = 0;
    1.27      private void timer_Tick(object sender, EventArgs e) {
    1.28        computer.Accept(updateVisitor);
    1.29        treeView.Invalidate();
    1.30 @@ -508,8 +510,12 @@
    1.31        if (wmiProvider != null)
    1.32          wmiProvider.Update();
    1.33  
    1.34 -      if (logSensors.Value)
    1.35 +
    1.36 +      if (logSensors.Value && delayCount >= 4)
    1.37          logger.Log();
    1.38 +
    1.39 +      if (delayCount < 4)
    1.40 +        delayCount++;
    1.41      }
    1.42  
    1.43      private void SaveConfiguration() {
     2.1 --- a/Properties/AssemblyVersion.cs	Sun Aug 11 21:26:56 2013 +0000
     2.2 +++ b/Properties/AssemblyVersion.cs	Sun Aug 18 21:44:08 2013 +0000
     2.3 @@ -10,5 +10,5 @@
     2.4  
     2.5  using System.Reflection;
     2.6  
     2.7 -[assembly: AssemblyVersion("0.6.0.8")]
     2.8 -[assembly: AssemblyInformationalVersion("0.6.0.8 Alpha")]
     2.9 \ No newline at end of file
    2.10 +[assembly: AssemblyVersion("0.6.0.9")]
    2.11 +[assembly: AssemblyInformationalVersion("0.6.0.9 Alpha")]
    2.12 \ No newline at end of file
     3.1 --- a/Utilities/Logger.cs	Sun Aug 11 21:26:56 2013 +0000
     3.2 +++ b/Utilities/Logger.cs	Sun Aug 18 21:44:08 2013 +0000
     3.3 @@ -25,6 +25,7 @@
     3.4  
     3.5      private DateTime day = DateTime.MinValue;
     3.6      private string fileName;
     3.7 +    private string[] identifiers;
     3.8      private ISensor[] sensors;
     3.9  
    3.10      public Logger(IComputer computer) {
    3.11 @@ -34,6 +35,7 @@
    3.12      }
    3.13  
    3.14      private void HardwareRemoved(IHardware hardware) {
    3.15 +      hardware.SensorAdded -= SensorAdded;
    3.16        hardware.SensorRemoved -= SensorRemoved;
    3.17        foreach (ISensor sensor in hardware.Sensors)
    3.18          SensorRemoved(sensor);
    3.19 @@ -42,12 +44,28 @@
    3.20      }
    3.21  
    3.22      private void HardwareAdded(IHardware hardware) {
    3.23 +      foreach (ISensor sensor in hardware.Sensors)
    3.24 +        SensorAdded(sensor);
    3.25 +      hardware.SensorAdded += SensorAdded;
    3.26        hardware.SensorRemoved += SensorRemoved;
    3.27        foreach (IHardware subHardware in hardware.SubHardware)
    3.28          HardwareAdded(subHardware);
    3.29      }
    3.30  
    3.31 +    private void SensorAdded(ISensor sensor) {
    3.32 +      if (sensors == null)
    3.33 +        return;
    3.34 +
    3.35 +      for (int i = 0; i < sensors.Length; i++) {
    3.36 +        if (sensor.Identifier.ToString() == identifiers[i])
    3.37 +          sensors[i] = sensor;
    3.38 +      }
    3.39 +    }
    3.40 +
    3.41      private void SensorRemoved(ISensor sensor) {
    3.42 +      if (sensors == null)
    3.43 +        return;
    3.44 +
    3.45        for (int i = 0; i < sensors.Length; i++) {
    3.46          if (sensor == sensors[i])
    3.47            sensors[i] = null;
    3.48 @@ -59,27 +77,10 @@
    3.49          Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
    3.50      }
    3.51  
    3.52 -    private void AddSensorRemovedHandler() {
    3.53 -      for (int i = 0; i < sensors.Length; i++) {
    3.54 -        ISensor sensor = sensors[i];
    3.55 -        int index = i;
    3.56 -        SensorEventHandler handler = null;        
    3.57 -        handler = s => {
    3.58 -          if (s != sensor) 
    3.59 -            return;
    3.60 -
    3.61 -          sensors[index] = null;
    3.62 -          sensor.Hardware.SensorRemoved -= handler;          
    3.63 -        };
    3.64 -        sensor.Hardware.SensorRemoved += handler;
    3.65 -      }
    3.66 -    }
    3.67 -
    3.68      private bool OpenExistingLogFile() {
    3.69        if (!File.Exists(fileName))
    3.70          return false;
    3.71  
    3.72 -      string[] identifiers;
    3.73        try {
    3.74          String line;
    3.75          using (StreamReader reader = new StreamReader(fileName)) 
    3.76 @@ -90,11 +91,14 @@
    3.77          
    3.78          identifiers = line.Split(',').Skip(1).ToArray();
    3.79        } catch {
    3.80 +        identifiers = null;
    3.81          return false;
    3.82        }
    3.83  
    3.84 -      if (identifiers.Length == 0)
    3.85 +      if (identifiers.Length == 0) {
    3.86 +        identifiers = null;
    3.87          return false;
    3.88 +      }
    3.89  
    3.90        sensors = new ISensor[identifiers.Length];
    3.91        SensorVisitor visitor = new SensorVisitor(sensor => {
    3.92 @@ -112,7 +116,8 @@
    3.93          list.Add(sensor);
    3.94        });
    3.95        visitor.VisitComputer(computer);
    3.96 -      sensors = list.ToArray();      
    3.97 +      sensors = list.ToArray();
    3.98 +      identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
    3.99  
   3.100        using (StreamWriter writer = new StreamWriter(fileName, false)) {
   3.101          writer.Write(",");
   3.102 @@ -139,7 +144,7 @@
   3.103  
   3.104      public void Log() {
   3.105        var now = DateTime.Now;
   3.106 -      if (day != now.Date) {
   3.107 +      if (day != now.Date || !File.Exists(fileName)) {
   3.108          day = now.Date;
   3.109          fileName = GetFileName(day);
   3.110  
   3.111 @@ -147,23 +152,25 @@
   3.112            CreateNewLogFile();
   3.113        }
   3.114  
   3.115 -      using (StreamWriter writer = new StreamWriter(fileName, true)) {
   3.116 -        writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
   3.117 -        writer.Write(",");
   3.118 -        for (int i = 0; i < sensors.Length; i++) {
   3.119 -          if (sensors[i] != null) {
   3.120 -            float? value = sensors[i].Value;
   3.121 -            if (value.HasValue)
   3.122 -              writer.Write(
   3.123 -                value.Value.ToString("R", CultureInfo.InvariantCulture));
   3.124 +      try {
   3.125 +        using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
   3.126 +          FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
   3.127 +          writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
   3.128 +          writer.Write(",");
   3.129 +          for (int i = 0; i < sensors.Length; i++) {
   3.130 +            if (sensors[i] != null) {
   3.131 +              float? value = sensors[i].Value;
   3.132 +              if (value.HasValue)
   3.133 +                writer.Write(
   3.134 +                  value.Value.ToString("R", CultureInfo.InvariantCulture));
   3.135 +            }
   3.136 +            if (i < sensors.Length - 1)
   3.137 +              writer.Write(",");
   3.138 +            else
   3.139 +              writer.WriteLine();
   3.140            }
   3.141 -          if (i < sensors.Length - 1)
   3.142 -            writer.Write(",");
   3.143 -          else
   3.144 -            writer.WriteLine();
   3.145          }
   3.146 -      }
   3.147 -
   3.148 +      } catch (IOException) { }
   3.149      }
   3.150    }
   3.151  }