Skip to content

Commit

Permalink
New logging tool
Browse files Browse the repository at this point in the history
- Can now Serialize any object to debugging string
  • Loading branch information
Erag0n001 committed Oct 17, 2024
1 parent fdac2f4 commit b68c926
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
114 changes: 114 additions & 0 deletions Source/Shared/Misc/StringUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System.Reflection;
using System;
using System.Collections;

namespace Shared
{
public static class StringUtilities
{
public static string ToString(object obj, int tabbing = 0)
{
string str = "";
if (tabbing == 0)
{
str += Indent(tabbing) + $"{obj.GetType().Name}:\n";
tabbing++;
}
foreach (FieldInfo field in obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
object value = field.GetValue(obj);
str += Indent(tabbing);

if (value != null)
{
if (field.FieldType.IsPrimitive || field.FieldType.IsValueType || field.FieldType == typeof(string))
{
str += $"{field.Name}: {value}\n"; // Is Primitive
}
else if (value is Array) // Is Array
{
str += $"{field.Name}: {HandleArray((Array)value, tabbing + 1)}";
}
else if (typeof(IEnumerable).IsAssignableFrom(field.FieldType) && field.FieldType != typeof(string)) // Is List
{
str += $"{field.Name}: {HandleIEnumerable((IEnumerable)value, tabbing + 1)}";
}
else //Is Object
{
str += $"{field.Name}: \n{ToString(value, tabbing + 1)}";
}
}
else
{
str += $"{field.Name}: was null\n"; // Is Null
}
}
return str;
}

private static string HandleArray(Array array, int tabbing)
{
string str = "";
if(array.Length == 0)
{
return str += Indent(tabbing) + "Empty";
}
str += "\n";
foreach (var item in array)
{
str += "" + ProcessItem(item, tabbing);
}

return str;
}

private static string HandleIEnumerable(IEnumerable enumerable, int tabbing)
{
string str = "";
if (!enumerable.GetEnumerator().MoveNext())
{
return str += "Empty\n";
}
foreach (var item in enumerable)
{
str += ProcessItem(item, tabbing);
}

return str;
}

private static string ProcessItem(object item, int tabbing)
{
string str = "";
str += Indent(tabbing);
if (item != null)
{
if (item.GetType().IsPrimitive || item.GetType().IsValueType || item.GetType() == typeof(string)) // Is primitive
{
str += $"{item.GetType().Name}: {item}\n";
}
else if (item.GetType() == typeof(IEnumerable)) //Is List
{
str += $"{item.GetType().Name}: \n{HandleIEnumerable((IEnumerable)item, tabbing + 1)}";
}
else if (item.GetType() == typeof(Array)) //Is Array
{
str += $"{item.GetType().Name}: \n{HandleArray((Array)item, tabbing + 1)}";
}
else //Is Object
{
str += $"{item.GetType().Name}: \n{ToString(item, tabbing + 1)}";
}
}
else
{
str += $"{item.GetType().Name}: was null\n"; // Is Null
}
return str;
}
private static string Indent(int level)
{
return new string('\t', level);
}
}
}
1 change: 1 addition & 0 deletions Source/Shared/Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Misc\GZip.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Misc\Hasher.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Misc\Serializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)misc\StringUtilities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PacketData\AidData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PacketData\EventData.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PacketData\OfflineActivityData.cs" />
Expand Down

0 comments on commit b68c926

Please sign in to comment.