glog_flags.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Go support for leveled logs, analogous to https://github.com/google/glog.
  2. //
  3. // Copyright 2023 Google Inc. All Rights Reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package glog
  17. import (
  18. "bytes"
  19. "errors"
  20. "flag"
  21. "fmt"
  22. "path/filepath"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "sync/atomic"
  28. "github.com/golang/glog/internal/logsink"
  29. )
  30. // modulePat contains a filter for the -vmodule flag.
  31. // It holds a verbosity level and a file pattern to match.
  32. type modulePat struct {
  33. pattern string
  34. literal bool // The pattern is a literal string
  35. full bool // The pattern wants to match the full path
  36. level Level
  37. }
  38. // match reports whether the file matches the pattern. It uses a string
  39. // comparison if the pattern contains no metacharacters.
  40. func (m *modulePat) match(full, file string) bool {
  41. if m.literal {
  42. if m.full {
  43. return full == m.pattern
  44. }
  45. return file == m.pattern
  46. }
  47. if m.full {
  48. match, _ := filepath.Match(m.pattern, full)
  49. return match
  50. }
  51. match, _ := filepath.Match(m.pattern, file)
  52. return match
  53. }
  54. // isLiteral reports whether the pattern is a literal string, that is, has no metacharacters
  55. // that require filepath.Match to be called to match the pattern.
  56. func isLiteral(pattern string) bool {
  57. return !strings.ContainsAny(pattern, `\*?[]`)
  58. }
  59. // isFull reports whether the pattern matches the full file path, that is,
  60. // whether it contains /.
  61. func isFull(pattern string) bool {
  62. return strings.ContainsRune(pattern, '/')
  63. }
  64. // verboseFlags represents the setting of the -v and -vmodule flags.
  65. type verboseFlags struct {
  66. // moduleLevelCache is a sync.Map storing the -vmodule Level for each V()
  67. // call site, identified by PC. If there is no matching -vmodule filter,
  68. // the cached value is exactly v. moduleLevelCache is replaced with a new
  69. // Map whenever the -vmodule or -v flag changes state.
  70. moduleLevelCache atomic.Value
  71. // mu guards all fields below.
  72. mu sync.Mutex
  73. // v stores the value of the -v flag. It may be read safely using
  74. // sync.LoadInt32, but is only modified under mu.
  75. v Level
  76. // module stores the parsed -vmodule flag.
  77. module []modulePat
  78. // moduleLength caches len(module). If greater than zero, it
  79. // means vmodule is enabled. It may be read safely using sync.LoadInt32, but
  80. // is only modified under mu.
  81. moduleLength int32
  82. }
  83. // NOTE: For compatibility with the open-sourced v1 version of this
  84. // package (github.com/golang/glog) we need to retain that flag.Level
  85. // implements the flag.Value interface. See also go/log-vs-glog.
  86. // String is part of the flag.Value interface.
  87. func (l *Level) String() string {
  88. return strconv.FormatInt(int64(l.Get().(Level)), 10)
  89. }
  90. // Get is part of the flag.Value interface.
  91. func (l *Level) Get() any {
  92. if l == &vflags.v {
  93. // l is the value registered for the -v flag.
  94. return Level(atomic.LoadInt32((*int32)(l)))
  95. }
  96. return *l
  97. }
  98. // Set is part of the flag.Value interface.
  99. func (l *Level) Set(value string) error {
  100. v, err := strconv.Atoi(value)
  101. if err != nil {
  102. return err
  103. }
  104. if l == &vflags.v {
  105. // l is the value registered for the -v flag.
  106. vflags.mu.Lock()
  107. defer vflags.mu.Unlock()
  108. vflags.moduleLevelCache.Store(&sync.Map{})
  109. atomic.StoreInt32((*int32)(l), int32(v))
  110. return nil
  111. }
  112. *l = Level(v)
  113. return nil
  114. }
  115. // vModuleFlag is the flag.Value for the --vmodule flag.
  116. type vModuleFlag struct{ *verboseFlags }
  117. func (f vModuleFlag) String() string {
  118. // Do not panic on the zero value.
  119. // https://groups.google.com/g/golang-nuts/c/Atlr8uAjn6U/m/iId17Td5BQAJ.
  120. if f.verboseFlags == nil {
  121. return ""
  122. }
  123. f.mu.Lock()
  124. defer f.mu.Unlock()
  125. var b bytes.Buffer
  126. for i, f := range f.module {
  127. if i > 0 {
  128. b.WriteRune(',')
  129. }
  130. fmt.Fprintf(&b, "%s=%d", f.pattern, f.level)
  131. }
  132. return b.String()
  133. }
  134. // Get returns nil for this flag type since the struct is not exported.
  135. func (f vModuleFlag) Get() any { return nil }
  136. var errVmoduleSyntax = errors.New("syntax error: expect comma-separated list of filename=N")
  137. // Syntax: -vmodule=recordio=2,foo/bar/baz=1,gfs*=3
  138. func (f vModuleFlag) Set(value string) error {
  139. var filter []modulePat
  140. for _, pat := range strings.Split(value, ",") {
  141. if len(pat) == 0 {
  142. // Empty strings such as from a trailing comma can be ignored.
  143. continue
  144. }
  145. patLev := strings.Split(pat, "=")
  146. if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 {
  147. return errVmoduleSyntax
  148. }
  149. pattern := patLev[0]
  150. v, err := strconv.Atoi(patLev[1])
  151. if err != nil {
  152. return errors.New("syntax error: expect comma-separated list of filename=N")
  153. }
  154. // TODO: check syntax of filter?
  155. filter = append(filter, modulePat{pattern, isLiteral(pattern), isFull(pattern), Level(v)})
  156. }
  157. f.mu.Lock()
  158. defer f.mu.Unlock()
  159. f.module = filter
  160. atomic.StoreInt32((*int32)(&f.moduleLength), int32(len(f.module)))
  161. f.moduleLevelCache.Store(&sync.Map{})
  162. return nil
  163. }
  164. func (f *verboseFlags) levelForPC(pc uintptr) Level {
  165. if level, ok := f.moduleLevelCache.Load().(*sync.Map).Load(pc); ok {
  166. return level.(Level)
  167. }
  168. f.mu.Lock()
  169. defer f.mu.Unlock()
  170. level := Level(f.v)
  171. fn := runtime.FuncForPC(pc)
  172. file, _ := fn.FileLine(pc)
  173. // The file is something like /a/b/c/d.go. We want just the d for
  174. // regular matches, /a/b/c/d for full matches.
  175. file = strings.TrimSuffix(file, ".go")
  176. full := file
  177. if slash := strings.LastIndex(file, "/"); slash >= 0 {
  178. file = file[slash+1:]
  179. }
  180. for _, filter := range f.module {
  181. if filter.match(full, file) {
  182. level = filter.level
  183. break // Use the first matching level.
  184. }
  185. }
  186. f.moduleLevelCache.Load().(*sync.Map).Store(pc, level)
  187. return level
  188. }
  189. func (f *verboseFlags) enabled(callerDepth int, level Level) bool {
  190. if atomic.LoadInt32(&f.moduleLength) == 0 {
  191. // No vmodule values specified, so compare against v level.
  192. return Level(atomic.LoadInt32((*int32)(&f.v))) >= level
  193. }
  194. pcs := [1]uintptr{}
  195. if runtime.Callers(callerDepth+2, pcs[:]) < 1 {
  196. return false
  197. }
  198. frame, _ := runtime.CallersFrames(pcs[:]).Next()
  199. return f.levelForPC(frame.Entry) >= level
  200. }
  201. // traceLocation represents an entry in the -log_backtrace_at flag.
  202. type traceLocation struct {
  203. file string
  204. line int
  205. }
  206. var errTraceSyntax = errors.New("syntax error: expect file.go:234")
  207. func parseTraceLocation(value string) (traceLocation, error) {
  208. fields := strings.Split(value, ":")
  209. if len(fields) != 2 {
  210. return traceLocation{}, errTraceSyntax
  211. }
  212. file, lineStr := fields[0], fields[1]
  213. if !strings.Contains(file, ".") {
  214. return traceLocation{}, errTraceSyntax
  215. }
  216. line, err := strconv.Atoi(lineStr)
  217. if err != nil {
  218. return traceLocation{}, errTraceSyntax
  219. }
  220. if line < 0 {
  221. return traceLocation{}, errors.New("negative value for line")
  222. }
  223. return traceLocation{file, line}, nil
  224. }
  225. // match reports whether the specified file and line matches the trace location.
  226. // The argument file name is the full path, not the basename specified in the flag.
  227. func (t traceLocation) match(file string, line int) bool {
  228. if t.line != line {
  229. return false
  230. }
  231. if i := strings.LastIndex(file, "/"); i >= 0 {
  232. file = file[i+1:]
  233. }
  234. return t.file == file
  235. }
  236. func (t traceLocation) String() string {
  237. return fmt.Sprintf("%s:%d", t.file, t.line)
  238. }
  239. // traceLocations represents the -log_backtrace_at flag.
  240. // Syntax: -log_backtrace_at=recordio.go:234,sstable.go:456
  241. // Note that unlike vmodule the file extension is included here.
  242. type traceLocations struct {
  243. mu sync.Mutex
  244. locsLen int32 // Safe for atomic read without mu.
  245. locs []traceLocation
  246. }
  247. func (t *traceLocations) String() string {
  248. t.mu.Lock()
  249. defer t.mu.Unlock()
  250. var buf bytes.Buffer
  251. for i, tl := range t.locs {
  252. if i > 0 {
  253. buf.WriteString(",")
  254. }
  255. buf.WriteString(tl.String())
  256. }
  257. return buf.String()
  258. }
  259. // Get always returns nil for this flag type since the struct is not exported
  260. func (t *traceLocations) Get() any { return nil }
  261. func (t *traceLocations) Set(value string) error {
  262. var locs []traceLocation
  263. for _, s := range strings.Split(value, ",") {
  264. if s == "" {
  265. continue
  266. }
  267. loc, err := parseTraceLocation(s)
  268. if err != nil {
  269. return err
  270. }
  271. locs = append(locs, loc)
  272. }
  273. t.mu.Lock()
  274. defer t.mu.Unlock()
  275. atomic.StoreInt32(&t.locsLen, int32(len(locs)))
  276. t.locs = locs
  277. return nil
  278. }
  279. func (t *traceLocations) match(file string, line int) bool {
  280. if atomic.LoadInt32(&t.locsLen) == 0 {
  281. return false
  282. }
  283. t.mu.Lock()
  284. defer t.mu.Unlock()
  285. for _, tl := range t.locs {
  286. if tl.match(file, line) {
  287. return true
  288. }
  289. }
  290. return false
  291. }
  292. // severityFlag is an atomic flag.Value implementation for logsink.Severity.
  293. type severityFlag int32
  294. func (s *severityFlag) get() logsink.Severity {
  295. return logsink.Severity(atomic.LoadInt32((*int32)(s)))
  296. }
  297. func (s *severityFlag) String() string { return strconv.FormatInt(int64(*s), 10) }
  298. func (s *severityFlag) Get() any { return s.get() }
  299. func (s *severityFlag) Set(value string) error {
  300. threshold, err := logsink.ParseSeverity(value)
  301. if err != nil {
  302. // Not a severity name. Try a raw number.
  303. v, err := strconv.Atoi(value)
  304. if err != nil {
  305. return err
  306. }
  307. threshold = logsink.Severity(v)
  308. if threshold < logsink.Info || threshold > logsink.Fatal {
  309. return fmt.Errorf("Severity %d out of range (min %d, max %d).", v, logsink.Info, logsink.Fatal)
  310. }
  311. }
  312. atomic.StoreInt32((*int32)(s), int32(threshold))
  313. return nil
  314. }
  315. var (
  316. vflags verboseFlags // The -v and -vmodule flags.
  317. logBacktraceAt traceLocations // The -log_backtrace_at flag.
  318. // Boolean flags. Not handled atomically because the flag.Value interface
  319. // does not let us avoid the =true, and that shorthand is necessary for
  320. // compatibility. TODO: does this matter enough to fix? Seems unlikely.
  321. toStderr bool // The -logtostderr flag.
  322. alsoToStderr bool // The -alsologtostderr flag.
  323. stderrThreshold severityFlag // The -stderrthreshold flag.
  324. )
  325. // verboseEnabled returns whether the caller at the given depth should emit
  326. // verbose logs at the given level, with depth 0 identifying the caller of
  327. // verboseEnabled.
  328. func verboseEnabled(callerDepth int, level Level) bool {
  329. return vflags.enabled(callerDepth+1, level)
  330. }
  331. // backtraceAt returns whether the logging call at the given function and line
  332. // should also emit a backtrace of the current call stack.
  333. func backtraceAt(file string, line int) bool {
  334. return logBacktraceAt.match(file, line)
  335. }
  336. func init() {
  337. vflags.moduleLevelCache.Store(&sync.Map{})
  338. flag.Var(&vflags.v, "v", "log level for V logs")
  339. flag.Var(vModuleFlag{&vflags}, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
  340. flag.Var(&logBacktraceAt, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")
  341. stderrThreshold = severityFlag(logsink.Error)
  342. flag.BoolVar(&toStderr, "logtostderr", false, "log to standard error instead of files")
  343. flag.BoolVar(&alsoToStderr, "alsologtostderr", false, "log to standard error as well as files")
  344. flag.Var(&stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
  345. }