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 ;
7
4
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 ;
27
12
28
- [ ImportingConstructor ]
29
- public Log ( [ ImportMany ] IEnumerable < ILogOutput > outputs )
30
- {
31
- this . outputs . AddRange ( outputs ) ;
32
- }
13
+ namespace OngekiFumenEditor . Utils ;
33
14
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 ( ) ;
38
21
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 ) ;
45
23
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
+ }
51
29
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 > ( ) ) ;
57
32
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
+ }
59
37
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
+ }
62
44
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
+ }
64
50
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 ( ) ;
67
56
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 ) ;
71
59
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 ) ;
79
62
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 ) ;
87
64
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 ( ) ;
95
67
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 ( ) ;
102
69
}
70
+ }
103
71
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 } \n Contains 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 } \n Contains exception:{ e . Message } \n { e . StackTrace } ", severity ,
115
+ newLine , time , prefix ) ;
116
+ instance . Output ( severity , msg ) ;
117
+ }
118
+ }
0 commit comments