aboutsummaryrefslogtreecommitdiff
path: root/v2/trace.go
blob: cdb03a8f447baf471d823a3b6e80ac1d39f1f875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2020 Google Inc.
//
// 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.

package classifier

import (
	"fmt"
	"strings"
)

// This file contains routines for a simple trace execution mechanism.

// TraceConfiguration specifies the configuration for tracing execution of the
// license classifier.
type TraceConfiguration struct {
	// Comma-separated list of phases to be traced. Can use * for all phases.
	TracePhases string
	// Comma-separated list of licenses to be traced. Can use * as a suffix to
	// match prefixes, or by itself to match all licenses.
	TraceLicenses string

	// Tracer specifies a TraceFunc used to capture tracing information.
	// If not supplied, emits using fmt.Printf
	Tracer        TraceFunc
	tracePhases   map[string]bool
	traceLicenses map[string]bool
}

func (t *TraceConfiguration) init() {
	if t == nil {
		return
	}
	// Sample the config values to create the lookup maps
	t.traceLicenses = make(map[string]bool)
	t.tracePhases = make(map[string]bool)

	if len(t.TraceLicenses) > 0 {
		for _, lic := range strings.Split(t.TraceLicenses, ",") {
			t.traceLicenses[lic] = true
		}
	}

	if len(t.TracePhases) > 0 {
		for _, phase := range strings.Split(t.TracePhases, ",") {
			t.tracePhases[phase] = true
		}
	}
}

var traceLicenses map[string]bool
var tracePhases map[string]bool

func (t *TraceConfiguration) shouldTrace(phase string) bool {
	if t == nil {
		return false
	}
	if t.tracePhases["*"] {
		return true
	}
	return t.tracePhases[phase]
}

func (t *TraceConfiguration) isTraceLicense(lic string) bool {
	if t == nil {
		return false
	}
	if t.traceLicenses[lic] {
		return true
	}

	for e := range t.traceLicenses {
		if idx := strings.Index(e, "*"); idx != -1 {
			if strings.HasPrefix(lic, e[0:idx]) {
				return true
			}
		}
	}

	return false
}

func (t *TraceConfiguration) trace(f string, args ...interface{}) {
	if t == nil || t.Tracer == nil {
		fmt.Printf(f, args...)
		fmt.Println()
		return
	}

	t.Tracer(f, args...)
}

func (t *TraceConfiguration) traceSearchset(lic string) bool {
	return t.isTraceLicense(lic) && t.shouldTrace("searchset")
}

func (t *TraceConfiguration) traceTokenize(lic string) bool {
	return t.isTraceLicense(lic) && t.shouldTrace("tokenize")
}

func (t *TraceConfiguration) traceScoring(lic string) bool {
	return t.isTraceLicense(lic) && t.shouldTrace("score")
}

func (t *TraceConfiguration) traceFrequency(lic string) bool {
	return t.isTraceLicense(lic) && t.shouldTrace("frequency")
}

// TraceFunc works like fmt.Printf to emit tracing data for the
// classifier.
type TraceFunc func(string, ...interface{})