NTP Analyzer  0.8.2
Analyze the operation of time servers
ActivityLog.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections;
24 using System.Collections.Generic;
25 
26 namespace Ntp.Common.Log
27 {
28  public sealed class ActivityLog : LogBase, IEnumerable<string>
29  {
30  internal ActivityLog()
31  : base(Severity.Debug)
32  {
33  activity = new List<string>(2001);
34  }
35 
36  private readonly List<string> activity;
37  private readonly object locker = new object();
38 
39  public IEnumerator<string> GetEnumerator()
40  {
41  return activity.GetEnumerator();
42  }
43 
44  IEnumerator IEnumerable.GetEnumerator()
45  {
46  return activity.GetEnumerator();
47  }
48 
49  public override void Close()
50  {
51  activity.Clear();
52  }
53 
54  public override void Initialize()
55  {
56  activity.Clear();
57  }
58 
59  public override void Resume()
60  {
61  }
62 
63  public override void Suspend()
64  {
65  }
66 
67  public override void WriteLine(string text, Severity severity)
68  {
69  string severityText;
70 
71  if (severity == Severity.Trace)
72  return;
73 
74  switch (severity)
75  {
76  case Severity.Error:
77  severityText = "E";
78  break;
79  case Severity.Warn:
80  severityText = "W";
81  break;
82  case Severity.Notice:
83  severityText = "N";
84  break;
85  case Severity.Info:
86  severityText = "I";
87  break;
88  case Severity.Debug:
89  severityText = "D";
90  break;
91  case Severity.Trace:
92  severityText = "T";
93  break;
94  default:
95  throw new ArgumentOutOfRangeException(nameof(severity));
96  }
97 
98  var now = DateTime.Now;
99  string entry = string.Concat(
100  now.ToString("HH:mm:ss"),
101  " ", severityText, " ",
102  text);
103 
104  lock (locker)
105  {
106  if (activity.Count == 1000)
107  activity.RemoveAt(0);
108 
109  activity.Add(entry);
110  }
111  }
112 
113  public override void WriteLine(Exception exception)
114  {
115  WriteLine(exception.Message, Severity.Error);
116  WriteLine(exception.StackTrace, Severity.Debug);
117  }
118 
119  public override void WriteLine(Exception exception, Severity severity)
120  {
121  if (severity == Severity.Trace)
122  return;
123 
124  WriteLine(exception.Message, severity);
125  WriteLine(exception.StackTrace, severity);
126  }
127  }
128 }
readonly List< string > activity
Definition: ActivityLog.cs:36
override void WriteLine(Exception exception)
Definition: ActivityLog.cs:113
override void WriteLine(string text, Severity severity)
Definition: ActivityLog.cs:67
override void Initialize()
Definition: ActivityLog.cs:54
override void Close()
Definition: ActivityLog.cs:49
override void WriteLine(Exception exception, Severity severity)
Definition: ActivityLog.cs:119
IEnumerator< string > GetEnumerator()
Definition: ActivityLog.cs:39
override void Suspend()
Definition: ActivityLog.cs:63
override void Resume()
Definition: ActivityLog.cs:59