forked from Solarint/SAIN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
75 lines (63 loc) · 2.45 KB
/
Logger.cs
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
using BepInEx.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace SAIN
{
internal static class Logger
{
public static void LogInfo(object data) => Log(LogLevel.Info, data);
public static void LogDebug(object data) => Log(LogLevel.Debug, data);
public static void LogWarning(object data) => Log(LogLevel.Warning, data);
public static void LogError(object data) => Log(LogLevel.Error, data);
private static void Log(LogLevel level, object data)
{
string methods = string.Empty;
Type declaringType = null;
if (level != LogLevel.Debug)
{
int max = GetMaxFrames(level);
int count = 0;
StackTrace stackTrace = new StackTrace();
for (int i = 0; i < stackTrace.FrameCount; i++)
{
var method = stackTrace.GetFrame(i).GetMethod();
Type methodType = method.DeclaringType;
if (methodType == typeof(Logger)) continue;
declaringType = declaringType ?? methodType;
methods += "." + method.Name;
if (count >= max) break;
count++;
}
methods = $"[{methods}]:";
}
LastLogData = data;
string result = $"{methods} [{data}]";
SelectLogSource(declaringType).Log(level, result);
}
private static int GetMaxFrames(LogLevel level)
{
switch (level)
{
case LogLevel.Debug: return 0;
case LogLevel.Info: return 1;
case LogLevel.Warning: return 2;
case LogLevel.Error: return 3;
case LogLevel.Fatal: return 4;
default: return 0;
}
}
public static object LastLogData { get; private set; }
private static ManualLogSource SelectLogSource(Type type = null)
{
string name = type?.Name ?? "SAIN";
if (!LogSourcesDictionary.ContainsKey(name))
{
LogSourcesDictionary.Add(name, BepInEx.Logging.Logger.CreateLogSource(name));
}
return LogSourcesDictionary[name];
}
public static Dictionary<string, ManualLogSource> LogSourcesDictionary = new Dictionary<string, ManualLogSource>();
}
}