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
Line 
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]
32  public class ThreadSafeLog : Log, IDeepCloneable {
33    protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
34
35    public override IEnumerable<string> Messages {
36      get {
37        locker.EnterReadLock();
38        try {
39          return messages.ToArray(); // return copy of messages
40        }
41        finally { locker.ExitReadLock(); }
42      }
43    }
44
45    [StorableConstructor]
46    protected ThreadSafeLog(bool deserializing) : base(deserializing) { }
47    public ThreadSafeLog(long maxMessageCount = -1)
48      : base(maxMessageCount) {
49    }
50
51    protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner) {
52      original.locker.EnterReadLock();
53      try {
54        cloner.RegisterClonedObject(original, this);
55        this.messages = new List<string>(original.messages);
56        this.maxMessageCount = original.maxMessageCount;
57      }
58      finally { original.locker.ExitReadLock(); }
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new ThreadSafeLog(this, cloner);
63    }
64
65    public override void Clear() {
66      locker.EnterWriteLock();
67      try {
68        messages.Clear();
69      }
70      finally { locker.ExitWriteLock(); }
71      OnCleared();
72    }
73
74    public override void LogMessage(string message) {
75      string s = FormatLogMessage(message);
76      locker.EnterWriteLock();
77      try {
78        messages.Add(s);
79        CapMessages();
80      }
81      finally { locker.ExitWriteLock(); }
82      OnMessageAdded(s);
83    }
84
85    public override void LogException(Exception ex) {
86      string s = FormatException(ex);
87      locker.EnterWriteLock();
88      try {
89        messages.Add(s);
90        CapMessages();
91      }
92      finally { locker.ExitWriteLock(); }
93      OnMessageAdded(s);
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.