source: branches/eraser6/Eraser/LogForm.cs @ 903

Revision 903, 3.7 KB checked in by lowjoel, 6 years ago (diff)

Ran Static code analysis on the Eraser project and implemented a few recommendations.

-Use more specific exceptions to allow code to determine the type of exception.
-Made GUIProgram implement IDisposable since the global mutex must be freed.
-Mde AboutForm? implement IDisposable, clearing the caching bitmaps on dispose.
-Made a few CommandLineProgram? functions static since they don't reference this.
-Name parameters/local with more unique, descriptive names for clarity.
-Use EventHandler?<EventArg?> instead of declaring our own delegate and event types as the type of the evenr handler

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29
30using Eraser.Manager;
31using System.Globalization;
32using Eraser.Util;
33
34namespace Eraser
35{
36    public partial class LogForm : Form
37    {
38        public LogForm(Task task)
39        {
40            InitializeComponent();
41            this.task = task;
42
43            //Update the title
44            Text = string.Format("{0} - {1}", Text, task.UIText);
45
46            //Add all the existing log messages
47            this.log.BeginUpdate();
48            Dictionary<DateTime, List<LogEntry>> log = task.Log.Entries;
49            foreach (DateTime sessionTime in log.Keys)
50            {
51                this.log.Groups.Add(new ListViewGroup(S._("Session: {0}", sessionTime.ToString(DATEPATTERN))));
52                foreach (LogEntry entry in log[sessionTime])
53                    task_Logged(entry);
54            }
55
56            //Register our event handler to get live log messages
57            task.Log.OnLogged += new Logger.LogEventFunction(task_Logged);
58            this.log.EndUpdate();
59        }
60
61        private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
62        {
63            task.Log.OnLogged -= new Logger.LogEventFunction(task_Logged);
64        }
65
66        private void task_Logged(LogEntry e)
67        {
68            if (InvokeRequired)
69            {
70                //Todo: I get crashes here... but alas, I can't fix it!
71                try
72                {
73                    Invoke(new Logger.LogEventFunction(task_Logged), new object[] { e });
74                }
75                catch (ObjectDisposedException)
76                {
77                }
78                catch (InvalidOperationException)
79                {
80                }
81                return;
82            }
83
84            ListViewItem item = log.Items.Add(e.Timestamp.ToString(DATEPATTERN));
85            item.SubItems.Add(e.Level.ToString());
86            item.SubItems.Add(e.Message);
87            if (log.Groups.Count != 0)
88                item.Group = log.Groups[log.Groups.Count - 1];
89
90            switch (e.Level)
91            {
92                case LogLevel.FATAL:
93                case LogLevel.ERROR:
94                    item.ForeColor = Color.Red;
95                    break;
96                case LogLevel.WARNING:
97                    item.ForeColor = Color.OrangeRed;
98                    break;
99            }
100        }
101
102        private void clear_Click(object sender, EventArgs e)
103        {
104            this.task.Log.Clear();
105            log.Items.Clear();
106        }
107
108        private void copy_Click(object sender, EventArgs e)
109        {
110            StringBuilder text = new StringBuilder();
111            Dictionary<DateTime, List<LogEntry>> logEntries = task.Log.Entries;
112            foreach (DateTime sessionTime in logEntries.Keys)
113            {
114                text.AppendLine(S._("Session: {0}", sessionTime.ToString(DATEPATTERN)));
115                foreach (LogEntry entry in logEntries[sessionTime])
116                {
117                    text.AppendFormat("{0}  {1} {2}\n",
118                        entry.Timestamp.ToString(DATEPATTERN).Replace("\"", "\"\""),
119                        entry.Level.ToString(), entry.Message);
120                }
121            }
122
123            Clipboard.SetText(text.ToString(), TextDataFormat.UnicodeText);
124        }
125
126        private void close_Click(object sender, EventArgs e)
127        {
128            Close();
129        }
130
131        private Task task;
132        private static string DATEPATTERN =
133            DateTimeFormatInfo.CurrentInfo.ShortDatePattern + " " +
134            DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
135    }
136}
Note: See TracBrowser for help on using the repository browser.