NTP Analyzer  0.8.2
Analyze the operation of time servers
TextLog.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.Generic;
24 using System.IO;
25 
26 namespace Ntp.Common.Log
27 {
28  public abstract class TextLog : LogBase
29  {
30  protected TextLog(Severity threshold, string timeFormat)
31  : base(threshold)
32  {
33  this.timeFormat = timeFormat;
34  Initialized = false;
35  suspended = false;
36  }
37 
38  private readonly List<string> cache = new List<string>();
39 
40  protected readonly object Locker = new object();
41  private readonly string timeFormat;
42  protected bool Closed;
43  protected bool Initialized;
44  private bool suspended;
45  private TextWriter writer;
46 
47  public bool ShowTimeStamp { get; set; }
48 
49  public bool ShowSeverity { get; set; }
50 
51  public override void Initialize()
52  {
53  lock (Locker)
54  {
55  if (!Initialized && !suspended && !Closed)
56  {
57  writer = CreateWriter();
58  Initialized = true;
59  }
60  }
61  }
62 
63  public override void Resume()
64  {
65  lock (Locker)
66  {
67  suspended = false;
68  }
69  }
70 
71  public override void Suspend()
72  {
73  lock (Locker)
74  {
75  if (writer != null)
76  {
77  writer.Close();
78  writer.Dispose();
79  writer = null;
80  }
81  Initialized = false;
82  suspended = true;
83  }
84  }
85 
86  public override void WriteLine(string text, Severity severity)
87  {
88  if (!Initialized)
89  Initialize();
90 
91  if (!Initialized)
92  return;
93 
94  lock (Locker)
95  {
96  if (Initialized && !suspended && cache.Count != 0)
97  {
98  foreach (string line in cache)
99  {
100  writer.WriteLine(line);
101  }
102  writer.Flush();
103  cache.Clear();
104  }
105  }
106 
107  if (severity < Threshold)
108  return;
109 
110  string severityText = string.Empty;
111 
112  if (ShowSeverity)
113  {
114  string pad = string.Empty;
115 
116  switch (severity)
117  {
118  case Severity.Error:
119  severityText = "ERROR";
120  break;
121  case Severity.Warn:
122  severityText = "WARN";
123  pad = " ";
124  break;
125  case Severity.Notice:
126  severityText = "NOTICE";
127  break;
128  case Severity.Info:
129  severityText = "INFO";
130  pad = " ";
131  break;
132  case Severity.Debug:
133  severityText = "DEBUG";
134  break;
135  case Severity.Trace:
136  severityText = "TRACE";
137  break;
138  default:
139  throw new ApplicationException("Unknown severity level.");
140  }
141 
142  severityText = string.Concat(" [", severityText, "] ", pad);
143  }
144 
145  DateTime now = DateTime.Now;
146 
147  string stamp =
148  ShowTimeStamp && timeFormat != null
149  ? now.ToString(timeFormat)
150  : string.Empty;
151 
152  string entry = string.Concat(stamp, severityText, text);
153 
154  lock (Locker)
155  {
156  if (Initialized)
157  {
158  writer.WriteLine(entry);
159  writer.Flush();
160  }
161  else
162  {
163  cache.Add(entry);
164  }
165  }
166  }
167 
168  public override void WriteLine(Exception exception)
169  {
170  WriteLine(exception.Message, Severity.Error);
171  WriteLine(exception.StackTrace, Severity.Debug);
172  }
173 
174  public override void WriteLine(Exception exception, Severity severity)
175  {
176  if (severity < Threshold)
177  return;
178 
179  WriteLine(exception.Message, severity);
180  WriteLine(exception.StackTrace, severity);
181  }
182 
183  protected abstract TextWriter CreateWriter();
184  }
185 }
override void Initialize()
Definition: TextLog.cs:51
override void Suspend()
Definition: TextLog.cs:71
override void WriteLine(Exception exception, Severity severity)
Definition: TextLog.cs:174
override void WriteLine(Exception exception)
Definition: TextLog.cs:168
TextWriter writer
Definition: TextLog.cs:45
override void WriteLine(string text, Severity severity)
Definition: TextLog.cs:86
override void Resume()
Definition: TextLog.cs:63
TextLog(Severity threshold, string timeFormat)
Definition: TextLog.cs:30
readonly string timeFormat
Definition: TextLog.cs:41