-
Notifications
You must be signed in to change notification settings - Fork 14
/
ctx.go
200 lines (176 loc) · 4.63 KB
/
ctx.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package dsl
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/Comcast/plax/subst"
)
var (
// DefaultLogger is the default logger.
DefaultLogger Logger = &GoLogger{}
// DefaultLogLevel is the default log level.
//
// ToDo: Use an enum.
DefaultLogLevel = "info"
)
// Ctx includes a context.Context, logging specifications, and some
// directories for various file inclusions.
type Ctx struct {
context.Context
Logger
IncludeDirs []string
Dir string
LogLevel string
*Redactions
}
// NewCtx build a new dsl.Ctx
func NewCtx(ctx context.Context) *Ctx {
if ctx == nil {
ctx = context.Background()
}
// Make default redactions
redactions := NewRedactions()
// If the context was a dsl.Ctx then use the redactions from the original context
if dslCtx, ok := ctx.(*Ctx); ok {
redactions = dslCtx.Redactions
}
return &Ctx{
Context: ctx,
Logger: DefaultLogger,
LogLevel: DefaultLogLevel,
IncludeDirs: make([]string, 0, 1),
Dir: ".",
Redactions: redactions,
}
}
// WithCancel builds a new dsl.Ctx with a cancel function.
func (c *Ctx) WithCancel() (*Ctx, func()) {
ctx, cancel := context.WithCancel(c.Context)
return &Ctx{
Context: ctx,
Logger: DefaultLogger,
LogLevel: c.LogLevel,
IncludeDirs: c.IncludeDirs,
Dir: c.Dir,
Redactions: c.Redactions, // not copying
}, cancel
}
// WithTimeout builds a new dsl.Ctx with a timeout function.
func (c *Ctx) WithTimeout(d time.Duration) (*Ctx, func()) {
ctx, cancel := context.WithTimeout(c.Context, d)
return &Ctx{
Context: ctx,
Logger: DefaultLogger,
LogLevel: c.LogLevel,
IncludeDirs: c.IncludeDirs,
Dir: c.Dir,
Redactions: c.Redactions, // not copying
}, cancel
}
// SetLogLevel sets the dsl.Ctx LogLevel.
func (c *Ctx) SetLogLevel(level string) error {
canonical := strings.ToLower(level)
// No strings.TrimSpace.
switch canonical {
case "info", "debug", "none":
default:
return fmt.Errorf("Ctx.LogLevel '%s' isn't 'info', 'debug', or 'none'", canonical)
}
c.LogLevel = canonical
return nil
}
// bindingRedactions adds redaction patterns for values of binding
// variables that start with X_.
func (ctx *Ctx) BindingsRedactions(bs Bindings) error {
for p, v := range bs {
if WantsRedaction(p) {
var s string
switch vv := v.(type) {
case string:
s = vv
case interface{}:
bs, err := subst.JSONMarshal(vv)
if err != nil {
return err
}
s = string(bs)
}
pat := regexp.QuoteMeta(s)
if err := ctx.AddRedaction(pat); err != nil {
return err
}
}
}
return nil
}
// Indf emits a log line starting with a '|' when ctx.LogLevel isn't 'none'.
func (c *Ctx) Indf(format string, args ...interface{}) {
switch c.LogLevel {
case "none", "NONE":
default:
c.Redactf("| "+format, args...)
}
}
// Inddf emits a log line starting with a '|' when ctx.LogLevel is 'debug';
//
// The second 'd' is for "debug".
func (c *Ctx) Inddf(format string, args ...interface{}) {
switch c.LogLevel {
case "debug", "DEBUG":
c.Redactf("| "+format, args...)
}
}
// Warnf emits a log with a '!' prefix.
func (c *Ctx) Warnf(format string, args ...interface{}) {
c.Redactf("! "+format, args...)
}
// Logf emits a log line starting with a '>' when ctx.LogLevel isn't 'none'.
func (c *Ctx) Logf(format string, args ...interface{}) {
switch c.LogLevel {
case "none", "NONE":
default:
c.Redactf("> "+format, args...)
}
}
// Logdf emits a log line starting with a '>' when ctx.LogLevel is 'debug';
//
// The second 'd' is for "debug".
func (c *Ctx) Logdf(format string, args ...interface{}) {
switch c.LogLevel {
case "debug", "DEBUG":
c.Redactf("> "+format, args...)
}
}
// Logger is an interface that allows for pluggable loggers.
//
// Used in the Plax Lambda.
type Logger interface {
Printf(format string, args ...interface{})
}
// GoLogger is just basic Go logging.
type GoLogger struct {
}
// Printf logs
func (l *GoLogger) Printf(format string, args ...interface{}) {
log.Printf(format, args...)
}