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