From 23b94422d32946ab68a4f1423274bf4daa33cef9 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 1 Jun 2017 18:52:11 +0200 Subject: pkg/log: move from log --- db/db.go | 2 +- log/log.go | 98 ------------------------------------------ log/log_test.go | 32 -------------- pkg/log/log.go | 98 ++++++++++++++++++++++++++++++++++++++++++ pkg/log/log_test.go | 32 ++++++++++++++ repro/repro.go | 2 +- rpctype/rpc.go | 2 +- syz-fuzzer/fuzzer.go | 2 +- syz-gce/http.go | 2 +- syz-gce/syz-gce.go | 2 +- syz-hub/http.go | 2 +- syz-hub/hub.go | 2 +- syz-hub/state/state.go | 2 +- syz-manager/cover.go | 2 +- syz-manager/html.go | 2 +- syz-manager/manager.go | 2 +- syz-manager/persistent.go | 2 +- tools/syz-crush/crush.go | 2 +- tools/syz-execprog/execprog.go | 2 +- tools/syz-repro/repro.go | 2 +- tools/syz-stress/stress.go | 2 +- vm/adb/adb.go | 2 +- vm/gce/gce.go | 2 +- vm/odroid/odroid.go | 2 +- vm/qemu/qemu.go | 2 +- 25 files changed, 151 insertions(+), 151 deletions(-) delete mode 100644 log/log.go delete mode 100644 log/log_test.go create mode 100644 pkg/log/log.go create mode 100644 pkg/log/log_test.go diff --git a/db/db.go b/db/db.go index 1bd654782..0277cb3d1 100644 --- a/db/db.go +++ b/db/db.go @@ -18,7 +18,7 @@ import ( "io/ioutil" "os" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" ) type DB struct { diff --git a/log/log.go b/log/log.go deleted file mode 100644 index d57e8c1ad..000000000 --- a/log/log.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2016 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -// log package provides functionality similar to standard log package with some extensions: -// - verbosity levels -// - global verbosity setting that can be used by multiple packages -// - ability to disable all output -// - ability to cache recent output in memory -package log - -import ( - "bytes" - "flag" - "fmt" - golog "log" - "sync" - "time" -) - -var ( - flagV = flag.Int("v", 0, "verbosity") - mu sync.Mutex - cacheMem int - cacheMaxMem int - cachePos int - cacheEntries []string - prependTime = true // for testing -) - -// EnableCaching enables in memory caching of log output. -// Caches up to maxLines, but no more than maxMem bytes. -// Cached output can later be queried with CachedOutput. -func EnableLogCaching(maxLines, maxMem int) { - mu.Lock() - defer mu.Unlock() - if cacheEntries != nil { - Fatalf("log caching is already enabled") - } - if maxLines < 1 || maxMem < 1 { - panic("invalid maxLines/maxMem") - } - cacheMaxMem = maxMem - cacheEntries = make([]string, maxLines) -} - -// Retrieves cached log output. -func CachedLogOutput() string { - mu.Lock() - defer mu.Unlock() - buf := new(bytes.Buffer) - for i := range cacheEntries { - pos := (cachePos + i) % len(cacheEntries) - if cacheEntries[pos] == "" { - continue - } - buf.WriteString(cacheEntries[pos]) - buf.Write([]byte{'\n'}) - } - return buf.String() -} - -func Logf(v int, msg string, args ...interface{}) { - mu.Lock() - doLog := v <= *flagV - 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...) - cacheMem += len(cacheEntries[cachePos]) - cachePos++ - if cachePos == len(cacheEntries) { - cachePos = 0 - } - for i := 0; i < len(cacheEntries)-1 && cacheMem > cacheMaxMem; i++ { - pos := (cachePos + i) % len(cacheEntries) - cacheMem -= len(cacheEntries[pos]) - cacheEntries[pos] = "" - } - if cacheMem < 0 { - panic("log cache size underflow") - } - } - mu.Unlock() - - if doLog { - golog.Printf(msg, args...) - } -} - -func Fatalf(msg string, args ...interface{}) { - golog.Fatalf(msg, args...) -} diff --git a/log/log_test.go b/log/log_test.go deleted file mode 100644 index cb40aceab..000000000 --- a/log/log_test.go +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright 2016 syzkaller project authors. All rights reserved. -// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. - -package log - -import ( - "testing" -) - -func TestCaching(t *testing.T) { - tests := []struct{ str, want string }{ - {"", ""}, - {"a", "a\n"}, - {"bb", "a\nbb\n"}, - {"ccc", "a\nbb\nccc\n"}, - {"dddd", "a\nbb\nccc\ndddd\n"}, - {"eeeee", "bb\nccc\ndddd\neeeee\n"}, - {"ffffff", "ccc\ndddd\neeeee\nffffff\n"}, - {"ggggggg", "eeeee\nffffff\nggggggg\n"}, - {"hhhhhhhh", "ggggggg\nhhhhhhhh\n"}, - {"jjjjjjjjjjjjjjjjjjjjjjjjj", "jjjjjjjjjjjjjjjjjjjjjjjjj\n"}, - } - EnableLogCaching(4, 20) - prependTime = false - for _, test := range tests { - Logf(1, test.str) - out := CachedLogOutput() - if out != test.want { - t.Fatalf("wrote: %v\nwant: %v\ngot: %v", test.str, test.want, out) - } - } -} diff --git a/pkg/log/log.go b/pkg/log/log.go new file mode 100644 index 000000000..d57e8c1ad --- /dev/null +++ b/pkg/log/log.go @@ -0,0 +1,98 @@ +// Copyright 2016 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +// log package provides functionality similar to standard log package with some extensions: +// - verbosity levels +// - global verbosity setting that can be used by multiple packages +// - ability to disable all output +// - ability to cache recent output in memory +package log + +import ( + "bytes" + "flag" + "fmt" + golog "log" + "sync" + "time" +) + +var ( + flagV = flag.Int("v", 0, "verbosity") + mu sync.Mutex + cacheMem int + cacheMaxMem int + cachePos int + cacheEntries []string + prependTime = true // for testing +) + +// EnableCaching enables in memory caching of log output. +// Caches up to maxLines, but no more than maxMem bytes. +// Cached output can later be queried with CachedOutput. +func EnableLogCaching(maxLines, maxMem int) { + mu.Lock() + defer mu.Unlock() + if cacheEntries != nil { + Fatalf("log caching is already enabled") + } + if maxLines < 1 || maxMem < 1 { + panic("invalid maxLines/maxMem") + } + cacheMaxMem = maxMem + cacheEntries = make([]string, maxLines) +} + +// Retrieves cached log output. +func CachedLogOutput() string { + mu.Lock() + defer mu.Unlock() + buf := new(bytes.Buffer) + for i := range cacheEntries { + pos := (cachePos + i) % len(cacheEntries) + if cacheEntries[pos] == "" { + continue + } + buf.WriteString(cacheEntries[pos]) + buf.Write([]byte{'\n'}) + } + return buf.String() +} + +func Logf(v int, msg string, args ...interface{}) { + mu.Lock() + doLog := v <= *flagV + 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...) + cacheMem += len(cacheEntries[cachePos]) + cachePos++ + if cachePos == len(cacheEntries) { + cachePos = 0 + } + for i := 0; i < len(cacheEntries)-1 && cacheMem > cacheMaxMem; i++ { + pos := (cachePos + i) % len(cacheEntries) + cacheMem -= len(cacheEntries[pos]) + cacheEntries[pos] = "" + } + if cacheMem < 0 { + panic("log cache size underflow") + } + } + mu.Unlock() + + if doLog { + golog.Printf(msg, args...) + } +} + +func Fatalf(msg string, args ...interface{}) { + golog.Fatalf(msg, args...) +} diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go new file mode 100644 index 000000000..cb40aceab --- /dev/null +++ b/pkg/log/log_test.go @@ -0,0 +1,32 @@ +// Copyright 2016 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package log + +import ( + "testing" +) + +func TestCaching(t *testing.T) { + tests := []struct{ str, want string }{ + {"", ""}, + {"a", "a\n"}, + {"bb", "a\nbb\n"}, + {"ccc", "a\nbb\nccc\n"}, + {"dddd", "a\nbb\nccc\ndddd\n"}, + {"eeeee", "bb\nccc\ndddd\neeeee\n"}, + {"ffffff", "ccc\ndddd\neeeee\nffffff\n"}, + {"ggggggg", "eeeee\nffffff\nggggggg\n"}, + {"hhhhhhhh", "ggggggg\nhhhhhhhh\n"}, + {"jjjjjjjjjjjjjjjjjjjjjjjjj", "jjjjjjjjjjjjjjjjjjjjjjjjj\n"}, + } + EnableLogCaching(4, 20) + prependTime = false + for _, test := range tests { + Logf(1, test.str) + out := CachedLogOutput() + if out != test.want { + t.Fatalf("wrote: %v\nwant: %v\ngot: %v", test.str, test.want, out) + } + } +} diff --git a/repro/repro.go b/repro/repro.go index c89a914f3..1cdd3efbe 100644 --- a/repro/repro.go +++ b/repro/repro.go @@ -12,8 +12,8 @@ import ( "time" "github.com/google/syzkaller/csource" - . "github.com/google/syzkaller/log" "github.com/google/syzkaller/pkg/fileutil" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/report" "github.com/google/syzkaller/syz-manager/config" diff --git a/rpctype/rpc.go b/rpctype/rpc.go index 44569a676..8400b0e95 100644 --- a/rpctype/rpc.go +++ b/rpctype/rpc.go @@ -9,7 +9,7 @@ import ( "net/rpc" "time" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" ) type RpcServer struct { diff --git a/syz-fuzzer/fuzzer.go b/syz-fuzzer/fuzzer.go index 6ab3ac631..6b2b0bc6d 100644 --- a/syz-fuzzer/fuzzer.go +++ b/syz-fuzzer/fuzzer.go @@ -24,8 +24,8 @@ import ( "github.com/google/syzkaller/cover" "github.com/google/syzkaller/host" "github.com/google/syzkaller/ipc" - . "github.com/google/syzkaller/log" "github.com/google/syzkaller/pkg/hash" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" . "github.com/google/syzkaller/rpctype" "github.com/google/syzkaller/sys" diff --git a/syz-gce/http.go b/syz-gce/http.go index 9ce0a4990..ed2a118a8 100644 --- a/syz-gce/http.go +++ b/syz-gce/http.go @@ -12,7 +12,7 @@ import ( "strings" "sync/atomic" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" ) func initHttp(addr string) { diff --git a/syz-gce/syz-gce.go b/syz-gce/syz-gce.go index bea3818dc..318887c50 100644 --- a/syz-gce/syz-gce.go +++ b/syz-gce/syz-gce.go @@ -38,10 +38,10 @@ import ( "github.com/google/syzkaller/dashboard" "github.com/google/syzkaller/gce" - . "github.com/google/syzkaller/log" pkgconfig "github.com/google/syzkaller/pkg/config" "github.com/google/syzkaller/pkg/gcs" "github.com/google/syzkaller/pkg/git" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/syz-manager/config" ) diff --git a/syz-hub/http.go b/syz-hub/http.go index b2fb0c12e..1dde8e98f 100644 --- a/syz-hub/http.go +++ b/syz-hub/http.go @@ -11,7 +11,7 @@ import ( "sort" "strings" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" ) func (hub *Hub) initHttp(addr string) { diff --git a/syz-hub/hub.go b/syz-hub/hub.go index 74a8740d9..1c237ddac 100644 --- a/syz-hub/hub.go +++ b/syz-hub/hub.go @@ -10,7 +10,7 @@ import ( "io/ioutil" "sync" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" . "github.com/google/syzkaller/rpctype" "github.com/google/syzkaller/syz-hub/state" ) diff --git a/syz-hub/state/state.go b/syz-hub/state/state.go index 6564050ac..90f294ac3 100644 --- a/syz-hub/state/state.go +++ b/syz-hub/state/state.go @@ -13,8 +13,8 @@ import ( "time" "github.com/google/syzkaller/db" - . "github.com/google/syzkaller/log" "github.com/google/syzkaller/pkg/hash" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" ) diff --git a/syz-manager/cover.go b/syz-manager/cover.go index e27224a47..94de3e22f 100644 --- a/syz-manager/cover.go +++ b/syz-manager/cover.go @@ -16,7 +16,7 @@ import ( "strings" "github.com/google/syzkaller/cover" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/symbolizer" ) diff --git a/syz-manager/html.go b/syz-manager/html.go index 952c7de40..204cab2da 100644 --- a/syz-manager/html.go +++ b/syz-manager/html.go @@ -20,7 +20,7 @@ import ( "time" "github.com/google/syzkaller/cover" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/sys" ) diff --git a/syz-manager/manager.go b/syz-manager/manager.go index 6d533eff8..912b2d4a2 100644 --- a/syz-manager/manager.go +++ b/syz-manager/manager.go @@ -23,8 +23,8 @@ import ( "github.com/google/syzkaller/csource" "github.com/google/syzkaller/dashboard" "github.com/google/syzkaller/db" - . "github.com/google/syzkaller/log" "github.com/google/syzkaller/pkg/hash" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/report" "github.com/google/syzkaller/repro" diff --git a/syz-manager/persistent.go b/syz-manager/persistent.go index d0cb5fe53..42561c01c 100644 --- a/syz-manager/persistent.go +++ b/syz-manager/persistent.go @@ -10,8 +10,8 @@ import ( "path/filepath" "github.com/google/syzkaller/db" - . "github.com/google/syzkaller/log" "github.com/google/syzkaller/pkg/hash" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" ) diff --git a/tools/syz-crush/crush.go b/tools/syz-crush/crush.go index f1796baf0..5a785a264 100644 --- a/tools/syz-crush/crush.go +++ b/tools/syz-crush/crush.go @@ -18,7 +18,7 @@ import ( "syscall" "time" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/syz-manager/config" "github.com/google/syzkaller/vm" _ "github.com/google/syzkaller/vm/adb" diff --git a/tools/syz-execprog/execprog.go b/tools/syz-execprog/execprog.go index 373bbf256..a6d746169 100644 --- a/tools/syz-execprog/execprog.go +++ b/tools/syz-execprog/execprog.go @@ -20,7 +20,7 @@ import ( "github.com/google/syzkaller/cover" "github.com/google/syzkaller/ipc" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" ) diff --git a/tools/syz-repro/repro.go b/tools/syz-repro/repro.go index 064846991..f984714a8 100644 --- a/tools/syz-repro/repro.go +++ b/tools/syz-repro/repro.go @@ -12,7 +12,7 @@ import ( "syscall" "github.com/google/syzkaller/csource" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/repro" "github.com/google/syzkaller/syz-manager/config" "github.com/google/syzkaller/vm" diff --git a/tools/syz-stress/stress.go b/tools/syz-stress/stress.go index 4cf8d931f..b67f95efa 100644 --- a/tools/syz-stress/stress.go +++ b/tools/syz-stress/stress.go @@ -16,7 +16,7 @@ import ( "github.com/google/syzkaller/db" "github.com/google/syzkaller/host" "github.com/google/syzkaller/ipc" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/prog" "github.com/google/syzkaller/sys" ) diff --git a/vm/adb/adb.go b/vm/adb/adb.go index 6e01869b8..59313c3cb 100644 --- a/vm/adb/adb.go +++ b/vm/adb/adb.go @@ -17,7 +17,7 @@ import ( "sync" "time" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/vm" ) diff --git a/vm/gce/gce.go b/vm/gce/gce.go index 364a44f29..dff6963ca 100644 --- a/vm/gce/gce.go +++ b/vm/gce/gce.go @@ -21,7 +21,7 @@ import ( "time" "github.com/google/syzkaller/gce" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/vm" ) diff --git a/vm/odroid/odroid.go b/vm/odroid/odroid.go index 59b3a577b..2277f0548 100644 --- a/vm/odroid/odroid.go +++ b/vm/odroid/odroid.go @@ -22,7 +22,7 @@ import ( "time" "unsafe" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/vm" ) diff --git a/vm/qemu/qemu.go b/vm/qemu/qemu.go index e0c5f6ad0..b0d677685 100644 --- a/vm/qemu/qemu.go +++ b/vm/qemu/qemu.go @@ -16,7 +16,7 @@ import ( "strings" "time" - . "github.com/google/syzkaller/log" + . "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/vm" ) -- cgit mrf-deployment