mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Added option --enable-logging to enable diagnostic logging (#106)
* Added option --enable-logging to enable diagnostic logging * Addressing feedback * Addressing feedback, round 2
This commit is contained in:
@@ -23,9 +23,16 @@ namespace Microsoft.SqlTools.ServiceLayer
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
internal static void Main(string[] args)
|
internal static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
// read command-line arguments
|
||||||
|
CommandOptions commandOptions = new CommandOptions(args);
|
||||||
|
if (commandOptions.ShouldExit)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// turn on Verbose logging during early development
|
// turn on Verbose logging during early development
|
||||||
// we need to switch to Normal when preparing for public preview
|
// we need to switch to Normal when preparing for public preview
|
||||||
Logger.Initialize(minimumLogLevel: LogLevel.Verbose);
|
Logger.Initialize(minimumLogLevel: LogLevel.Verbose, isEnabled: commandOptions.EnableLogging);
|
||||||
Logger.Write(LogLevel.Normal, "Starting SQL Tools Service Host");
|
Logger.Write(LogLevel.Normal, "Starting SQL Tools Service Host");
|
||||||
|
|
||||||
// set up the host details and profile paths
|
// set up the host details and profile paths
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The command-line options helper class.
|
||||||
|
/// </summary>
|
||||||
|
internal class CommandOptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Construct and parse command line options from the arguments array
|
||||||
|
/// </summary>
|
||||||
|
public CommandOptions(string[] args)
|
||||||
|
{
|
||||||
|
ErrorMessage = string.Empty;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
for (int i = 0; i < args.Length; ++i)
|
||||||
|
{
|
||||||
|
string arg = args[i];
|
||||||
|
if (arg.StartsWith("--") || arg.StartsWith("-"))
|
||||||
|
{
|
||||||
|
arg = arg.Substring(1).ToLowerInvariant();
|
||||||
|
switch (arg)
|
||||||
|
{
|
||||||
|
case "-enable-logging":
|
||||||
|
EnableLogging = true;
|
||||||
|
break;
|
||||||
|
case "h":
|
||||||
|
case "-help":
|
||||||
|
ShouldExit = true;
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
ErrorMessage += String.Format("Unknown argument \"{0}\"" + Environment.NewLine, arg);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ErrorMessage += ex.ToString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(ErrorMessage) || ShouldExit)
|
||||||
|
{
|
||||||
|
Console.WriteLine(Usage);
|
||||||
|
ShouldExit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal string ErrorMessage { get; private set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether diagnostic logging is enabled
|
||||||
|
/// </summary>
|
||||||
|
public bool EnableLogging { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether the program should exit immediately. Set to true when the usage is printed.
|
||||||
|
/// </summary>
|
||||||
|
public bool ShouldExit { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the usage string describing command-line arguments for the program
|
||||||
|
/// </summary>
|
||||||
|
public string Usage
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var str = string.Format("{0}" + Environment.NewLine +
|
||||||
|
"Microsoft.SqlTools.ServiceLayer.exe " + Environment.NewLine +
|
||||||
|
" Options:" + Environment.NewLine +
|
||||||
|
" [--enable-logging]" + Environment.NewLine +
|
||||||
|
" [--help]" + Environment.NewLine,
|
||||||
|
ErrorMessage);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.Test.Utility
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for the CommandOptions class
|
||||||
|
/// </summary>
|
||||||
|
public class CommandOptionsTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void LoggingEnabledWhenFlagProvided()
|
||||||
|
{
|
||||||
|
var args = new string[] {"--enable-logging"};
|
||||||
|
CommandOptions options = new CommandOptions(args);
|
||||||
|
Assert.NotNull(options);
|
||||||
|
|
||||||
|
Assert.True(options.EnableLogging);
|
||||||
|
Assert.False(options.ShouldExit);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void LoggingDisabledWhenFlagNotProvided()
|
||||||
|
{
|
||||||
|
var args = new string[] {};
|
||||||
|
CommandOptions options = new CommandOptions(args);
|
||||||
|
Assert.NotNull(options);
|
||||||
|
|
||||||
|
Assert.False(options.EnableLogging);
|
||||||
|
Assert.False(options.ShouldExit);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UsageIsShownWhenHelpFlagProvided()
|
||||||
|
{
|
||||||
|
var args = new string[] {"--help"};
|
||||||
|
CommandOptions options = new CommandOptions(args);
|
||||||
|
Assert.NotNull(options);
|
||||||
|
|
||||||
|
Assert.True(options.ShouldExit);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void UsageIsShownWhenBadArgumentsProvided()
|
||||||
|
{
|
||||||
|
var args = new string[] {"--unknown-argument", "/bad-argument"};
|
||||||
|
CommandOptions options = new CommandOptions(args);
|
||||||
|
Assert.NotNull(options);
|
||||||
|
|
||||||
|
Assert.True(options.ShouldExit);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DefaultValuesAreUsedWhenNoArgumentsAreProvided()
|
||||||
|
{
|
||||||
|
var args = new string[] {};
|
||||||
|
CommandOptions options = new CommandOptions(args);
|
||||||
|
Assert.NotNull(options);
|
||||||
|
|
||||||
|
Assert.False(options.EnableLogging);
|
||||||
|
Assert.False(options.ShouldExit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user