Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Core/3.3/ThreadSafeLog.cs @ 12214

Last change on this file since 12214 was 11576, checked in by swagner, 10 years ago

#2205: Merged changes r11062:11557 from trunk/sources into branches/OptimizationNetworks

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 sealed class ThreadSafeLog : Log {
33    private 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        } finally { locker.ExitReadLock(); }
41      }
42    }
43
44    [StorableConstructor]
45    private ThreadSafeLog(bool deserializing) : base(deserializing) { }
46    public ThreadSafeLog(long maxMessageCount = -1)
47      : base(maxMessageCount) {
48    }
49
50    private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
51      : base(original, cloner) { }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      locker.EnterReadLock();
55      try {
56        return new ThreadSafeLog(this, cloner);
57      } finally { locker.ExitReadLock(); }
58    }
59
60    public override void Clear() {
61      locker.EnterWriteLock();
62      try {
63        messages.Clear();
64      } finally { locker.ExitWriteLock(); }
65      OnCleared();
66    }
67
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(); }
75      OnMessageAdded(s);
76    }
77
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(); }
85      OnMessageAdded(s);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.