Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package apilog_test |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
| 9 | "io/ioutil" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "regexp" |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 13 | "testing" |
| 14 | |
| 15 | "v.io/x/lib/vlog" |
| 16 | |
Cosmos Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 17 | "v.io/v23/context" |
| 18 | |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 19 | "v.io/x/ref/lib/apilog" |
| 20 | ) |
| 21 | |
| 22 | func 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 Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 47 | func myLoggedFunc(ctx *context.T) { |
| 48 | f := apilog.LogCall(ctx, "entry") |
| 49 | f(ctx, "exit") |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | func 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 Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 58 | 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 | |
| 67 | func TestLogCallNoContext(t *testing.T) { |
| 68 | dir, err := ioutil.TempDir("", "logtest") |
| 69 | defer os.RemoveAll(dir) |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 70 | if err != nil { |
| 71 | t.Fatalf("unexpected error: %s", err) |
| 72 | } |
Cosmos Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 73 | 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 | |
| 83 | func 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 Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 88 | if want, got := 2, len(contents); want != got { |
| 89 | t.Errorf("Expected %d info lines, got %d instead", want, got) |
| 90 | } |
Cosmos Nicolaou | 86e3960 | 2015-06-04 16:04:13 -0700 | [diff] [blame] | 91 | logCallLineRE := regexp.MustCompile(`\S+ \S+\s+\S+ ([^:]*):.*(call|return)\[(\S*)`) |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 92 | for _, line := range contents { |
| 93 | match := logCallLineRE.FindStringSubmatch(line) |
Cosmos Nicolaou | 86e3960 | 2015-06-04 16:04:13 -0700 | [diff] [blame] | 94 | if len(match) != 4 { |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 95 | t.Errorf("failed to match %s", line) |
| 96 | continue |
| 97 | } |
Cosmos Nicolaou | 86e3960 | 2015-06-04 16:04:13 -0700 | [diff] [blame] | 98 | fileName, callType, funcName := match[1], match[2], match[3] |
Cosmos Nicolaou | 92c5748 | 2015-05-22 13:29:15 -0700 | [diff] [blame] | 99 | if fileName != "apilog_test.go" { |
Cosmos Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 100 | 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 Nicolaou | 4c53aed | 2015-05-19 22:13:03 -0700 | [diff] [blame] | 106 | if funcName != "apilog_test.myLoggedFunc" { |
| 107 | t.Errorf("unexpected func name: %s", funcName) |
| 108 | } |
| 109 | } |
| 110 | } |