[3289] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3758] | 27 | using HeuristicLab.PluginInfrastructure;
|
---|
[3289] | 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
| 30 | [Item("Log", "A log for logging string messages.")]
|
---|
| 31 | [StorableClass]
|
---|
[4419] | 32 | public class Log : Item, ILog, IStorableContent {
|
---|
| 33 | public string Filename { get; set; }
|
---|
| 34 |
|
---|
[3289] | 35 | public override Image ItemImage {
|
---|
[5287] | 36 | get { return HeuristicLab.Common.Resources.VSImageLibrary.File; }
|
---|
[3289] | 37 | }
|
---|
| 38 |
|
---|
| 39 | [Storable]
|
---|
| 40 | protected IList<string> messages;
|
---|
| 41 | public virtual IEnumerable<string> Messages {
|
---|
| 42 | get { return messages; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[4722] | 45 | [StorableConstructor]
|
---|
| 46 | protected Log(bool deserializing) : base(deserializing) { }
|
---|
| 47 | protected Log(Log original, Cloner cloner)
|
---|
| 48 | : base(original, cloner) {
|
---|
| 49 | messages = new List<string>(original.messages);
|
---|
| 50 | }
|
---|
[3289] | 51 | public Log()
|
---|
| 52 | : base() {
|
---|
| 53 | messages = new List<string>();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 57 | return new Log(this, cloner);
|
---|
[3289] | 58 | }
|
---|
| 59 |
|
---|
| 60 | public virtual void Clear() {
|
---|
| 61 | messages.Clear();
|
---|
| 62 | OnCleared();
|
---|
| 63 | }
|
---|
| 64 | public virtual void LogMessage(string message) {
|
---|
| 65 | string s = DateTime.Now.ToString() + "\t" + message;
|
---|
| 66 | messages.Add(s);
|
---|
| 67 | OnMessageAdded(s);
|
---|
| 68 | }
|
---|
| 69 | public virtual void LogException(Exception ex) {
|
---|
[3758] | 70 | string s = DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
|
---|
[3289] | 71 | messages.Add(s);
|
---|
| 72 | OnMessageAdded(s);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public event EventHandler<EventArgs<string>> MessageAdded;
|
---|
| 76 | protected virtual void OnMessageAdded(string message) {
|
---|
| 77 | EventHandler<EventArgs<string>> handler = MessageAdded;
|
---|
| 78 | if (handler != null) handler(this, new EventArgs<string>(message));
|
---|
| 79 | }
|
---|
| 80 | public event EventHandler Cleared;
|
---|
| 81 | protected virtual void OnCleared() {
|
---|
| 82 | EventHandler handler = Cleared;
|
---|
| 83 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|