[13368] | 1 | #region License Information
|
---|
[6424] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[11596] | 23 | using System.Collections.Concurrent;
|
---|
[6424] | 24 | using System.Collections.Generic;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Core {
|
---|
| 29 | [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
|
---|
[14711] | 30 | [StorableType("FFC9ACB8-BEA9-439A-B6D7-E0CBBEBAD297")]
|
---|
[11596] | 31 | public sealed class ThreadSafeLog : Item, ILog, IStorableContent {
|
---|
| 32 | public string Filename { get; set; }
|
---|
| 33 | private ConcurrentQueue<string> messages;
|
---|
[6424] | 34 |
|
---|
[11596] | 35 | public IEnumerable<string> Messages {
|
---|
| 36 | get { return messages.ToArray(); }
|
---|
[6424] | 37 | }
|
---|
| 38 |
|
---|
[11596] | 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; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6424] | 51 | [StorableConstructor]
|
---|
[7113] | 52 | private ThreadSafeLog(bool deserializing) : base(deserializing) { }
|
---|
[11596] | 53 | private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
|
---|
| 54 | : base(original, cloner) {
|
---|
| 55 | this.messages = new ConcurrentQueue<string>(original.messages);
|
---|
| 56 | this.maxMessageCount = original.maxMessageCount;
|
---|
[6424] | 57 | }
|
---|
[11596] | 58 | public ThreadSafeLog(long maxMessageCount = int.MaxValue) {
|
---|
| 59 | this.messages = new ConcurrentQueue<string>();
|
---|
| 60 | this.maxMessageCount = maxMessageCount;
|
---|
| 61 | }
|
---|
[6424] | 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[11596] | 64 | return new ThreadSafeLog(this, cloner);
|
---|
[6424] | 65 | }
|
---|
| 66 |
|
---|
[11596] | 67 | public void Clear() {
|
---|
| 68 | messages = new ConcurrentQueue<string>();
|
---|
[6862] | 69 | OnCleared();
|
---|
[6424] | 70 | }
|
---|
| 71 |
|
---|
[11596] | 72 | public void LogMessage(string message) {
|
---|
| 73 | var s = Log.FormatLogMessage(message);
|
---|
| 74 | messages.Enqueue(s);
|
---|
| 75 | CapMessages();
|
---|
[6862] | 76 | OnMessageAdded(s);
|
---|
[6424] | 77 | }
|
---|
| 78 |
|
---|
[11596] | 79 | public void LogException(Exception ex) {
|
---|
| 80 | var s = Log.FormatException(ex);
|
---|
| 81 | messages.Enqueue(s);
|
---|
| 82 | CapMessages();
|
---|
[6862] | 83 | OnMessageAdded(s);
|
---|
[6424] | 84 | }
|
---|
[11596] | 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);
|
---|
| 105 | }
|
---|
[6424] | 106 | }
|
---|
| 107 | }
|
---|