aboutsummaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2023-05-04 18:47:54 +0200
committerAleksandr Nogikh <wp32pw@gmail.com>2023-05-05 12:18:48 +0200
commitc38192e51d98bb4767c06ba3e025faa4c5161a0d (patch)
tree9bc22395c381887b4667548695924159d57014fa /pkg
parent3871d0dcfa5d260a1c6e8f190ee9ebaa908ab3aa (diff)
pkg/log: facilitate automated log capture
Update the log format to include two more parameters: 1) The name of the instance. 2) The severity of the problem. This should simplify automated processing of syz-manager logs.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/log/log.go54
1 files changed, 40 insertions, 14 deletions
diff --git a/pkg/log/log.go b/pkg/log/log.go
index 935b0d1cd..2a2fd6481 100644
--- a/pkg/log/log.go
+++ b/pkg/log/log.go
@@ -13,6 +13,7 @@ import (
"flag"
"fmt"
golog "log"
+ "strings"
"sync"
"time"
)
@@ -24,6 +25,7 @@ var (
cacheMaxMem int
cachePos int
cacheEntries []string
+ instanceName string
prependTime = true // for testing
)
@@ -59,6 +61,11 @@ func CachedLogOutput() string {
return buf.String()
}
+// If the name is set, it will be displayed for all logs.
+func SetName(name string) {
+ instanceName = name
+}
+
// V reports whether verbosity at the call site is at least the requested level.
// See https://pkg.go.dev/github.com/golang/glog#V for details.
func V(level int) bool {
@@ -66,17 +73,44 @@ func V(level int) bool {
}
func Logf(v int, msg string, args ...interface{}) {
+ writeMessage(v, "", msg, args...)
+}
+
+func Errorf(msg string, args ...interface{}) {
+ writeMessage(0, "ERROR", msg, args...)
+}
+
+func Fatal(err error) {
+ golog.Fatal("SYZFATAL: ", err)
+}
+
+func Fatalf(msg string, args ...interface{}) {
+ golog.Fatalf("SYZFATAL: "+msg, args...)
+}
+
+func writeMessage(v int, severity, msg string, args ...interface{}) {
+ var sb strings.Builder
+ if prependTime {
+ sb.WriteString(time.Now().Format("2006/01/02 15:04:05 "))
+ }
+ if severity != "" {
+ fmt.Fprintf(&sb, "[%s] ", severity)
+ }
+ if instanceName != "" {
+ fmt.Fprintf(&sb, "%s: ", instanceName)
+ }
+ fmt.Fprintf(&sb, msg, args...)
+ writeRawMessage(v, sb.String())
+}
+
+func writeRawMessage(v int, msg string) {
mu.Lock()
if cacheEntries != nil && v <= 1 {
cacheMem -= len(cacheEntries[cachePos])
if cacheMem < 0 {
panic("log cache size underflow")
}
- timeStr := ""
- if prependTime {
- timeStr = time.Now().Format("2006/01/02 15:04:05 ")
- }
- cacheEntries[cachePos] = fmt.Sprintf(timeStr+msg, args...)
+ cacheEntries[cachePos] = msg
cacheMem += len(cacheEntries[cachePos])
cachePos++
if cachePos == len(cacheEntries) {
@@ -94,18 +128,10 @@ func Logf(v int, msg string, args ...interface{}) {
mu.Unlock()
if V(v) {
- golog.Printf(msg, args...)
+ golog.Print(msg)
}
}
-func Fatal(err error) {
- golog.Fatal("SYZFATAL: ", err)
-}
-
-func Fatalf(msg string, args ...interface{}) {
- golog.Fatalf("SYZFATAL: "+msg, args...)
-}
-
type VerboseWriter int
func (w VerboseWriter) Write(data []byte) (int, error) {