From fdd66605fac1f10401601f8faf4bedbe1b58ce1c Mon Sep 17 00:00:00 2001 From: emkael Date: Wed, 28 Aug 2019 19:04:33 +0200 Subject: Logging facility --- spedytor/Form1.Designer.cs | 13 +++++-- spedytor/Form1.cs | 15 ++++++--- spedytor/Form1.resx | 3 ++ spedytor/Logger.cs | 84 ++++++++++++++++++++++++++++++++++++++++++++++ spedytor/spedytor.csproj | 1 + 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 spedytor/Logger.cs diff --git a/spedytor/Form1.Designer.cs b/spedytor/Form1.Designer.cs index 6dafac8..f411cae 100644 --- a/spedytor/Form1.Designer.cs +++ b/spedytor/Form1.Designer.cs @@ -42,6 +42,7 @@ this.bSave = new System.Windows.Forms.Button(); this.bToggleLog = new System.Windows.Forms.Button(); this.tbLog = new System.Windows.Forms.TextBox(); + this.tLogger = new System.Windows.Forms.Timer(this.components); ((System.ComponentModel.ISupportInitialize)(this.nInterval)).BeginInit(); this.SuspendLayout(); // @@ -169,12 +170,19 @@ // // tbLog // - this.tbLog.Location = new System.Drawing.Point(12, 211); + this.tbLog.Location = new System.Drawing.Point(13, 210); this.tbLog.Multiline = true; this.tbLog.Name = "tbLog"; - this.tbLog.Size = new System.Drawing.Size(282, 231); + this.tbLog.ReadOnly = true; + this.tbLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; + this.tbLog.Size = new System.Drawing.Size(282, 229); this.tbLog.TabIndex = 11; // + // tLogger + // + this.tLogger.Enabled = true; + this.tLogger.Tick += new System.EventHandler(this.tLogger_Tick); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -217,6 +225,7 @@ private System.Windows.Forms.Timer tInterval; private System.Windows.Forms.Button bToggleLog; private System.Windows.Forms.TextBox tbLog; + private System.Windows.Forms.Timer tLogger; } } diff --git a/spedytor/Form1.cs b/spedytor/Form1.cs index 603e78d..f3f47c6 100644 --- a/spedytor/Form1.cs +++ b/spedytor/Form1.cs @@ -12,6 +12,8 @@ namespace spedytor { public partial class Form1 : Form { + const string LOG_FILENAME = "spedytor.log"; + public Form1() { InitializeComponent(); @@ -89,17 +91,17 @@ namespace spedytor MySQL c = new MySQL(dbName); c.backup(filePath); c.close(); - MessageBox.Show("Wyeksportowano pomyślnie do pliku: " + filePath, "Sukces eksportu!", MessageBoxButtons.OK, MessageBoxIcon.Information); + Logger.getLogger(this.tbLog, LOG_FILENAME).log("Wyeksportowano pomyślnie do pliku: " + filePath); if (s3Bucket != null) { S3 s3Client = new S3(); s3Client.send(s3Bucket, filePath, dbName + ".sql"); - MessageBox.Show("Wysłano!", "Sukces eksportu!", MessageBoxButtons.OK, MessageBoxIcon.Information); + Logger.getLogger(this.tbLog, LOG_FILENAME).log("Wysłano!"); } } catch (Exception ex) { - MessageBox.Show(ex.Message, "Błąd eksportu!", MessageBoxButtons.OK, MessageBoxIcon.Error); + Logger.getLogger(this.tbLog, LOG_FILENAME).log(ex.Message); } } @@ -107,7 +109,7 @@ namespace spedytor { foreach (Control control in this.Controls) { - if (control != sender) + if (control != sender && control != this.tbLog && control != this.bToggleLog) { control.Enabled = state; } @@ -174,5 +176,10 @@ namespace spedytor this.prevHeight = this.Height; } } + + private void tLogger_Tick(object sender, EventArgs e) + { + Logger.getLogger(this.tbLog, LOG_FILENAME).tick(); + } } } diff --git a/spedytor/Form1.resx b/spedytor/Form1.resx index e1a8c87..4332045 100644 --- a/spedytor/Form1.resx +++ b/spedytor/Form1.resx @@ -120,6 +120,9 @@ 17, 17 + + 113, 17 + True diff --git a/spedytor/Logger.cs b/spedytor/Logger.cs new file mode 100644 index 0000000..c32d91a --- /dev/null +++ b/spedytor/Logger.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Forms; +using System.IO; + +namespace spedytor +{ + class Logger + { + private static Logger instance; + private StreamWriter file; + private TextBox field; + private List messages; + private object queueLog; + + private Logger() + { + Logger.instance = this; + this.messages = new List(); + this.queueLog = new object(); + } + + private void setFile(string filename) { + if (this.file == null) + { + this.file = new StreamWriter(filename); + } + } + + private void setField(TextBox field) + { + this.field = field; + } + + public static Logger getLogger(TextBox textField = null, string file = null) + { + if (Logger.instance == null) + { + Logger.instance = new Logger(); + } + if (file != null) + { + Logger.instance.setFile(file); + } + if (textField != null) + { + Logger.instance.setField(textField); + } + return Logger.instance; + } + + public void log(string message) + { + lock (this.queueLog) + { + this.messages.Add(message); + } + } + + public void tick() + { + List messages = new List(); + lock (this.queueLog) + { + messages = this.messages.GetRange(0, this.messages.Count); + this.messages.Clear(); + } + foreach (string message in messages) + { + string line = String.Format("[{0}] {1}{2}", DateTime.Now.ToString(), message, Environment.NewLine); + if (this.field != null) + { + this.field.Text += line; + } + if (this.file != null) + { + this.file.Write(line); + } + } + } + } +} diff --git a/spedytor/spedytor.csproj b/spedytor/spedytor.csproj index 76534c7..4915ab8 100644 --- a/spedytor/spedytor.csproj +++ b/spedytor/spedytor.csproj @@ -76,6 +76,7 @@ Form1.cs + Form -- cgit v1.2.3