1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:01:28 +00:00

Fixed bug and added more tests

This commit is contained in:
Faissal Elamraoui 2016-07-28 11:37:28 +02:00
parent 2b867f8152
commit d85293b7c0
4 changed files with 95 additions and 46 deletions

28
logs/color.go Normal file
View File

@ -0,0 +1,28 @@
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows
package logs
import "io"
type ansiColorWriter struct {
w io.Writer
mode outputMode
}
func (cw *ansiColorWriter) Write(p []byte) (int, error) {
return cw.w.Write(p)
}

View File

@ -25,7 +25,6 @@ import (
) )
type ( type (
outputMode int
csiState int csiState int
parseResult int parseResult int
) )
@ -427,35 +426,3 @@ func (cw *ansiColorWriter) Write(p []byte) (int, error) {
return r, err return r, err
} }
// DiscardNonColorEscSeq supports the divided color escape sequence.
// But non-color escape sequence is not output.
// Please use the OutputNonColorEscSeq If you want to output a non-color
// escape sequences such as ncurses. However, it does not support the divided
// color escape sequence.
const (
_ outputMode = iota
DiscardNonColorEscSeq
OutputNonColorEscSeq
)
// NewAnsiColorWriter creates and initializes a new ansiColorWriter
// using io.Writer w as its initial contents.
// In the console of Windows, which change the foreground and background
// colors of the text by the escape sequence.
// In the console of other systems, which writes to w all text.
func NewAnsiColorWriter(w io.Writer) io.Writer {
return NewModeAnsiColorWriter(w, DiscardNonColorEscSeq)
}
// NewModeAnsiColorWriter create and initializes a new ansiColorWriter
// by specifying the outputMode.
func NewModeAnsiColorWriter(w io.Writer, mode outputMode) io.Writer {
if _, ok := w.(*ansiColorWriter); !ok {
return &ansiColorWriter{
w: w,
mode: mode,
}
}
return w
}

View File

@ -38,18 +38,54 @@ func (lg *logWriter) println(when time.Time, msg string) {
lg.Unlock() lg.Unlock()
} }
const y1 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999` type outputMode int
const y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789`
const mo1 = `000000000111` // DiscardNonColorEscSeq supports the divided color escape sequence.
const mo2 = `123456789012` // But non-color escape sequence is not output.
const d1 = `0000000001111111111222222222233` // Please use the OutputNonColorEscSeq If you want to output a non-color
const d2 = `1234567890123456789012345678901` // escape sequences such as ncurses. However, it does not support the divided
const h1 = `000000000011111111112222` // color escape sequence.
const h2 = `012345678901234567890123` const (
const mi1 = `000000000011111111112222222222333333333344444444445555555555` _ outputMode = iota
const mi2 = `012345678901234567890123456789012345678901234567890123456789` DiscardNonColorEscSeq
const s1 = `000000000011111111112222222222333333333344444444445555555555` OutputNonColorEscSeq
const s2 = `012345678901234567890123456789012345678901234567890123456789` )
// NewAnsiColorWriter creates and initializes a new ansiColorWriter
// using io.Writer w as its initial contents.
// In the console of Windows, which change the foreground and background
// colors of the text by the escape sequence.
// In the console of other systems, which writes to w all text.
func NewAnsiColorWriter(w io.Writer) io.Writer {
return NewModeAnsiColorWriter(w, DiscardNonColorEscSeq)
}
// NewModeAnsiColorWriter create and initializes a new ansiColorWriter
// by specifying the outputMode.
func NewModeAnsiColorWriter(w io.Writer, mode outputMode) io.Writer {
if _, ok := w.(*ansiColorWriter); !ok {
return &ansiColorWriter{
w: w,
mode: mode,
}
}
return w
}
const (
y1 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999`
y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789`
mo1 = `000000000111`
mo2 = `123456789012`
d1 = `0000000001111111111222222222233`
d2 = `1234567890123456789012345678901`
h1 = `000000000011111111112222`
h2 = `012345678901234567890123`
mi1 = `000000000011111111112222222222333333333344444444445555555555`
mi2 = `012345678901234567890123456789012345678901234567890123456789`
s1 = `000000000011111111112222222222333333333344444444445555555555`
s2 = `012345678901234567890123456789012345678901234567890123456789`
)
func formatTimeHeader(when time.Time) ([]byte, int) { func formatTimeHeader(when time.Time) ([]byte, int) {
y, mo, d := when.Date() y, mo, d := when.Date()

View File

@ -17,6 +17,7 @@ package logs
import ( import (
"testing" "testing"
"time" "time"
"bytes"
) )
func TestFormatHeader_0(t *testing.T) { func TestFormatHeader_0(t *testing.T) {
@ -55,3 +56,20 @@ func TestFormatHeader_1(t *testing.T) {
tm = tm.Add(dur) tm = tm.Add(dur)
} }
} }
func TestNewAnsiColor1(t *testing.T) {
inner := bytes.NewBufferString("")
w := NewAnsiColorWriter(inner)
if w == inner {
t.Errorf("Get %#v, want %#v", w, inner)
}
}
func TestNewAnsiColor2(t *testing.T) {
inner := bytes.NewBufferString("")
w1 := NewAnsiColorWriter(inner)
w2 := NewAnsiColorWriter(w1)
if w1 != w2 {
t.Errorf("Get %#v, want %#v", w1, w2)
}
}