-
-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gtk: Write GTK logs to debuglog instead of stdout
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters