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

Revision 974, 4.1 KB checked in by lowjoel, 5 years ago (diff)

-Don't change the dialog font and add the bevel line.
-Factor out the code dealing with copy/clear button enabling to the EnableButtons? function.

  • 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            UxThemeAPI.UpdateControlTheme(this);
42            this.task = task;
43
44            //Update the title
45            Text = string.Format(CultureInfo.InvariantCulture, "{0} - {1}", Text, task.UIText);
46
47            //Add all the existing log messages
48            this.log.BeginUpdate();
49            LogSessionDictionary log = task.Log.Entries;
50            foreach (DateTime sessionTime in log.Keys)
51            {
52                this.log.Groups.Add(new ListViewGroup(S._("Session: {0:F}", sessionTime)));
53                foreach (LogEntry entry in log[sessionTime])
54                    task_Logged(this, new LogEventArgs(entry));
55            }
56
57            //Register our event handler to get live log messages
58            task.Log.Logged += task_Logged;
59            this.log.EndUpdate();
60            EnableButtons();
61        }
62
63        private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
64        {
65            task.Log.Logged -= task_Logged;
66        }
67
68        private void task_Logged(object sender, LogEventArgs e)
69        {
70            if (InvokeRequired)
71            {
72                //Todo: I get crashes here... but alas, I can't fix it!
73                try
74                {
75                    Invoke(new EventHandler<LogEventArgs>(task_Logged), sender, e);
76                }
77                catch (ObjectDisposedException)
78                {
79                }
80                catch (InvalidOperationException)
81                {
82                }
83                return;
84            }
85
86            ListViewItem item = log.Items.Add(e.LogEntry.Timestamp.ToString("F", CultureInfo.CurrentCulture));
87            item.SubItems.Add(e.LogEntry.Level.ToString());
88            item.SubItems.Add(e.LogEntry.Message);
89            if (log.Groups.Count != 0)
90                item.Group = log.Groups[log.Groups.Count - 1];
91
92            switch (e.LogEntry.Level)
93            {
94                case LogLevel.Fatal:
95                    item.ForeColor = Color.Red;
96                    break;
97                case LogLevel.Error:
98                    item.ForeColor = Color.OrangeRed;
99                    break;
100                case LogLevel.Warning:
101                    item.ForeColor = Color.Orange;
102                    break;
103            }
104
105            //Enable the clear and copy log buttons only if we have entries to copy.
106            EnableButtons();
107        }
108
109        private void clear_Click(object sender, EventArgs e)
110        {
111            task.Log.Clear();
112            log.Items.Clear();
113            EnableButtons();
114        }
115
116        private void copy_Click(object sender, EventArgs e)
117        {
118            StringBuilder text = new StringBuilder();
119            LogSessionDictionary logEntries = task.Log.Entries;
120            foreach (DateTime sessionTime in logEntries.Keys)
121            {
122                text.AppendLine(S._("Session: {0:F}", sessionTime));
123                foreach (LogEntry entry in logEntries[sessionTime])
124                {
125                    text.AppendFormat("{0}  {1} {2}\n",
126                        entry.Timestamp.ToString("F", CultureInfo.CurrentCulture).Replace("\"", "\"\""),
127                        entry.Level.ToString(), entry.Message);
128                }
129            }
130
131            if (text.Length > 0)
132                Clipboard.SetText(text.ToString(), TextDataFormat.CommaSeparatedValue);
133        }
134
135        private void close_Click(object sender, EventArgs e)
136        {
137            Close();
138        }
139
140        /// <summary>
141        /// Enables/disables buttons based on the current system state.
142        /// </summary>
143        private void EnableButtons()
144        {
145            clear.Enabled = copy.Enabled = task.Log.Entries.Count > 0;
146        }
147
148        /// <summary>
149        /// The task which this log is displaying entries for
150        /// </summary>
151        private Task task;
152    }
153}
Note: See TracBrowser for help on using the repository browser.