Add command line option to specify the logging directory (#336)

Two changes in this PR:
* Add a --log-dir command line parameter and fix command line parsing
* Fix command line parsing where arguments with parameter were parsed incorrectly
This commit is contained in:
Brian O'Neill
2017-05-10 08:55:46 -07:00
committed by GitHub
parent 2e9843cec1
commit 7625c8d83d
6 changed files with 124 additions and 55 deletions

View File

@@ -6,6 +6,7 @@ using Microsoft.SqlTools.ServiceLayer.Hosting;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;
using System.IO;
namespace Microsoft.SqlTools.ServiceLayer
{
@@ -19,26 +20,39 @@ namespace Microsoft.SqlTools.ServiceLayer
/// </summary>
internal static void Main(string[] args)
{
// read command-line arguments
CommandOptions commandOptions = new CommandOptions(args);
if (commandOptions.ShouldExit)
try
{
return;
// read command-line arguments
CommandOptions commandOptions = new CommandOptions(args);
if (commandOptions.ShouldExit)
{
return;
}
string logFilePath = "sqltools";
if (!string.IsNullOrWhiteSpace(commandOptions.LoggingDirectory))
{
logFilePath = Path.Combine(commandOptions.LoggingDirectory, logFilePath);
}
// turn on Verbose logging during early development
// we need to switch to Normal when preparing for public preview
Logger.Initialize(logFilePath: logFilePath, minimumLogLevel: LogLevel.Verbose, isEnabled: commandOptions.EnableLogging);
Logger.Write(LogLevel.Normal, "Starting SQL Tools Service Host");
// set up the host details and profile paths
var hostDetails = new HostDetails(version: new Version(1, 0));
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext);
serviceHost.WaitForExit();
}
catch (Exception e)
{
Logger.Write(LogLevel.Error, string.Format("An unhandled exception occurred: {0}", e));
Environment.Exit(1);
}
// turn on Verbose logging during early development
// we need to switch to Normal when preparing for public preview
Logger.Initialize(minimumLogLevel: LogLevel.Verbose, isEnabled: commandOptions.EnableLogging);
Logger.Write(LogLevel.Normal, "Starting SQL Tools Service Host");
// set up the host details and profile paths
var hostDetails = new HostDetails(version: new Version(1, 0));
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails);
ServiceHost serviceHost = HostLoader.CreateAndStartServiceHost(sqlToolsContext);
serviceHost.WaitForExit();
}
}
}

View File

@@ -5,6 +5,7 @@
using System;
using System.Globalization;
using System.IO;
namespace Microsoft.SqlTools.ServiceLayer.Utility
{
@@ -31,28 +32,24 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
// Extracting arguments and properties
arg = arg.Substring(1).ToLowerInvariant();
string argName = arg;
string argProperty = "";
int splitIndex = arg.IndexOf(' ');
if (splitIndex > 0)
{
argName = arg.Substring(0, splitIndex);
argProperty = arg.Substring(splitIndex + 1);
}
switch (argName)
{
case "-enable-logging":
EnableLogging = true;
break;
case "-log-dir":
SetLoggingDirectory(args[++i]);
break;
case "-locale":
SetLocale(argProperty);
SetLocale(args[++i]);
break;
case "h":
case "-help":
ShouldExit = true;
return;
default:
ErrorMessage += String.Format("Unknown argument \"{0}\" with property \"{1}\"" + Environment.NewLine, argName, argProperty);
ErrorMessage += String.Format("Unknown argument \"{0}\"" + Environment.NewLine, argName);
break;
}
}
@@ -81,6 +78,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
/// </summary>
public bool EnableLogging { get; private set; }
/// <summary>
/// Gets the directory where log files are output.
/// </summary>
public string LoggingDirectory { get; private set; }
/// <summary>
/// Whether the program should exit immediately. Set to true when the usage is printed.
/// </summary>
@@ -102,6 +104,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
"Microsoft.SqlTools.ServiceLayer.exe " + Environment.NewLine +
" Options:" + Environment.NewLine +
" [--enable-logging]" + Environment.NewLine +
" [--log-dir **] (default: current directory)" + Environment.NewLine +
" [--help]" + Environment.NewLine +
" [--locale **] (default: 'en')" + Environment.NewLine,
ErrorMessage);
@@ -109,6 +112,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
}
}
private void SetLoggingDirectory(string loggingDirectory)
{
if (string.IsNullOrWhiteSpace(loggingDirectory))
{
return;
}
this.LoggingDirectory = Path.GetFullPath(loggingDirectory);
}
private void SetLocale(string locale)
{
try
@@ -124,10 +137,11 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
// Setting our internal SR culture to our global culture
SR.Culture = CultureInfo.CurrentCulture;
}
catch(Exception ex)
catch (CultureNotFoundException)
{
// Warn user of invalid locale, and fall back to english
Console.WriteLine(ex);
// Ignore CultureNotFoundException since it only is thrown before Windows 10. Windows 10,
// along with macOS and Linux, pick up the default culture if an invalid locale is passed
// into the CultureInfo constructor.
}
}
}