File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package unixgram
2
2
3
3
import (
4
4
"bytes"
5
+ "encoding/hex"
5
6
"logger"
6
7
"net"
7
8
"os"
@@ -29,7 +30,28 @@ func NewUnixgramSource(sockPath string) *UnixgramSource {
29
30
return & unixsrc
30
31
}
31
32
32
- func (src UnixgramSource ) Run () error {
33
+ func (src * UnixgramSource ) decodeNginxLog (log []byte ) int {
34
+ var (
35
+ writeIndex , readIndex int // next index to write to
36
+ )
37
+ for readIndex = 0 ; readIndex < len (log ); readIndex ++ {
38
+ var writeChar [1 ]byte // next byte value to write
39
+
40
+ writeChar [0 ] = log [readIndex ]
41
+
42
+ if len (log )- 4 > readIndex &&
43
+ log [readIndex ] == '\\' && log [readIndex + 1 ] == 'x' && (log [readIndex + 2 ] == '2' || log [readIndex + 2 ] == '5' ) {
44
+ hex .Decode (writeChar [:], log [readIndex + 2 :readIndex + 4 ])
45
+ readIndex += 3
46
+ }
47
+
48
+ log [writeIndex ] = writeChar [0 ]
49
+ writeIndex += 1
50
+ }
51
+ return writeIndex
52
+ }
53
+
54
+ func (src * UnixgramSource ) Run () error {
33
55
//the socket has to run from the same goroutine because it is not thread safe
34
56
//memory barrier executed when goroutines moved between threads
35
57
//reference: https://groups.google.com/forum/#!topic/golang-nuts/eABYrBA5LEk
@@ -66,8 +88,9 @@ func (src UnixgramSource) Run() error {
66
88
// a separate message
67
89
for _ , msg := range bytes .Split (buf [:nr ], lf ) {
68
90
if len (msg ) > 0 {
91
+ wi := src .decodeNginxLog (msg )
69
92
sent ++
70
- src .Out () <- msg
93
+ src .Out () <- msg [: wi ]
71
94
}
72
95
}
73
96
You can’t perform that action at this time.
0 commit comments