1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // These tests are too simple.
20 Rdate
= `[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]`
21 Rtime
= `[0-9][0-9]:[0-9][0-9]:[0-9][0-9]`
22 Rmicroseconds
= `\.[0-9][0-9][0-9][0-9][0-9][0-9]`
23 Rline
= `(60|62):` // must update if the calls to l.Printf / l.Print below move
24 Rlongfile
= `.*/[A-Za-z0-9_\-]+\.go:` + Rline
25 Rshortfile
= `[A-Za-z0-9_\-]+\.go:` + Rline
31 pattern
string // regexp that log output must match; we add ^ and expected_text$ always
38 {Ldate
, "", Rdate
+ " "},
39 {Ltime
, "", Rtime
+ " "},
40 {Ltime | Lmsgprefix
, "XXX", Rtime
+ " XXX"},
41 {Ltime | Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "},
42 {Lmicroseconds
, "", Rtime
+ Rmicroseconds
+ " "}, // microsec implies time
43 {Llongfile
, "", Rlongfile
+ " "},
44 {Lshortfile
, "", Rshortfile
+ " "},
45 {Llongfile | Lshortfile
, "", Rshortfile
+ " "}, // shortfile overrides longfile
46 // everything at once:
47 {Ldate | Ltime | Lmicroseconds | Llongfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " "},
48 {Ldate | Ltime | Lmicroseconds | Lshortfile
, "XXX", "XXX" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " "},
49 {Ldate | Ltime | Lmicroseconds | Llongfile | Lmsgprefix
, "XXX", Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rlongfile
+ " XXX"},
50 {Ldate | Ltime | Lmicroseconds | Lshortfile | Lmsgprefix
, "XXX", Rdate
+ " " + Rtime
+ Rmicroseconds
+ " " + Rshortfile
+ " XXX"},
53 // Test using Println("hello", 23, "world") or using Printf("hello %d world", 23)
54 func testPrint(t
*testing
.T
, flag
int, prefix
string, pattern
string, useFormat
bool) {
55 buf
:= new(bytes
.Buffer
)
60 Printf("hello %d world", 23)
62 Println("hello", 23, "world")
65 line
= line
[0 : len(line
)-1]
66 pattern
= "^" + pattern
+ "hello 23 world$"
67 matched
, err
:= regexp
.MatchString(pattern
, line
)
69 t
.Fatal("pattern did not compile:", err
)
72 t
.Errorf("log output should match %q is %q", pattern
, line
)
77 func TestAll(t
*testing
.T
) {
78 for _
, testcase
:= range tests
{
79 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, false)
80 testPrint(t
, testcase
.flag
, testcase
.prefix
, testcase
.pattern
, true)
84 func TestOutput(t
*testing
.T
) {
85 const testString
= "test"
89 if expect
:= testString
+ "\n"; b
.String() != expect
{
90 t
.Errorf("log output should match %q is %q", expect
, b
.String())
94 func TestOutputRace(t
*testing
.T
) {
97 for i
:= 0; i
< 100; i
++ {
105 func TestFlagAndPrefixSetting(t
*testing
.T
) {
107 l
:= New(&b
, "Test:", LstdFlags
)
110 t
.Errorf("Flags 1: expected %x got %x", LstdFlags
, f
)
112 l
.SetFlags(f | Lmicroseconds
)
114 if f
!= LstdFlags|Lmicroseconds
{
115 t
.Errorf("Flags 2: expected %x got %x", LstdFlags|Lmicroseconds
, f
)
119 t
.Errorf(`Prefix: expected "Test:" got %q`, p
)
121 l
.SetPrefix("Reality:")
124 t
.Errorf(`Prefix: expected "Reality:" got %q`, p
)
126 // Verify a log message looks right, with our prefix and microseconds present.
128 pattern
:= "^Reality:" + Rdate
+ " " + Rtime
+ Rmicroseconds
+ " hello\n"
129 matched
, err
:= regexp
.Match(pattern
, b
.Bytes())
131 t
.Fatalf("pattern %q did not compile: %s", pattern
, err
)
134 t
.Error("message did not match pattern")
138 func TestUTCFlag(t
*testing
.T
) {
140 l
:= New(&b
, "Test:", LstdFlags
)
141 l
.SetFlags(Ldate | Ltime | LUTC
)
142 // Verify a log message looks right in the right time zone. Quantize to the second only.
143 now
:= time
.Now().UTC()
145 want
:= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
146 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
151 // It's possible we crossed a second boundary between getting now and logging,
152 // so add a second and try again. This should very nearly always work.
153 now
= now
.Add(time
.Second
)
154 want
= fmt
.Sprintf("Test:%d/%.2d/%.2d %.2d:%.2d:%.2d hello\n",
155 now
.Year(), now
.Month(), now
.Day(), now
.Hour(), now
.Minute(), now
.Second())
159 t
.Errorf("got %q; want %q", got
, want
)
162 func TestEmptyPrintCreatesLine(t
*testing
.T
) {
164 l
:= New(&b
, "Header:", LstdFlags
)
166 l
.Println("non-empty")
168 if n
:= strings
.Count(output
, "Header"); n
!= 2 {
169 t
.Errorf("expected 2 headers, got %d", n
)
171 if n
:= strings
.Count(output
, "\n"); n
!= 2 {
172 t
.Errorf("expected 2 lines, got %d", n
)
176 func BenchmarkItoa(b
*testing
.B
) {
177 dst
:= make([]byte, 0, 64)
178 for i
:= 0; i
< b
.N
; i
++ {
180 itoa(&dst
, 2015, 4) // year
181 itoa(&dst
, 1, 2) // month
182 itoa(&dst
, 30, 2) // day
183 itoa(&dst
, 12, 2) // hour
184 itoa(&dst
, 56, 2) // minute
185 itoa(&dst
, 0, 2) // second
186 itoa(&dst
, 987654, 6) // microsecond
190 func BenchmarkPrintln(b
*testing
.B
) {
191 const testString
= "test"
193 l
:= New(&buf
, "", LstdFlags
)
194 for i
:= 0; i
< b
.N
; i
++ {
196 l
.Println(testString
)
200 func BenchmarkPrintlnNoFlags(b
*testing
.B
) {
201 const testString
= "test"
203 l
:= New(&buf
, "", 0)
204 for i
:= 0; i
< b
.N
; i
++ {
206 l
.Println(testString
)