forked from Caraxi/SimpleTweaksPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleLog.cs
115 lines (91 loc) · 6.35 KB
/
SimpleLog.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Dalamud.IoC;
using Dalamud.Plugin.Services;
namespace SimpleTweaksPlugin;
public sealed class SimpleLog {
[PluginService] private static IPluginLog PluginLog { get; set; } = null;
#if DEBUG
private static int _subStrIndex;
public static void SetupBuildPath([CallerFilePath] string callerPath = "") {
var p = Path.GetDirectoryName(callerPath);
if (p != null) _subStrIndex = p.Length + 1;
}
private static string CleanCallerPath(string callerPath = "", string callerName = "", int lineNumber = -1) {
if (SimpleTweaksPlugin.Plugin.PluginConfig.NoCallerInLog) return string.Empty;
if (string.IsNullOrEmpty(callerPath) && string.IsNullOrEmpty(callerName) && lineNumber == -1) return string.Empty;
var sb = new StringBuilder();
sb.Append('[');
if (!string.IsNullOrWhiteSpace(callerPath) && callerPath.Length >= _subStrIndex) {
sb.Append(callerPath.Substring(_subStrIndex));
sb.Append("::");
}
sb.Append($"{callerName}:{lineNumber}");
sb.Append("] ");
return sb.ToString();
}
public static void Verbose(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Verbose($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Debug(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Debug($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Information(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Information($"{CleanCallerPath(callerPath, callerName, lineNumber)}{message}");
}
public static void Fatal(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Fatal($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Log(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Information($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Warning(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Warning($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Error(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Error($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
public static void Error(Exception ex, string message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage($"{message}\n{ex}")) PluginLog.Error($"{CleanCallerPath(callerPath, callerName, lineNumber)}{m}");
}
#else
public static void Verbose(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach(var m in SplitMessage(message)) PluginLog.Verbose($"{m}");
}
public static void Debug(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Debug($"{m}");
}
public static void Information(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Information($"{m}");
}
public static void Fatal(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Fatal($"{m}");
}
public static void Log(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Information($"{m}");
}
public static void Warning(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Warning($"{m}");
}
public static void Error(object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage(message)) PluginLog.Error($"{m}");
}
public static void Error(Exception ex, object message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage($"{message}\n{ex}")) PluginLog.Error($"{m}");
}
public static void Error(Exception ex, string message, [CallerFilePath] string callerPath = "", [CallerMemberName] string callerName = "", [CallerLineNumber] int lineNumber = -1) {
foreach (var m in SplitMessage($"{message}\n{ex}")) PluginLog.Error($"{m}");
}
#endif
private static IEnumerable<string> SplitMessage(object message) {
if (message is IList list) {
return list.Cast<object>().Select((t, i) => $"{i}: {t}");
}
return $"{message}".Split('\n');
}
}