Skip to content

Commit 85d090e

Browse files
committed
LogDebug() only for DEBUG build configuration
1 parent 7fad74a commit 85d090e

File tree

1 file changed

+101
-95
lines changed

1 file changed

+101
-95
lines changed

OngekiFumenEditor/Utils/Log.cs

Lines changed: 101 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,118 @@
1-
using Caliburn.Micro;
2-
using OngekiFumenEditor.Utils.Logs;
3-
using OngekiFumenEditor.Utils.Logs.DefaultImpls;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.ComponentModel.Composition;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
74
using System.Diagnostics;
8-
using System.Linq;
9-
using System.Runtime.CompilerServices;
10-
using System.Text;
11-
using System.Threading;
12-
using static OngekiFumenEditor.Utils.Logs.ILogOutput;
13-
14-
namespace OngekiFumenEditor.Utils
15-
{
16-
[Export(typeof(Log))]
17-
[PartCreationPolicy(CreationPolicy.Shared)]
18-
public class Log
19-
{
20-
private List<ILogOutput> outputs = new List<ILogOutput>();
21-
private IEnumerable<ILogOutput> LogOutputs => outputs;
22-
23-
private StringBuilder sb = new StringBuilder(2048);
24-
25-
private static Log cacheInstance;
26-
public static Log Instance => cacheInstance ?? (cacheInstance = IoC.Get<Log>());
5+
using System.Linq;
6+
using System.Runtime.CompilerServices;
7+
using System.Text;
8+
using System.Threading;
9+
using Caliburn.Micro;
10+
using OngekiFumenEditor.Utils.Logs;
11+
using static OngekiFumenEditor.Utils.Logs.ILogOutput;
2712

28-
[ImportingConstructor]
29-
public Log([ImportMany] IEnumerable<ILogOutput> outputs)
30-
{
31-
this.outputs.AddRange(outputs);
32-
}
13+
namespace OngekiFumenEditor.Utils;
3314

34-
public void RemoveOutputIfNotExist<T>() where T : ILogOutput
35-
{
36-
outputs.RemoveAll(x => x is T);
37-
}
15+
[Export(typeof(Log))]
16+
[PartCreationPolicy(CreationPolicy.Shared)]
17+
public class Log
18+
{
19+
private static Log cacheInstance;
20+
private readonly List<ILogOutput> outputs = new();
3821

39-
public void AddOutputIfNotExist<T>() where T : ILogOutput, new()
40-
{
41-
if (outputs.OfType<T>().Any())
42-
return;
43-
outputs.Add(new T());
44-
}
22+
private readonly StringBuilder sb = new(2048);
4523

46-
internal void Output(Severity severity, string message)
47-
{
48-
foreach (var output in LogOutputs)
49-
output.WriteLog(severity, message);
50-
}
24+
[ImportingConstructor]
25+
public Log([ImportMany] IEnumerable<ILogOutput> outputs)
26+
{
27+
this.outputs.AddRange(outputs);
28+
}
5129

52-
private string BuildLogMessage(string message, Severity severity, bool new_line, bool time, string prefix)
53-
{
54-
lock (sb)
55-
{
56-
sb.Clear();
30+
private IEnumerable<ILogOutput> LogOutputs => outputs;
31+
public static Log Instance => cacheInstance ?? (cacheInstance = IoC.Get<Log>());
5732

58-
sb.AppendFormat("[{0} {1}:{2}]", time ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") : string.Empty, severity.ToString().ToUpper(), Thread.CurrentThread.ManagedThreadId);
33+
public void RemoveOutputIfNotExist<T>() where T : ILogOutput
34+
{
35+
outputs.RemoveAll(x => x is T);
36+
}
5937

60-
if (!string.IsNullOrWhiteSpace(prefix))
61-
sb.AppendFormat("{0}", prefix);
38+
public void AddOutputIfNotExist<T>() where T : ILogOutput, new()
39+
{
40+
if (outputs.OfType<T>().Any())
41+
return;
42+
outputs.Add(new T());
43+
}
6244

63-
sb.AppendFormat(":{0}", message);
45+
internal void Output(Severity severity, string message)
46+
{
47+
foreach (var output in LogOutputs)
48+
output.WriteLog(severity, message);
49+
}
6450

65-
if (new_line)
66-
sb.AppendLine();
51+
private string BuildLogMessage(string message, Severity severity, bool new_line, bool time, string prefix)
52+
{
53+
lock (sb)
54+
{
55+
sb.Clear();
6756

68-
return sb.ToString();
69-
}
70-
}
57+
sb.AppendFormat("[{0} {1}:{2}]", time ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") : string.Empty,
58+
severity.ToString().ToUpper(), Thread.CurrentThread.ManagedThreadId);
7159

72-
public static void LogDebug(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
73-
{
74-
var instance = Instance;
75-
var severity = Severity.Debug;
76-
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
77-
instance.Output(severity, msg);
78-
}
60+
if (!string.IsNullOrWhiteSpace(prefix))
61+
sb.AppendFormat("{0}", prefix);
7962

80-
public static void LogInfo(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
81-
{
82-
var instance = Instance;
83-
var severity = Severity.Info;
84-
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
85-
instance.Output(severity, msg);
86-
}
63+
sb.AppendFormat(":{0}", message);
8764

88-
public static void LogWarn(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
89-
{
90-
var instance = Instance;
91-
var severity = Severity.Warn;
92-
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
93-
instance.Output(severity, msg);
94-
}
65+
if (new_line)
66+
sb.AppendLine();
9567

96-
public static void LogError(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
97-
{
98-
var instance = Instance;
99-
var severity = Severity.Error;
100-
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
101-
instance.Output(severity, msg);
68+
return sb.ToString();
10269
}
70+
}
10371

104-
public static void LogError(string message, Exception e, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
105-
{
106-
var instance = Instance;
107-
var severity = Severity.Error;
108-
var msg = instance.BuildLogMessage($"{message}\nContains exception:{e.Message}\n{e.StackTrace}", severity, newLine, time, prefix);
109-
instance.Output(severity, msg);
110-
}
111-
}
112-
}
72+
[Conditional("DEBUG")]
73+
public static void LogDebug(string message, bool newLine = true, bool time = true,
74+
[CallerMemberName] string prefix = "<Unknown>")
75+
{
76+
var instance = Instance;
77+
var severity = Severity.Debug;
78+
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
79+
instance.Output(severity, msg);
80+
}
81+
82+
public static void LogInfo(string message, bool newLine = true, bool time = true,
83+
[CallerMemberName] string prefix = "<Unknown>")
84+
{
85+
var instance = Instance;
86+
var severity = Severity.Info;
87+
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
88+
instance.Output(severity, msg);
89+
}
90+
91+
public static void LogWarn(string message, bool newLine = true, bool time = true,
92+
[CallerMemberName] string prefix = "<Unknown>")
93+
{
94+
var instance = Instance;
95+
var severity = Severity.Warn;
96+
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
97+
instance.Output(severity, msg);
98+
}
99+
100+
public static void LogError(string message, bool newLine = true, bool time = true,
101+
[CallerMemberName] string prefix = "<Unknown>")
102+
{
103+
var instance = Instance;
104+
var severity = Severity.Error;
105+
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
106+
instance.Output(severity, msg);
107+
}
108+
109+
public static void LogError(string message, Exception e, bool newLine = true, bool time = true,
110+
[CallerMemberName] string prefix = "<Unknown>")
111+
{
112+
var instance = Instance;
113+
var severity = Severity.Error;
114+
var msg = instance.BuildLogMessage($"{message}\nContains exception:{e.Message}\n{e.StackTrace}", severity,
115+
newLine, time, prefix);
116+
instance.Output(severity, msg);
117+
}
118+
}

0 commit comments

Comments
 (0)