NTP Analyzer  0.8.2
Analyze the operation of time servers
SysLog.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 Mono.Unix;
24 using Mono.Unix.Native;
25 
26 namespace Ntp.Common.Log
27 {
28  public sealed class SysLog : LogBase
29  {
30  public SysLog(string name, Severity threshold)
31  : base(threshold, true)
32  {
33  this.name = name;
34  }
35 
36  private const SyslogFacility Facility = SyslogFacility.LOG_DAEMON;
37  private const SyslogOptions Options = SyslogOptions.LOG_PID;
38  private static bool initialized;
39 
40  private readonly string name;
41 
42  public override void Close()
43  {
44  if (initialized)
45  return;
46 
47  Syscall.closelog();
48  initialized = false;
49  }
50 
51  public override void Initialize()
52  {
53  if (initialized)
54  return;
55 
56  string ident = name;
57  Syscall.openlog(UnixMarshal.StringToHeap(ident), Options, Facility);
58  initialized = true;
59  }
60 
61  public override void Resume()
62  {
63  // SysLog cannot resume
64  }
65 
66  public override void Suspend()
67  {
68  // SysLog cannot suspend
69  }
70 
71  public override void WriteLine(Exception exception)
72  {
73  WriteLine(exception, Severity.Error);
74  }
75 
76  public override void WriteLine(Exception exception, Severity severity)
77  {
78  WriteLine(exception.Message, Severity.Error);
79  }
80 
81  public override void WriteLine(string text, Severity severity)
82  {
83  if (initialized)
84  Initialize();
85 
86  if (severity < Threshold)
87  return;
88 
89  Syscall.syslog(Facility, GetSyslogLevel(severity), text);
90  }
91 
92  private static SyslogLevel GetSyslogLevel(Severity severity)
93  {
94  switch (severity)
95  {
96  case Severity.Error:
97  return SyslogLevel.LOG_ERR;
98  case Severity.Warn:
99  return SyslogLevel.LOG_WARNING;
100  case Severity.Notice:
101  return SyslogLevel.LOG_NOTICE;
102  case Severity.Info:
103  return SyslogLevel.LOG_INFO;
104  case Severity.Debug:
105  case Severity.Trace:
106  return SyslogLevel.LOG_DEBUG;
107  default:
108  return SyslogLevel.LOG_DEBUG;
109  }
110  }
111  }
112 }
override void Resume()
Definition: SysLog.cs:61
override void Initialize()
Definition: SysLog.cs:51
override void WriteLine(string text, Severity severity)
Definition: SysLog.cs:81
override void WriteLine(Exception exception, Severity severity)
Definition: SysLog.cs:76
override void Close()
Definition: SysLog.cs:42
static SyslogLevel GetSyslogLevel(Severity severity)
Definition: SysLog.cs:92
readonly string name
Definition: SysLog.cs:40
override void WriteLine(Exception exception)
Definition: SysLog.cs:71
override void Suspend()
Definition: SysLog.cs:66
static bool initialized
Definition: SysLog.cs:38
SysLog(string name, Severity threshold)
Definition: SysLog.cs:30