| 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 | |
|---|
| 34 | namespace 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 | } |
|---|