[6357] | 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.Core;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Clients.Hive {
|
---|
| 32 | [Item("Log", "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 HeuristicLab.Common.Resources.VSImageLibrary.File; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public IEnumerable<string> Messages {
|
---|
| 44 | get {
|
---|
| 45 | locker.EnterReadLock();
|
---|
| 46 | try {
|
---|
| 47 | return log.Messages.ToArray();
|
---|
| 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 | protected ThreadSafeLog(ThreadSafeLog original, Cloner cloner)
|
---|
| 59 | : base(original, cloner) {
|
---|
| 60 | log = cloner.Clone(original.log);
|
---|
| 61 | }
|
---|
| 62 | public ThreadSafeLog(ILog log)
|
---|
| 63 | : base() {
|
---|
| 64 | this.log = log;
|
---|
| 65 | RegisterLogEvents();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 69 | return new ThreadSafeLog(this, cloner);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | public virtual void Clear() {
|
---|
| 73 | locker.EnterWriteLock();
|
---|
| 74 | try {
|
---|
| 75 | log.Clear();
|
---|
| 76 | }
|
---|
| 77 | finally { locker.ExitWriteLock(); }
|
---|
| 78 | }
|
---|
| 79 | public virtual void LogMessage(string message) {
|
---|
| 80 | locker.EnterWriteLock();
|
---|
| 81 | try {
|
---|
| 82 | log.LogMessage(message);
|
---|
| 83 | }
|
---|
| 84 | finally { locker.ExitWriteLock(); }
|
---|
| 85 | }
|
---|
| 86 | public virtual void LogException(Exception ex) {
|
---|
| 87 | locker.EnterWriteLock();
|
---|
| 88 | try {
|
---|
| 89 | log.LogException(ex);
|
---|
| 90 | }
|
---|
| 91 | finally { locker.ExitWriteLock(); }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | #region Log Events
|
---|
| 95 | private void RegisterLogEvents() {
|
---|
| 96 | this.log.Cleared += new EventHandler(log_Cleared);
|
---|
| 97 | this.log.MessageAdded += new EventHandler<EventArgs<string>>(log_MessageAdded);
|
---|
| 98 | this.log.ToStringChanged += new EventHandler(log_ToStringChanged);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | private void log_ToStringChanged(object sender, EventArgs e) {
|
---|
| 102 | OnToStringChanged();
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | private void log_MessageAdded(object sender, EventArgs<string> e) {
|
---|
| 106 | OnMessageAdded(e.Value);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | private void log_Cleared(object sender, EventArgs e) {
|
---|
| 110 | OnCleared();
|
---|
| 111 | }
|
---|
| 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region Event Handler
|
---|
| 115 | public event EventHandler<EventArgs<string>> MessageAdded;
|
---|
| 116 | protected virtual void OnMessageAdded(string message) {
|
---|
| 117 | EventHandler<EventArgs<string>> handler = MessageAdded;
|
---|
| 118 | if (handler != null) handler(this, new EventArgs<string>(message));
|
---|
| 119 | }
|
---|
| 120 | public event EventHandler Cleared;
|
---|
| 121 | protected virtual void OnCleared() {
|
---|
| 122 | EventHandler handler = Cleared;
|
---|
| 123 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 124 | }
|
---|
| 125 | #endregion
|
---|
| 126 | }
|
---|
| 127 | }
|
---|