From f7a9301015a9b2819196b2c60fdd8586c76f521a Mon Sep 17 00:00:00 2001 From: Szymon Porwolik Date: Wed, 17 Feb 2021 23:40:23 +0100 Subject: [PATCH] Added settings option for log rotation --- 02_Windows_App/Perun_v1/01_Classes/Globals.cs | 3 ++- .../Perun_v1/01_Classes/LogController.cs | 15 +++++++++------ .../Perun_v1/02_Forms/form_Main.Designer.cs | 17 +++++++++++++++++ 02_Windows_App/Perun_v1/02_Forms/form_Main.cs | 14 ++++++++++++++ .../Perun_v1/Properties/Settings.Designer.cs | 12 ++++++++++++ .../Perun_v1/Properties/Settings.settings | 3 +++ 02_Windows_App/Perun_v1/app.config | 3 +++ 7 files changed, 60 insertions(+), 7 deletions(-) diff --git a/02_Windows_App/Perun_v1/01_Classes/Globals.cs b/02_Windows_App/Perun_v1/01_Classes/Globals.cs index 722b6af..1261c2e 100644 --- a/02_Windows_App/Perun_v1/01_Classes/Globals.cs +++ b/02_Windows_App/Perun_v1/01_Classes/Globals.cs @@ -78,7 +78,8 @@ internal class Globals public static string VersionDatabase = ""; // Version - Database public static string VersionPerun = "DEBUG"; // Version - Perun - public static string LastLogLocation = ""; // Last used log location + public static string LastLogLocation = ""; // Last used log location + public static bool RotateLogs = false; // Rotate logs true/false public static CurrentMissionClass CurrentMission = new CurrentMissionClass(); // Actual mission information single ton diff --git a/02_Windows_App/Perun_v1/01_Classes/LogController.cs b/02_Windows_App/Perun_v1/01_Classes/LogController.cs index e7aee34..9dad7c1 100644 --- a/02_Windows_App/Perun_v1/01_Classes/LogController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/LogController.cs @@ -59,14 +59,17 @@ class LogController } // Delete old files - log rotation - DirectoryInfo di = new DirectoryInfo(LogFileDir); - FileInfo[] files = di.GetFiles("*.log"); - - foreach (FileInfo fi in files) + if (Globals.RotateLogs) { - if (fi.LastAccessTime < DateTime.Now.AddDays(-7)) + DirectoryInfo di = new DirectoryInfo(LogFileDir); + FileInfo[] files = di.GetFiles("*.log"); + + foreach (FileInfo fi in files) { - fi.Delete(); + if (fi.LastAccessTime < DateTime.Now.AddDays(-7)) + { + fi.Delete(); + } } } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs index 6479982..eeb5344 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.Designer.cs @@ -101,6 +101,7 @@ this.label20 = new System.Windows.Forms.Label(); this.tim_HW_status = new System.Windows.Forms.Timer(this.components); this.con_Button_See_log = new System.Windows.Forms.Button(); + this.con_check_delete_logs = new System.Windows.Forms.CheckBox(); this.con_GroupBox_1.SuspendLayout(); this.con_GroupBox_2.SuspendLayout(); this.con_GroupBox_3.SuspendLayout(); @@ -621,6 +622,7 @@ // // con_GroupBox_5 // + this.con_GroupBox_5.Controls.Add(this.con_check_delete_logs); this.con_GroupBox_5.Controls.Add(this.con_check_minimize_to_tray); this.con_GroupBox_5.Location = new System.Drawing.Point(13, 370); this.con_GroupBox_5.Name = "con_GroupBox_5"; @@ -816,6 +818,20 @@ this.con_Button_See_log.UseVisualStyleBackColor = true; this.con_Button_See_log.Click += new System.EventHandler(this.con_Button_See_log_Click); // + // con_check_delete_logs + // + this.con_check_delete_logs.AutoSize = true; + this.con_check_delete_logs.Checked = true; + this.con_check_delete_logs.CheckState = System.Windows.Forms.CheckState.Checked; + this.con_check_delete_logs.Location = new System.Drawing.Point(105, 19); + this.con_check_delete_logs.Name = "con_check_delete_logs"; + this.con_check_delete_logs.Size = new System.Drawing.Size(138, 17); + this.con_check_delete_logs.TabIndex = 25; + this.con_check_delete_logs.Text = "Rotate logs after 7 days"; + this.con_check_delete_logs.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; + this.con_check_delete_logs.UseVisualStyleBackColor = true; + this.con_check_delete_logs.Validated += new System.EventHandler(this.con_check_delete_logs_Validated); + // // form_Main // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -945,6 +961,7 @@ private System.Windows.Forms.Label label20; private System.Windows.Forms.Timer tim_HW_status; private System.Windows.Forms.Button con_Button_See_log; + private System.Windows.Forms.CheckBox con_check_delete_logs; } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index 70a38bb..5ff0b59 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -127,6 +127,7 @@ namespace Perun_v1 con_txt_dcs_instance.Text = Properties.Settings.Default.DCS_Instance.ToString(); con_com_loglevel.SelectedIndex = Properties.Settings.Default.OTHER_Log_Level; con_check_minimize_to_tray.Checked = Properties.Settings.Default.OTHER_Minimize_to_Tray; + con_check_delete_logs.Checked = Properties.Settings.Default.OTHER_Log_Rotate; } private void form_Main_SaveSettings() @@ -147,6 +148,7 @@ namespace Perun_v1 Properties.Settings.Default.DCS_Instance = Int32.Parse(con_txt_dcs_instance.Text); Properties.Settings.Default.OTHER_Log_Level = con_com_loglevel.SelectedIndex; Properties.Settings.Default.OTHER_Minimize_to_Tray = con_check_minimize_to_tray.Checked; + Properties.Settings.Default.OTHER_Log_Rotate = con_check_delete_logs.Checked; Properties.Settings.Default.Save(); } @@ -355,6 +357,12 @@ namespace Perun_v1 Properties.Settings.Default.OTHER_Minimize_to_Tray = con_check_minimize_to_tray.Checked; } + private void con_check_delete_logs_Validated(object sender, EventArgs e) + { + // Changed rotate logs state + Properties.Settings.Default.OTHER_Log_Rotate = con_check_delete_logs.Checked; + } + private void con_Button_Reset_Flags_Click(object sender, EventArgs e) { // Reset error flags @@ -572,6 +580,10 @@ namespace Perun_v1 Globals.StatusHistoryConnection = Globals.StatusConnection; Globals.ErrorsHistoryGame = Globals.ErrorsGame; } + + // Check if we shall rate logs + Globals.RotateLogs = con_check_minimize_to_tray.Checked; + // Update status icons at main form - SRS if ((ExtSRSStatus != Globals.StatusSRS) || Globals.AppForceIconReload) { @@ -776,5 +788,7 @@ namespace Perun_v1 Process.Start(Globals.LastLogLocation); } } + + } } diff --git a/02_Windows_App/Perun_v1/Properties/Settings.Designer.cs b/02_Windows_App/Perun_v1/Properties/Settings.Designer.cs index 01c1ec2..2f448a8 100644 --- a/02_Windows_App/Perun_v1/Properties/Settings.Designer.cs +++ b/02_Windows_App/Perun_v1/Properties/Settings.Designer.cs @@ -178,5 +178,17 @@ namespace Perun_v1.Properties { this["OTHER_Minimize_to_Tray"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool OTHER_Log_Rotate { + get { + return ((bool)(this["OTHER_Log_Rotate"])); + } + set { + this["OTHER_Log_Rotate"] = value; + } + } } } diff --git a/02_Windows_App/Perun_v1/Properties/Settings.settings b/02_Windows_App/Perun_v1/Properties/Settings.settings index 34ec5fa..99680ba 100644 --- a/02_Windows_App/Perun_v1/Properties/Settings.settings +++ b/02_Windows_App/Perun_v1/Properties/Settings.settings @@ -41,5 +41,8 @@ True + + True + \ No newline at end of file diff --git a/02_Windows_App/Perun_v1/app.config b/02_Windows_App/Perun_v1/app.config index e6ee353..db0c0f9 100644 --- a/02_Windows_App/Perun_v1/app.config +++ b/02_Windows_App/Perun_v1/app.config @@ -46,6 +46,9 @@ True + + True +