Utilities/Logger.cs
author StephaneLenclud
Mon, 02 Feb 2015 20:58:28 +0100
branchMiniDisplay
changeset 448 10c04c79527e
parent 421 055a9ec117d2
permissions -rw-r--r--
Sync: Adding shamap and documentation to enable easier sync next time OHM is updated.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7 	Copyright (C) 2013 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Globalization;
    14 using System.IO;
    15 using System.Linq;
    16 using OpenHardwareMonitor.Hardware;
    17 
    18 namespace OpenHardwareMonitor.Utilities {
    19   public class Logger {
    20 
    21     private const string fileNameFormat = 
    22       "OpenHardwareMonitorLog-{0:yyyy-MM-dd}.csv";
    23 
    24     private readonly IComputer computer;
    25 
    26     private DateTime day = DateTime.MinValue;
    27     private string fileName;
    28     private string[] identifiers;
    29     private ISensor[] sensors;
    30 
    31     private DateTime lastLoggedTime = DateTime.MinValue;
    32 
    33     public Logger(IComputer computer) {
    34       this.computer = computer;
    35       this.computer.HardwareAdded += HardwareAdded;
    36       this.computer.HardwareRemoved += HardwareRemoved;      
    37     }
    38 
    39     private void HardwareRemoved(IHardware hardware) {
    40       hardware.SensorAdded -= SensorAdded;
    41       hardware.SensorRemoved -= SensorRemoved;
    42       foreach (ISensor sensor in hardware.Sensors)
    43         SensorRemoved(sensor);
    44       foreach (IHardware subHardware in hardware.SubHardware)
    45         HardwareRemoved(subHardware);
    46     }
    47 
    48     private void HardwareAdded(IHardware hardware) {
    49       foreach (ISensor sensor in hardware.Sensors)
    50         SensorAdded(sensor);
    51       hardware.SensorAdded += SensorAdded;
    52       hardware.SensorRemoved += SensorRemoved;
    53       foreach (IHardware subHardware in hardware.SubHardware)
    54         HardwareAdded(subHardware);
    55     }
    56 
    57     private void SensorAdded(ISensor sensor) {
    58       if (sensors == null)
    59         return;
    60 
    61       for (int i = 0; i < sensors.Length; i++) {
    62         if (sensor.Identifier.ToString() == identifiers[i])
    63           sensors[i] = sensor;
    64       }
    65     }
    66 
    67     private void SensorRemoved(ISensor sensor) {
    68       if (sensors == null)
    69         return;
    70 
    71       for (int i = 0; i < sensors.Length; i++) {
    72         if (sensor == sensors[i])
    73           sensors[i] = null;
    74       }
    75     }
    76 
    77     private static string GetFileName(DateTime date) {
    78       return AppDomain.CurrentDomain.BaseDirectory +
    79         Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
    80     }
    81 
    82     private bool OpenExistingLogFile() {
    83       if (!File.Exists(fileName))
    84         return false;
    85 
    86       try {
    87         String line;
    88         using (StreamReader reader = new StreamReader(fileName)) 
    89           line = reader.ReadLine(); 
    90        
    91         if (string.IsNullOrEmpty(line))
    92           return false;
    93         
    94         identifiers = line.Split(',').Skip(1).ToArray();
    95       } catch {
    96         identifiers = null;
    97         return false;
    98       }
    99 
   100       if (identifiers.Length == 0) {
   101         identifiers = null;
   102         return false;
   103       }
   104 
   105       sensors = new ISensor[identifiers.Length];
   106       SensorVisitor visitor = new SensorVisitor(sensor => {
   107         for (int i = 0; i < identifiers.Length; i++)
   108           if (sensor.Identifier.ToString() == identifiers[i])
   109             sensors[i] = sensor;
   110       });
   111       visitor.VisitComputer(computer);
   112       return true;
   113     }
   114 
   115     private void CreateNewLogFile() {
   116       IList<ISensor> list = new List<ISensor>();
   117       SensorVisitor visitor = new SensorVisitor(sensor => {
   118         list.Add(sensor);
   119       });
   120       visitor.VisitComputer(computer);
   121       sensors = list.ToArray();
   122       identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
   123 
   124       using (StreamWriter writer = new StreamWriter(fileName, false)) {
   125         writer.Write(",");
   126         for (int i = 0; i < sensors.Length; i++) {
   127           writer.Write(sensors[i].Identifier);
   128           if (i < sensors.Length - 1)
   129             writer.Write(",");
   130           else
   131             writer.WriteLine();
   132         }
   133 
   134         writer.Write("Time,");
   135         for (int i = 0; i < sensors.Length; i++) {
   136           writer.Write('"');
   137           writer.Write(sensors[i].Name);
   138           writer.Write('"');
   139           if (i < sensors.Length - 1)
   140             writer.Write(",");
   141           else
   142             writer.WriteLine();
   143         }
   144       }
   145     }
   146 
   147     public TimeSpan LoggingInterval { get; set; }
   148 
   149     public void Log() {      
   150       var now = DateTime.Now;
   151 
   152       if (lastLoggedTime + LoggingInterval - new TimeSpan(5000000) > now)
   153         return;      
   154 
   155       if (day != now.Date || !File.Exists(fileName)) {
   156         day = now.Date;
   157         fileName = GetFileName(day);
   158 
   159         if (!OpenExistingLogFile())
   160           CreateNewLogFile();
   161       }
   162 
   163       try {
   164         using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
   165           FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
   166           writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
   167           writer.Write(",");
   168           for (int i = 0; i < sensors.Length; i++) {
   169             if (sensors[i] != null) {
   170               float? value = sensors[i].Value;
   171               if (value.HasValue)
   172                 writer.Write(
   173                   value.Value.ToString("R", CultureInfo.InvariantCulture));
   174             }
   175             if (i < sensors.Length - 1)
   176               writer.Write(",");
   177             else
   178               writer.WriteLine();
   179           }
   180         }
   181       } catch (IOException) { }
   182 
   183       lastLoggedTime = now;
   184     }
   185   }
   186 }