Skip to content

Commit

Permalink
add #!log command
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur authored and colombod committed Feb 21, 2020
1 parent fc93f16 commit 07df4c7
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
59 changes: 58 additions & 1 deletion Microsoft.DotNet.Interactive/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Pocket;
using CompositeDisposable = System.Reactive.Disposables.CompositeDisposable;

namespace Microsoft.DotNet.Interactive
{
Expand Down Expand Up @@ -49,6 +52,60 @@ public static Task<IKernelCommandResult> SubmitCodeAsync(
return kernel.SendAsync(new SubmitCode(code), CancellationToken.None);
}

public static T UseLog<T>(this T kernel)
where T : KernelBase
{
var command = new Command("#!log", "Enables session logging.");

var logStarted = false;

command.Handler = CommandHandler.Create<KernelInvocationContext>(async context =>
{
if (logStarted)
{
return;
}

logStarted = true;

kernel.AddMiddleware(async (kernelCommand, context, next) =>
{
await context.DisplayAsync(kernelCommand.ToLogString());

await next(kernelCommand, context);
});

var disposable = new CompositeDisposable();

disposable.Add(kernel.KernelEvents.Subscribe(async e =>
{
if (KernelInvocationContext.Current is {} currentContext)
{
if (!(e is DisplayEventBase))
{
await currentContext.DisplayAsync(e.ToLogString());
}
}
}));

disposable.Add(LogEvents.Subscribe(async e =>
{
if (KernelInvocationContext.Current is {} currentContext)
{
await currentContext.DisplayAsync(e.ToLogString());
}
}));

kernel.RegisterForDisposal(disposable);

await context.DisplayAsync("Logging enabled");
});

kernel.AddDirective(command);

return kernel;
}

[DebuggerStepThrough]
public static T LogEventsToPocketLogger<T>(this T kernel)
where T : IKernel
Expand Down
12 changes: 8 additions & 4 deletions Microsoft.DotNet.Interactive/Microsoft.DotNet.Interactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<PackageReference Include="PocketLogger" Version="$(PocketLoggerVersion)">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="PocketLogger.Subscribe" Version="0.6.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand All @@ -37,8 +41,8 @@
<NuspecProperty Include="SystemCommandLineVersion=$(SystemCommandLineVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Formatting\Microsoft.DotNet.Interactive.Formatting.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive.Formatting\Microsoft.DotNet.Interactive.Formatting.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Microsoft.DotNet.Interactive/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class StringExtensions
{
public static string TruncateForDisplay(
this string value,
int length = 25)
int length = 50)
{
value = value.Trim();

Expand Down
1 change: 1 addition & 0 deletions dotnet-interactive/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private static IKernel CreateKernel(

var kernel = compositeKernel
.UseDefaultMagicCommands()
.UseLog()
.UseAbout();

kernel.DefaultKernelName = defaultKernelName;
Expand Down

0 comments on commit 07df4c7

Please sign in to comment.