Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Log.cs @ 16003

Last change on this file since 16003 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.5 KB
RevLine 
[3289]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3289]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;
[3758]27using HeuristicLab.PluginInfrastructure;
[3289]28
29namespace HeuristicLab.Core {
30  [Item("Log", "A log for logging string messages.")]
31  [StorableClass]
[11821]32  public sealed class Log : Item, ILog, IStorableContent {
[4419]33    public string Filename { get; set; }
34
[7201]35    public static new Image StaticItemImage {
[5287]36      get { return HeuristicLab.Common.Resources.VSImageLibrary.File; }
[3289]37    }
38
39    [Storable]
[11821]40    private IList<string> messages;
41    public IEnumerable<string> Messages {
[3289]42      get { return messages; }
43    }
44
[6423]45    [Storable]
[11821]46    private long maxMessageCount;
47    public long MaxMessageCount {
[6423]48      get { return maxMessageCount; }
49    }
50
[4722]51    [StorableConstructor]
[11821]52    private Log(bool deserializing) : base(deserializing) { }
53    private Log(Log original, Cloner cloner)
[4722]54      : base(original, cloner) {
[6423]55      this.messages = new List<string>(original.messages);
56      this.maxMessageCount = original.maxMessageCount;
[4722]57    }
[6423]58    public Log(long maxMessageCount = -1)
[3289]59      : base() {
[6423]60      this.messages = new List<string>();
61      this.maxMessageCount = maxMessageCount;
[3289]62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
[4722]65      return new Log(this, cloner);
[3289]66    }
67
[11821]68    public void Clear() {
[3289]69      messages.Clear();
70      OnCleared();
71    }
[11821]72    public void LogMessage(string message) {
[6862]73      string s = FormatLogMessage(message);
[3289]74      messages.Add(s);
[6423]75      CapMessages();
[3289]76      OnMessageAdded(s);
77    }
[11821]78    public void LogException(Exception ex) {
[6862]79      string s = FormatException(ex);
[3289]80      messages.Add(s);
[6423]81      CapMessages();
[3289]82      OnMessageAdded(s);
83    }
[11821]84    private void CapMessages() {
[6423]85      while (maxMessageCount >= 0 && messages.Count > maxMessageCount) {
86        messages.RemoveAt(0);
87      }
88    }
[11821]89    public static string FormatLogMessage(string message) {
[6862]90      return DateTime.Now.ToString() + "\t" + message;
91    }
[11821]92    public static string FormatException(Exception ex) {
[6862]93      return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
94    }
[3289]95
96    public event EventHandler<EventArgs<string>> MessageAdded;
[11821]97    private void OnMessageAdded(string message) {
[3289]98      EventHandler<EventArgs<string>> handler = MessageAdded;
99      if (handler != null) handler(this, new EventArgs<string>(message));
100    }
101    public event EventHandler Cleared;
[11821]102    private void OnCleared() {
[3289]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.