From e2f0807ab197072238e34cc99c976e8b4f1e1c95 Mon Sep 17 00:00:00 2001 From: Kyle873 Date: Sat, 21 Dec 2024 23:45:36 -0500 Subject: [PATCH] reflection-based field logger for req/res msgs --- EpinelPS/Utils/LogUtils.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 EpinelPS/Utils/LogUtils.cs diff --git a/EpinelPS/Utils/LogUtils.cs b/EpinelPS/Utils/LogUtils.cs new file mode 100644 index 0000000..9766383 --- /dev/null +++ b/EpinelPS/Utils/LogUtils.cs @@ -0,0 +1,30 @@ +using Google.Protobuf; +using System.Reflection; + +namespace EpinelPS.Utils +{ + public static class LogUtils + { + public static void LogMessageFields(IMessage message) + { + Type type = message.GetType(); + FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); + + Console.ForegroundColor = ConsoleColor.Cyan; + + Console.WriteLine(type.Name + ":"); + + foreach (FieldInfo field in fields) + { + if (field.Name.ToLower().Contains("unknown")) + continue; + + object? value = field.GetValue(message); + + Console.WriteLine($"\t{field.Name}: {value}"); + } + + Console.ResetColor(); + } + } +}