blob: e440cb17da8843ec937abb76ebff5f32e23bdfae [file] [log] [blame]
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -07001// Copyright 2015 The Vanadium 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.
4
5package apilog_test
6
7import (
8 "bufio"
9 "io/ioutil"
10 "os"
11 "path/filepath"
12 "regexp"
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070013 "testing"
14
15 "v.io/x/lib/vlog"
16
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070017 "v.io/v23/context"
18
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070019 "v.io/x/ref/lib/apilog"
20)
21
22func readLogFiles(dir string) ([]string, error) {
23 files, err := ioutil.ReadDir(dir)
24 if err != nil {
25 return nil, err
26 }
27 var contents []string
28 for _, fi := range files {
29 // Skip symlinks to avoid double-counting log lines.
30 if !fi.Mode().IsRegular() {
31 continue
32 }
33 file, err := os.Open(filepath.Join(dir, fi.Name()))
34 if err != nil {
35 return nil, err
36 }
37 scanner := bufio.NewScanner(file)
38 for scanner.Scan() {
39 if line := scanner.Text(); len(line) > 0 && line[0] == 'I' {
40 contents = append(contents, line)
41 }
42 }
43 }
44 return contents, nil
45}
46
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070047func myLoggedFunc(ctx *context.T) {
48 f := apilog.LogCall(ctx, "entry")
49 f(ctx, "exit")
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070050}
51
52func TestLogCall(t *testing.T) {
53 dir, err := ioutil.TempDir("", "logtest")
54 defer os.RemoveAll(dir)
55 if err != nil {
56 t.Fatalf("unexpected error: %s", err)
57 }
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070058 l := vlog.NewLogger("testHeader")
59 l.Configure(vlog.LogDir(dir), vlog.Level(2))
60 ctx, _ := context.RootContext()
61 ctx = context.WithLogger(ctx, l)
62 myLoggedFunc(ctx)
63 ctx.FlushLog()
64 testLogOutput(t, dir)
65}
66
67func TestLogCallNoContext(t *testing.T) {
68 dir, err := ioutil.TempDir("", "logtest")
69 defer os.RemoveAll(dir)
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070070 if err != nil {
71 t.Fatalf("unexpected error: %s", err)
72 }
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070073 l := vlog.NewLogger("testHeader")
74 l.Configure(vlog.LogDir(dir), vlog.Level(2))
75 saved := vlog.Log
76 vlog.Log = l
77 defer func() { vlog.Log = saved }()
78 myLoggedFunc(nil)
79 vlog.FlushLog()
80 testLogOutput(t, dir)
81}
82
83func testLogOutput(t *testing.T, dir string) {
84 contents, err := readLogFiles(dir)
85 if err != nil {
86 t.Fatalf("unexpected error: %q %s", dir, err)
87 }
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070088 if want, got := 2, len(contents); want != got {
89 t.Errorf("Expected %d info lines, got %d instead", want, got)
90 }
Cosmos Nicolaou86e39602015-06-04 16:04:13 -070091 logCallLineRE := regexp.MustCompile(`\S+ \S+\s+\S+ ([^:]*):.*(call|return)\[(\S*)`)
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070092 for _, line := range contents {
93 match := logCallLineRE.FindStringSubmatch(line)
Cosmos Nicolaou86e39602015-06-04 16:04:13 -070094 if len(match) != 4 {
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -070095 t.Errorf("failed to match %s", line)
96 continue
97 }
Cosmos Nicolaou86e39602015-06-04 16:04:13 -070098 fileName, callType, funcName := match[1], match[2], match[3]
Cosmos Nicolaou92c57482015-05-22 13:29:15 -070099 if fileName != "apilog_test.go" {
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -0700100 t.Errorf("unexpected file name: %s", fileName)
101 continue
102 }
103 if callType != "call" && callType != "return" {
104 t.Errorf("unexpected call type: %s", callType)
105 }
Cosmos Nicolaou4c53aed2015-05-19 22:13:03 -0700106 if funcName != "apilog_test.myLoggedFunc" {
107 t.Errorf("unexpected func name: %s", funcName)
108 }
109 }
110}