Fixed a few stability issues in the logging implementation. Added support for logging sensors once the reappear.

This commit is contained in:
Michael Möller 2013-08-18 21:44:08 +00:00
parent 92d4954da6
commit 94cbde3e97
3 changed files with 53 additions and 40 deletions

View File

@ -150,6 +150,8 @@ namespace OpenHardwareMonitor.GUI {
wmiProvider = new WmiProvider(computer); wmiProvider = new WmiProvider(computer);
} }
logger = new Logger(computer);
plotColorPalette = new Color[13]; plotColorPalette = new Color[13];
plotColorPalette[0] = Color.Blue; plotColorPalette[0] = Color.Blue;
plotColorPalette[1] = Color.OrangeRed; plotColorPalette[1] = Color.OrangeRed;
@ -281,8 +283,7 @@ namespace OpenHardwareMonitor.GUI {
}; };
logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem, logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
settings); settings);
logger = new Logger(computer);
InitializePlotForm(); InitializePlotForm();
@ -497,6 +498,7 @@ namespace OpenHardwareMonitor.GUI {
Close(); Close();
} }
private int delayCount = 0;
private void timer_Tick(object sender, EventArgs e) { private void timer_Tick(object sender, EventArgs e) {
computer.Accept(updateVisitor); computer.Accept(updateVisitor);
treeView.Invalidate(); treeView.Invalidate();
@ -508,8 +510,12 @@ namespace OpenHardwareMonitor.GUI {
if (wmiProvider != null) if (wmiProvider != null)
wmiProvider.Update(); wmiProvider.Update();
if (logSensors.Value)
if (logSensors.Value && delayCount >= 4)
logger.Log(); logger.Log();
if (delayCount < 4)
delayCount++;
} }
private void SaveConfiguration() { private void SaveConfiguration() {

View File

@ -10,5 +10,5 @@
using System.Reflection; using System.Reflection;
[assembly: AssemblyVersion("0.6.0.8")] [assembly: AssemblyVersion("0.6.0.9")]
[assembly: AssemblyInformationalVersion("0.6.0.8 Alpha")] [assembly: AssemblyInformationalVersion("0.6.0.9 Alpha")]

View File

@ -25,6 +25,7 @@ namespace OpenHardwareMonitor.Utilities {
private DateTime day = DateTime.MinValue; private DateTime day = DateTime.MinValue;
private string fileName; private string fileName;
private string[] identifiers;
private ISensor[] sensors; private ISensor[] sensors;
public Logger(IComputer computer) { public Logger(IComputer computer) {
@ -34,6 +35,7 @@ namespace OpenHardwareMonitor.Utilities {
} }
private void HardwareRemoved(IHardware hardware) { private void HardwareRemoved(IHardware hardware) {
hardware.SensorAdded -= SensorAdded;
hardware.SensorRemoved -= SensorRemoved; hardware.SensorRemoved -= SensorRemoved;
foreach (ISensor sensor in hardware.Sensors) foreach (ISensor sensor in hardware.Sensors)
SensorRemoved(sensor); SensorRemoved(sensor);
@ -42,12 +44,28 @@ namespace OpenHardwareMonitor.Utilities {
} }
private void HardwareAdded(IHardware hardware) { private void HardwareAdded(IHardware hardware) {
foreach (ISensor sensor in hardware.Sensors)
SensorAdded(sensor);
hardware.SensorAdded += SensorAdded;
hardware.SensorRemoved += SensorRemoved; hardware.SensorRemoved += SensorRemoved;
foreach (IHardware subHardware in hardware.SubHardware) foreach (IHardware subHardware in hardware.SubHardware)
HardwareAdded(subHardware); HardwareAdded(subHardware);
} }
private void SensorAdded(ISensor sensor) {
if (sensors == null)
return;
for (int i = 0; i < sensors.Length; i++) {
if (sensor.Identifier.ToString() == identifiers[i])
sensors[i] = sensor;
}
}
private void SensorRemoved(ISensor sensor) { private void SensorRemoved(ISensor sensor) {
if (sensors == null)
return;
for (int i = 0; i < sensors.Length; i++) { for (int i = 0; i < sensors.Length; i++) {
if (sensor == sensors[i]) if (sensor == sensors[i])
sensors[i] = null; sensors[i] = null;
@ -59,27 +77,10 @@ namespace OpenHardwareMonitor.Utilities {
Path.DirectorySeparatorChar + string.Format(fileNameFormat, date); Path.DirectorySeparatorChar + string.Format(fileNameFormat, date);
} }
private void AddSensorRemovedHandler() {
for (int i = 0; i < sensors.Length; i++) {
ISensor sensor = sensors[i];
int index = i;
SensorEventHandler handler = null;
handler = s => {
if (s != sensor)
return;
sensors[index] = null;
sensor.Hardware.SensorRemoved -= handler;
};
sensor.Hardware.SensorRemoved += handler;
}
}
private bool OpenExistingLogFile() { private bool OpenExistingLogFile() {
if (!File.Exists(fileName)) if (!File.Exists(fileName))
return false; return false;
string[] identifiers;
try { try {
String line; String line;
using (StreamReader reader = new StreamReader(fileName)) using (StreamReader reader = new StreamReader(fileName))
@ -90,11 +91,14 @@ namespace OpenHardwareMonitor.Utilities {
identifiers = line.Split(',').Skip(1).ToArray(); identifiers = line.Split(',').Skip(1).ToArray();
} catch { } catch {
identifiers = null;
return false; return false;
} }
if (identifiers.Length == 0) if (identifiers.Length == 0) {
identifiers = null;
return false; return false;
}
sensors = new ISensor[identifiers.Length]; sensors = new ISensor[identifiers.Length];
SensorVisitor visitor = new SensorVisitor(sensor => { SensorVisitor visitor = new SensorVisitor(sensor => {
@ -112,7 +116,8 @@ namespace OpenHardwareMonitor.Utilities {
list.Add(sensor); list.Add(sensor);
}); });
visitor.VisitComputer(computer); visitor.VisitComputer(computer);
sensors = list.ToArray(); sensors = list.ToArray();
identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
using (StreamWriter writer = new StreamWriter(fileName, false)) { using (StreamWriter writer = new StreamWriter(fileName, false)) {
writer.Write(","); writer.Write(",");
@ -139,7 +144,7 @@ namespace OpenHardwareMonitor.Utilities {
public void Log() { public void Log() {
var now = DateTime.Now; var now = DateTime.Now;
if (day != now.Date) { if (day != now.Date || !File.Exists(fileName)) {
day = now.Date; day = now.Date;
fileName = GetFileName(day); fileName = GetFileName(day);
@ -147,23 +152,25 @@ namespace OpenHardwareMonitor.Utilities {
CreateNewLogFile(); CreateNewLogFile();
} }
using (StreamWriter writer = new StreamWriter(fileName, true)) { try {
writer.Write(now.ToString("G", CultureInfo.InvariantCulture)); using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
writer.Write(","); FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
for (int i = 0; i < sensors.Length; i++) { writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
if (sensors[i] != null) { writer.Write(",");
float? value = sensors[i].Value; for (int i = 0; i < sensors.Length; i++) {
if (value.HasValue) if (sensors[i] != null) {
writer.Write( float? value = sensors[i].Value;
value.Value.ToString("R", CultureInfo.InvariantCulture)); if (value.HasValue)
writer.Write(
value.Value.ToString("R", CultureInfo.InvariantCulture));
}
if (i < sensors.Length - 1)
writer.Write(",");
else
writer.WriteLine();
} }
if (i < sensors.Length - 1)
writer.Write(",");
else
writer.WriteLine();
} }
} } catch (IOException) { }
} }
} }
} }