Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Log.cs @ 3923

Last change on this file since 3923 was 3758, checked in by swagner, 15 years ago

Implemented ErrorDialog and OperatorExecutionException (#1007)

File size: 2.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Core {
30  [Item("Log", "A log for logging string messages.")]
31  [StorableClass]
32  public class Log : Item, ILog {
33    public override Image ItemImage {
34      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.File; }
35    }
36
37    [Storable]
38    protected IList<string> messages;
39    public virtual IEnumerable<string> Messages {
40      get { return messages; }
41    }
42
43    public Log()
44      : base() {
45      messages = new List<string>();
46    }
47    [StorableConstructor]
48    protected Log(bool deserializing) : base(deserializing) { }
49
50    public override IDeepCloneable Clone(Cloner cloner) {
51      Log clone = (Log)base.Clone(cloner);
52      clone.messages = new List<string>(messages);
53      return clone;
54    }
55
56    public virtual void Clear() {
57      messages.Clear();
58      OnCleared();
59    }
60    public virtual void LogMessage(string message) {
61      string s = DateTime.Now.ToString() + "\t" + message;
62      messages.Add(s);
63      OnMessageAdded(s);
64    }
65    public virtual void LogException(Exception ex) {
66      string s = DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
67      messages.Add(s);
68      OnMessageAdded(s);
69    }
70
71    public event EventHandler<EventArgs<string>> MessageAdded;
72    protected virtual void OnMessageAdded(string message) {
73      EventHandler<EventArgs<string>> handler = MessageAdded;
74      if (handler != null) handler(this, new EventArgs<string>(message));
75    }
76    public event EventHandler Cleared;
77    protected virtual void OnCleared() {
78      EventHandler handler = Cleared;
79      if (handler != null) handler(this, EventArgs.Empty);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.