diff options
author | Paulo Gomes <pjbgf@linux.com> | 2023-11-15 22:01:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-15 22:01:03 +0000 |
commit | 63b586b9559508baf7442c39db3327c91d37486c (patch) | |
tree | ddb2dbc15aaae84cad245a777282ab6f88517c35 /internal/trace/trace.go | |
parent | eb1b04dbe45f7c9e866eb52e8ccccf3885a06217 (diff) | |
parent | c3843453da23043b4a4dcb9f7968f72c1e4ae8cc (diff) | |
download | go-git-63b586b9559508baf7442c39db3327c91d37486c.tar.gz |
Merge pull request #916 from aymanbagabas/trace
git: add tracer package
Diffstat (limited to 'internal/trace/trace.go')
-rw-r--r-- | internal/trace/trace.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/trace/trace.go b/internal/trace/trace.go new file mode 100644 index 0000000..3e15c5b --- /dev/null +++ b/internal/trace/trace.go @@ -0,0 +1,55 @@ +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 + } +} |