diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-11-16 16:34:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-16 16:34:27 +0000 |
commit | 226d25b5ee3194bf7c13457927c4d400e802daaf (patch) | |
tree | fefa6785e1acd7ed855afb8dc04881efd569a1a1 /internal | |
parent | 63b586b9559508baf7442c39db3327c91d37486c (diff) | |
parent | c62aa3e780da6f85f6e1ddd7628ab30ec9d92b53 (diff) | |
download | go-git-226d25b5ee3194bf7c13457927c4d400e802daaf.tar.gz |
Merge pull request #931 from aymanbagabas/trace-utils
utils: move trace to utils
Diffstat (limited to 'internal')
-rw-r--r-- | internal/trace/trace.go | 55 | ||||
-rw-r--r-- | internal/trace/trace_test.go | 95 |
2 files changed, 0 insertions, 150 deletions
diff --git a/internal/trace/trace.go b/internal/trace/trace.go deleted file mode 100644 index 3e15c5b..0000000 --- a/internal/trace/trace.go +++ /dev/null @@ -1,55 +0,0 @@ -package trace - -import ( - "fmt" - "log" - "os" - "sync/atomic" -) - -var ( - // logger is the logger to use for tracing. - logger = newLogger() - - // current is the targets that are enabled for tracing. - current atomic.Int32 -) - -func newLogger() *log.Logger { - return log.New(os.Stderr, "", log.Ltime|log.Lmicroseconds|log.Lshortfile) -} - -// Target is a tracing target. -type Target int32 - -const ( - // General traces general operations. - General Target = 1 << iota - - // Packet traces git packets. - Packet -) - -// SetTarget sets the tracing targets. -func SetTarget(target Target) { - current.Store(int32(target)) -} - -// SetLogger sets the logger to use for tracing. -func SetLogger(l *log.Logger) { - logger = l -} - -// Print prints the given message only if the target is enabled. -func (t Target) Print(args ...interface{}) { - if int32(t)¤t.Load() != 0 { - logger.Output(2, fmt.Sprint(args...)) // nolint: errcheck - } -} - -// Printf prints the given message only if the target is enabled. -func (t Target) Printf(format string, args ...interface{}) { - if int32(t)¤t.Load() != 0 { - logger.Output(2, fmt.Sprintf(format, args...)) // nolint: errcheck - } -} diff --git a/internal/trace/trace_test.go b/internal/trace/trace_test.go deleted file mode 100644 index 6f8f140..0000000 --- a/internal/trace/trace_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package trace - -import ( - "bytes" - "io" - "log" - "testing" -) - -func TestMain(m *testing.M) { - defer SetLogger(newLogger()) - if code := m.Run(); code != 0 { - panic(code) - } -} - -func setUpTest(t testing.TB, buf *bytes.Buffer) { - t.Cleanup(func() { - if buf != nil { - buf.Reset() - } - SetTarget(0) - }) - w := io.Discard - if buf != nil { - w = buf - } - SetLogger(log.New(w, "", 0)) -} - -func TestEmpty(t *testing.T) { - var buf bytes.Buffer - setUpTest(t, &buf) - General.Print("test") - if buf.String() != "" { - t.Error("expected empty string") - } -} - -func TestOneTarget(t *testing.T) { - var buf bytes.Buffer - setUpTest(t, &buf) - SetTarget(General) - General.Print("test") - if buf.String() != "test\n" { - t.Error("expected 'test'") - } -} - -func TestMultipleTargets(t *testing.T) { - var buf bytes.Buffer - setUpTest(t, &buf) - SetTarget(General | Packet) - General.Print("a") - Packet.Print("b") - if buf.String() != "a\nb\n" { - t.Error("expected 'a\nb\n'") - } -} - -func TestPrintf(t *testing.T) { - var buf bytes.Buffer - setUpTest(t, &buf) - SetTarget(General) - General.Printf("a %d", 1) - if buf.String() != "a 1\n" { - t.Error("expected 'a 1\n'") - } -} - -func TestDisabledMultipleTargets(t *testing.T) { - var buf bytes.Buffer - setUpTest(t, &buf) - SetTarget(General) - General.Print("a") - Packet.Print("b") - if buf.String() != "a\n" { - t.Error("expected 'a\n'") - } -} - -func BenchmarkDisabledTarget(b *testing.B) { - setUpTest(b, nil) - for i := 0; i < b.N; i++ { - General.Print("test") - } -} - -func BenchmarkEnabledTarget(b *testing.B) { - setUpTest(b, nil) - SetTarget(General) - for i := 0; i < b.N; i++ { - General.Print("test") - } -} |