@@ -38,66 +38,91 @@ import (
38
38
"regexp"
39
39
"strconv"
40
40
"strings"
41
+ "sync"
41
42
)
42
43
43
44
var PLACEHOLDER = regexp .MustCompile ("{(\\ d)}" )
44
45
45
46
type Logger interface {
46
47
Fprintln (w io.Writer , format string , a ... interface {})
48
+ UnformattedFprintln (w io.Writer , s string )
47
49
Println (format string , a ... interface {})
48
50
Name () string
49
51
}
50
52
51
53
type NoopLogger struct {}
52
54
53
- func (s NoopLogger ) Fprintln (w io.Writer , format string , a ... interface {}) {}
55
+ func (l NoopLogger ) Fprintln (w io.Writer , format string , a ... interface {}) {}
54
56
55
- func (s NoopLogger ) Println ( format string , a ... interface {}) {}
57
+ func (l NoopLogger ) UnformattedFprintln ( w io. Writer , format string , a ... interface {}) {}
56
58
57
- func (s NoopLogger ) Name () string {
59
+ func (l NoopLogger ) Println (format string , a ... interface {}) {}
60
+
61
+ func (l NoopLogger ) Name () string {
58
62
return "noop"
59
63
}
60
64
61
65
type HumanLogger struct {}
62
66
63
- func (s HumanLogger ) Fprintln (w io.Writer , format string , a ... interface {}) {
64
- fmt .Fprintln (w , Format (format , a ... ))
67
+ func (l HumanLogger ) Fprintln (w io.Writer , format string , a ... interface {}) {
68
+ fprintln (w , Format (format , a ... ))
69
+ }
70
+
71
+ func (l HumanLogger ) UnformattedFprintln (w io.Writer , s string ) {
72
+ fprintln (w , s )
65
73
}
66
74
67
- func (s HumanLogger ) Println (format string , a ... interface {}) {
68
- s . Fprintln (os .Stdout , Format (format , a ... ))
75
+ func (l HumanLogger ) Println (format string , a ... interface {}) {
76
+ fprintln (os .Stdout , Format (format , a ... ))
69
77
}
70
78
71
- func (s HumanLogger ) Name () string {
79
+ func (l HumanLogger ) Name () string {
72
80
return "human"
73
81
}
74
82
75
83
type MachineLogger struct {}
76
84
77
- func (s MachineLogger ) printWithoutFormatting (w io.Writer , format string , a []interface {}) {
85
+ func (l MachineLogger ) printMachineFormattedLogLine (w io.Writer , format string , a []interface {}) {
78
86
a = append ([]interface {}(nil ), a ... )
79
87
for idx , value := range a {
80
88
typeof := reflect .Indirect (reflect .ValueOf (value )).Kind ()
81
89
if typeof == reflect .String {
82
90
a [idx ] = url .QueryEscape (value .(string ))
83
91
}
84
92
}
85
- fmt .Fprintf (w , "===%s ||| %s" , format , a )
86
- fmt .Fprintln (w )
93
+ fprintf (w , "===%s ||| %s\n " , format , a )
94
+ }
95
+
96
+ func (l MachineLogger ) Fprintln (w io.Writer , format string , a ... interface {}) {
97
+ l .printMachineFormattedLogLine (w , format , a )
87
98
}
88
99
89
- func (s MachineLogger ) Fprintln ( w io. Writer , format string , a ... interface {}) {
90
- s . printWithoutFormatting ( w , format , a )
100
+ func (l MachineLogger ) Println ( format string , a ... interface {}) {
101
+ l . printMachineFormattedLogLine ( os . Stdout , format , a )
91
102
}
92
103
93
- func (s MachineLogger ) Println ( format string , a ... interface {} ) {
94
- s . printWithoutFormatting ( os . Stdout , format , a )
104
+ func (l MachineLogger ) UnformattedFprintln ( w io. Writer , s string ) {
105
+ fprintln ( w , s )
95
106
}
96
107
97
- func (s MachineLogger ) Name () string {
108
+ func (l MachineLogger ) Name () string {
98
109
return "machine"
99
110
}
100
111
112
+ var lock sync.Mutex
113
+
114
+ func fprintln (w io.Writer , s string ) {
115
+ lock .Lock ()
116
+ defer lock .Unlock ()
117
+ fmt .Fprintln (w , s )
118
+ }
119
+
120
+ func fprintf (w io.Writer , format string , a ... interface {}) {
121
+ lock .Lock ()
122
+ defer lock .Unlock ()
123
+ fmt .Fprintf (w , format , a )
124
+ }
125
+
101
126
func FromJavaToGoSyntax (s string ) string {
102
127
submatches := PLACEHOLDER .FindAllStringSubmatch (s , - 1 )
103
128
for _ , submatch := range submatches {
0 commit comments