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

View File

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

View File

@ -25,6 +25,7 @@ namespace OpenHardwareMonitor.Utilities {
private DateTime day = DateTime.MinValue;
private string fileName;
private string[] identifiers;
private ISensor[] sensors;
public Logger(IComputer computer) {
@ -34,6 +35,7 @@ namespace OpenHardwareMonitor.Utilities {
}
private void HardwareRemoved(IHardware hardware) {
hardware.SensorAdded -= SensorAdded;
hardware.SensorRemoved -= SensorRemoved;
foreach (ISensor sensor in hardware.Sensors)
SensorRemoved(sensor);
@ -42,12 +44,28 @@ namespace OpenHardwareMonitor.Utilities {
}
private void HardwareAdded(IHardware hardware) {
foreach (ISensor sensor in hardware.Sensors)
SensorAdded(sensor);
hardware.SensorAdded += SensorAdded;
hardware.SensorRemoved += SensorRemoved;
foreach (IHardware subHardware in hardware.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) {
if (sensors == null)
return;
for (int i = 0; i < sensors.Length; i++) {
if (sensor == sensors[i])
sensors[i] = null;
@ -59,27 +77,10 @@ namespace OpenHardwareMonitor.Utilities {
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() {
if (!File.Exists(fileName))
return false;
string[] identifiers;
try {
String line;
using (StreamReader reader = new StreamReader(fileName))
@ -90,11 +91,14 @@ namespace OpenHardwareMonitor.Utilities {
identifiers = line.Split(',').Skip(1).ToArray();
} catch {
identifiers = null;
return false;
}
if (identifiers.Length == 0)
if (identifiers.Length == 0) {
identifiers = null;
return false;
}
sensors = new ISensor[identifiers.Length];
SensorVisitor visitor = new SensorVisitor(sensor => {
@ -113,6 +117,7 @@ namespace OpenHardwareMonitor.Utilities {
});
visitor.VisitComputer(computer);
sensors = list.ToArray();
identifiers = sensors.Select(s => s.Identifier.ToString()).ToArray();
using (StreamWriter writer = new StreamWriter(fileName, false)) {
writer.Write(",");
@ -139,7 +144,7 @@ namespace OpenHardwareMonitor.Utilities {
public void Log() {
var now = DateTime.Now;
if (day != now.Date) {
if (day != now.Date || !File.Exists(fileName)) {
day = now.Date;
fileName = GetFileName(day);
@ -147,7 +152,9 @@ namespace OpenHardwareMonitor.Utilities {
CreateNewLogFile();
}
using (StreamWriter writer = new StreamWriter(fileName, true)) {
try {
using (StreamWriter writer = new StreamWriter(new FileStream(fileName,
FileMode.Append, FileAccess.Write, FileShare.ReadWrite))) {
writer.Write(now.ToString("G", CultureInfo.InvariantCulture));
writer.Write(",");
for (int i = 0; i < sensors.Length; i++) {
@ -163,7 +170,7 @@ namespace OpenHardwareMonitor.Utilities {
writer.WriteLine();
}
}
} catch (IOException) { }
}
}
}