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.Net;
25 using Ntp.Analyzer.Config.Node;
26 using Ntp.Analyzer.Monitor.Server;
27 using Ntp.Analyzer.Objects;
28 using Ntp.Common.Log;
29 
30 namespace Ntp.Analyzer.Process.Log
31 {
32     internal static class LogExtensions
33     {
AddErrors(this LogBase log, IEnumerable<string> errors)34         internal static void AddErrors(this LogBase log, IEnumerable<string> errors)
35         {
36             foreach (string error in errors)
37                 log.WriteLine(error, Severity.Error);
38         }
39 
ClusterError(this LogBase log, Exception e)40         internal static void ClusterError(this LogBase log, Exception e)
41         {
42             log.WriteLine("Error during initialization of cluster node.", Severity.Error);
43             log.WriteLine(e);
44         }
45 
ClusterReady(this LogBase log, NodeConfiguration node)46         internal static void ClusterReady(this LogBase log, NodeConfiguration node)
47         {
48             log.WriteLine($"Requesting to cluster {node.Address}", Severity.Notice);
49         }
50 
ConfigFile(this LogBase log, string file)51         internal static void ConfigFile(this LogBase log, string file)
52         {
53             log.WriteLine($"Using configuration {file}", Severity.Notice);
54         }
55 
DatabaseError(this LogBase log, Exception e)56         internal static void DatabaseError(this LogBase log, Exception e)
57         {
58             log.WriteLine("Error during initialization of database.", Severity.Error);
59             log.WriteLine(e);
60         }
61 
HostImportError(this LogBase log, Exception e)62         internal static void HostImportError(this LogBase log, Exception e)
63         {
64             log.WriteLine($"Could not import host data {e.Message}", Severity.Warn);
65             log.WriteLine(e, Severity.Trace);
66         }
67 
HostNotFound(this LogBase log, int hostId)68         internal static void HostNotFound(this LogBase log, int hostId)
69         {
70             log.WriteLine($"Host with ID {hostId} does not exists in database.", Severity.Warn);
71         }
72 
InitializationError(this LogBase log, Exception e)73         internal static void InitializationError(this LogBase log, Exception e)
74         {
75             log.WriteLine($"Cannot create logs: {e.Message}", Severity.Error);
76             log.WriteLine(e);
77         }
78 
InstanceName(this LogBase log, string name)79         internal static void InstanceName(this LogBase log, string name)
80         {
81             log.WriteLine($"Instance named {name}", Severity.Notice);
82         }
83 
JobInitEnd(this LogBase log)84         internal static void JobInitEnd(this LogBase log)
85         {
86             log.WriteLine("All jobs initialized.", Severity.Debug);
87         }
88 
JobInitStart(this LogBase log)89         internal static void JobInitStart(this LogBase log)
90         {
91             log.WriteLine("Initializing jobs.", Severity.Info);
92         }
93 
KnownServer(this LogBase log, int id, string name)94         internal static void KnownServer(this LogBase log, int id, string name)
95         {
96             log.WriteLine(
97                 $"Found known time server {name} with id {id} in list.",
98                 Severity.Info);
99         }
100 
ListenerError(this LogBase log, Exception e)101         internal static void ListenerError(this LogBase log, Exception e)
102         {
103             log.WriteLine("Error during initialization of command channels.", Severity.Warn);
104             log.WriteLine(e);
105         }
106 
ListenerReady(this LogBase log, Listener listener)107         internal static void ListenerReady(this LogBase log, Listener listener)
108         {
109             log.WriteLine($"Opening command channel on {listener}", Severity.Notice);
110         }
111 
NewHost(this LogBase log, HostConfiguration server, IPAddress ip)112         internal static void NewHost(this LogBase log, HostConfiguration server, IPAddress ip)
113         {
114             log.WriteLine(
115                 $"Created a new host in database ID {server.HostId} with name {server.ServerName} and IP {ip}.",
116                 Severity.Info);
117         }
118 
NewPeer(this LogBase log, AssociationEntry entry)119         internal static void NewPeer(this LogBase log, AssociationEntry entry)
120         {
121             log.WriteLine(
122                 $"Created a new peer in database with name {entry.Remote} and IP {entry.Remote}.",
123                 Severity.Info);
124         }
125 
NoConfig(this LogBase log, string file)126         internal static void NoConfig(this LogBase log, string file)
127         {
128             log.WriteLine($"Cannot find configuration file {file}", Severity.Error);
129         }
130 
OpenNtpConfigError(this LogBase log)131         internal static void OpenNtpConfigError(this LogBase log)
132         {
133             log.WriteLine("Traffic data collection is not supported for OpenNTP.", Severity.Warn);
134         }
135 
PeerImportError(this LogBase log, Exception e)136         internal static void PeerImportError(this LogBase log, Exception e)
137         {
138             log.WriteLine($"Could not import peer data {e.Message}", Severity.Warn);
139             log.WriteLine(e, Severity.Trace);
140         }
141 
PeerIpAmbiguous(this LogBase log, AssociationEntry entry)142         internal static void PeerIpAmbiguous(this LogBase log, AssociationEntry entry)
143         {
144             log.WriteLine(
145                 $"Ambiguous peer IP {entry.Remote}. Multiple occurrences found in database.",
146                 Severity.Warn);
147         }
148 
PeerIpNotFound(this LogBase log, AssociationEntry entry)149         internal static void PeerIpNotFound(this LogBase log, AssociationEntry entry)
150         {
151             log.WriteLine($"Peer with IP {entry.Remote} does not exists in database.", Severity.Warn);
152         }
153 
PidFileError(this LogBase log, Exception e)154         internal static void PidFileError(this LogBase log, Exception e)
155         {
156             log.WriteLine($"Cannot create pid file. {e.Message}", Severity.Warn);
157         }
158 
ProcessId(this LogBase log, int pid)159         internal static void ProcessId(this LogBase log, int pid)
160         {
161             log.WriteLine($"Running with process ID {pid}", Severity.Notice);
162         }
163 
SchedulerError(this LogBase log, Exception e)164         internal static void SchedulerError(this LogBase log, Exception e)
165         {
166             log.WriteLine("Error during initialization of scheduler.", Severity.Error);
167             log.WriteLine(e);
168         }
169 
Starting(this LogBase log, string version)170         internal static void Starting(this LogBase log, string version)
171         {
172             log.WriteLine($"NTP Analyzer {version} started.", Severity.Notice);
173         }
174 
TableError(this LogBase log, Exception e)175         internal static void TableError(this LogBase log, Exception e)
176         {
177             log.WriteLine("Could not populate tables with data.", Severity.Error);
178             log.WriteLine(e);
179         }
180 
UserId(this LogBase log, uint userId)181         internal static void UserId(this LogBase log, uint userId)
182         {
183             log.WriteLine($"Running under user id {userId}", Severity.Info);
184         }
185 
UserIdError(this LogBase log, uint userId)186         internal static void UserIdError(this LogBase log, uint userId)
187         {
188             log.WriteLine($"Failed to set user id {userId}", Severity.Warn);
189         }
190 
HostNameNotFound(this LogBase log, string ip, Exception e)191         internal static void HostNameNotFound(this LogBase log, string ip, Exception e)
192         {
193             log.WriteLine($"Could not find host name for IP {ip}.", Severity.Info);
194             log.WriteLine(e, Severity.Debug);
195         }
196     }
197 }