Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Core/3.3/ThreadSafeLog.cs @ 16752

Last change on this file since 16752 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Concurrent;
24using System.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
30  [StorableClass]
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(bool deserializing) : base(deserializing) { }
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}
Note: See TracBrowser for help on using the repository browser.