Code cleanup

This commit is contained in:
VladMordock
2021-02-17 18:25:59 +01:00
parent bf2d436fef
commit e8ee60ef5e
4 changed files with 34 additions and 43 deletions

View File

@@ -6,62 +6,50 @@ using System.Management;
class HardwareMonitorClass
{
// Class for HW monitoring
protected PerformanceCounter PerformanceCounterCPU;
protected PerformanceCounter PerformanceCounterRAM;
protected PerformanceCounter cpuCounter;
protected PerformanceCounter ramCounter;
public string LastCurrentCpuUsage = "-1"; // Last measured CPU usage
public string LastCurrentRamUsage = "-1"; // Last measured RAM usage
public HardwareMonitorClass()
{
// Prepare CPU counter
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
PerformanceCounterCPU = new PerformanceCounter();
PerformanceCounterCPU.CategoryName = "Processor";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = "_Total";
// Prepare RAM counter
ramCounter = new PerformanceCounter();
ramCounter.CategoryName = "Memory";
ramCounter.CounterName = "% Committed Bytes In Use";
PerformanceCounterRAM = new PerformanceCounter();
PerformanceCounterRAM.CategoryName = "Memory";
PerformanceCounterRAM.CounterName = "% Committed Bytes In Use";
}
// Get CPU usage
public string getCurrentCpuUsage()
{
return cpuCounter.NextValue().ToString("0.00");
LastCurrentCpuUsage = PerformanceCounterCPU.NextValue().ToString("0.00");
return LastCurrentCpuUsage;
}
// Get available RAM memory
public string getAvailableRAM()
public string getCurrentRamUsage()
{
return ramCounter.NextValue().ToString("0.00");
LastCurrentRamUsage = PerformanceCounterRAM.NextValue().ToString("0.00");
return LastCurrentRamUsage;
}
}
class CurrentMissionClass
{
// Class holding current mission/DCS server status
public string Theatre = ""; // Mission theathre
public string Mission = ""; // Mission name
public string Pause = ""; // Mission pause
public int PlayerCount = 0; // Actual player time
public string ModelTime = "";
public string RealTime = "";
public string ToInfoString()
{
// Return mission information as string
if (this.Mission != "")
{
// Return mission information as string
return "Mission: " + this.Mission + "(" + this.Theatre + ") Pause:" + this.Pause + " Players: " + this.PlayerCount;
} else
{
// Mission information is not available
return "Mission: Unknown";
}
}
public string ModelTime = ""; // Mission time
public string RealTime = ""; // Server time
}
internal class Globals
@@ -88,8 +76,8 @@ internal class Globals
public static string VersionDatabase = ""; // Version - Database
public static string VersionPerun = "DEBUG"; // Version - Perun
public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information
public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information single ton
public static HardwareMonitorClass HardwareMonitor = new HardwareMonitorClass();
public static HardwareMonitorClass HardwareMonitor = new HardwareMonitorClass(); // Hardware monitor singleton
}

View File

@@ -1,9 +1,9 @@
using System;
using System.IO;
// Singleton class - log controller
class LogController
{
// Singleton class - log controller
private static LogController _instance = new LogController(); // Singleton instance
public int level; // Level of logging
@@ -17,8 +17,10 @@ class LogController
public void WriteLog(int logLevel, string strLog)
{
// Write log entry to file
if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit
// Declare variables
StreamWriter LogStreamWriter;
FileStream LogFileStream = null;
DirectoryInfo LogDirectoryInfo = null;
@@ -41,7 +43,7 @@ class LogController
}
catch
{
// Do nothing
// Do nothing - TBD error handling
}
}
try
@@ -52,7 +54,7 @@ class LogController
}
catch
{
// Do nothing
// Do nothing - TBD error handling
}
// TBD: log rotation

View File

@@ -35,6 +35,7 @@ internal class PerunHelper
// Declare values
string LogDirection;
string LogMarker;
// Set direction marker
switch (intDirection)
{
@@ -76,7 +77,7 @@ internal class PerunHelper
// Set frame type
strType = strType.PadLeft(3, ' ');
if (!bSkipGui)
if (!bSkipGui) // Shall we skip GUI?
{
// Rotate log history
for (int i = 0; i < arrLogHistory.Length - 1; i++)
@@ -85,13 +86,13 @@ internal class PerunHelper
}
// Add new entry
arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss") + " " + LogDirection + " " + LogType + " " + strEntryToAdd; // Add entry at the last position
arrLogHistory[arrLogHistory.Length - 1] = DateTime.Now.ToString("HH:mm:ss.fff") + " " + LogDirection + " " + LogType + " " + strEntryToAdd; // Add entry at the last position
// Update control at my window
Globals.AppUpdateGUI = true;
}
// Add the entry to log file
LogController.instance.WriteLog(logLevel, DateTime.Now.ToString("yyyy-MM-dd ") + " " + DateTime.Now.ToString("HH:mm:ss") + " | Instance: " + Globals.AppInstanceID + " | " + LogMarker + " | " + LogDirection + " | " + strType + " | " + strEntryToAdd);
LogController.instance.WriteLog(logLevel, DateTime.Now.ToString("yyyy-MM-dd ") + " " + DateTime.Now.ToString("HH:mm:ss.fff") + " | Instance: " + Globals.AppInstanceID + " | " + LogMarker + " | " + LogDirection + " | " + strType + " | " + strEntryToAdd);
}
// Gets build version
@@ -141,7 +142,7 @@ internal class PerunHelper
}
public static string ConvertSecoundsToString (Double NumberOfSecounds){
// TBD
// TBD - convert number of secounds to HHhMMm format
return NumberOfSecounds.ToString();
}
}