Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11821


Ignore:
Timestamp:
01/26/15 09:29:05 (10 years ago)
Author:
ascheibe
Message:

#2112 merged r11596,r11597,r11600 into stable branch

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Core/3.3/Log.cs

    r11170 r11821  
    3030  [Item("Log", "A log for logging string messages.")]
    3131  [StorableClass]
    32   public class Log : Item, ILog, IStorableContent {
     32  public sealed class Log : Item, ILog, IStorableContent {
    3333    public string Filename { get; set; }
    3434
     
    3838
    3939    [Storable]
    40     protected IList<string> messages;
    41     public virtual IEnumerable<string> Messages {
     40    private IList<string> messages;
     41    public IEnumerable<string> Messages {
    4242      get { return messages; }
    4343    }
    4444
    4545    [Storable]
    46     protected long maxMessageCount;
    47     public virtual long MaxMessageCount {
     46    private long maxMessageCount;
     47    public long MaxMessageCount {
    4848      get { return maxMessageCount; }
    4949    }
    5050
    5151    [StorableConstructor]
    52     protected Log(bool deserializing) : base(deserializing) { }
    53     protected Log(Log original, Cloner cloner)
     52    private Log(bool deserializing) : base(deserializing) { }
     53    private Log(Log original, Cloner cloner)
    5454      : base(original, cloner) {
    5555      this.messages = new List<string>(original.messages);
     
    6666    }
    6767
    68     public virtual void Clear() {
     68    public void Clear() {
    6969      messages.Clear();
    7070      OnCleared();
    7171    }
    72     public virtual void LogMessage(string message) {
     72    public void LogMessage(string message) {
    7373      string s = FormatLogMessage(message);
    7474      messages.Add(s);
     
    7676      OnMessageAdded(s);
    7777    }
    78     public virtual void LogException(Exception ex) {
     78    public void LogException(Exception ex) {
    7979      string s = FormatException(ex);
    8080      messages.Add(s);
     
    8282      OnMessageAdded(s);
    8383    }
    84     protected virtual void CapMessages() {
     84    private void CapMessages() {
    8585      while (maxMessageCount >= 0 && messages.Count > maxMessageCount) {
    8686        messages.RemoveAt(0);
    8787      }
    8888    }
    89     protected virtual string FormatLogMessage(string message) {
     89    public static string FormatLogMessage(string message) {
    9090      return DateTime.Now.ToString() + "\t" + message;
    9191    }
    92     protected virtual string FormatException(Exception ex) {
     92    public static string FormatException(Exception ex) {
    9393      return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex);
    9494    }
    9595
    9696    public event EventHandler<EventArgs<string>> MessageAdded;
    97     protected virtual void OnMessageAdded(string message) {
     97    private void OnMessageAdded(string message) {
    9898      EventHandler<EventArgs<string>> handler = MessageAdded;
    9999      if (handler != null) handler(this, new EventArgs<string>(message));
    100100    }
    101101    public event EventHandler Cleared;
    102     protected virtual void OnCleared() {
     102    private void OnCleared() {
    103103      EventHandler handler = Cleared;
    104104      if (handler != null) handler(this, EventArgs.Empty);
  • stable/HeuristicLab.Core/3.3/ThreadSafeLog.cs

    r11170 r11821  
    2121
    2222using System;
     23using System.Collections.Concurrent;
    2324using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Threading;
    2625using HeuristicLab.Common;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3029  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
    3130  [StorableClass]
    32   public sealed class ThreadSafeLog : Log {
    33     private ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
     31  public sealed class ThreadSafeLog : Item, ILog, IStorableContent {
     32    public string Filename { get; set; }
     33    private ConcurrentQueue<string> messages;
    3434
    35     public override IEnumerable<string> Messages {
    36       get {
    37         locker.EnterReadLock();
    38         try {
    39           return messages.ToArray(); // return copy of messages
    40         } finally { locker.ExitReadLock(); }
    41       }
     35    public IEnumerable<string> Messages {
     36      get { return messages.ToArray(); }
     37    }
     38
     39    [Storable(Name = "messages")]
     40    private IEnumerable<string> StorableMessages {
     41      get { return Messages; }
     42      set { messages = new ConcurrentQueue<string>(value); }
     43    }
     44
     45    [Storable]
     46    private long maxMessageCount;
     47    public long MaxMessageCount {
     48      get { return maxMessageCount; }
    4249    }
    4350
    4451    [StorableConstructor]
    4552    private ThreadSafeLog(bool deserializing) : base(deserializing) { }
    46     public ThreadSafeLog(long maxMessageCount = -1)
    47       : base(maxMessageCount) {
     53    private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
     54      : base(original, cloner) {
     55      this.messages = new ConcurrentQueue<string>(original.messages);
     56      this.maxMessageCount = original.maxMessageCount;
     57    }
     58    public ThreadSafeLog(long maxMessageCount = int.MaxValue) {
     59      this.messages = new ConcurrentQueue<string>();
     60      this.maxMessageCount = maxMessageCount;
    4861    }
    4962
    50     private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
    51       : base(original, cloner) { }
    52 
    5363    public override IDeepCloneable Clone(Cloner cloner) {
    54       locker.EnterReadLock();
    55       try {
    56         return new ThreadSafeLog(this, cloner);
    57       } finally { locker.ExitReadLock(); }
     64      return new ThreadSafeLog(this, cloner);
    5865    }
    5966
    60     public override void Clear() {
    61       locker.EnterWriteLock();
    62       try {
    63         messages.Clear();
    64       } finally { locker.ExitWriteLock(); }
     67    public void Clear() {
     68      messages = new ConcurrentQueue<string>();
    6569      OnCleared();
    6670    }
    6771
    68     public override void LogMessage(string message) {
    69       string s = FormatLogMessage(message);
    70       locker.EnterWriteLock();
    71       try {
    72         messages.Add(s);
    73         CapMessages();
    74       } finally { locker.ExitWriteLock(); }
     72    public void LogMessage(string message) {
     73      var s = Log.FormatLogMessage(message);
     74      messages.Enqueue(s);
     75      CapMessages();
    7576      OnMessageAdded(s);
    7677    }
    7778
    78     public override void LogException(Exception ex) {
    79       string s = FormatException(ex);
    80       locker.EnterWriteLock();
    81       try {
    82         messages.Add(s);
    83         CapMessages();
    84       } finally { locker.ExitWriteLock(); }
     79    public void LogException(Exception ex) {
     80      var s = Log.FormatException(ex);
     81      messages.Enqueue(s);
     82      CapMessages();
    8583      OnMessageAdded(s);
     84    }
     85
     86    private readonly object capLock = new object();
     87    private void CapMessages() {
     88      lock (capLock) {
     89        string s;
     90        while (messages.Count > maxMessageCount)
     91          if (!messages.TryDequeue(out s)) break;
     92      }
     93    }
     94
     95    public event EventHandler<EventArgs<string>> MessageAdded;
     96    private void OnMessageAdded(string message) {
     97      var handler = MessageAdded;
     98      if (handler != null) handler(this, new EventArgs<string>(message));
     99    }
     100
     101    public event EventHandler Cleared;
     102    private void OnCleared() {
     103      var handler = Cleared;
     104      if (handler != null) handler(this, EventArgs.Empty);
    86105    }
    87106  }
  • stable/HeuristicLab.Tests

  • stable/HeuristicLab.Tests/HeuristicLab-3.3/ThreadSafeLogTest.cs

    r11170 r11821  
    2828  [TestClass]
    2929  public class ThreadSafeLogTest {
    30 
    3130    [TestMethod]
    3231    [TestCategory("General")]
     
    4342      Assert.AreEqual(count, log.Messages.Count());
    4443    }
     44
     45    [TestMethod]
     46    [TestCategory("General")]
     47    [TestProperty("Time", "short")]
     48    public void ThreadSafeLogCapTest() {
     49      int count = 10000;
     50      int cap = 500;
     51      ThreadSafeLog log = new ThreadSafeLog(cap);
     52
     53      Parallel.For(0, count, (i) => {
     54        log.LogMessage("Message " + i); // write something
     55        log.Messages.Count(); // iterate over all messages
     56      });
     57
     58      Assert.AreEqual(cap, log.Messages.Count());
     59    }
    4560  }
    4661}
Note: See TracChangeset for help on using the changeset viewer.