Skip to content

Commit 1718e65

Browse files
authored
Merge pull request #12 from hatena/fix-enc-bytes
fix nbytes returning from Read with encoding.
2 parents 8a75f4f + e6bbb3b commit 1718e65

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

check-log/check-log.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/jessevdk/go-flags"
1717
"github.com/mackerelio/checkers"
1818
"github.com/mattn/go-encoding"
19+
enc "golang.org/x/text/encoding"
1920
)
2021

2122
type logOpts struct {
@@ -37,6 +38,7 @@ type logOpts struct {
3738
excludeReg *regexp.Regexp
3839
fileList []string
3940
origArgs []string
41+
decoder *enc.Decoder
4042
}
4143

4244
func (opts *logOpts) prepare() error {
@@ -220,11 +222,11 @@ func (opts *logOpts) searchLog(logFile string) (int64, int64, string, error) {
220222

221223
var r io.Reader = f
222224
if opts.Encoding != "" {
223-
enc := encoding.GetEncoding(opts.Encoding)
224-
if enc == nil {
225+
e := encoding.GetEncoding(opts.Encoding)
226+
if e == nil {
225227
return 0, 0, "", fmt.Errorf("unknown encoding:" + opts.Encoding)
226228
}
227-
r = enc.NewDecoder().Reader(f)
229+
opts.decoder = e.NewDecoder()
228230
}
229231

230232
warnNum, critNum, readBytes, errLines, err := opts.searchReader(r)
@@ -258,6 +260,13 @@ func (opts *logOpts) searchReader(rdr io.Reader) (warnNum, critNum, readBytes in
258260
break
259261
}
260262
readBytes += int64(len(lineBytes))
263+
264+
if opts.decoder != nil {
265+
lineBytes, err = opts.decoder.Bytes(lineBytes)
266+
if err != nil {
267+
break
268+
}
269+
}
261270
line := strings.Trim(string(lineBytes), "\r\n")
262271
if matched, matches := opts.match(line); matched {
263272
if len(matches) > 1 && (opts.WarnLevel > 0 || opts.CritLevel > 0) {

check-log/check_log_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,27 @@ func TestRunWithEncoding(t *testing.T) {
330330
opts, _ := parseArgs([]string{"-s", dir, "-f", logf, "-p", `エラー`, "--encoding", "euc-jp"})
331331
opts.prepare()
332332

333-
fatal := "\xa5\xa8\xa5\xe9\xa1\xbc\n" // エラー
334333
testEncoding := func() {
335-
fh.Write([]byte(fatal))
334+
fh.Write([]byte("\xa5\xa8\xa5\xe9\xa1\xbc\n")) // エラー
336335
w, c, errLines, err := opts.searchLog(logf)
337336
assert.Equal(t, err, nil, "err should be nil")
338337
assert.Equal(t, int64(1), w, "something went wrong")
339338
assert.Equal(t, int64(1), c, "something went wrong")
340339
assert.Equal(t, "エラー\n", errLines, "something went wrong")
340+
341+
fh.Write([]byte("\xb0\xdb\xbe\xef\n")) // 異常
342+
w, c, errLines, err = opts.searchLog(logf)
343+
assert.Equal(t, err, nil, "err should be nil")
344+
assert.Equal(t, int64(0), w, "something went wrong")
345+
assert.Equal(t, int64(0), c, "something went wrong")
346+
assert.Equal(t, "", errLines, "something went wrong")
347+
348+
fh.Write([]byte("\xa5\xa8\xa5\xe9\xa1\xbc\n")) // エラー
349+
w, c, errLines, err = opts.searchLog(logf)
350+
assert.Equal(t, err, nil, "err should be nil")
351+
assert.Equal(t, int64(1), w, "something went wrong")
352+
assert.Equal(t, int64(1), c, "something went wrong")
353+
assert.Equal(t, "エラー\n", errLines, "something went wrong")
341354
}
342355
testEncoding()
343356
}

0 commit comments

Comments
 (0)