try this again

This commit is contained in:
rubyist 2014-10-04 12:59:35 -04:00
parent d73ac37433
commit 084655b60b
3 changed files with 185 additions and 0 deletions

@ -0,0 +1,22 @@
(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.

@ -0,0 +1,38 @@
# 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")
```
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
```
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)

@ -0,0 +1,125 @@
// 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.
//
package tracerx
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
var (
DefaultKey = "TRACERX"
Prefix = "trace: "
tracers map[string]*tracer
tracerLock sync.Mutex
)
type tracer struct {
enabled 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{}) {
uppedKey := strings.ToUpper(key)
tracer, ok := tracers[uppedKey]
if !ok {
tracer = initializeTracer(uppedKey)
}
if tracer.enabled {
fmt.Fprintf(tracer.w, Prefix+format+"\n", args...)
return
}
}
// 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 initializeTracer(key string) *tracer {
tracerLock.Lock()
defer tracerLock.Unlock()
if tracer, ok := tracers[key]; ok {
return tracer
}
tracer := &tracer{false, os.Stderr}
tracers[key] = tracer
trace := os.Getenv(fmt.Sprintf("%s_TRACE", key))
if trace == "" || strings.ToLower(trace) == "false" {
return tracer
}
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)
}