NTP Analyzer  0.8.2
Analyze the operation of time servers
Program.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.Globalization;
24 using System.IO;
25 using System.Threading;
26 using Ntp.Analyzer.Process;
27 using Ntp.Common.IO;
28 using Ntp.Common.Log;
29 using Ntp.Common.System;
30 
31 namespace Ntp.Analyzer.Cli
32 {
33  public static class Program
34  {
35  private static bool usage;
36 
37  public static void Main(string[] args)
38  {
39  Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
40  string name = "ntpa";
41  string tempDir = Directory.GetCurrentDirectory();
42  string configFile = null;
43  string pidFile = null;
44 
45  var initlog = LogFactory.CreateErrorLog(name);
46  initlog.Add(LogFactory.CreateSysLog(name));
47 
48  var p = new OptionSet
49  {
50  {"h|?|help", v => { ShowUsage(); }},
51  {"daemon=", v => { name = v; }},
52  {"config=", v => { configFile = v; }},
53  {"writepid=", v => { pidFile = v; }},
54  {"temp=", v => { tempDir = v; }}
55  };
56 
57  var rem = p.Parse(args).ToArray();
58 
59  if (usage)
60  {
61  return;
62  }
63 
64  if (rem.Length > 0)
65  {
66  initlog.WriteLine("Unknown option: " + rem[0], Severity.Error);
67  return;
68  }
69 
70  if (configFile == null)
71  {
72  initlog.WriteLine("Please specify configuration file with option --config", Severity.Error);
73  return;
74  }
75 
77  int pid = ProcessInfo.ProcessId;
78 
79  try
80  {
81  var main = new Main(configFile, pid, pidFile, name, initlog);
82  main.Run();
83  }
84  catch (Exception e)
85  {
86  initlog.WriteLine("Unexpected error: " + e.Message, Severity.Error);
87  initlog.WriteLine(e.StackTrace, Severity.Error);
88  }
89  }
90 
91  private static void ShowUsage()
92  {
93  Console.WriteLine("NTP Analyzer v0.8.2");
94  Console.WriteLine("Usage: ntpa --config file [--temp dir] [--writepid file] [--daemon name]");
95  usage = true;
96  }
97  }
98 }
void Add(LogBase log)
Definition: LogGroup.cs:71
static bool usage
Definition: Program.cs:35
static void Main(string[] args)
Definition: Program.cs:37
static LogBase CreateSysLog(string name)
Definition: LogFactory.cs:89
static string WorkingDirectory
Definition: ShellCommand.cs:42
var e
Definition: bootstrap.min.js:6
static void ShowUsage()
Definition: Program.cs:91
static LogGroup CreateErrorLog(string name)
Definition: LogFactory.cs:50