Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3668 was 3289, checked in by swagner, 15 years ago

Implemented logs of engines (#963).

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