Skip to content

Commit e12cb64

Browse files
committed
Clean up the logger APIs to avoid static data
1 parent 86ff99f commit e12cb64

File tree

3 files changed

+28
-129
lines changed

3 files changed

+28
-129
lines changed

src/com.unity.editor.tasks/Editor/Base/TaskBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ protected virtual void RaiseOnStart()
769769
/// </summary>
770770
protected void RaiseOnStartInternal()
771771
{
772-
Logger.Trace($"OnStart: {Name} [{(TaskManager.InUIThread ? "UI Thread" : Affinity.ToString())}]");
772+
Logger.Debug($"OnStart: {Name} [{(TaskManager.InUIThread ? "UI Thread" : Affinity.ToString())}]");
773773
OnStart?.Invoke(this);
774774
}
775775

src/com.unity.editor.tasks/Editor/Logging/LogFacade.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ public class LogFacade : ILogging
1313
{
1414
private readonly LogAdapterBase logger;
1515
private bool? traceEnabled;
16+
private bool? verbose;
1617

1718
public bool TracingEnabled
1819
{
19-
get => traceEnabled.HasValue ? traceEnabled.Value : LogHelper.TracingEnabled;
20+
get => traceEnabled ?? LogHelper.TracingEnabled;
2021
set
2122
{
2223
if (traceEnabled.HasValue)
@@ -26,7 +27,17 @@ public bool TracingEnabled
2627
}
2728
}
2829

29-
public bool Verbose { get => LogHelper.Verbose; set => LogHelper.Verbose = value; }
30+
public bool Verbose
31+
{
32+
get => verbose ?? LogHelper.Verbose;
33+
set
34+
{
35+
if (verbose.HasValue)
36+
verbose = value;
37+
else
38+
LogHelper.Verbose = value;
39+
}
40+
}
3041

3142
private readonly string context;
3243

@@ -36,11 +47,12 @@ public LogFacade(string context)
3647
logger = LogHelper.LogAdapter;
3748
}
3849

39-
public LogFacade(string context, LogAdapterBase logger, bool traceEnabled = false)
50+
public LogFacade(string context, LogAdapterBase logger, bool traceEnabled = false, bool verbose = false)
4051
{
4152
this.context = context;
4253
this.logger = logger;
4354
this.traceEnabled = traceEnabled;
55+
this.verbose = verbose;
4456
}
4557

4658
public void Info(string message)
@@ -50,9 +62,8 @@ public void Info(string message)
5062

5163
public void Debug(string message)
5264
{
53-
#if DEVELOPER_BUILD
65+
if (!Verbose) return;
5466
logger.Debug(context, message);
55-
#endif
5667
}
5768

5869
public void Trace(string message)
@@ -83,57 +94,49 @@ public void Info(Exception ex, string format, params object[] objects)
8394

8495
public void Debug(string format, params object[] objects)
8596
{
86-
#if DEVELOPER_BUILD
97+
if (!Verbose) return;
8798
Debug(string.Format(format, objects));
88-
#endif
8999
}
90100

91101
public void Debug(Exception ex, string message)
92102
{
93-
#if DEVELOPER_BUILD
94-
Debug(string.Concat(message, Environment.NewLine, Verbose ? ex.GetExceptionMessage() : ex.GetExceptionMessageShort()));
95-
#endif
103+
if (!Verbose) return;
104+
Debug(string.Concat(message, Environment.NewLine, ex.GetExceptionMessage()));
96105
}
97106

98107
public void Debug(Exception ex)
99108
{
100-
#if DEVELOPER_BUILD
109+
if (!Verbose) return;
101110
Debug(ex, string.Empty);
102-
#endif
103111
}
104112

105113
public void Debug(Exception ex, string format, params object[] objects)
106114
{
107-
#if DEVELOPER_BUILD
115+
if (!Verbose) return;
108116
Debug(ex, String.Format(format, objects));
109-
#endif
110117
}
111118

112119
public void Trace(string format, params object[] objects)
113120
{
114121
if (!TracingEnabled) return;
115-
116122
Trace(string.Format(format, objects));
117123
}
118124

119125
public void Trace(Exception ex, string message)
120126
{
121127
if (!TracingEnabled) return;
122-
123128
Trace(string.Concat(message, Environment.NewLine, Verbose ? ex.GetExceptionMessage() : ex.GetExceptionMessageShort()));
124129
}
125130

126131
public void Trace(Exception ex)
127132
{
128133
if (!TracingEnabled) return;
129-
130134
Trace(ex, string.Empty);
131135
}
132136

133137
public void Trace(Exception ex, string format, params object[] objects)
134138
{
135139
if (!TracingEnabled) return;
136-
137140
Trace(ex, string.Format(format, objects));
138141
}
139142

src/com.unity.editor.tasks/Editor/Logging/LogHelper.cs

Lines changed: 6 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -18,114 +18,22 @@ public static class LogHelper
1818
private static ILogging instance;
1919

2020
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
21-
public static ILogging GetLogger<T>()
22-
{
23-
return GetLogger(typeof(T));
24-
}
21+
public static ILogging GetLogger<T>() => GetLogger(typeof(T));
2522

2623
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
27-
public static ILogging GetLogger(Type type)
28-
{
29-
return GetLogger(type.Name);
30-
}
31-
32-
public static ILogging GetLogger(string context = null)
33-
{
34-
return new LogFacade($"<{context ?? "Global"}>");
35-
}
36-
37-
public static void Info(string s)
38-
{
39-
Instance.Info(s);
40-
}
41-
42-
public static void Debug(string s)
43-
{
44-
Instance.Debug(s);
45-
}
46-
47-
public static void Trace(string s)
48-
{
49-
Instance.Trace(s);
50-
}
51-
52-
public static void Warning(string s)
53-
{
54-
Instance.Warning(s);
55-
}
56-
57-
public static void Error(string s)
58-
{
59-
Instance.Error(s);
60-
}
61-
62-
public static void Error(Exception exception)
63-
{
64-
Instance.Error(exception);
65-
}
66-
67-
public static void Info(string format, params object[] objects)
68-
{
69-
Instance.Info(format, objects);
70-
}
71-
72-
public static void Debug(string format, params object[] objects)
73-
{
74-
Instance.Debug(format, objects);
75-
}
24+
public static ILogging GetLogger(Type type) => GetLogger(type.Name);
7625

77-
public static void Trace(string format, params object[] objects)
78-
{
79-
Instance.Trace(format, objects);
80-
}
81-
82-
public static void Warning(string format, params object[] objects)
83-
{
84-
Instance.Warning(format, objects);
85-
}
86-
87-
public static void Error(string format, params object[] objects)
88-
{
89-
Instance.Error(format, objects);
90-
}
91-
92-
public static void Info(Exception ex, string s)
93-
{
94-
Instance.Info(ex, s);
95-
}
96-
97-
public static void Debug(Exception ex, string s)
98-
{
99-
Instance.Debug(ex, s);
100-
}
101-
102-
public static void Trace(Exception ex, string s)
103-
{
104-
Instance.Trace(ex, s);
105-
}
106-
107-
public static void Warning(Exception ex, string s)
108-
{
109-
Instance.Warning(ex, s);
110-
}
111-
112-
public static void Error(Exception ex, string s)
113-
{
114-
Instance.Error(ex, s);
115-
}
26+
public static ILogging GetLogger(string context) => new LogFacade($"<{context ?? "Global"}>");
11627

11728
public static bool TracingEnabled
11829
{
119-
get
120-
{
121-
return tracingEnabled;
122-
}
30+
get => tracingEnabled;
12331
set
12432
{
12533
if (tracingEnabled != value)
12634
{
12735
tracingEnabled = value;
128-
Instance.Info("Trace Logging " + (value ? "Enabled" : "Disabled"));
36+
LogAdapter.Info("Global", "Trace Logging " + (value ? "Enabled" : "Disabled"));
12937
}
13038
}
13139
}
@@ -134,20 +42,8 @@ public static bool TracingEnabled
13442

13543
public static LogAdapterBase LogAdapter
13644
{
137-
get { return logAdapter; }
45+
get => logAdapter;
13846
set { logAdapter = value ?? nullLogAdapter; }
13947
}
140-
141-
public static ILogging Instance
142-
{
143-
get {
144-
if (instance == null)
145-
{
146-
instance = GetLogger();
147-
}
148-
return instance;
149-
}
150-
set { instance = value; }
151-
}
15248
}
15349
}

0 commit comments

Comments
 (0)