[6424] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6424] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
| 30 | [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
|
---|
| 31 | [StorableClass]
|
---|
[7113] | 32 | public sealed class ThreadSafeLog : Log {
|
---|
| 33 | private 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
|
---|
[7113] | 40 | } finally { locker.ExitReadLock(); }
|
---|
[6424] | 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
[7113] | 45 | private ThreadSafeLog(bool deserializing) : base(deserializing) { }
|
---|
[6862] | 46 | public ThreadSafeLog(long maxMessageCount = -1)
|
---|
| 47 | : base(maxMessageCount) {
|
---|
[6424] | 48 | }
|
---|
| 49 |
|
---|
[7113] | 50 | private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
|
---|
| 51 | : base(original, cloner) { }
|
---|
[6862] | 52 |
|
---|
[6424] | 53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[7113] | 54 | locker.EnterReadLock();
|
---|
| 55 | try {
|
---|
| 56 | return new ThreadSafeLog(this, cloner);
|
---|
| 57 | } finally { locker.ExitReadLock(); }
|
---|
[6424] | 58 | }
|
---|
| 59 |
|
---|
[6862] | 60 | public override void Clear() {
|
---|
[6424] | 61 | locker.EnterWriteLock();
|
---|
| 62 | try {
|
---|
[6862] | 63 | messages.Clear();
|
---|
[7113] | 64 | } finally { locker.ExitWriteLock(); }
|
---|
[6862] | 65 | OnCleared();
|
---|
[6424] | 66 | }
|
---|
| 67 |
|
---|
[6862] | 68 | public override void LogMessage(string message) {
|
---|
| 69 | string s = FormatLogMessage(message);
|
---|
[6424] | 70 | locker.EnterWriteLock();
|
---|
| 71 | try {
|
---|
[6862] | 72 | messages.Add(s);
|
---|
| 73 | CapMessages();
|
---|
[7113] | 74 | } finally { locker.ExitWriteLock(); }
|
---|
[6862] | 75 | OnMessageAdded(s);
|
---|
[6424] | 76 | }
|
---|
| 77 |
|
---|
[6862] | 78 | public override void LogException(Exception ex) {
|
---|
| 79 | string s = FormatException(ex);
|
---|
[6424] | 80 | locker.EnterWriteLock();
|
---|
| 81 | try {
|
---|
[6862] | 82 | messages.Add(s);
|
---|
| 83 | CapMessages();
|
---|
[7113] | 84 | } finally { locker.ExitWriteLock(); }
|
---|
[6862] | 85 | OnMessageAdded(s);
|
---|
[6424] | 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|