Changeset 11596 for trunk/sources
- Timestamp:
- 11/27/14 15:54:13 (10 years ago)
- Location:
- trunk/sources/HeuristicLab.Core/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/Log.cs
r11171 r11596 30 30 [Item("Log", "A log for logging string messages.")] 31 31 [StorableClass] 32 public class Log : Item, ILog, IStorableContent {32 public sealed class Log : Item, ILog, IStorableContent { 33 33 public string Filename { get; set; } 34 34 … … 38 38 39 39 [Storable] 40 pr otectedIList<string> messages;41 public virtualIEnumerable<string> Messages {40 private IList<string> messages; 41 public IEnumerable<string> Messages { 42 42 get { return messages; } 43 43 } 44 44 45 45 [Storable] 46 pr otectedlong maxMessageCount;47 public virtuallong MaxMessageCount {46 private long maxMessageCount; 47 public long MaxMessageCount { 48 48 get { return maxMessageCount; } 49 49 } 50 50 51 51 [StorableConstructor] 52 pr otectedLog(bool deserializing) : base(deserializing) { }53 pr otectedLog(Log original, Cloner cloner)52 private Log(bool deserializing) : base(deserializing) { } 53 private Log(Log original, Cloner cloner) 54 54 : base(original, cloner) { 55 55 this.messages = new List<string>(original.messages); … … 66 66 } 67 67 68 public v irtual void Clear() {68 public void Clear() { 69 69 messages.Clear(); 70 70 OnCleared(); 71 71 } 72 public v irtual void LogMessage(string message) {72 public void LogMessage(string message) { 73 73 string s = FormatLogMessage(message); 74 74 messages.Add(s); … … 76 76 OnMessageAdded(s); 77 77 } 78 public v irtual void LogException(Exception ex) {78 public void LogException(Exception ex) { 79 79 string s = FormatException(ex); 80 80 messages.Add(s); … … 82 82 OnMessageAdded(s); 83 83 } 84 pr otected virtualvoid CapMessages() {84 private void CapMessages() { 85 85 while (maxMessageCount >= 0 && messages.Count > maxMessageCount) { 86 86 messages.RemoveAt(0); 87 87 } 88 88 } 89 p rotected virtualstring FormatLogMessage(string message) {89 public static string FormatLogMessage(string message) { 90 90 return DateTime.Now.ToString() + "\t" + message; 91 91 } 92 p rotected virtualstring FormatException(Exception ex) {92 public static string FormatException(Exception ex) { 93 93 return DateTime.Now.ToString() + "\t" + "Exception occurred:" + Environment.NewLine + ErrorHandling.BuildErrorMessage(ex); 94 94 } 95 95 96 96 public event EventHandler<EventArgs<string>> MessageAdded; 97 pr otected virtualvoid OnMessageAdded(string message) {97 private void OnMessageAdded(string message) { 98 98 EventHandler<EventArgs<string>> handler = MessageAdded; 99 99 if (handler != null) handler(this, new EventArgs<string>(message)); 100 100 } 101 101 public event EventHandler Cleared; 102 pr otected virtualvoid OnCleared() {102 private void OnCleared() { 103 103 EventHandler handler = Cleared; 104 104 if (handler != null) handler(this, EventArgs.Empty); -
trunk/sources/HeuristicLab.Core/3.3/ThreadSafeLog.cs
r11171 r11596 21 21 22 22 using System; 23 using System.Collections.Concurrent; 23 24 using System.Collections.Generic; 24 using System.Linq;25 using System.Threading;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 30 29 [Item("ThreadSafeLog", "A thread-safe log for logging string messages.")] 31 30 [StorableClass] 32 public sealed class ThreadSafeLog : Log { 33 private ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); 31 public sealed class ThreadSafeLog : Item, ILog, IStorableContent { 32 public string Filename { get; set; } 33 private ConcurrentQueue<string> messages; 34 34 35 public override IEnumerable<string> Messages { 36 get { 37 locker.EnterReadLock(); 38 try { 39 return messages.ToArray(); // return copy of messages 40 } finally { locker.ExitReadLock(); } 41 } 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; } 42 49 } 43 50 44 51 [StorableConstructor] 45 52 private ThreadSafeLog(bool deserializing) : base(deserializing) { } 46 public ThreadSafeLog(long maxMessageCount = -1) 47 : base(maxMessageCount) { 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; 48 61 } 49 62 50 private ThreadSafeLog(ThreadSafeLog original, Cloner cloner)51 : base(original, cloner) { }52 53 63 public override IDeepCloneable Clone(Cloner cloner) { 54 locker.EnterReadLock(); 55 try { 56 return new ThreadSafeLog(this, cloner); 57 } finally { locker.ExitReadLock(); } 64 return new ThreadSafeLog(this, cloner); 58 65 } 59 66 60 public override void Clear() { 61 locker.EnterWriteLock(); 62 try { 63 messages.Clear(); 64 } finally { locker.ExitWriteLock(); } 67 public void Clear() { 68 messages = new ConcurrentQueue<string>(); 65 69 OnCleared(); 66 70 } 67 71 68 public override void LogMessage(string message) { 69 string s = FormatLogMessage(message); 70 locker.EnterWriteLock(); 71 try { 72 messages.Add(s); 73 CapMessages(); 74 } finally { locker.ExitWriteLock(); } 72 public void LogMessage(string message) { 73 var s = Log.FormatLogMessage(message); 74 messages.Enqueue(s); 75 CapMessages(); 75 76 OnMessageAdded(s); 76 77 } 77 78 78 public override void LogException(Exception ex) { 79 string s = FormatException(ex); 80 locker.EnterWriteLock(); 81 try { 82 messages.Add(s); 83 CapMessages(); 84 } finally { locker.ExitWriteLock(); } 79 public void LogException(Exception ex) { 80 var s = Log.FormatException(ex); 81 messages.Enqueue(s); 82 CapMessages(); 85 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); 86 105 } 87 106 }
Note: See TracChangeset
for help on using the changeset viewer.