Skip to content

Commit

Permalink
feat: expose extism_log_file on Plugin (#34)
Browse files Browse the repository at this point in the history
We used to expose it on Context, but Context doesn't exist anymore
  • Loading branch information
mhmd-azeez authored Dec 12, 2023
1 parent 42920fd commit 234610e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
48 changes: 18 additions & 30 deletions src/Extism.Sdk/LibExtism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ internal struct ExtismPlugin { }
unsafe internal static extern IntPtr extism_plugin_output_data(ExtismPlugin* plugin);

/// <summary>
/// Set log file and level.
/// Set log file and level for file logger.
/// </summary>
/// <param name="filename"></param>
/// <param name="logLevel"></param>
Expand All @@ -298,40 +298,28 @@ internal struct ExtismPlugin { }
internal static extern bool extism_log_file(string filename, string logLevel);

/// <summary>
/// Get Extism Runtime version.
/// Enable a custom log handler, this will buffer logs until `extism_log_drain` is called.
/// this will buffer logs until `extism_log_drain` is called
/// </summary>
/// <param name="logLevel"></param>
/// <returns></returns>
[DllImport("extism")]
internal static extern IntPtr extism_version();
internal static extern bool extism_log_custom(string logLevel);

internal delegate void LoggingSink(string line, ulong length);

/// <summary>
/// Extism Log Levels
/// Calls the provided callback function for each buffered log line.
/// This is only needed when `extism_log_custom` is used.
/// </summary>
internal static class LogLevels
{
/// <summary>
/// Designates very serious errors.
/// </summary>
internal const string Error = "Error";

/// <summary>
/// Designates hazardous situations.
/// </summary>
internal const string Warn = "Warn";

/// <summary>
/// Designates useful information.
/// </summary>
internal const string Info = "Info";

/// <summary>
/// Designates lower priority information.
/// </summary>
internal const string Debug = "Debug";
/// <param name="callback"></param>
[DllImport("extism")]
internal static extern void extism_log_drain(LoggingSink callback);

/// <summary>
/// Designates very low priority, often extremely verbose, information.
/// </summary>
internal const string Trace = "Trace";
}
/// <summary>
/// Get Extism Runtime version.
/// </summary>
/// <returns></returns>
[DllImport("extism")]
internal static extern IntPtr extism_version();
}
4 changes: 2 additions & 2 deletions src/Extism.Sdk/LogLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public enum LogLevel
/// <summary>
/// Designates very serious errors.
/// </summary>
Error,
Error = 1,

/// <summary>
/// Designates hazardous situations.
/// </summary>
Warning,
Warn,

/// <summary>
/// Designates useful information.
Expand Down
42 changes: 41 additions & 1 deletion src/Extism.Sdk/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,44 @@ public static string ExtismVersion()
var version = LibExtism.extism_version();
return Marshal.PtrToStringAnsi(version);
}
}

/// <summary>
/// Set log file and level
/// </summary>
/// <param name="path">Log file path</param>
/// <param name="level">Minimum log level</param>
public static void ConfigureFileLogging(string path, LogLevel level)
{
var logLevel = Enum.GetName(typeof(LogLevel), level).ToLowerInvariant();
LibExtism.extism_log_file(path, logLevel);
}

/// <summary>
/// Enable a custom log handler, this will buffer logs until <see cref="DrainCustomLogs(LoggingSink)"/> is called.
/// </summary>
/// <param name="level"></param>
public static void ConfigureCustomLogging(LogLevel level)
{
var logLevel = Enum.GetName(typeof(LogLevel), level).ToLowerInvariant();
LibExtism.extism_log_custom(logLevel);
}

/// <summary>
/// Calls the provided callback function for each buffered log line.
/// This only needed when <see cref="ConfigureCustomLogging(LogLevel)"/> is used.
/// </summary>
/// <param name="callback"></param>
public static void DrainCustomLogs(LoggingSink callback)
{
LibExtism.extism_log_drain((line, length) =>
{
callback(line);
});
}
}

/// <summary>
/// Custom logging callback.
/// </summary>
/// <param name="line"></param>
public delegate void LoggingSink(string line);
47 changes: 47 additions & 0 deletions test/Extism.Sdk/BasicTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Extism.Sdk.Native;

using Shouldly;

using System.Runtime.InteropServices;
using System.Text;

Expand Down Expand Up @@ -178,6 +180,51 @@ public void HostFunctionsWithMemory()
Encoding.UTF8.GetString(response).ShouldBe("HELLO FRODO!");
}

[Fact]
public void FileLog()
{
var tempFile = Path.GetTempFileName();
Plugin.ConfigureFileLogging(tempFile, LogLevel.Warn);
using (var plugin = Helpers.LoadPlugin("log.wasm"))
{
plugin.Call("run_test", Array.Empty<byte>());
}

// HACK: tempFile gets locked by the Extism runtime
var tempFile2 = Path.GetTempFileName();
File.Copy(tempFile, tempFile2, true);

var content = File.ReadAllText(tempFile2);
content.ShouldContain("warn");
content.ShouldContain("error");
content.ShouldNotContain("info");
content.ShouldNotContain("debug");
content.ShouldNotContain("trace");
}


// [Fact]
// Interferes with FileLog
internal void CustomLog()
{
var builder = new StringBuilder();

Plugin.ConfigureCustomLogging(LogLevel.Warn);
using (var plugin = Helpers.LoadPlugin("log.wasm"))
{
plugin.Call("run_test", Array.Empty<byte>());
}

Plugin.DrainCustomLogs(line => builder.AppendLine(line));

var content = builder.ToString();
content.ShouldContain("warn");
content.ShouldContain("error");
content.ShouldNotContain("info");
content.ShouldNotContain("debug");
content.ShouldNotContain("trace");
}

public class CountVowelsResponse
{
public int Count { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion test/Extism.Sdk/Extism.Sdk.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
Expand Down

0 comments on commit 234610e

Please sign in to comment.