Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11569 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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, IStorableContent {
33    public string Filename { get; set; }
34
35    public static new Image StaticItemImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.File; }
37    }
38
39    [Storable]
40    protected IList<string> messages;
41    public virtual IEnumerable<string> Messages {
42      get { return messages; }
43    }
44
45    [Storable]
46    protected long maxMessageCount;
47    public virtual long MaxMessageCount {
48      get { return maxMessageCount; }
49    }
50
51    [StorableConstructor]
52    protected Log(bool deserializing) : base(deserializing) { }
53    protected Log(Log original, Cloner cloner)
54      : base(original, cloner) {
55      this.messages = new List<string>(original.messages);
56      this.maxMessageCount = original.maxMessageCount;
57    }
58    public Log(long maxMessageCount = -1)
59      : base() {
60      this.messages = new List<string>();
61      this.maxMessageCount = maxMessageCount;
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new Log(this, cloner);
66    }
67
68    public virtual void Clear() {
69      messages.Clear();
70      OnCleared();
71    }
72    public virtual void LogMessage(string message) {
73      string s = FormatLogMessage(message);
74      messages.Add(s);
75      CapMessages();
76      OnMessageAdded(s);
77    }
78    public virtual void LogException(Exception ex) {
79      string s = FormatException(ex);
80      messages.Add(s);
81      CapMessages();
82      OnMessageAdded(s);
83    }
84    protected virtual void CapMessages() {
85      while (maxMessageCount >= 0 && messages.Count > maxMessageCount) {
86        messages.RemoveAt(0);
87      }
88    }
89    protected virtual string FormatLogMessage(string message) {
90      return DateTime.Now.ToString() + "\t" + message;
91    }
92    protected virtual string FormatException(Exception ex) {
93      return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
94    }
95
96    public event EventHandler<EventArgs<string>> MessageAdded;
97    protected virtual void OnMessageAdded(string message) {
98      EventHandler<EventArgs<string>> handler = MessageAdded;
99      if (handler != null) handler(this, new EventArgs<string>(message));
100    }
101    public event EventHandler Cleared;
102    protected virtual void OnCleared() {
103      EventHandler handler = Cleared;
104      if (handler != null) handler(this, EventArgs.Empty);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.