diff options
author | emkael <emkael@tlen.pl> | 2019-08-28 19:04:33 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2019-10-27 14:53:42 +0100 |
commit | fdd66605fac1f10401601f8faf4bedbe1b58ce1c (patch) | |
tree | 6982e32aa13cd51db0330d8a88849cf8d7727c11 | |
parent | 8af6e52d92921ecbfa48a46e8fa1940e57aa9256 (diff) |
Logging facility
-rw-r--r-- | spedytor/Form1.Designer.cs | 13 | ||||
-rw-r--r-- | spedytor/Form1.cs | 15 | ||||
-rw-r--r-- | spedytor/Form1.resx | 3 | ||||
-rw-r--r-- | spedytor/Logger.cs | 84 | ||||
-rw-r--r-- | spedytor/spedytor.csproj | 1 |
5 files changed, 110 insertions, 6 deletions
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 @@ <metadata name="tInterval.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <value>17, 17</value> </metadata> + <metadata name="tLogger.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <value>113, 17</value> + </metadata> <metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <value>True</value> </metadata> 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<string> messages; + private object queueLog; + + private Logger() + { + Logger.instance = this; + this.messages = new List<string>(); + 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<string> messages = new List<string>(); + 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 @@ <Compile Include="Form1.Designer.cs"> <DependentUpon>Form1.cs</DependentUpon> </Compile> + <Compile Include="Logger.cs" /> <Compile Include="MySQL.cs" /> <Compile Include="MysqlSettings.cs"> <SubType>Form</SubType> |