pax_global_header00006660000000000000000000000064131627514040014515gustar00rootroot0000000000000052 comment=787959303086f44a8c361240dfac53d3e9d53ed2 tracerx-787959303086f44a8c361240dfac53d3e9d53ed2/000077500000000000000000000000001316275140400177635ustar00rootroot00000000000000tracerx-787959303086f44a8c361240dfac53d3e9d53ed2/LICENSE000066400000000000000000000020631316275140400207710ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Scott Barron Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tracerx-787959303086f44a8c361240dfac53d3e9d53ed2/README.md000066400000000000000000000023061316275140400212430ustar00rootroot00000000000000# tracerx Tracerx is a simple tracing package that logs messages depending on environment variables. It is very much inspired by git's GIT_TRACE mechanism. [![GoDoc](https://godoc.org/github.com/rubyist/tracerx?status.svg)](https://godoc.org/github.com/rubyist/tracerx) ## Installation ``` go get github.com/rubyist/tracerx ``` ## Example ```go tracerx.DefaultKey = "FOO" tracerx.Printf("send message") tracerx.PrintfKey("BAR", "do a thing") t := time.Now() // Do some stuff tracerx.PerformanceSince("BAR", "command x", t) ``` This example will send tracing output based on the environment variables `FOO_TRACE` and `BAR_TRACE`. The values control where the tracing is output as follows: ``` unset, 0, or "false": no output 1, 2: stderr absolute path: output will be written to the file 3 - 10: output will be written to that file descriptor ``` If an associated `BAR_TRACE_PERFORMANCE` is set to 1 or "true", the `PerformanceSince` line will output timing information. Keys can also be disabled. See the GoDoc for full API documentation. ## Bugs, Issues, Feedback Right here on GitHub: [https://github.com/rubyist/tracerx](https://github.com/rubyist/tracerx) tracerx-787959303086f44a8c361240dfac53d3e9d53ed2/tracerx.go000066400000000000000000000077411316275140400217730ustar00rootroot00000000000000// Package tracerx implements a simple tracer function that uses environment // variables to control the output. It is a generalized package inspired by // git's GIT_TRACE mechanism. // // By default, tracerx will look for the TRACERX_TRACE environment variable. // The default can by changed by setting the DefaultKey. // // The values control where the tracing is output as follows: // unset, 0, or "false": no output // 1, 2: stderr // absolute path: output will be written to the file // 3 - 10: output will be written to that file descriptor // // By default, messages will be prefixed with "trace: ". This prefix can be // modified by setting Prefix. // // Each key can have an associated performance key, e.g. TRACERX_TRACE_PERFORMANCE. // If this key is 1 or "true" performance output will be written to the same output // as the tracing output. package tracerx import ( "fmt" "io" "os" "path/filepath" "strconv" "strings" "sync" "time" ) var ( DefaultKey = "TRACERX" Prefix = "trace: " tracers map[string]*tracer tracerLock sync.Mutex ) type tracer struct { enabled bool performance bool w io.Writer } // Printf writes a trace message for the DefaultKey func Printf(format string, args ...interface{}) { PrintfKey(DefaultKey, format, args...) } // PrintfKey writes a trace message for the given key func PrintfKey(key, format string, args ...interface{}) { tracer := getTracer(key) if tracer.enabled { fmt.Fprintf(tracer.w, time.Now().Format("15:04:05.000000 ")+Prefix+format+"\n", args...) return } } // PerformanceSince writes out the time since the given time, if // tracing for the default key is enabled and the performance key is set func PerformanceSince(what string, t time.Time) { PerformanceSinceKey(DefaultKey, what, t) } // PerformanceSince writes out the time since the given time, if // tracing for the given key is enabled and the performance key is set func PerformanceSinceKey(key, what string, t time.Time) { tracer := getTracer(key) if tracer.performance { since := time.Since(t) fmt.Fprintf(tracer.w, time.Now().Format("15:04:05.000000 ")+"performance %s: %.9f s\n", what, since.Seconds()) } } // Disable will disable tracing for the given key, regardless of // the environment variable func Disable(key string) { uppedKey := strings.ToUpper(key) if tracer, ok := tracers[uppedKey]; ok { tracer.enabled = false } } // Enable will enable tracing for the given key, regardless of // the environment variable func Enable(key string) { uppedKey := strings.ToUpper(key) if tracer, ok := tracers[uppedKey]; ok { tracer.enabled = true } } func getTracer(key string) *tracer { uppedKey := strings.ToUpper(key) tracerLock.Lock() tracer, ok := tracers[uppedKey] if !ok { tracer = initializeTracer(uppedKey) } tracerLock.Unlock() return tracer } func initializeTracer(key string) *tracer { if tracer, ok := tracers[key]; ok { return tracer // Someone else initialized while we were blocked } tracer := &tracer{false, false, os.Stderr} tracers[key] = tracer perf := os.Getenv(fmt.Sprintf("%s_TRACE_PERFORMANCE", key)) if perf == "1" || strings.ToLower(perf) == "true" { tracer.performance = true } trace := os.Getenv(fmt.Sprintf("%s_TRACE", key)) fd, err := strconv.Atoi(trace) if err != nil { // Not a number, it could be a path for a log file if filepath.IsAbs(trace) { tracerOut, err := os.OpenFile(trace, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { fmt.Fprintf(os.Stderr, "Could not open '%s' for tracing: %s\nDefaulting to tracing on stderr...\n", trace, err) tracerOut = os.Stderr } tracer.w = tracerOut tracer.enabled = true } else if strings.ToLower(trace) == "true" { tracer.enabled = true } } else { switch fd { case 0: case 1, 2: tracer.enabled = true default: tracer.w = os.NewFile(uintptr(fd), "trace") tracer.enabled = true } } return tracer } func init() { tracers = make(map[string]*tracer, 0) }