#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Core {
[Item("Log", "A log for logging string messages.")]
[StorableClass("16244B4A-1C3B-4616-8670-4BF388F44D34")]
public sealed class Log : Item, ILog, IStorableContent {
public string Filename { get; set; }
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.File; }
}
[Storable]
private IList messages;
public IEnumerable Messages {
get { return messages; }
}
[Storable]
private long maxMessageCount;
public long MaxMessageCount {
get { return maxMessageCount; }
}
[StorableConstructor]
private Log(bool deserializing) : base(deserializing) { }
private Log(Log original, Cloner cloner)
: base(original, cloner) {
this.messages = new List(original.messages);
this.maxMessageCount = original.maxMessageCount;
}
public Log(long maxMessageCount = -1)
: base() {
this.messages = new List();
this.maxMessageCount = maxMessageCount;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new Log(this, cloner);
}
public void Clear() {
messages.Clear();
OnCleared();
}
public void LogMessage(string message) {
string s = FormatLogMessage(message);
messages.Add(s);
CapMessages();
OnMessageAdded(s);
}
public void LogException(Exception ex) {
string s = FormatException(ex);
messages.Add(s);
CapMessages();
OnMessageAdded(s);
}
private void CapMessages() {
while (maxMessageCount >= 0 && messages.Count > maxMessageCount) {
messages.RemoveAt(0);
}
}
public static string FormatLogMessage(string message) {
return DateTime.Now.ToString() + "\t" + message;
}
public static string FormatException(Exception ex) {
return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
}
public event EventHandler> MessageAdded;
private void OnMessageAdded(string message) {
EventHandler> handler = MessageAdded;
if (handler != null) handler(this, new EventArgs(message));
}
public event EventHandler Cleared;
private void OnCleared() {
EventHandler handler = Cleared;
if (handler != null) handler(this, EventArgs.Empty);
}
}
}