Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Core/3.3/ThreadSafeLog.cs @ 6727

Last change on this file since 6727 was 6618, checked in by mkommend, 13 years ago

#1479: Integrated trunk changes.

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Drawing;
25using System.Linq;
26using System.Threading;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Core {
32  [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")]
33  [StorableClass]
34  public class ThreadSafeLog : Item, ILog, IStorableContent {
35    protected ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
36
37    public string Filename { get; set; }
38
39    public override Image ItemImage {
40      get { return VSImageLibrary.File; }
41    }
42
43    public IEnumerable<string> Messages {
44      get {
45        locker.EnterReadLock();
46        try {
47          return log.Messages.ToArray(); // return copy of messages
48        }
49        finally { locker.ExitReadLock(); }
50      }
51    }
52
53    [Storable]
54    protected ILog log;
55
56    [StorableConstructor]
57    protected ThreadSafeLog(bool deserializing) : base(deserializing) { }
58    public ThreadSafeLog()
59      : base() {
60      this.log = new Log();
61      RegisterLogEvents();
62    }
63    public ThreadSafeLog(ILog log)
64      : base() {
65      this.log = log;
66      RegisterLogEvents();
67    }
68
69    protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
70      : base(original, cloner) {
71      original.locker.EnterReadLock();
72      try {
73        log = cloner.Clone(original.log);
74      }
75      finally { locker.ExitReadLock(); }
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new ThreadSafeLog(this, cloner);
79    }
80
81    public virtual void Clear() {
82      locker.EnterWriteLock();
83      try {
84        log.Clear();
85      }
86      finally { locker.ExitWriteLock(); }
87    }
88
89    public virtual void LogMessage(string message) {
90      locker.EnterWriteLock();
91      try {
92        log.LogMessage(message);
93      }
94      finally { locker.ExitWriteLock(); }
95    }
96
97    public virtual void LogException(Exception ex) {
98      locker.EnterWriteLock();
99      try {
100        log.LogException(ex);
101      }
102      finally { locker.ExitWriteLock(); }
103    }
104
105    #region Log Events
106    private void RegisterLogEvents() {
107      this.log.Cleared += new EventHandler(log_Cleared);
108      this.log.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded);
109      this.log.ToStringChanged += new EventHandler(log_ToStringChanged);
110    }
111
112    private void log_ToStringChanged(object sender, EventArgs e) {
113      OnToStringChanged();
114    }
115
116    private void log_MessageAdded(object sender, EventArgs<string> e) {
117      OnMessageAdded(e.Value);
118    }
119
120    private void log_Cleared(object sender, EventArgs e) {
121      OnCleared();
122    }
123    #endregion
124
125    #region Event Handler
126    public event EventHandler<EventArgs<string>> MessageAdded;
127    protected virtual void OnMessageAdded(string message) {
128      EventHandler<EventArgs<string>> handler = MessageAdded;
129      if (handler != null) handler(this, new EventArgs<string>(message));
130    }
131    public event EventHandler Cleared;
132    protected virtual void OnCleared() {
133      EventHandler handler = Cleared;
134      if (handler != null) handler(this, EventArgs.Empty);
135    }
136    #endregion
137
138
139  }
140}
Note: See TracBrowser for help on using the repository browser.