| 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 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.ComponentModel; |
|---|
| 25 | using System.Data; |
|---|
| 26 | using System.Drawing; |
|---|
| 27 | using System.Text; |
|---|
| 28 | using System.Windows.Forms; |
|---|
| 29 | |
|---|
| 30 | using Eraser.Manager; |
|---|
| 31 | using System.Globalization; |
|---|
| 32 | using Eraser.Util; |
|---|
| 33 | using System.IO; |
|---|
| 34 | |
|---|
| 35 | namespace 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 | //Todo: I get crashes here... but alas, I can't fix it! |
|---|
| 74 | try |
|---|
| 75 | { |
|---|
| 76 | Invoke(new EventHandler<LogEventArgs>(task_Logged), sender, e); |
|---|
| 77 | } |
|---|
| 78 | catch (ObjectDisposedException) |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | catch (InvalidOperationException) |
|---|
| 82 | { |
|---|
| 83 | } |
|---|
| 84 | return; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | ListViewItem item = log.Items.Add(e.LogEntry.Timestamp.ToString("F", CultureInfo.CurrentCulture)); |
|---|
| 88 | item.SubItems.Add(e.LogEntry.Level.ToString()); |
|---|
| 89 | item.SubItems.Add(e.LogEntry.Message); |
|---|
| 90 | if (log.Groups.Count != 0) |
|---|
| 91 | item.Group = log.Groups[log.Groups.Count - 1]; |
|---|
| 92 | |
|---|
| 93 | switch (e.LogEntry.Level) |
|---|
| 94 | { |
|---|
| 95 | case LogLevel.Fatal: |
|---|
| 96 | item.ForeColor = Color.Red; |
|---|
| 97 | break; |
|---|
| 98 | case LogLevel.Error: |
|---|
| 99 | item.ForeColor = Color.OrangeRed; |
|---|
| 100 | break; |
|---|
| 101 | case LogLevel.Warning: |
|---|
| 102 | item.ForeColor = Color.Orange; |
|---|
| 103 | break; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | //Enable the clear and copy log buttons only if we have entries to copy. |
|---|
| 107 | EnableButtons(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | private void clear_Click(object sender, EventArgs e) |
|---|
| 111 | { |
|---|
| 112 | task.Log.Clear(); |
|---|
| 113 | log.Items.Clear(); |
|---|
| 114 | EnableButtons(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | private void copy_Click(object sender, EventArgs e) |
|---|
| 118 | { |
|---|
| 119 | StringBuilder csvText = new StringBuilder(); |
|---|
| 120 | StringBuilder rawText = new StringBuilder(); |
|---|
| 121 | LogSessionDictionary logEntries = task.Log.Entries; |
|---|
| 122 | |
|---|
| 123 | foreach (DateTime sessionTime in logEntries.Keys) |
|---|
| 124 | { |
|---|
| 125 | csvText.AppendLine(S._("Session: {0:F}", sessionTime)); |
|---|
| 126 | rawText.AppendLine(S._("Session: {0:F}", sessionTime)); |
|---|
| 127 | foreach (LogEntry entry in logEntries[sessionTime]) |
|---|
| 128 | { |
|---|
| 129 | string timeStamp = entry.Timestamp.ToString("F", CultureInfo.CurrentCulture); |
|---|
| 130 | string message = entry.Message; |
|---|
| 131 | csvText.AppendFormat("\"{0}\",\"{1}\",\"{2}\"\n", |
|---|
| 132 | timeStamp.Replace("\"", "\"\""), entry.Level.ToString(), |
|---|
| 133 | message.Replace("\"", "\"\"")); |
|---|
| 134 | rawText.AppendFormat("{0} {1} {2}", timeStamp, entry.Level.ToString(), |
|---|
| 135 | message); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | if (csvText.Length > 0 || rawText.Length > 0) |
|---|
| 140 | { |
|---|
| 141 | //Set the simple text data for data-unaware applications like Word |
|---|
| 142 | DataObject tableText = new DataObject(); |
|---|
| 143 | tableText.SetText(rawText.ToString()); |
|---|
| 144 | |
|---|
| 145 | //Then a UTF-8 stream CSV for Excel |
|---|
| 146 | byte[] bytes = Encoding.UTF8.GetBytes(csvText.ToString()); |
|---|
| 147 | MemoryStream tableStream = new MemoryStream(bytes); |
|---|
| 148 | tableText.SetData(DataFormats.CommaSeparatedValue, tableStream); |
|---|
| 149 | |
|---|
| 150 | //Set the clipboard |
|---|
| 151 | Clipboard.SetDataObject(tableText, true); |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | private void close_Click(object sender, EventArgs e) |
|---|
| 156 | { |
|---|
| 157 | Close(); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /// <summary> |
|---|
| 161 | /// Enables/disables buttons based on the current system state. |
|---|
| 162 | /// </summary> |
|---|
| 163 | private void EnableButtons() |
|---|
| 164 | { |
|---|
| 165 | clear.Enabled = copy.Enabled = task.Log.Entries.Count > 0; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /// <summary> |
|---|
| 169 | /// The task which this log is displaying entries for |
|---|
| 170 | /// </summary> |
|---|
| 171 | private Task task; |
|---|
| 172 | } |
|---|
| 173 | } |
|---|