Sync: Adding shamap and documentation to enable easier sync next time OHM is updated.
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/.
7 Copyright (C) 2013 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
16 using OpenHardwareMonitor.Hardware;
18 namespace OpenHardwareMonitor.Utilities {
21 private const string fileNameFormat =
22 "OpenHardwareMonitorLog-{0:yyyy-MM-dd}.csv";
24 private readonly IComputer computer;
26 private DateTime day = DateTime.MinValue;
27 private string fileName;
28 private string[] identifiers;
29 private ISensor[] sensors;
31 private DateTime lastLoggedTime = DateTime.MinValue;
33 public Logger(IComputer computer) {
34 this.computer = computer;
35 this.computer.HardwareAdded += HardwareAdded;
36 this.computer.HardwareRemoved += HardwareRemoved;
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);
48 private void HardwareAdded(IHardware hardware) {
49 foreach (ISensor sensor in hardware.Sensors)
51 hardware.SensorAdded += SensorAdded;
52 hardware.SensorRemoved += SensorRemoved;
53 foreach (IHardware subHardware in hardware.SubHardware)
54 HardwareAdded(subHardware);
57 private void SensorAdded(ISensor sensor) {
61 for (int i = 0; i < sensors.Length; i++) {
62 if (sensor.Identifier.ToString() == identifiers[i])
67 private void SensorRemoved(ISensor sensor) {
71 for (int i = 0; i < sensors.Length; i++) {
72 if (sensor == sensors[i])
77 private static string GetFileName(DateTime date) {
78 return AppDomain.CurrentDomain.BaseDirectory +
79 Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
82 private bool OpenExistingLogFile() {
83 if (!File.Exists(fileName))
88 using (StreamReader reader = new StreamReader(fileName))
89 line = reader.ReadLine();
91 if (string.IsNullOrEmpty(line))
94 identifiers = line.Split(',').Skip(1).ToArray();
100 if (identifiers.Length == 0) {
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])
111 visitor.VisitComputer(computer);
115 private void CreateNewLogFile() {
116 IList<ISensor> list = new List<ISensor>();
117 SensorVisitor visitor = new SensorVisitor(sensor => {
120 visitor.VisitComputer(computer);
121 sensors = list.ToArray();
122 identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
124 using (StreamWriter writer = new StreamWriter(fileName, false)) {
126 for (int i = 0; i < sensors.Length; i++) {
127 writer.Write(sensors[i].Identifier);
128 if (i < sensors.Length - 1)
134 writer.Write("Time,");
135 for (int i = 0; i < sensors.Length; i++) {
137 writer.Write(sensors[i].Name);
139 if (i < sensors.Length - 1)
147 public TimeSpan LoggingInterval { get; set; }
150 var now = DateTime.Now;
152 if (lastLoggedTime + LoggingInterval - new TimeSpan(5000000) > now)
155 if (day != now.Date || !File.Exists(fileName)) {
157 fileName = GetFileName(day);
159 if (!OpenExistingLogFile())
164 using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
165 FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
166 writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
168 for (int i = 0; i < sensors.Length; i++) {
169 if (sensors[i] != null) {
170 float? value = sensors[i].Value;
173 value.Value.ToString("R", CultureInfo.InvariantCulture));
175 if (i < sensors.Length - 1)
181 } catch (IOException) { }
183 lastLoggedTime = now;