Skip to content

Commit

Permalink
Merge pull request #30 from xushiwei/q
Browse files Browse the repository at this point in the history
github.com/qiniu/x/log
  • Loading branch information
xushiwei authored Aug 4, 2021
2 parents dbd9931 + 81f2728 commit ac02073
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
19 changes: 11 additions & 8 deletions log/logext.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line
if l.prefix != "" {
buf.WriteString(l.prefix)
}
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if l.flag&Ldate != 0 {
flag := l.flag
if flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if flag&Ldate != 0 {
year, month, day := t.Date()
itoa(buf, year, 4)
buf.WriteByte('/')
Expand All @@ -128,14 +129,14 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line
itoa(buf, day, 2)
buf.WriteByte(' ')
}
if l.flag&(Ltime|Lmicroseconds) != 0 {
if flag&(Ltime|Lmicroseconds) != 0 {
hour, min, sec := t.Clock()
itoa(buf, hour, 2)
buf.WriteByte(':')
itoa(buf, min, 2)
buf.WriteByte(':')
itoa(buf, sec, 2)
if l.flag&Lmicroseconds != 0 {
if flag&Lmicroseconds != 0 {
buf.WriteByte('.')
itoa(buf, t.Nanosecond()/1e3, 6)
}
Expand All @@ -147,18 +148,20 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line
buf.WriteString(reqID)
buf.WriteByte(']')
}
if l.flag&Llevel != 0 {
if flag&Llevel != 0 {
buf.WriteString(levels[lvl])
}
if l.flag&(Lshortfile|Llongfile) != 0 {
if l.flag&Lshortfile != 0 {
file = shortFile(file, l.flag)
if flag&(Lshortfile|Llongfile) != 0 {
if flag&Lshortfile != 0 {
file = shortFile(file, flag)
}
buf.WriteByte(' ')
buf.WriteString(file)
buf.WriteByte(':')
itoa(buf, line, -1)
buf.WriteString(": ")
} else if flag&Llevel != 0 {
buf.WriteByte(' ')
}
}

Expand Down
4 changes: 2 additions & 2 deletions mockhttp/mockhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ func (p *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
req.Body.Close()

ctlen := int64(-1)
if v := rw.HeaderMap.Get("Content-Length"); v != "" {
if v := rw.Header().Get("Content-Length"); v != "" {
ctlen, _ = strconv.ParseInt(v, 10, 64)
}

return &http.Response{
Status: "",
StatusCode: rw.Code,
Header: rw.HeaderMap,
Header: rw.Header(),
Body: ioutil.NopCloser(rw.Body),
ContentLength: ctlen,
TransferEncoding: nil,
Expand Down
9 changes: 5 additions & 4 deletions mockhttp/mockhttp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mockhttp_test

import (
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -17,7 +18,6 @@ import (
// --------------------------------------------------------------------

func reply(w http.ResponseWriter, code int, data interface{}) {

msg, _ := json.Marshal(data)
h := w.Header()
h.Set("Content-Length", strconv.Itoa(len(msg)))
Expand Down Expand Up @@ -66,10 +66,11 @@ func TestBasic(t *testing.T) {

mockhttp.ListenAndServe("foo.com", nil)

c := rpc.Client{mockhttp.DefaultClient}
ctx := context.TODO()
c := rpc.Client{Client: mockhttp.DefaultClient}
{
var foo FooRet
err := c.Call(nil, &foo, "POST", "http://foo.com/foo")
err := c.Call(ctx, &foo, "POST", "http://foo.com/foo")
if err != nil {
t.Fatal("call foo failed:", err)
}
Expand All @@ -80,7 +81,7 @@ func TestBasic(t *testing.T) {
}
{
var ret map[string]string
err := c.Call(nil, &ret, "POST", "http://foo.com/bar")
err := c.Call(ctx, &ret, "POST", "http://foo.com/bar")
if err != nil {
t.Fatal("call foo failed:", err)
}
Expand Down

0 comments on commit ac02073

Please sign in to comment.