2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
// These tests are too simple.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-10-31 01:59:47 +01:00
|
|
|
"fmt"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2015-10-31 01:59:47 +01:00
|
|
|
"strings"
|
2010-12-03 05:34:57 +01:00
|
|
|
"testing"
|
2015-10-31 01:59:47 +01:00
|
|
|
"time"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Rdate = `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
|
|
|
|
Rtime = `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
|
|
|
|
Rmicroseconds = `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
|
2020-01-03 00:05:27 +01:00
|
|
|
Rline = `(60|62):` // must update if the calls to l.Printf / l.Print below move
|
2012-03-07 02:16:20 +01:00
|
|
|
Rlongfile = `.*/[A-Za-z0-9_\-]+\.go:` + Rline
|
|
|
|
Rshortfile = `[A-Za-z0-9_\-]+\.go:` + Rline
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type tester struct {
|
|
|
|
flag int
|
|
|
|
prefix string
|
|
|
|
pattern string // regexp that log output must match; we add ^ and expected_text$ always
|
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []tester{
|
|
|
|
// individual pieces:
|
|
|
|
{0, "", ""},
|
|
|
|
{0, "XXX", "XXX"},
|
|
|
|
{Ldate, "", Rdate + " "},
|
|
|
|
{Ltime, "", Rtime + " "},
|
2020-01-03 00:05:27 +01:00
|
|
|
{Ltime | Lmsgprefix, "XXX", Rtime + " XXX"},
|
2010-12-03 05:34:57 +01:00
|
|
|
{Ltime | Lmicroseconds, "", Rtime + Rmicroseconds + " "},
|
|
|
|
{Lmicroseconds, "", Rtime + Rmicroseconds + " "}, // microsec implies time
|
|
|
|
{Llongfile, "", Rlongfile + " "},
|
|
|
|
{Lshortfile, "", Rshortfile + " "},
|
|
|
|
{Llongfile | Lshortfile, "", Rshortfile + " "}, // shortfile overrides longfile
|
|
|
|
// everything at once:
|
|
|
|
{Ldate | Ltime | Lmicroseconds | Llongfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " "},
|
|
|
|
{Ldate | Ltime | Lmicroseconds | Lshortfile, "XXX", "XXX" + Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " "},
|
2020-01-03 00:05:27 +01:00
|
|
|
{Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rlongfile + " XXX"},
|
|
|
|
{Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix, "XXX", Rdate + " " + Rtime + Rmicroseconds + " " + Rshortfile + " XXX"},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
|
|
|
|
func testPrint(t *testing.T, flag int, prefix string, pattern string, useFormat bool) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
SetOutput(buf)
|
|
|
|
SetFlags(flag)
|
|
|
|
SetPrefix(prefix)
|
|
|
|
if useFormat {
|
|
|
|
Printf("hello %d world", 23)
|
|
|
|
} else {
|
|
|
|
Println("hello", 23, "world")
|
|
|
|
}
|
|
|
|
line := buf.String()
|
|
|
|
line = line[0 : len(line)-1]
|
|
|
|
pattern = "^" + pattern + "hello 23 world$"
|
2019-09-06 20:12:46 +02:00
|
|
|
matched, err := regexp.MatchString(pattern, line)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("pattern did not compile:", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
t.Errorf("log output should match %q is %q", pattern, line)
|
|
|
|
}
|
|
|
|
SetOutput(os.Stderr)
|
|
|
|
}
|
|
|
|
|
2020-12-23 18:57:37 +01:00
|
|
|
func TestDefault(t *testing.T) {
|
|
|
|
if got := Default(); got != std {
|
|
|
|
t.Errorf("Default [%p] should be std [%p]", got, std)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func TestAll(t *testing.T) {
|
|
|
|
for _, testcase := range tests {
|
|
|
|
testPrint(t, testcase.flag, testcase.prefix, testcase.pattern, false)
|
|
|
|
testPrint(t, testcase.flag, testcase.prefix, testcase.pattern, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutput(t *testing.T) {
|
|
|
|
const testString = "test"
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := New(&b, "", 0)
|
|
|
|
l.Println(testString)
|
|
|
|
if expect := testString + "\n"; b.String() != expect {
|
|
|
|
t.Errorf("log output should match %q is %q", expect, b.String())
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 19:09:10 +02:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func TestOutputRace(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := New(&b, "", 0)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func() {
|
|
|
|
l.SetFlags(0)
|
|
|
|
}()
|
|
|
|
l.Output(0, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
func TestFlagAndPrefixSetting(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := New(&b, "Test:", LstdFlags)
|
|
|
|
f := l.Flags()
|
|
|
|
if f != LstdFlags {
|
|
|
|
t.Errorf("Flags 1: expected %x got %x", LstdFlags, f)
|
|
|
|
}
|
|
|
|
l.SetFlags(f | Lmicroseconds)
|
|
|
|
f = l.Flags()
|
|
|
|
if f != LstdFlags|Lmicroseconds {
|
|
|
|
t.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds, f)
|
|
|
|
}
|
|
|
|
p := l.Prefix()
|
|
|
|
if p != "Test:" {
|
|
|
|
t.Errorf(`Prefix: expected "Test:" got %q`, p)
|
|
|
|
}
|
|
|
|
l.SetPrefix("Reality:")
|
|
|
|
p = l.Prefix()
|
|
|
|
if p != "Reality:" {
|
|
|
|
t.Errorf(`Prefix: expected "Reality:" got %q`, p)
|
|
|
|
}
|
|
|
|
// Verify a log message looks right, with our prefix and microseconds present.
|
|
|
|
l.Print("hello")
|
|
|
|
pattern := "^Reality:" + Rdate + " " + Rtime + Rmicroseconds + " hello\n"
|
|
|
|
matched, err := regexp.Match(pattern, b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("pattern %q did not compile: %s", pattern, err)
|
|
|
|
}
|
|
|
|
if !matched {
|
|
|
|
t.Error("message did not match pattern")
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
func TestUTCFlag(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := New(&b, "Test:", LstdFlags)
|
|
|
|
l.SetFlags(Ldate | Ltime | LUTC)
|
|
|
|
// Verify a log message looks right in the right time zone. Quantize to the second only.
|
|
|
|
now := time.Now().UTC()
|
|
|
|
l.Print("hello")
|
|
|
|
want := fmt.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
|
|
|
|
now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
|
|
|
|
got := b.String()
|
|
|
|
if got == want {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// It's possible we crossed a second boundary between getting now and logging,
|
|
|
|
// so add a second and try again. This should very nearly always work.
|
|
|
|
now = now.Add(time.Second)
|
|
|
|
want = fmt.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
|
|
|
|
now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
|
|
|
|
if got == want {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Errorf("got %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyPrintCreatesLine(t *testing.T) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
l := New(&b, "Header:", LstdFlags)
|
|
|
|
l.Print()
|
|
|
|
l.Println("non-empty")
|
|
|
|
output := b.String()
|
|
|
|
if n := strings.Count(output, "Header"); n != 2 {
|
|
|
|
t.Errorf("expected 2 headers, got %d", n)
|
|
|
|
}
|
|
|
|
if n := strings.Count(output, "\n"); n != 2 {
|
|
|
|
t.Errorf("expected 2 lines, got %d", n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkItoa(b *testing.B) {
|
|
|
|
dst := make([]byte, 0, 64)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
dst = dst[0:0]
|
|
|
|
itoa(&dst, 2015, 4) // year
|
|
|
|
itoa(&dst, 1, 2) // month
|
|
|
|
itoa(&dst, 30, 2) // day
|
|
|
|
itoa(&dst, 12, 2) // hour
|
|
|
|
itoa(&dst, 56, 2) // minute
|
|
|
|
itoa(&dst, 0, 2) // second
|
|
|
|
itoa(&dst, 987654, 6) // microsecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPrintln(b *testing.B) {
|
|
|
|
const testString = "test"
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l := New(&buf, "", LstdFlags)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Reset()
|
|
|
|
l.Println(testString)
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
|
|
|
|
func BenchmarkPrintlnNoFlags(b *testing.B) {
|
|
|
|
const testString = "test"
|
|
|
|
var buf bytes.Buffer
|
|
|
|
l := New(&buf, "", 0)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Reset()
|
|
|
|
l.Println(testString)
|
|
|
|
}
|
|
|
|
}
|