dendrite/vendor/src/github.com/tj/go-debug/debug.go
Robert Swain 2d202cec07 mediaapi: Add thumbnail support (#132)
* vendor: Add bimg image processing library

bimg is MIT licensed. It depends on the C library libvips which is LGPL
v2.1+ licensed. libvips must be installed separately.

* mediaapi: Add YAML config file support

* mediaapi: Add thumbnail support

* mediaapi: Add missing thumbnail files

* travis: Add ppa and install libvips-dev

* travis: Another ppa and install libvips-dev attempt

* travis: Add sudo: required for sudo apt* usage

* mediaapi/thumbnailer: Make comparison code more readable

* mediaapi: Simplify logging of thumbnail properties

* mediaapi/thumbnailer: Rename metrics to fitness

Metrics is used in the context of monitoring with Prometheus so renaming
to avoid confusion.

* mediaapi/thumbnailer: Use math.Inf() for max aspect and size

* mediaapi/thumbnailer: Limit number of parallel generators

Fall back to selecting from already-/pre-generated thumbnails or serving
the original.

* mediaapi/thumbnailer: Split bimg code into separate file

* vendor: Add github.com/nfnt/resize pure go image scaler

* mediaapi/thumbnailer: Add nfnt/resize thumbnailer

* travis: Don't install libvips-dev via ppa

* mediaapi: Add notes to README about resizers

* mediaapi: Elaborate on scaling libs in README
2017-06-07 01:12:49 +02:00

128 lines
2.5 KiB
Go

package debug
import (
"fmt"
"io"
"math/rand"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var (
writer io.Writer = os.Stderr
reg *regexp.Regexp
m sync.Mutex
enabled = false
)
// Debugger function.
type DebugFunction func(string, ...interface{})
// Terminal colors used at random.
var colors []string = []string{
"31",
"32",
"33",
"34",
"35",
"36",
}
// Initialize with DEBUG environment variable.
func init() {
env := os.Getenv("DEBUG")
if "" != env {
Enable(env)
}
}
// SetWriter replaces the default of os.Stderr with `w`.
func SetWriter(w io.Writer) {
m.Lock()
defer m.Unlock()
writer = w
}
// Disable all pattern matching. This function is thread-safe.
func Disable() {
m.Lock()
defer m.Unlock()
enabled = false
}
// Enable the given debug `pattern`. Patterns take a glob-like form,
// for example if you wanted to enable everything, just use "*", or
// if you had a library named mongodb you could use "mongodb:connection",
// or "mongodb:*". Multiple matches can be made with a comma, for
// example "mongo*,redis*".
//
// This function is thread-safe.
func Enable(pattern string) {
m.Lock()
defer m.Unlock()
pattern = regexp.QuoteMeta(pattern)
pattern = strings.Replace(pattern, "\\*", ".*?", -1)
pattern = strings.Replace(pattern, ",", "|", -1)
pattern = "^(" + pattern + ")$"
reg = regexp.MustCompile(pattern)
enabled = true
}
// Debug creates a debug function for `name` which you call
// with printf-style arguments in your application or library.
func Debug(name string) DebugFunction {
prevGlobal := time.Now()
color := colors[rand.Intn(len(colors))]
prev := time.Now()
return func(format string, args ...interface{}) {
if !enabled {
return
}
if !reg.MatchString(name) {
return
}
d := deltas(prevGlobal, prev, color)
fmt.Fprintf(writer, d+" \033["+color+"m"+name+"\033[0m - "+format+"\n", args...)
prevGlobal = time.Now()
prev = time.Now()
}
}
// Return formatting for deltas.
func deltas(prevGlobal, prev time.Time, color string) string {
now := time.Now()
global := now.Sub(prevGlobal).Nanoseconds()
delta := now.Sub(prev).Nanoseconds()
ts := now.UTC().Format("15:04:05.000")
deltas := fmt.Sprintf("%s %-6s \033["+color+"m%-6s", ts, humanizeNano(global), humanizeNano(delta))
return deltas
}
// Humanize nanoseconds to a string.
func humanizeNano(n int64) string {
var suffix string
switch {
case n > 1e9:
n /= 1e9
suffix = "s"
case n > 1e6:
n /= 1e6
suffix = "ms"
case n > 1e3:
n /= 1e3
suffix = "us"
default:
suffix = "ns"
}
return strconv.Itoa(int(n)) + suffix
}