Skip to content

Commit 8de8353

Browse files
author
Ian Pye
committed
Thanks to ray, decoding needed hex coded before passing values on
1 parent d439289 commit 8de8353

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

stream/source/unixgram/unixgram.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package unixgram
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"logger"
67
"net"
78
"os"
@@ -29,7 +30,28 @@ func NewUnixgramSource(sockPath string) *UnixgramSource {
2930
return &unixsrc
3031
}
3132

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 {
3355
//the socket has to run from the same goroutine because it is not thread safe
3456
//memory barrier executed when goroutines moved between threads
3557
//reference: https://groups.google.com/forum/#!topic/golang-nuts/eABYrBA5LEk
@@ -66,8 +88,9 @@ func (src UnixgramSource) Run() error {
6688
// a separate message
6789
for _, msg := range bytes.Split(buf[:nr], lf) {
6890
if len(msg) > 0 {
91+
wi := src.decodeNginxLog(msg)
6992
sent++
70-
src.Out() <- msg
93+
src.Out() <- msg[:wi]
7194
}
7295
}
7396

0 commit comments

Comments
 (0)