-
Notifications
You must be signed in to change notification settings - Fork 1
/
FluentLogger.cs
132 lines (119 loc) · 3.81 KB
/
FluentLogger.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
using System;
using System.Threading;
using UnityEngine;
using Wully.Extensions;
using static Wully.Helpers.BetterLogger;
namespace Wully.Helpers {
/// <summary>
/// A fluent class for wrapping callerMemberName and lineNumbers, giving more detailed information in logs
/// </summary>
public class FluentLogger {
private string _callerMemberName;
private int _callerLineNumber;
private string _callerFilePath;
private LogLevel logLevel;
private BetterLogger log;
private string className;
public FluentLogger( BetterLogger betterLogger, LogLevel level, string callerMemberName, string callerFilePath, int callerLineNumber ) {
log = betterLogger;
className = log.ClassName();
logLevel = level;
_callerMemberName = callerMemberName;
_callerFilePath = callerFilePath;
_callerLineNumber = callerLineNumber;
}
/// <summary>
/// Log message text
/// </summary>
/// <param name="message"></param>
/// <param name="messageArgs"></param>
public void Message( string message, params object[] messageArgs ) {
try {
//If the global instance is available and enabled, use its log level for ALL loggers.
if ( BetterLogger.local == null || !BetterLogger.local.IsEnabled() ) {
// dont print if logging disabled
if ( !log.IsEnabled() ) {
return;
}
// dont print if current log level is below this messages log level
if ( log.GetLogLevel() < logLevel ) {
return;
}
}
switch ( logLevel ) {
case LogLevel.Exception:
UnityEngine.Debug.LogError(Format(message, messageArgs));
break;
case LogLevel.Error:
UnityEngine.Debug.LogError(Format(message, messageArgs));
break;
case LogLevel.Warn:
UnityEngine.Debug.LogWarning(Format(message, messageArgs));
break;
default: //Info/Debug/Default all use Log()
UnityEngine.Debug.Log(Format(message, messageArgs));
break;
}
} catch ( Exception e ) {
log.Exception().Message($"Exception Caught: {e.StackTrace}");
}
}
private string Format( string format, params object[] args ) {
try {
DateTime dateTime = DateTime.Now;
string dt = String.Format("{0:u}", dateTime);
string callingMethod;
if (log.ShouldLogFullNamespace()) {
callingMethod = String.Join(".", className, _callerMemberName, _callerLineNumber).Color(Color.cyan);
} else {
callingMethod = String.Join(".", _callerMemberName, _callerLineNumber).Color(Color.cyan);
}
int thread = Thread.CurrentThread.ManagedThreadId;
return
$"{dt}\t{Time.time}\t{LogLevelColor(logLevel).Bold()}\t{thread}\t{callingMethod}\t: {string.Format(format, args)}";
} catch ( Exception e ) {
log.Exception().Message($"Exception Caught: {e.GetType()} {e.StackTrace}");
return "Error, could not log";
}
}
private string LogLevelColor( LogLevel level ) {
switch ( level ) {
case LogLevel.Error:
return logLevel.ToString().Color(Color.red).Bold();
case LogLevel.Exception:
return logLevel.ToString().Color(Color.red);
case LogLevel.Warn:
return logLevel.ToString().Color(Color.yellow);
default:
return logLevel.ToString().Color(Color.green);
}
}
/// <summary>
/// Convert colour to html string
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static string ConvertToHtml( Color color, string text ) {
return $"<color=#{ColorUtility.ToHtmlStringRGB(color)}>{text}</color>";
}
public static string ConvertToHtml( Style style, int value, string text ) {
switch ( style ) {
case Style.bold:
return $"<b>{text}</b>";
case Style.italic:
return $"<i>{text}</i>";
case Style.size:
return $"<size={value}>{text}</size>";
case Style.normal:
return text;
}
return text;
}
public enum Style {
normal,
italic,
bold,
size
}
}
}