Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions 02_Windows_App/Perun_v1/01_Classes/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,24 @@ public int SendToMySql(string RawTCPFrame, bool CheckConnection = false)
}

// Parse frame
dynamic TCPFrame = JsonConvert.DeserializeObject(RawTCPFrame); // Deserialize raw data
string TCPFrameType = TCPFrame.type; // Frame type
string TCPFrameTimestamp = (TCPFrame.timestamp != null) ? $"'{TCPFrame.timestamp}'" : "CURRENT_TIMESTAMP()"; // Frame timestamp (Some frames may come without timestamp, use database timestamp then)
string TCPFrameInstance = TCPFrame.instance; // Frame instance
string TCPFramePayload; // Frame payload (we will deserialise only for specific frame types)
dynamic TCPFrame;
string TCPFrameType;
string TCPFrameTimestamp;
string TCPFrameInstance;
try
{
TCPFrame = JsonConvert.DeserializeObject(RawTCPFrame); // Deserialize raw data
TCPFrameType = TCPFrame.type;
TCPFrameTimestamp = (TCPFrame.timestamp != null) ? $"'{TCPFrame.timestamp}'" : "CURRENT_TIMESTAMP()"; // Frame timestamp (Some frames may come without timestamp, use database timestamp then)
TCPFrameInstance = TCPFrame.instance;
}
catch (Exception)
{
LogController.instance.LogError("ERROR - SendToMySql cannot deserialize tcp frame !", RawTCPFrame);
return 0;
}

string TCPFramePayload; // Frame payload (we will deserialise only for specific frame types)

// Connect to mysql and execute sql
try
Expand Down
35 changes: 33 additions & 2 deletions 02_Windows_App/Perun_v1/01_Classes/LogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,28 @@ public static LogController instance
}
}

public void WriteLog(int logLevel, string strLog)
public void LogError(string strLog, string content = null)
{
this.WriteLog(0, strLog, content);
}

public void LogWarning(string strLog, string content = null)
{
this.WriteLog(1, strLog, content);
}

public void LogInfo(string strLog, string content = null)
{
this.WriteLog(2, strLog, content);
}

public void LogDebug(string strLog, string content = null)
{
this.WriteLog(3, strLog, content);
}

// TBD - done via https://stackoverflow.com/questions/20185015/how-to-write-log-file-in-c
public void WriteLog(int logLevel, string strLog, string content = null)
{
// Write log entry to file
if (logLevel > this.level) return; // Check if we shall log it with the current log level, if not - exit
Expand All @@ -25,6 +46,7 @@ public void WriteLog(int logLevel, string strLog)
FileStream LogFileStream = null;
DirectoryInfo LogDirectoryInfo = null;
FileInfo LogFileInfo;
string message = strLog;

string LogFileDir = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents") + "\\Perun\\";
string LogFilePath = LogFileDir + "Perun_Log_" + Globals.AppInstanceID + "_" + System.DateTime.Today.ToString("yyyyMMdd") + "." + "log";
Expand All @@ -46,10 +68,19 @@ public void WriteLog(int logLevel, string strLog)
// Do nothing - TBD error handling
}
}

try
{
if (content != null)
{
// write the content to a file and add the name of this file to the message
string contentFilename = "Perun_LogContent_" + System.DateTime.Today.ToString("o").Replace('T', '-').Replace(':', '-').Replace('+','-') + "." + "txt";
string contentFilepath = LogFileInfo + contentFilename;
File.WriteAllText(contentFilepath, content);
message = strLog + " - content stored in " + contentFilename;
}
LogStreamWriter = new StreamWriter(LogFileStream);
LogStreamWriter.WriteLine(strLog);
LogStreamWriter.WriteLine(message);
LogStreamWriter.Close();
Globals.LastLogLocation = LogFilePath;
}
Expand Down
5 changes: 2 additions & 3 deletions 02_Windows_App/Perun_v1/01_Classes/PerunHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public static void LogDebug(ref string[] arrLogHistory, string strEntryToAdd, in
AddLog(3, ref arrLogHistory, strEntryToAdd, intDirection, intMarker, strType, bSkipGui);
}

// Add log
private static void AddLog(int logLevel, ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false)
private static void AddLog(int logLevel, ref string[] arrLogHistory, string strEntryToAdd, int intDirection = 0, int intMarker = 0, string strType = " ", bool bSkipGui = false, string content = null)
{
// Declare values
string LogDirection;
Expand Down Expand Up @@ -106,7 +105,7 @@ private static void AddLog(int logLevel, ref string[] arrLogHistory, string strE
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.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.LastFrameTime.ToString("000000.")} | {Globals.LastFrameDelay.ToString("000000.")} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}");
LogController.instance.WriteLog(logLevel, $"{DateTime.Now.ToString("yyyy-MM-dd ")} {DateTime.Now.ToString("HH:mm:ss.fff")} | {Globals.HardwareMonitor.LastCurrentCpuUsage} | {Globals.HardwareMonitor.LastCurrentRamUsage} | {Globals.LastFrameTime.ToString("000000.")} | {Globals.LastFrameDelay.ToString("000000.")} | {Globals.AppInstanceID} | {LogMarker} | {LogDirection} | {strType} | {strEntryToAdd}", content);
}

// Gets build version
Expand Down