Skip to content

Commit

Permalink
Adds Console Log Formatting APIs (#38616)
Browse files Browse the repository at this point in the history
  • Loading branch information
maryamariyan authored Jul 15, 2020
1 parent 687177b commit 69d98ee
Show file tree
Hide file tree
Showing 47 changed files with 3,033 additions and 727 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ internal static bool IsGetConsoleModeCallSuccessful(IntPtr handle)
internal static extern bool SetConsoleMode(IntPtr handle, int mode);

internal const int ENABLE_PROCESSED_INPUT = 0x0001;
internal const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
internal const int STD_OUTPUT_HANDLE = -11;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -166,4 +168,14 @@ private void CheckAndResizeBuffer(int sizeHint)
Debug.Assert(_rentedBuffer.Length - _index >= sizeHint);
}
}

internal static partial class ThrowHelper
{
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowOutOfMemoryException_BufferMaximumSizeExceeded(uint capacity)
{
throw new OutOfMemoryException(SR.Format(SR.BufferMaximumSizeExceeded, capacity));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ public enum LogLevel
}
namespace Microsoft.Extensions.Logging.Abstractions
{
public readonly partial struct LogEntry<TState>
{
private readonly TState _State_k__BackingField;
private readonly object _dummy;
private readonly int _dummyPrimitive;
public LogEntry(Microsoft.Extensions.Logging.LogLevel logLevel, string category, Microsoft.Extensions.Logging.EventId eventId, TState state, System.Exception exception, System.Func<TState, System.Exception, string> formatter) { throw null; }
public string Category { get { throw null; } }
public Microsoft.Extensions.Logging.EventId EventId { get { throw null; } }
public System.Exception Exception { get { throw null; } }
public System.Func<TState, System.Exception, string> Formatter { get { throw null; } }
public Microsoft.Extensions.Logging.LogLevel LogLevel { get { throw null; } }
public TState State { get { throw null; } }
}
public partial class NullLogger : Microsoft.Extensions.Logging.ILogger
{
internal NullLogger() { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.Extensions.Logging.Abstractions
{
/// <summary>
/// Holds the information for a single log entry.
/// </summary>
public readonly struct LogEntry<TState>
{
/// <summary>
/// Initializes an instance of the LogEntry struct.
/// </summary>
/// <param name="logLevel">The log level.</param>
/// <param name="category">The category name for the log.</param>
/// <param name="eventId">The log event Id.</param>
/// <param name="state">The state for which log is being written.</param>
/// <param name="exception">The log exception.</param>
/// <param name="formatter">The formatter.</param>
public LogEntry(LogLevel logLevel, string category, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
LogLevel = logLevel;
Category = category;
EventId = eventId;
State = state;
Exception = exception;
Formatter = formatter;
}

/// <summary>
/// Gets the LogLevel
/// </summary>
public LogLevel LogLevel { get; }

/// <summary>
/// Gets the log category
/// </summary>
public string Category { get; }

/// <summary>
/// Gets the log EventId
/// </summary>
public EventId EventId { get; }

/// <summary>
/// Gets the TState
/// </summary>
public TState State { get; }

/// <summary>
/// Gets the log exception
/// </summary>
public Exception Exception { get; }

/// <summary>
/// Gets the formatter
/// </summary>
public Func<TState, Exception, string> Formatter { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,35 @@ public static partial class ConsoleLoggerExtensions
{
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsoleFormatter<TFormatter, TOptions>(this Microsoft.Extensions.Logging.ILoggingBuilder builder) where TFormatter : Microsoft.Extensions.Logging.Console.ConsoleFormatter where TOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddConsoleFormatter<TFormatter, TOptions>(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<TOptions> configure) where TFormatter : Microsoft.Extensions.Logging.Console.ConsoleFormatter where TOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddJsonConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.JsonConsoleFormatterOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSimpleConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.SimpleConsoleFormatterOptions> configure) { throw null; }
public static Microsoft.Extensions.Logging.ILoggingBuilder AddSystemdConsole(this Microsoft.Extensions.Logging.ILoggingBuilder builder, System.Action<Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions> configure) { throw null; }
}
}
namespace Microsoft.Extensions.Logging.Console
{
public abstract partial class ConsoleFormatter
{
protected ConsoleFormatter(string name) { }
public string Name { get { throw null; } }
public abstract void Write<TState>(in Microsoft.Extensions.Logging.Abstractions.LogEntry<TState> logEntry, Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider, System.IO.TextWriter textWriter);
}
public static partial class ConsoleFormatterNames
{
public const string Json = "json";
public const string Simple = "simple";
public const string Systemd = "systemd";
}
public partial class ConsoleFormatterOptions
{
public ConsoleFormatterOptions() { }
public bool IncludeScopes { get { throw null; } set { } }
public string TimestampFormat { get { throw null; } set { } }
public bool UseUtcTimestamp { get { throw null; } set { } }
}
[System.ObsoleteAttribute("ConsoleLoggerFormat has been deprecated.", false)]
public enum ConsoleLoggerFormat
{
Default = 0,
Expand All @@ -22,19 +47,37 @@ public enum ConsoleLoggerFormat
public partial class ConsoleLoggerOptions
{
public ConsoleLoggerOptions() { }
[System.ObsoleteAttribute("ConsoleLoggerOptions.DisableColors has been deprecated. Please use SimpleConsoleFormatterOptions.DisableColors instead.", false)]
public bool DisableColors { get { throw null; } set { } }
[System.ObsoleteAttribute("ConsoleLoggerOptions.Format has been deprecated. Please use ConsoleLoggerOptions.FormatterName instead.", false)]
public Microsoft.Extensions.Logging.Console.ConsoleLoggerFormat Format { get { throw null; } set { } }
public string FormatterName { get { throw null; } set { } }
[System.ObsoleteAttribute("ConsoleLoggerOptions.IncludeScopes has been deprecated. Please use ConsoleFormatterOptions.IncludeScopes instead.", false)]
public bool IncludeScopes { get { throw null; } set { } }
public Microsoft.Extensions.Logging.LogLevel LogToStandardErrorThreshold { get { throw null; } set { } }
[System.ObsoleteAttribute("ConsoleLoggerOptions.TimestampFormat has been deprecated. Please use ConsoleFormatterOptions.TimestampFormat instead.", false)]
public string TimestampFormat { get { throw null; } set { } }
[System.ObsoleteAttribute("ConsoleLoggerOptions.UseUtcTimestamp has been deprecated. Please use ConsoleFormatterOptions.UseUtcTimestamp instead.", false)]
public bool UseUtcTimestamp { get { throw null; } set { } }
}
[Microsoft.Extensions.Logging.ProviderAliasAttribute("Console")]
public partial class ConsoleLoggerProvider : Microsoft.Extensions.Logging.ILoggerProvider, Microsoft.Extensions.Logging.ISupportExternalScope, System.IDisposable
{
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options) { }
public ConsoleLoggerProvider(Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions> options, System.Collections.Generic.IEnumerable<Microsoft.Extensions.Logging.Console.ConsoleFormatter> formatters) { }
public Microsoft.Extensions.Logging.ILogger CreateLogger(string name) { throw null; }
public void Dispose() { }
public void SetScopeProvider(Microsoft.Extensions.Logging.IExternalScopeProvider scopeProvider) { }
}
public partial class JsonConsoleFormatterOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions
{
public JsonConsoleFormatterOptions() { }
public System.Text.Json.JsonWriterOptions JsonWriterOptions { get { throw null; } set { } }
}
public partial class SimpleConsoleFormatterOptions : Microsoft.Extensions.Logging.Console.ConsoleFormatterOptions
{
public SimpleConsoleFormatterOptions() { }
public bool DisableColors { get { throw null; } set { } }
public bool SingleLine { get { throw null; } set { } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<ItemGroup>
<Compile Include="Microsoft.Extensions.Logging.Console.cs" />
<ProjectReference Include="..\..\System.Text.Json\ref\System.Text.Json.csproj" />
<ProjectReference Include="..\..\Microsoft.Extensions.Logging.Abstractions\ref\Microsoft.Extensions.Logging.Abstractions.csproj" />
<ProjectReference Include="..\..\Microsoft.Extensions.Logging\ref\Microsoft.Extensions.Logging.csproj" />
<ProjectReference Include="..\..\Microsoft.Extensions.Options\ref\Microsoft.Extensions.Options.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,119 +7,20 @@
namespace Microsoft.Extensions.Logging.Console
{
/// <summary>
/// For non-Windows platform consoles which understand the ANSI escape code sequences to represent color
/// For consoles which understand the ANSI escape code sequences to represent color
/// </summary>
internal class AnsiLogConsole : IConsole
{
private readonly StringBuilder _outputBuilder;
private readonly IAnsiSystemConsole _systemConsole;

public AnsiLogConsole(IAnsiSystemConsole systemConsole)
public AnsiLogConsole(bool stdErr = false)
{
_outputBuilder = new StringBuilder();
_systemConsole = systemConsole;
_systemConsole = new AnsiSystemConsole(stdErr);
}

public void Write(string message, ConsoleColor? background, ConsoleColor? foreground)
public void Write(string message)
{
// Order: backgroundcolor, foregroundcolor, Message, reset foregroundcolor, reset backgroundcolor
if (background.HasValue)
{
_outputBuilder.Append(GetBackgroundColorEscapeCode(background.Value));
}

if (foreground.HasValue)
{
_outputBuilder.Append(GetForegroundColorEscapeCode(foreground.Value));
}

_outputBuilder.Append(message);

if (foreground.HasValue)
{
_outputBuilder.Append("\x1B[39m\x1B[22m"); // reset to default foreground color
}

if (background.HasValue)
{
_outputBuilder.Append("\x1B[49m"); // reset to the background color
}
}

public void WriteLine(string message, ConsoleColor? background, ConsoleColor? foreground)
{
Write(message, background, foreground);
_outputBuilder.AppendLine();
}

public void Flush()
{
_systemConsole.Write(_outputBuilder.ToString());
_outputBuilder.Clear();
}

private static string GetForegroundColorEscapeCode(ConsoleColor color)
{
switch (color)
{
case ConsoleColor.Black:
return "\x1B[30m";
case ConsoleColor.DarkRed:
return "\x1B[31m";
case ConsoleColor.DarkGreen:
return "\x1B[32m";
case ConsoleColor.DarkYellow:
return "\x1B[33m";
case ConsoleColor.DarkBlue:
return "\x1B[34m";
case ConsoleColor.DarkMagenta:
return "\x1B[35m";
case ConsoleColor.DarkCyan:
return "\x1B[36m";
case ConsoleColor.Gray:
return "\x1B[37m";
case ConsoleColor.Red:
return "\x1B[1m\x1B[31m";
case ConsoleColor.Green:
return "\x1B[1m\x1B[32m";
case ConsoleColor.Yellow:
return "\x1B[1m\x1B[33m";
case ConsoleColor.Blue:
return "\x1B[1m\x1B[34m";
case ConsoleColor.Magenta:
return "\x1B[1m\x1B[35m";
case ConsoleColor.Cyan:
return "\x1B[1m\x1B[36m";
case ConsoleColor.White:
return "\x1B[1m\x1B[37m";
default:
return "\x1B[39m\x1B[22m"; // default foreground color
}
}

private static string GetBackgroundColorEscapeCode(ConsoleColor color)
{
switch (color)
{
case ConsoleColor.Black:
return "\x1B[40m";
case ConsoleColor.Red:
return "\x1B[41m";
case ConsoleColor.Green:
return "\x1B[42m";
case ConsoleColor.Yellow:
return "\x1B[43m";
case ConsoleColor.Blue:
return "\x1B[44m";
case ConsoleColor.Magenta:
return "\x1B[45m";
case ConsoleColor.Cyan:
return "\x1B[46m";
case ConsoleColor.White:
return "\x1B[47m";
default:
return "\x1B[49m"; // Use default background color
}
_systemConsole.Write(message);
}
}
}
Loading

0 comments on commit 69d98ee

Please sign in to comment.