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.Threading;
24 using Ntp.Common.Process;
25 
26 namespace Ntp.Common.Log
27 {
28     internal static class LogExtensions
29     {
ClusterError(this LogBase log, Exception e)30         internal static void ClusterError(this LogBase log, Exception e)
31         {
32             log.WriteLine(
33                 "Error in scheduler module. Aborting.",
34                 Severity.Error);
35 
36             log.WriteLine(e);
37         }
38 
ClusterNodeActive(this LogBase log, IRequest request)39         internal static void ClusterNodeActive(this LogBase log, IRequest request)
40         {
41             log.WriteLine(
42                 $"Cluster node {request} is active.",
43                 Severity.Notice);
44         }
45 
ClusterNodeAlive(this LogBase log, IRequest request)46         internal static void ClusterNodeAlive(this LogBase log, IRequest request)
47         {
48             log.WriteLine(
49                 $"Cluster node {request} is alive.",
50                 Severity.Notice);
51         }
52 
ClusterNodeDead(this LogBase log, IRequest request)53         internal static void ClusterNodeDead(this LogBase log, IRequest request)
54         {
55             log.WriteLine(
56                 $"Cluster node {request} is dead.",
57                 Severity.Notice);
58         }
59 
ClusterNodeError(this LogBase log, IRequest request, Exception e)60         internal static void ClusterNodeError(this LogBase log, IRequest request, Exception e)
61         {
62             log.WriteLine(
63                 $"Error while contacting cluster node {request}.",
64                 Severity.Warn);
65 
66             log.WriteLine(e);
67         }
68 
ClusterStart(this LogBase log)69         internal static void ClusterStart(this LogBase log)
70         {
71             log.WriteLine(
72                 "Starting cluster module.",
73                 Severity.Info);
74         }
75 
HeartbeatStarted(this LogBase log, int interval)76         internal static void HeartbeatStarted(this LogBase log, int interval)
77         {
78             log.WriteLine(
79                 $"Heartbeat started with {interval} minutes interval.",
80                 Severity.Notice);
81         }
82 
HeartbeatUptime(this LogBase log, string time)83         internal static void HeartbeatUptime(this LogBase log, string time)
84         {
85             log.WriteLine(
86                 $"Heartbeat: Uptime is {time}.",
87                 Severity.Notice);
88         }
89 
JobError(this LogBase log, Job job, Exception e)90         internal static void JobError(this LogBase log, Job job, Exception e)
91         {
92             log.WriteLine(
93                 $"Error while executing {job}.",
94                 Severity.Error);
95 
96             log.WriteLine(e);
97         }
98 
JobExecutionStatus(this LogBase log, Job job, bool error)99         internal static void JobExecutionStatus(this LogBase log, Job job, bool error)
100         {
101             log.WriteLine(
102                 error
103                     ? $"{job} failed."
104                     : $"{job} is done.",
105                 Severity.Debug);
106         }
107 
ReceivedSignal(this LogBase log, string signal)108         internal static void ReceivedSignal(this LogBase log, string signal)
109         {
110             log.WriteLine(
111                 $"Received signal {signal}",
112                 Severity.Debug);
113         }
114 
SchedulerAbort(this LogBase log, Thread thread)115         internal static void SchedulerAbort(this LogBase log, Thread thread)
116         {
117             log.WriteLine(
118                 $"Aborting thread {thread.Name}.",
119                 Severity.Warn);
120         }
121 
SchedulerBehind(this LogBase log)122         internal static void SchedulerBehind(this LogBase log)
123         {
124             log.WriteLine(
125                 "Behind schedule. Trying to catch up.",
126                 Severity.Info);
127         }
128 
SchedulerError(this LogBase log, string name, Exception e)129         internal static void SchedulerError(this LogBase log, string name, Exception e)
130         {
131             log.WriteLine(
132                 $"Unexpected error in thread {name}.",
133                 Severity.Error);
134 
135             log.WriteLine(e);
136         }
137 
SchedulerFinished(this LogBase log)138         internal static void SchedulerFinished(this LogBase log)
139         {
140             log.WriteLine(
141                 "All threads finished.",
142                 Severity.Notice);
143         }
144 
SchedulerJobAdded(this LogBase log, JobDescription description, Job job)145         internal static void SchedulerJobAdded(this LogBase log, JobDescription description, Job job)
146         {
147             var type = job.Description.JobType;
148             var freq = job.Schedule.Frequency;
149 
150             var desc = string.IsNullOrWhiteSpace(job.Description.Name)
151                 ? string.Empty
152                 : " " + description.Name;
153 
154             var fix = job.Schedule.FixedRun
155                 ? "fixed "
156                 : string.Empty;
157 
158             log.WriteLine(
159                 freq != 0
160                     ? $"{type} job{desc} added to scheduler with {fix}{freq} minutes run interval."
161                     : $"{type} job{desc} added to scheduler for a single run.",
162                 Severity.Info);
163         }
164 
SchedulerJobExecuting(this LogBase log, ScheduledJob next)165         internal static void SchedulerJobExecuting(this LogBase log, ScheduledJob next)
166         {
167             log.WriteLine(
168                 $"{next.Job} started.",
169                 Severity.Info);
170         }
171 
SchedulerJobStatus(this LogBase log, ScheduledJob scheduledJob)172         internal static void SchedulerJobStatus(this LogBase log, ScheduledJob scheduledJob)
173         {
174             string re = scheduledJob.Job.RunCount == 0 ? string.Empty : "re";
175 
176             log.WriteLine(
177                 $"{scheduledJob.Job} {re}scheduled to run {scheduledJob.Run.ToString("HH:mm:ss")}",
178                 Severity.Debug);
179         }
180 
SchedulerStart(this LogBase log, int count)181         internal static void SchedulerStart(this LogBase log, int count)
182         {
183             log.WriteLine(
184                 $"Starting scheduler with {count} jobs.",
185                 Severity.Info);
186         }
187 
SchedulerWaiting(this LogBase log, int count)188         internal static void SchedulerWaiting(this LogBase log, int count)
189         {
190             log.WriteLine(
191                 $"Waiting for {count} threads to finish.",
192                 Severity.Notice);
193         }
194 
ShellCommandError(this LogBase log, string message, string error)195         internal static void ShellCommandError(this LogBase log, string message, string error)
196         {
197             log.WriteLine(
198                 $"{message} {error}",
199                 Severity.Warn);
200         }
201 
ShellCommandExecuting(this LogBase log, string command, string arguments)202         internal static void ShellCommandExecuting(this LogBase log, string command, string arguments)
203         {
204             log.WriteLine(
205                 $"Executing: {command} {arguments}",
206                 Severity.Debug);
207         }
208 
SignalClosing(this LogBase log)209         internal static void SignalClosing(this LogBase log)
210         {
211             log.WriteLine(
212                 "Closing down.",
213                 Severity.Notice);
214         }
215 
SignalError(this LogBase log, Exception e)216         internal static void SignalError(this LogBase log, Exception e)
217         {
218             log.WriteLine(
219                 "Unrecoverable error in UnixSignal.WaitAny()",
220                 Severity.Error);
221 
222             log.WriteLine(e);
223         }
224 
SignalHandlerError(this LogBase log, Exception e)225         internal static void SignalHandlerError(this LogBase log, Exception e)
226         {
227             log.WriteLine(
228                 "Unrecoverable error in SignalHandler: {e.Message}",
229                 Severity.Error);
230 
231             log.WriteLine(e);
232         }
233 
SignalInterProcError(this LogBase log)234         internal static void SignalInterProcError(this LogBase log)
235         {
236             log.WriteLine(
237                 "Error in inter-process communication.",
238                 Severity.Warn);
239         }
240 
SignalRefreshing(this LogBase log)241         internal static void SignalRefreshing(this LogBase log)
242         {
243             log.WriteLine(
244                 "Refreshing logs.",
245                 Severity.Notice);
246         }
247 
SignalReloading(this LogBase log)248         internal static void SignalReloading(this LogBase log)
249         {
250             log.WriteLine(
251                 "Reloading configuration.",
252                 Severity.Notice);
253         }
254 
UnexpectedSignal(this LogBase log)255         internal static void UnexpectedSignal(this LogBase log)
256         {
257             log.WriteLine(
258                 "Unexpected inter-process signal.",
259                 Severity.Warn);
260         }
261 
UnknownSignal(this LogBase log, string signal)262         internal static void UnknownSignal(this LogBase log, string signal)
263         {
264             log.WriteLine(
265                 $"Received unknown signal {signal}",
266                 Severity.Warn);
267         }
268     }
269 }