Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Core/3.3/Log.cs @ 15160

Last change on this file since 15160 was 13656, checked in by ascheibe, 8 years ago

#2582 created branch for Hive Web Job Manager

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