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

Revision 1057, 4.6 KB checked in by lowjoel, 5 years ago (diff)

This particular exception handler seems to be unnecessary now.

  • 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;
33using System.IO;
34
35namespace Eraser
36{
37    public partial class LogForm : Form
38    {
39        public LogForm(Task task)
40        {
41            InitializeComponent();
42            UXThemeApi.UpdateControlTheme(this);
43            this.task = task;
44
45            //Update the title
46            Text = string.Format(CultureInfo.InvariantCulture, "{0} - {1}", Text, task.UIText);
47
48            //Add all the existing log messages
49            this.log.BeginUpdate();
50            LogSessionDictionary log = task.Log.Entries;
51            foreach (DateTime sessionTime in log.Keys)
52            {
53                this.log.Groups.Add(new ListViewGroup(S._("Session: {0:F}", sessionTime)));
54                foreach (LogEntry entry in log[sessionTime])
55                    task_Logged(this, new LogEventArgs(entry));
56            }
57
58            //Register our event handler to get live log messages
59            task.Log.Logged += task_Logged;
60            this.log.EndUpdate();
61            EnableButtons();
62        }
63
64        private void LogForm_FormClosed(object sender, FormClosedEventArgs e)
65        {
66            task.Log.Logged -= task_Logged;
67        }
68
69        private void task_Logged(object sender, LogEventArgs e)
70        {
71            if (InvokeRequired)
72            {
73                Invoke(new EventHandler<LogEventArgs>(task_Logged), sender, e);
74                return;
75            }
76
77            ListViewItem item = log.Items.Add(e.LogEntry.Timestamp.ToString("F", CultureInfo.CurrentCulture));
78            item.SubItems.Add(e.LogEntry.Level.ToString());
79            item.SubItems.Add(e.LogEntry.Message);
80            if (log.Groups.Count != 0)
81                item.Group = log.Groups[log.Groups.Count - 1];
82
83            switch (e.LogEntry.Level)
84            {
85                case LogLevel.Fatal:
86                    item.ForeColor = Color.Red;
87                    break;
88                case LogLevel.Error:
89                    item.ForeColor = Color.OrangeRed;
90                    break;
91                case LogLevel.Warning:
92                    item.ForeColor = Color.Orange;
93                    break;
94            }
95
96            //Enable the clear and copy log buttons only if we have entries to copy.
97            EnableButtons();
98        }
99
100        private void clear_Click(object sender, EventArgs e)
101        {
102            task.Log.Clear();
103            log.Items.Clear();
104            EnableButtons();
105        }
106
107        private void copy_Click(object sender, EventArgs e)
108        {
109            StringBuilder csvText = new StringBuilder();
110            StringBuilder rawText = new StringBuilder();
111            LogSessionDictionary logEntries = task.Log.Entries;
112
113            foreach (DateTime sessionTime in logEntries.Keys)
114            {
115                csvText.AppendLine(S._("Session: {0:F}", sessionTime));
116                rawText.AppendLine(S._("Session: {0:F}", sessionTime));
117                foreach (LogEntry entry in logEntries[sessionTime])
118                {
119                    string timeStamp = entry.Timestamp.ToString("F", CultureInfo.CurrentCulture);
120                    string message = entry.Message;
121                    csvText.AppendFormat("\"{0}\",\"{1}\",\"{2}\"\n",
122                        timeStamp.Replace("\"", "\"\""), entry.Level.ToString(),
123                        message.Replace("\"", "\"\""));
124                    rawText.AppendFormat("{0}   {1} {2}", timeStamp, entry.Level.ToString(),
125                        message);
126                }
127            }
128
129            if (csvText.Length > 0 || rawText.Length > 0)
130            {
131                //Set the simple text data for data-unaware applications like Word
132                DataObject tableText = new DataObject();
133                tableText.SetText(rawText.ToString());
134
135                //Then a UTF-8 stream CSV for Excel
136                byte[] bytes = Encoding.UTF8.GetBytes(csvText.ToString());
137                MemoryStream tableStream = new MemoryStream(bytes);
138                tableText.SetData(DataFormats.CommaSeparatedValue, tableStream);
139
140                //Set the clipboard
141                Clipboard.SetDataObject(tableText, true);
142            }
143        }
144
145        private void close_Click(object sender, EventArgs e)
146        {
147            Close();
148        }
149
150        /// <summary>
151        /// Enables/disables buttons based on the current system state.
152        /// </summary>
153        private void EnableButtons()
154        {
155            clear.Enabled = copy.Enabled = task.Log.Entries.Count > 0;
156        }
157
158        /// <summary>
159        /// The task which this log is displaying entries for
160        /// </summary>
161        private Task task;
162    }
163}
Note: See TracBrowser for help on using the repository browser.