-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLogger.cs
160 lines (141 loc) · 6.16 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using BepInEx.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using EFT.Communications;
using UnityEngine;
using static Mono.Security.X509.X520;
using SAIN.Helpers;
using EFT.UI;
using Diz.LanguageExtensions;
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);
public static void NotifyInfo(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
=> NotifyMessage(data, duration, ENotificationIconType.Note);
public static void NotifyDebug(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
=> NotifyMessage(data, duration, ENotificationIconType.Note, Color.gray);
public static void NotifyWarning(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
=> NotifyMessage(data, duration, ENotificationIconType.Alert, Color.yellow);
public static void NotifyError(object data, ENotificationDurationType duration = ENotificationDurationType.Long)
=> NotifyMessage(data, duration, ENotificationIconType.Alert, Color.red, true);
public static void LogAndNotifyInfo(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
{
Log(LogLevel.Info, data);
NotifyMessage(data, duration, ENotificationIconType.Note);
}
public static void LogAndNotifyDebug(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
{
Log(LogLevel.Debug, data);
NotifyMessage(data, duration, ENotificationIconType.Note, Color.gray);
}
public static void LogAndNotifyWarning(object data, ENotificationDurationType duration = ENotificationDurationType.Default)
{
Log(LogLevel.Warning, data);
NotifyMessage(data, duration, ENotificationIconType.Alert, Color.yellow);
}
public static void LogAndNotifyError(object data, ENotificationDurationType duration = ENotificationDurationType.Long)
{
Log(LogLevel.Error, data);
string message = CreateErrorMessage(data);
NotificationManagerClass.DisplayMessageNotification(message, duration, ENotificationIconType.Alert, Color.red);
}
public static void NotifyMessage(object data,
ENotificationDurationType durationType = ENotificationDurationType.Default,
ENotificationIconType iconType = ENotificationIconType.Default,
UnityEngine.Color? textColor = null, bool Error = false)
{
if (_nextNotification < Time.time && SAINPlugin.DebugMode)
{
_nextNotification = Time.time + 0.1f;
string message = Error ? CreateErrorMessage(data) : data.ToString();
NotificationManagerClass.DisplayMessageNotification(message, durationType, iconType, textColor);
}
}
private static string CreateErrorMessage(object data)
{
StackTrace stackTrace = new StackTrace();
int max = Mathf.Clamp(stackTrace.FrameCount, 0, 10);
for (int i = 0; i < max; i++)
{
MethodBase method = stackTrace.GetFrame(i)?.GetMethod();
Type type = method?.DeclaringType;
if (type != null && type.DeclaringType != typeof(Logger))
{
string errorString = $"[{type} : {method}]: ERROR: {data}";
return errorString;
}
}
return data.ToString();
}
private static void Log(LogLevel level, object data)
{
string methodsString = string.Empty;
Type declaringType = null;
if (level != LogLevel.Debug)
{
int max = GetMaxFrames(level);
StackTrace stackTrace = new StackTrace(2);
max = Mathf.Clamp(max, 0, stackTrace.FrameCount);
for (int i = 0; i < max; i++)
{
var method = stackTrace.GetFrame(i).GetMethod();
if (method.DeclaringType == typeof(Logger)) continue;
if (declaringType == null)
{
declaringType = method.DeclaringType;
}
if (!methodsString.IsNullOrEmpty())
{
methodsString = "." + methodsString;
}
methodsString = $"{method.Name}()" + methodsString;
}
methodsString = $"[{methodsString}]:";
}
string result = $"[{declaringType}] : [{methodsString}] : [{data}]";
if (SAINLogger == null)
{
SAINLogger = BepInEx.Logging.Logger.CreateLogSource("SAIN");
}
if (level == LogLevel.Error || level == LogLevel.Fatal)
{
//NotifyError(data);
if (MonoBehaviourSingleton<PreloaderUI>.Instance?.Console != null)
{
//ConsoleScreen.LogError(data.ToString());
}
}
SAINLogger.Log(level, result);
}
private static float _nextNotification;
private static int GetMaxFrames(LogLevel level)
{
switch (level)
{
case LogLevel.Debug:
case LogLevel.Info:
return 1;
case LogLevel.Warning:
return 2;
case LogLevel.Error:
return 3;
case LogLevel.Fatal:
return 4;
default:
return 1;
}
}
private static ManualLogSource SAINLogger;
}
}