Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Core/3.3/ThreadSafeLog.cs @ 14563

Last change on this file since 14563 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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        } finally { locker.ExitReadLock(); }
49      }
50    }
51
52    [Storable]
53    protected ILog log;
54
55    [StorableConstructor]
56    protected ThreadSafeLog(bool deserializing) : base(deserializing) { }
57    public ThreadSafeLog()
58      : base() {
59      this.log = new Log();
60      RegisterLogEvents();
61    }
62    public ThreadSafeLog(ILog log)
63      : base() {
64      this.log = log;
65      RegisterLogEvents();
66    }
67
68    protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
69      : base(original, cloner) {
70      original.locker.EnterReadLock();
71      try {
72        log = cloner.Clone(original.log);
73      } finally { original.locker.ExitReadLock(); }
74    }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new ThreadSafeLog(this, cloner);
77    }
78
79    public virtual void Clear() {
80      locker.EnterWriteLock();
81      try {
82        log.Clear();
83      } finally { locker.ExitWriteLock(); }
84    }
85
86    public virtual void LogMessage(string message) {
87      locker.EnterWriteLock();
88      try {
89        log.LogMessage(message);
90      } finally { locker.ExitWriteLock(); }
91    }
92
93    public virtual void LogException(Exception ex) {
94      locker.EnterWriteLock();
95      try {
96        log.LogException(ex);
97      } finally { locker.ExitWriteLock(); }
98    }
99
100    #region Log Events
101    private void RegisterLogEvents() {
102      this.log.Cleared += new EventHandler(log_Cleared);
103      this.log.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded);
104      this.log.ToStringChanged += new EventHandler(log_ToStringChanged);
105    }
106
107    private void log_ToStringChanged(object sender, EventArgs e) {
108      OnToStringChanged();
109    }
110
111    private void log_MessageAdded(object sender, EventArgs<string> e) {
112      OnMessageAdded(e.Value);
113    }
114
115    private void log_Cleared(object sender, EventArgs e) {
116      OnCleared();
117    }
118    #endregion
119
120    #region Event Handler
121    public event EventHandler<EventArgs<string>> MessageAdded;
122    protected virtual void OnMessageAdded(string message) {
123      EventHandler<EventArgs<string>> handler = MessageAdded;
124      if (handler != null) handler(this, new EventArgs<string>(message));
125    }
126    public event EventHandler Cleared;
127    protected virtual void OnCleared() {
128      EventHandler handler = Cleared;
129      if (handler != null) handler(this, EventArgs.Empty);
130    }
131    #endregion
132
133
134  }
135}
Note: See TracBrowser for help on using the repository browser.