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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
// Go support for leveled logs, analogous to https://github.com/google/glog.
//
// Copyright 2023 Google Inc. All Rights Reserved.
//
// 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 glog
import (
"bytes"
"errors"
"flag"
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"github.com/golang/glog/internal/logsink"
)
// modulePat contains a filter for the -vmodule flag.
// It holds a verbosity level and a file pattern to match.
type modulePat struct {
pattern string
literal bool // The pattern is a literal string
full bool // The pattern wants to match the full path
level Level
}
// match reports whether the file matches the pattern. It uses a string
// comparison if the pattern contains no metacharacters.
func (m *modulePat) match(full, file string) bool {
if m.literal {
if m.full {
return full == m.pattern
}
return file == m.pattern
}
if m.full {
match, _ := filepath.Match(m.pattern, full)
return match
}
match, _ := filepath.Match(m.pattern, file)
return match
}
// isLiteral reports whether the pattern is a literal string, that is, has no metacharacters
// that require filepath.Match to be called to match the pattern.
func isLiteral(pattern string) bool {
return !strings.ContainsAny(pattern, `\*?[]`)
}
// isFull reports whether the pattern matches the full file path, that is,
// whether it contains /.
func isFull(pattern string) bool {
return strings.ContainsRune(pattern, '/')
}
// verboseFlags represents the setting of the -v and -vmodule flags.
type verboseFlags struct {
// moduleLevelCache is a sync.Map storing the -vmodule Level for each V()
// call site, identified by PC. If there is no matching -vmodule filter,
// the cached value is exactly v. moduleLevelCache is replaced with a new
// Map whenever the -vmodule or -v flag changes state.
moduleLevelCache atomic.Value
// mu guards all fields below.
mu sync.Mutex
// v stores the value of the -v flag. It may be read safely using
// sync.LoadInt32, but is only modified under mu.
v Level
// module stores the parsed -vmodule flag.
module []modulePat
// moduleLength caches len(module). If greater than zero, it
// means vmodule is enabled. It may be read safely using sync.LoadInt32, but
// is only modified under mu.
moduleLength int32
}
// NOTE: For compatibility with the open-sourced v1 version of this
// package (github.com/golang/glog) we need to retain that flag.Level
// implements the flag.Value interface. See also go/log-vs-glog.
// String is part of the flag.Value interface.
func (l *Level) String() string {
return strconv.FormatInt(int64(l.Get().(Level)), 10)
}
// Get is part of the flag.Value interface.
func (l *Level) Get() any {
if l == &vflags.v {
// l is the value registered for the -v flag.
return Level(atomic.LoadInt32((*int32)(l)))
}
return *l
}
// Set is part of the flag.Value interface.
func (l *Level) Set(value string) error {
v, err := strconv.Atoi(value)
if err != nil {
return err
}
if l == &vflags.v {
// l is the value registered for the -v flag.
vflags.mu.Lock()
defer vflags.mu.Unlock()
vflags.moduleLevelCache.Store(&sync.Map{})
atomic.StoreInt32((*int32)(l), int32(v))
return nil
}
*l = Level(v)
return nil
}
// vModuleFlag is the flag.Value for the --vmodule flag.
type vModuleFlag struct{ *verboseFlags }
func (f vModuleFlag) String() string {
// Do not panic on the zero value.
// https://groups.google.com/g/golang-nuts/c/Atlr8uAjn6U/m/iId17Td5BQAJ.
if f.verboseFlags == nil {
return ""
}
f.mu.Lock()
defer f.mu.Unlock()
var b bytes.Buffer
for i, f := range f.module {
if i > 0 {
b.WriteRune(',')
}
fmt.Fprintf(&b, "%s=%d", f.pattern, f.level)
}
return b.String()
}
// Get returns nil for this flag type since the struct is not exported.
func (f vModuleFlag) Get() any { return nil }
var errVmoduleSyntax = errors.New("syntax error: expect comma-separated list of filename=N")
// Syntax: -vmodule=recordio=2,foo/bar/baz=1,gfs*=3
func (f vModuleFlag) Set(value string) error {
var filter []modulePat
for _, pat := range strings.Split(value, ",") {
if len(pat) == 0 {
// Empty strings such as from a trailing comma can be ignored.
continue
}
patLev := strings.Split(pat, "=")
if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 {
return errVmoduleSyntax
}
pattern := patLev[0]
v, err := strconv.Atoi(patLev[1])
if err != nil {
return errors.New("syntax error: expect comma-separated list of filename=N")
}
// TODO: check syntax of filter?
filter = append(filter, modulePat{pattern, isLiteral(pattern), isFull(pattern), Level(v)})
}
f.mu.Lock()
defer f.mu.Unlock()
f.module = filter
atomic.StoreInt32((*int32)(&f.moduleLength), int32(len(f.module)))
f.moduleLevelCache.Store(&sync.Map{})
return nil
}
func (f *verboseFlags) levelForPC(pc uintptr) Level {
if level, ok := f.moduleLevelCache.Load().(*sync.Map).Load(pc); ok {
return level.(Level)
}
f.mu.Lock()
defer f.mu.Unlock()
level := Level(f.v)
fn := runtime.FuncForPC(pc)
file, _ := fn.FileLine(pc)
// The file is something like /a/b/c/d.go. We want just the d for
// regular matches, /a/b/c/d for full matches.
file = strings.TrimSuffix(file, ".go")
full := file
if slash := strings.LastIndex(file, "/"); slash >= 0 {
file = file[slash+1:]
}
for _, filter := range f.module {
if filter.match(full, file) {
level = filter.level
break // Use the first matching level.
}
}
f.moduleLevelCache.Load().(*sync.Map).Store(pc, level)
return level
}
func (f *verboseFlags) enabled(callerDepth int, level Level) bool {
if atomic.LoadInt32(&f.moduleLength) == 0 {
// No vmodule values specified, so compare against v level.
return Level(atomic.LoadInt32((*int32)(&f.v))) >= level
}
pcs := [1]uintptr{}
if runtime.Callers(callerDepth+2, pcs[:]) < 1 {
return false
}
frame, _ := runtime.CallersFrames(pcs[:]).Next()
return f.levelForPC(frame.Entry) >= level
}
// traceLocation represents an entry in the -log_backtrace_at flag.
type traceLocation struct {
file string
line int
}
var errTraceSyntax = errors.New("syntax error: expect file.go:234")
func parseTraceLocation(value string) (traceLocation, error) {
fields := strings.Split(value, ":")
if len(fields) != 2 {
return traceLocation{}, errTraceSyntax
}
file, lineStr := fields[0], fields[1]
if !strings.Contains(file, ".") {
return traceLocation{}, errTraceSyntax
}
line, err := strconv.Atoi(lineStr)
if err != nil {
return traceLocation{}, errTraceSyntax
}
if line < 0 {
return traceLocation{}, errors.New("negative value for line")
}
return traceLocation{file, line}, nil
}
// match reports whether the specified file and line matches the trace location.
// The argument file name is the full path, not the basename specified in the flag.
func (t traceLocation) match(file string, line int) bool {
if t.line != line {
return false
}
if i := strings.LastIndex(file, "/"); i >= 0 {
file = file[i+1:]
}
return t.file == file
}
func (t traceLocation) String() string {
return fmt.Sprintf("%s:%d", t.file, t.line)
}
// traceLocations represents the -log_backtrace_at flag.
// Syntax: -log_backtrace_at=recordio.go:234,sstable.go:456
// Note that unlike vmodule the file extension is included here.
type traceLocations struct {
mu sync.Mutex
locsLen int32 // Safe for atomic read without mu.
locs []traceLocation
}
func (t *traceLocations) String() string {
t.mu.Lock()
defer t.mu.Unlock()
var buf bytes.Buffer
for i, tl := range t.locs {
if i > 0 {
buf.WriteString(",")
}
buf.WriteString(tl.String())
}
return buf.String()
}
// Get always returns nil for this flag type since the struct is not exported
func (t *traceLocations) Get() any { return nil }
func (t *traceLocations) Set(value string) error {
var locs []traceLocation
for _, s := range strings.Split(value, ",") {
if s == "" {
continue
}
loc, err := parseTraceLocation(s)
if err != nil {
return err
}
locs = append(locs, loc)
}
t.mu.Lock()
defer t.mu.Unlock()
atomic.StoreInt32(&t.locsLen, int32(len(locs)))
t.locs = locs
return nil
}
func (t *traceLocations) match(file string, line int) bool {
if atomic.LoadInt32(&t.locsLen) == 0 {
return false
}
t.mu.Lock()
defer t.mu.Unlock()
for _, tl := range t.locs {
if tl.match(file, line) {
return true
}
}
return false
}
// severityFlag is an atomic flag.Value implementation for logsink.Severity.
type severityFlag int32
func (s *severityFlag) get() logsink.Severity {
return logsink.Severity(atomic.LoadInt32((*int32)(s)))
}
func (s *severityFlag) String() string { return strconv.FormatInt(int64(*s), 10) }
func (s *severityFlag) Get() any { return s.get() }
func (s *severityFlag) Set(value string) error {
threshold, err := logsink.ParseSeverity(value)
if err != nil {
// Not a severity name. Try a raw number.
v, err := strconv.Atoi(value)
if err != nil {
return err
}
threshold = logsink.Severity(v)
if threshold < logsink.Info || threshold > logsink.Fatal {
return fmt.Errorf("Severity %d out of range (min %d, max %d).", v, logsink.Info, logsink.Fatal)
}
}
atomic.StoreInt32((*int32)(s), int32(threshold))
return nil
}
var (
vflags verboseFlags // The -v and -vmodule flags.
logBacktraceAt traceLocations // The -log_backtrace_at flag.
// Boolean flags. Not handled atomically because the flag.Value interface
// does not let us avoid the =true, and that shorthand is necessary for
// compatibility. TODO: does this matter enough to fix? Seems unlikely.
toStderr bool // The -logtostderr flag.
alsoToStderr bool // The -alsologtostderr flag.
stderrThreshold severityFlag // The -stderrthreshold flag.
)
// verboseEnabled returns whether the caller at the given depth should emit
// verbose logs at the given level, with depth 0 identifying the caller of
// verboseEnabled.
func verboseEnabled(callerDepth int, level Level) bool {
return vflags.enabled(callerDepth+1, level)
}
// backtraceAt returns whether the logging call at the given function and line
// should also emit a backtrace of the current call stack.
func backtraceAt(file string, line int) bool {
return logBacktraceAt.match(file, line)
}
func init() {
vflags.moduleLevelCache.Store(&sync.Map{})
flag.Var(&vflags.v, "v", "log level for V logs")
flag.Var(vModuleFlag{&vflags}, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
flag.Var(&logBacktraceAt, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")
stderrThreshold = severityFlag(logsink.Error)
flag.BoolVar(&toStderr, "logtostderr", false, "log to standard error instead of files")
flag.BoolVar(&alsoToStderr, "alsologtostderr", false, "log to standard error as well as files")
flag.Var(&stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
}
|