Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ThreadSafeLog.cs @ 7090

Last change on this file since 7090 was 6862, checked in by ascheibe, 13 years ago

#1631 inherited ThreadSafeLog from Log to fix the LockRecursionException problem

File size: 2.9 KB
RevLine 
[6424]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Core {
30  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
31  [StorableClass]
[6862]32  public class ThreadSafeLog : Log, IDeepCloneable {
[6536]33    protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
[6424]34
[6862]35    public override IEnumerable<string> Messages {
[6424]36      get {
37        locker.EnterReadLock();
38        try {
[6862]39          return messages.ToArray(); // return copy of messages
40        }
41        finally { locker.ExitReadLock(); }
[6424]42      }
43    }
44
45    [StorableConstructor]
46    protected ThreadSafeLog(bool deserializing) : base(deserializing) { }
[6862]47    public ThreadSafeLog(long maxMessageCount = -1)
48      : base(maxMessageCount) {
[6424]49    }
50
[6862]51    protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) {
[6516]52      original.locker.EnterReadLock();
53      try {
[6862]54        cloner.RegisterClonedObject(original, this);
55        this.messages = new List<string>(original.messages);
56        this.maxMessageCount = original.maxMessageCount;
57      }
58      finally { original.locker.ExitReadLock(); }
[6424]59    }
[6862]60
[6424]61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new ThreadSafeLog(this, cloner);
63    }
64
[6862]65    public override void Clear() {
[6424]66      locker.EnterWriteLock();
67      try {
[6862]68        messages.Clear();
69      }
70      finally { locker.ExitWriteLock(); }
71      OnCleared();
[6424]72    }
73
[6862]74    public override void LogMessage(string message) {
75      string s = FormatLogMessage(message);
[6424]76      locker.EnterWriteLock();
77      try {
[6862]78        messages.Add(s);
79        CapMessages();
80      }
81      finally { locker.ExitWriteLock(); }
82      OnMessageAdded(s);
[6424]83    }
84
[6862]85    public override void LogException(Exception ex) {
86      string s = FormatException(ex);
[6424]87      locker.EnterWriteLock();
88      try {
[6862]89        messages.Add(s);
90        CapMessages();
91      }
92      finally { locker.ExitWriteLock(); }
93      OnMessageAdded(s);
[6424]94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.