-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogging.cs
More file actions
143 lines (122 loc) · 4.02 KB
/
Copy pathLogging.cs
File metadata and controls
143 lines (122 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine;
using HarmonyLib;
using BepInEx.Configuration;
using BepInEx.Logging;
namespace Logging;
/// <summary>
/// Helper class for properly logging from static contexts.
/// </summary>
internal static class Log
{
/// <summary>
/// Log level to control output to BepInEx log
/// </summary>
internal enum InfoLevel
{
Low = 0,
Medium = 1,
High = 2,
}
#region Verbosity
internal static ConfigEntry<InfoLevel> Verbosity { get; set; }
internal static InfoLevel VerbosityLevel => Verbosity.Value;
internal static bool IsVerbosityLow => Verbosity.Value >= InfoLevel.Low;
internal static bool IsVerbosityMedium => Verbosity.Value >= InfoLevel.Medium;
internal static bool IsVerbosityHigh => Verbosity.Value >= InfoLevel.High;
#endregion Verbosity
private static ManualLogSource logSource;
internal static void Init(ManualLogSource logSource)
{
Log.logSource = logSource;
}
internal static void LogDebug(object data) => logSource.LogDebug(data);
internal static void LogError(object data) => logSource.LogError(data);
internal static void LogFatal(object data) => logSource.LogFatal(data);
internal static void LogMessage(object data) => logSource.LogMessage(data);
internal static void LogWarning(object data) => logSource.LogWarning(data);
internal static void LogInfo(object data, InfoLevel level = InfoLevel.Low)
{
if (Verbosity is null || VerbosityLevel >= level)
{
logSource.LogInfo(data);
}
}
internal static void LogGameObject(GameObject prefab, bool includeChildren = false)
{
LogInfo("***** " + prefab.name + " *****");
foreach (Component compo in prefab.GetComponents<Component>())
{
LogComponent(compo);
}
if (!includeChildren) { return; }
LogInfo("***** " + prefab.name + " (children) *****");
foreach (Transform child in prefab.transform)
{
if (!child) { continue; }
LogInfo($" - {child.name}");
foreach (Component compo in child.GetComponents<Component>())
{
LogComponent(compo);
}
}
}
internal static void LogComponent(Component compo)
{
if (!compo) { return; }
try
{
LogInfo($"--- {compo.GetType().Name}: {compo.name} ---");
}
catch (Exception ex)
{
LogError(ex.ToString());
LogWarning("Could not get type name for component!");
return;
}
try
{
List<PropertyInfo> properties = AccessTools.GetDeclaredProperties(compo.GetType());
foreach (PropertyInfo property in properties)
{
try
{
LogInfo($" - {property.Name} = {property.GetValue(compo)}");
}
catch (Exception ex)
{
LogError(ex.ToString());
LogWarning($"Could not get property: {property.Name} for component!");
}
}
}
catch (Exception ex)
{
LogError(ex.ToString());
LogWarning("Could not get properties for component!");
}
try
{
List<FieldInfo> fields = AccessTools.GetDeclaredFields(compo.GetType());
foreach (FieldInfo field in fields)
{
try
{
LogInfo($" - {field.Name} = {field.GetValue(compo)}");
}
catch (Exception ex)
{
LogError(ex.ToString());
LogWarning($"Could not get field: {field.Name} for component!");
}
}
}
catch (Exception ex)
{
LogError(ex.ToString());
LogWarning("Could not get fields for component!");
}
}
}