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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Common.Resources;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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(LockRecursionPolicy.SupportsRecursion);
|
---|
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 | ~ThreadSafeLog() {
|
---|
69 | locker.Dispose();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
|
---|
73 | : base(original, cloner) {
|
---|
74 | log = cloner.Clone(original.log);
|
---|
75 | }
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new ThreadSafeLog(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public virtual void Clear() {
|
---|
81 | locker.EnterWriteLock();
|
---|
82 | try {
|
---|
83 | log.Clear();
|
---|
84 | }
|
---|
85 | finally { locker.ExitWriteLock(); }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public virtual void LogMessage(string message) {
|
---|
89 | locker.EnterWriteLock();
|
---|
90 | try {
|
---|
91 | log.LogMessage(message);
|
---|
92 | }
|
---|
93 | finally { locker.ExitWriteLock(); }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public virtual void LogException(Exception ex) {
|
---|
97 | locker.EnterWriteLock();
|
---|
98 | try {
|
---|
99 | log.LogException(ex);
|
---|
100 | }
|
---|
101 | finally { locker.ExitWriteLock(); }
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region Log Events
|
---|
105 | private void RegisterLogEvents() {
|
---|
106 | this.log.Cleared += new EventHandler(log_Cleared);
|
---|
107 | this.log.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded);
|
---|
108 | this.log.ToStringChanged += new EventHandler(log_ToStringChanged);
|
---|
109 | }
|
---|
110 |
|
---|
111 | private void log_ToStringChanged(object sender, EventArgs e) {
|
---|
112 | OnToStringChanged();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void log_MessageAdded(object sender, EventArgs<string> e) {
|
---|
116 | OnMessageAdded(e.Value);
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void log_Cleared(object sender, EventArgs e) {
|
---|
120 | OnCleared();
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | #region Event Handler
|
---|
125 | public event EventHandler<EventArgs<string>> MessageAdded;
|
---|
126 | protected virtual void OnMessageAdded(string message) {
|
---|
127 | EventHandler<EventArgs<string>> handler = MessageAdded;
|
---|
128 | if (handler != null) handler(this, new EventArgs<string>(message));
|
---|
129 | }
|
---|
130 | public event EventHandler Cleared;
|
---|
131 | protected virtual void OnCleared() {
|
---|
132 | EventHandler handler = Cleared;
|
---|
133 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
134 | }
|
---|
135 | #endregion
|
---|
136 | }
|
---|
137 | }
|
---|