| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2009 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.Text; |
|---|
| 25 | using System.Runtime.Serialization; |
|---|
| 26 | using System.Security.Permissions; |
|---|
| 27 | |
|---|
| 28 | namespace Eraser.Manager |
|---|
| 29 | { |
|---|
| 30 | /// <summary> |
|---|
| 31 | /// The levels of logging allowing for the filtering of messages. |
|---|
| 32 | /// </summary> |
|---|
| 33 | public enum LogLevel |
|---|
| 34 | { |
|---|
| 35 | /// <summary> |
|---|
| 36 | /// Informative messages. |
|---|
| 37 | /// </summary> |
|---|
| 38 | Information, |
|---|
| 39 | |
|---|
| 40 | /// <summary> |
|---|
| 41 | /// Notice messages. |
|---|
| 42 | /// </summary> |
|---|
| 43 | Notice, |
|---|
| 44 | |
|---|
| 45 | /// <summary> |
|---|
| 46 | /// Warning messages. |
|---|
| 47 | /// </summary> |
|---|
| 48 | Warning, |
|---|
| 49 | |
|---|
| 50 | /// <summary> |
|---|
| 51 | /// Error messages. |
|---|
| 52 | /// </summary> |
|---|
| 53 | Error, |
|---|
| 54 | |
|---|
| 55 | /// <summary> |
|---|
| 56 | /// Fatal errors. |
|---|
| 57 | /// </summary> |
|---|
| 58 | Fatal |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | /// <summary> |
|---|
| 62 | /// The Logger class which handles log entries and manages entries. |
|---|
| 63 | /// |
|---|
| 64 | /// The class has the notion of entries and sessions. Each session contains one |
|---|
| 65 | /// or more (log) entries. This allows the program to determine if the last |
|---|
| 66 | /// session had errors or not. |
|---|
| 67 | /// </summary> |
|---|
| 68 | [Serializable] |
|---|
| 69 | public class Logger : ISerializable |
|---|
| 70 | { |
|---|
| 71 | #region Serialization code |
|---|
| 72 | protected Logger(SerializationInfo info, StreamingContext context) |
|---|
| 73 | { |
|---|
| 74 | Entries = (LogSessionDictionary)info.GetValue("Entries", typeof(LogSessionDictionary)); |
|---|
| 75 | Entries.Owner = this; |
|---|
| 76 | foreach (DateTime key in Entries.Keys) |
|---|
| 77 | LastSession = key; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | [SecurityPermission(SecurityAction.Demand, SerializationFormatter=true)] |
|---|
| 81 | public virtual void GetObjectData(SerializationInfo info, StreamingContext context) |
|---|
| 82 | { |
|---|
| 83 | info.AddValue("Entries", Entries); |
|---|
| 84 | } |
|---|
| 85 | #endregion |
|---|
| 86 | |
|---|
| 87 | /// <summary> |
|---|
| 88 | /// Constructor. |
|---|
| 89 | /// </summary> |
|---|
| 90 | public Logger() |
|---|
| 91 | { |
|---|
| 92 | Entries = new LogSessionDictionary(this); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /// <summary> |
|---|
| 96 | /// All the registered event handlers for the log event of this task. |
|---|
| 97 | /// </summary> |
|---|
| 98 | public EventHandler<LogEventArgs> Logged { get; set; } |
|---|
| 99 | |
|---|
| 100 | internal void OnLogged(object sender, LogEventArgs e) |
|---|
| 101 | { |
|---|
| 102 | if (Logged != null) |
|---|
| 103 | Logged(sender, e); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /// <summary> |
|---|
| 107 | /// All the registered event handlers for handling when a new session has been |
|---|
| 108 | /// started. |
|---|
| 109 | /// </summary> |
|---|
| 110 | public EventHandler<EventArgs> NewSession { get; set; } |
|---|
| 111 | |
|---|
| 112 | internal void OnNewSession(object sender, EventArgs e) |
|---|
| 113 | { |
|---|
| 114 | if (NewSession != null) |
|---|
| 115 | NewSession(sender, e); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /// <summary> |
|---|
| 119 | /// Retrieves the log for this task. |
|---|
| 120 | /// </summary> |
|---|
| 121 | public LogSessionDictionary Entries { get; private set; } |
|---|
| 122 | |
|---|
| 123 | /// <summary> |
|---|
| 124 | /// Retrieves the log entries from the previous session. |
|---|
| 125 | /// </summary> |
|---|
| 126 | public LogEntryCollection LastSessionEntries |
|---|
| 127 | { |
|---|
| 128 | get |
|---|
| 129 | { |
|---|
| 130 | return Entries[LastSession]; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /// <summary> |
|---|
| 135 | /// Clears the log entries from the log. |
|---|
| 136 | /// </summary> |
|---|
| 137 | public void Clear() |
|---|
| 138 | { |
|---|
| 139 | LogEntryCollection lastSessionEntries = null; |
|---|
| 140 | if (Entries.ContainsKey(LastSession)) |
|---|
| 141 | lastSessionEntries = Entries[LastSession]; |
|---|
| 142 | Entries.Clear(); |
|---|
| 143 | |
|---|
| 144 | if (lastSessionEntries != null) |
|---|
| 145 | Entries.Add(LastSession, lastSessionEntries); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /// <summary> |
|---|
| 149 | /// The date and time of the last session. |
|---|
| 150 | /// </summary> |
|---|
| 151 | public DateTime LastSession |
|---|
| 152 | { |
|---|
| 153 | get { return lastSession; } |
|---|
| 154 | internal set { lastSession = value; OnNewSession(null, EventArgs.Empty); } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | private DateTime lastSession; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | public class LogEventArgs : EventArgs |
|---|
| 161 | { |
|---|
| 162 | /// <summary> |
|---|
| 163 | /// Constructor. |
|---|
| 164 | /// </summary> |
|---|
| 165 | /// <param name="entry">The log entry that was just logged.</param> |
|---|
| 166 | public LogEventArgs(LogEntry entry) |
|---|
| 167 | { |
|---|
| 168 | LogEntry = entry; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /// <summary> |
|---|
| 172 | /// The log entry that was just logged. |
|---|
| 173 | /// </summary> |
|---|
| 174 | public LogEntry LogEntry { get; private set; } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | [Serializable] |
|---|
| 178 | public class LogSessionDictionary : IDictionary<DateTime, LogEntryCollection>, |
|---|
| 179 | ISerializable |
|---|
| 180 | { |
|---|
| 181 | /// <summary> |
|---|
| 182 | /// Constructor. |
|---|
| 183 | /// </summary> |
|---|
| 184 | /// <param name="logger">The logger object managing the logging.</param> |
|---|
| 185 | public LogSessionDictionary(Logger logger) |
|---|
| 186 | { |
|---|
| 187 | Owner = logger; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | public void NewSession() |
|---|
| 191 | { |
|---|
| 192 | DateTime sessionTime = DateTime.Now; |
|---|
| 193 | Add(sessionTime, new LogEntryCollection(Owner)); |
|---|
| 194 | Owner.LastSession = sessionTime; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | #region ISerializable Members |
|---|
| 198 | protected LogSessionDictionary(SerializationInfo info, StreamingContext context) |
|---|
| 199 | { |
|---|
| 200 | dictionary = (Dictionary<DateTime, LogEntryCollection>)info.GetValue("Dictionary", |
|---|
| 201 | dictionary.GetType()); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] |
|---|
| 205 | public virtual void GetObjectData(SerializationInfo info, StreamingContext context) |
|---|
| 206 | { |
|---|
| 207 | lock (dictionary) |
|---|
| 208 | info.AddValue("Dictionary", dictionary); |
|---|
| 209 | } |
|---|
| 210 | #endregion |
|---|
| 211 | |
|---|
| 212 | #region IDictionary<DateTime,LogSessionEntryCollection> Members |
|---|
| 213 | public void Add(DateTime key, LogEntryCollection value) |
|---|
| 214 | { |
|---|
| 215 | lock (dictionary) |
|---|
| 216 | dictionary.Add(key, value); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | public bool ContainsKey(DateTime key) |
|---|
| 220 | { |
|---|
| 221 | lock (dictionary) |
|---|
| 222 | return dictionary.ContainsKey(key); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | public ICollection<DateTime> Keys |
|---|
| 226 | { |
|---|
| 227 | get |
|---|
| 228 | { |
|---|
| 229 | lock (dictionary) |
|---|
| 230 | { |
|---|
| 231 | DateTime[] result = new DateTime[dictionary.Keys.Count]; |
|---|
| 232 | dictionary.Keys.CopyTo(result, 0); |
|---|
| 233 | return result; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | public bool Remove(DateTime key) |
|---|
| 239 | { |
|---|
| 240 | lock (dictionary) |
|---|
| 241 | return dictionary.Remove(key); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | public bool TryGetValue(DateTime key, out LogEntryCollection value) |
|---|
| 245 | { |
|---|
| 246 | lock (dictionary) |
|---|
| 247 | return dictionary.TryGetValue(key, out value); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | public ICollection<LogEntryCollection> Values |
|---|
| 251 | { |
|---|
| 252 | get |
|---|
| 253 | { |
|---|
| 254 | lock (dictionary) |
|---|
| 255 | { |
|---|
| 256 | LogEntryCollection[] result = new LogEntryCollection[dictionary.Values.Count]; |
|---|
| 257 | dictionary.Values.CopyTo(result, 0); |
|---|
| 258 | return result; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | public LogEntryCollection this[DateTime key] |
|---|
| 264 | { |
|---|
| 265 | get |
|---|
| 266 | { |
|---|
| 267 | lock (dictionary) |
|---|
| 268 | return dictionary[key]; |
|---|
| 269 | } |
|---|
| 270 | set |
|---|
| 271 | { |
|---|
| 272 | lock (dictionary) |
|---|
| 273 | dictionary[key] = value; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | #endregion |
|---|
| 277 | |
|---|
| 278 | #region ICollection<KeyValuePair<DateTime,LogSessionEntryCollection>> Members |
|---|
| 279 | public void Add(KeyValuePair<DateTime, LogEntryCollection> item) |
|---|
| 280 | { |
|---|
| 281 | Add(item.Key, item.Value); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | public void Clear() |
|---|
| 285 | { |
|---|
| 286 | lock (dictionary) |
|---|
| 287 | dictionary.Clear(); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | public bool Contains(KeyValuePair<DateTime, LogEntryCollection> item) |
|---|
| 291 | { |
|---|
| 292 | lock (dictionary) |
|---|
| 293 | return dictionary.ContainsKey(item.Key) && dictionary[item.Key] == item.Value; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | public void CopyTo(KeyValuePair<DateTime, LogEntryCollection>[] array, int arrayIndex) |
|---|
| 297 | { |
|---|
| 298 | throw new NotImplementedException(); |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | public int Count |
|---|
| 302 | { |
|---|
| 303 | get |
|---|
| 304 | { |
|---|
| 305 | lock (dictionary) |
|---|
| 306 | return dictionary.Count; |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | public bool IsReadOnly |
|---|
| 311 | { |
|---|
| 312 | get { return false; } |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | public bool Remove(KeyValuePair<DateTime, LogEntryCollection> item) |
|---|
| 316 | { |
|---|
| 317 | lock (dictionary) |
|---|
| 318 | return dictionary.Remove(item.Key); |
|---|
| 319 | } |
|---|
| 320 | #endregion |
|---|
| 321 | |
|---|
| 322 | #region IEnumerable<KeyValuePair<DateTime,LogSessionEntryCollection>> Members |
|---|
| 323 | public IEnumerator<KeyValuePair<DateTime, LogEntryCollection>> GetEnumerator() |
|---|
| 324 | { |
|---|
| 325 | return dictionary.GetEnumerator(); |
|---|
| 326 | } |
|---|
| 327 | #endregion |
|---|
| 328 | |
|---|
| 329 | #region IEnumerable Members |
|---|
| 330 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|---|
| 331 | { |
|---|
| 332 | return GetEnumerator(); |
|---|
| 333 | } |
|---|
| 334 | #endregion |
|---|
| 335 | |
|---|
| 336 | /// <summary> |
|---|
| 337 | /// The log manager. |
|---|
| 338 | /// </summary> |
|---|
| 339 | internal Logger Owner |
|---|
| 340 | { |
|---|
| 341 | get |
|---|
| 342 | { |
|---|
| 343 | return owner; |
|---|
| 344 | } |
|---|
| 345 | set |
|---|
| 346 | { |
|---|
| 347 | lock (dictionary) |
|---|
| 348 | foreach (LogEntryCollection entries in dictionary.Values) |
|---|
| 349 | entries.owner = value; |
|---|
| 350 | owner = value; |
|---|
| 351 | } |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | /// <summary> |
|---|
| 355 | /// The log manager. |
|---|
| 356 | /// </summary> |
|---|
| 357 | private Logger owner; |
|---|
| 358 | |
|---|
| 359 | /// <summary> |
|---|
| 360 | /// The store for this object. |
|---|
| 361 | /// </summary> |
|---|
| 362 | private Dictionary<DateTime, LogEntryCollection> dictionary = |
|---|
| 363 | new Dictionary<DateTime, LogEntryCollection>(); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | [Serializable] |
|---|
| 367 | public class LogEntryCollection : IList<LogEntry> |
|---|
| 368 | { |
|---|
| 369 | /// <summary> |
|---|
| 370 | /// Constructor. |
|---|
| 371 | /// </summary> |
|---|
| 372 | /// <param name="logger">The <see cref="Logger"/> object handling logging.</param> |
|---|
| 373 | internal LogEntryCollection(Logger logger) |
|---|
| 374 | { |
|---|
| 375 | owner = logger; |
|---|
| 376 | } |
|---|
| 377 | |
|---|
| 378 | #region IList<LogEntry> Members |
|---|
| 379 | public int IndexOf(LogEntry item) |
|---|
| 380 | { |
|---|
| 381 | lock (list) |
|---|
| 382 | return list.IndexOf(item); |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | public void Insert(int index, LogEntry item) |
|---|
| 386 | { |
|---|
| 387 | lock (list) |
|---|
| 388 | list.Insert(index, item); |
|---|
| 389 | owner.OnLogged(owner, new LogEventArgs(item)); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | public void RemoveAt(int index) |
|---|
| 393 | { |
|---|
| 394 | throw new InvalidOperationException(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | public LogEntry this[int index] |
|---|
| 398 | { |
|---|
| 399 | get |
|---|
| 400 | { |
|---|
| 401 | lock (list) |
|---|
| 402 | return list[index]; |
|---|
| 403 | } |
|---|
| 404 | set |
|---|
| 405 | { |
|---|
| 406 | throw new InvalidOperationException(); |
|---|
| 407 | } |
|---|
| 408 | } |
|---|
| 409 | #endregion |
|---|
| 410 | |
|---|
| 411 | #region ICollection<LogEntry> Members |
|---|
| 412 | public void Add(LogEntry item) |
|---|
| 413 | { |
|---|
| 414 | Insert(Count, item); |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | public void Clear() |
|---|
| 418 | { |
|---|
| 419 | throw new InvalidOperationException(); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | public bool Contains(LogEntry item) |
|---|
| 423 | { |
|---|
| 424 | lock (list) |
|---|
| 425 | return list.Contains(item); |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | public void CopyTo(LogEntry[] array, int arrayIndex) |
|---|
| 429 | { |
|---|
| 430 | lock (list) |
|---|
| 431 | list.CopyTo(array, arrayIndex); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | public int Count |
|---|
| 435 | { |
|---|
| 436 | get |
|---|
| 437 | { |
|---|
| 438 | lock (list) |
|---|
| 439 | return list.Count; |
|---|
| 440 | } |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | public bool IsReadOnly |
|---|
| 444 | { |
|---|
| 445 | get { return false; } |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | public bool Remove(LogEntry item) |
|---|
| 449 | { |
|---|
| 450 | lock (list) |
|---|
| 451 | return list.Remove(item); |
|---|
| 452 | } |
|---|
| 453 | #endregion |
|---|
| 454 | |
|---|
| 455 | #region IEnumerable<LogEntry> Members |
|---|
| 456 | public IEnumerator<LogEntry> GetEnumerator() |
|---|
| 457 | { |
|---|
| 458 | return list.GetEnumerator(); |
|---|
| 459 | } |
|---|
| 460 | #endregion |
|---|
| 461 | |
|---|
| 462 | #region IEnumerable Members |
|---|
| 463 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|---|
| 464 | { |
|---|
| 465 | return GetEnumerator(); |
|---|
| 466 | } |
|---|
| 467 | #endregion |
|---|
| 468 | |
|---|
| 469 | /// <summary> |
|---|
| 470 | /// The Logger object managing logging. |
|---|
| 471 | /// </summary> |
|---|
| 472 | internal Logger owner; |
|---|
| 473 | |
|---|
| 474 | /// <summary> |
|---|
| 475 | /// The store for this object. |
|---|
| 476 | /// </summary> |
|---|
| 477 | private List<LogEntry> list = new List<LogEntry>(); |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | /// <summary> |
|---|
| 481 | /// Represents a log entry. |
|---|
| 482 | /// </summary> |
|---|
| 483 | [Serializable] |
|---|
| 484 | public struct LogEntry : ISerializable |
|---|
| 485 | { |
|---|
| 486 | #region Serialization code |
|---|
| 487 | private LogEntry(SerializationInfo info, StreamingContext context) |
|---|
| 488 | : this() |
|---|
| 489 | { |
|---|
| 490 | Level = (LogLevel)info.GetValue("Level", typeof(LogLevel)); |
|---|
| 491 | Timestamp = (DateTime)info.GetValue("Timestamp", typeof(DateTime)); |
|---|
| 492 | Message = (string)info.GetValue("Message", typeof(string)); |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)] |
|---|
| 496 | public void GetObjectData(SerializationInfo info, StreamingContext context) |
|---|
| 497 | { |
|---|
| 498 | info.AddValue("Level", Level); |
|---|
| 499 | info.AddValue("Timestamp", Timestamp); |
|---|
| 500 | info.AddValue("Message", Message); |
|---|
| 501 | } |
|---|
| 502 | #endregion |
|---|
| 503 | |
|---|
| 504 | /// <summary> |
|---|
| 505 | /// Creates a LogEntry structure. |
|---|
| 506 | /// </summary> |
|---|
| 507 | /// <param name="message">The log message.</param> |
|---|
| 508 | /// <param name="level">The type of log entry.</param> |
|---|
| 509 | public LogEntry(string message, LogLevel level) |
|---|
| 510 | : this() |
|---|
| 511 | { |
|---|
| 512 | Message = message; |
|---|
| 513 | Level = level; |
|---|
| 514 | Timestamp = DateTime.Now; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | /// <summary> |
|---|
| 518 | /// The type of log entry. |
|---|
| 519 | /// </summary> |
|---|
| 520 | public LogLevel Level { get; private set; } |
|---|
| 521 | |
|---|
| 522 | /// <summary> |
|---|
| 523 | /// The time which the message was logged. |
|---|
| 524 | /// </summary> |
|---|
| 525 | public DateTime Timestamp { get; private set; } |
|---|
| 526 | |
|---|
| 527 | /// <summary> |
|---|
| 528 | /// The log message. |
|---|
| 529 | /// </summary> |
|---|
| 530 | public string Message { get; private set; } |
|---|
| 531 | } |
|---|
| 532 | } |
|---|