Skip to content

Commit

Permalink
Gtk: Write GTK logs to debuglog instead of stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 14, 2024
1 parent 36cb118 commit 7895a6f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAPS2.Lib.Gtk/EntryPoints/GtkEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class GtkEntryPoint
public static int Run(string[] args)
{
GLib.ExceptionManager.UnhandledException += UnhandledGtkException;
GLibLogInterceptor.WriteToDebugLog();
EtoPlatform.Current = new GtkEtoPlatform();

var subArgs = args.Skip(1).ToArray();
Expand Down
84 changes: 84 additions & 0 deletions NAPS2.Lib.Gtk/Util/GLibLogInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Globalization;
using System.Runtime.InteropServices;
using GLib;
using Log = NAPS2.Logging.Log;

namespace NAPS2.Util;

public static class GLibLogInterceptor
{
/// <summary>
/// Intercepts GLib/Gtk logging and writes it to the NAPS2 debuglog instead of stdout.
/// </summary>
public static void WriteToDebugLog()
{
g_log_set_writer_func(Write, IntPtr.Zero, IntPtr.Zero);
}

private static void Write(LogLevelFlags flags, IntPtr fields, nint nFields, IntPtr userData)
{
string glibDomain = "?";
string message = "?";
string priority = "?";

var size = Marshal.SizeOf<GLogField>();
for (int i = 0; i < nFields; i++)
{
var field = Marshal.PtrToStructure<GLogField>(fields + i * size);
if (field.key == "PRIORITY")
{
var valueStr = Marshal.PtrToStringUTF8(field.value)!;
if (int.TryParse(valueStr, CultureInfo.InvariantCulture, out var parsedPriority))
{
if (parsedPriority is < 0 or > 4)
{
// Only log warning or worse
return;
}
priority = parsedPriority switch
{
0 => "EMERG",
1 => "ALERT",
2 => "CRITICAL",
3 => "ERROR",
4 => "WARNING",
_ => throw new InvalidOperationException()
};
}
}
if (field.key == "GLIB_DOMAIN")
{
var valueStr = Marshal.PtrToStringUTF8(field.value)!;
glibDomain = valueStr;
}
if (field.key == "MESSAGE")
{
var valueStr = Marshal.PtrToStringUTF8(field.value)!;
message = valueStr;
}
}

Log.Debug($"{glibDomain}-{priority}: {message}");
}

[DllImport("libglib-2.0.so.0")]
private static extern uint g_log_set_writer_func(
GLogWriterFunc log_func,
IntPtr user_data,
IntPtr user_data_free);


[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void GLogWriterFunc(
LogLevelFlags flags,
IntPtr fields,
nint nFields,
IntPtr user_data);

struct GLogField
{
public string key;
public IntPtr value;
public nint length;
}
}
5 changes: 5 additions & 0 deletions NAPS2.Lib/Logging/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static IEventLogger EventLogger
set => _eventLogger = value ?? throw new ArgumentNullException(nameof(value));
}

public static void Debug(string message)
{
_logger.LogDebug(message);
}

public static void Info(string message)
{
_logger.LogInformation(message);
Expand Down

0 comments on commit 7895a6f

Please sign in to comment.