Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Olivier Poitrey
committed
Nov 12, 2015
1 parent
350c3d5
commit 7864803
Showing
3 changed files
with
109 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package xlog | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
) | ||
|
||
type color int | ||
|
||
const ( | ||
red color = 31 | ||
green color = 32 | ||
yellow color = 33 | ||
blue color = 34 | ||
gray color = 37 | ||
) | ||
|
||
func colorPrint(w io.Writer, s string, c color) { | ||
w.Write([]byte{0x1b, '[', byte('0' + c/10), byte('0' + c%10), 'm'}) | ||
w.Write([]byte(s)) | ||
w.Write([]byte("\x1b[0m")) | ||
} | ||
|
||
func needsQuotedValueRune(r rune) bool { | ||
return r <= ' ' || r == '=' || r == '"' | ||
} | ||
|
||
// writeValue writes a value on the writer in a logfmt compatible way | ||
func writeValue(w io.Writer, v interface{}) (err error) { | ||
switch v := v.(type) { | ||
case nil: | ||
_, err = w.Write([]byte("null")) | ||
case string: | ||
if strings.IndexFunc(v, needsQuotedValueRune) != -1 { | ||
var b []byte | ||
b, err = json.Marshal(v) | ||
if err == nil { | ||
w.Write(b) | ||
} | ||
} else { | ||
_, err = w.Write([]byte(v)) | ||
} | ||
case error: | ||
s := v.Error() | ||
err = writeValue(w, s) | ||
default: | ||
s := fmt.Sprint(v) | ||
err = writeValue(w, s) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package xlog | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestColorPrint(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
colorPrint(buf, "test", red) | ||
assert.Equal(t, "\x1b[31mtest\x1b[0m", buf.String()) | ||
buf.Reset() | ||
colorPrint(buf, "test", green) | ||
assert.Equal(t, "\x1b[32mtest\x1b[0m", buf.String()) | ||
buf.Reset() | ||
colorPrint(buf, "test", yellow) | ||
assert.Equal(t, "\x1b[33mtest\x1b[0m", buf.String()) | ||
buf.Reset() | ||
colorPrint(buf, "test", blue) | ||
assert.Equal(t, "\x1b[34mtest\x1b[0m", buf.String()) | ||
buf.Reset() | ||
colorPrint(buf, "test", gray) | ||
assert.Equal(t, "\x1b[37mtest\x1b[0m", buf.String()) | ||
} | ||
|
||
func TestNeedsQuotedValueRune(t *testing.T) { | ||
assert.True(t, needsQuotedValueRune('=')) | ||
assert.True(t, needsQuotedValueRune('"')) | ||
assert.True(t, needsQuotedValueRune(' ')) | ||
assert.False(t, needsQuotedValueRune('a')) | ||
assert.False(t, needsQuotedValueRune('\'')) | ||
} | ||
|
||
func TestWriteValue(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
write := func(v interface{}) string { | ||
buf.Reset() | ||
err := writeValue(buf, v) | ||
if err == nil { | ||
return buf.String() | ||
} | ||
return "" | ||
} | ||
assert.Equal(t, `foobar`, write(`foobar`)) | ||
assert.Equal(t, `"foo=bar"`, write(`foo=bar`)) | ||
assert.Equal(t, `"foo bar"`, write(`foo bar`)) | ||
assert.Equal(t, `"foo\"bar"`, write(`foo"bar`)) | ||
assert.Equal(t, `"foo\nbar"`, write("foo\nbar")) | ||
assert.Equal(t, `null`, write(nil)) | ||
assert.Equal(t, `"2000-01-02 03:04:05 +0000 UTC"`, write(time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC))) | ||
assert.Equal(t, `"error \"with quote\""`, write(errors.New(`error "with quote"`))) | ||
} |