Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

35
app/tool/bgr/log/BUILD Normal file
View File

@@ -0,0 +1,35 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["log.go"],
importpath = "go-common/app/tool/bgr/log",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/tool/bgr/log/color:go_default_library",
"//vendor/golang.org/x/crypto/ssh/terminal:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/tool/bgr/log/color:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["color.go"],
importpath = "go-common/app/tool/bgr/log/color",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,51 @@
package colorful
const (
colorOff = string("\033[0m")
colorRed = string("\033[0;31m")
colorGreen = string("\033[0;32m")
colorOrange = string("\033[0;33m")
colorBlue = string("\033[0;34m")
colorPurple = string("\033[0;35m")
colorCyan = string("\033[0;36m")
colorGray = string("\033[0;37m")
)
func paint(data string, color string) string {
return color + data + colorOff
}
// Red draw red on data
func Red(data string) string {
return paint(data, colorRed)
}
// Green draw Green on data
func Green(data string) string {
return paint(data, colorGreen)
}
// Orange draw Orange on data
func Orange(data string) string {
return paint(data, colorOrange)
}
// Blue draw Blue on data
func Blue(data string) string {
return paint(data, colorBlue)
}
// Purple draw Purple on data
func Purple(data string) string {
return paint(data, colorPurple)
}
// Cyan draw Cyan on data
func Cyan(data string) string {
return paint(data, colorCyan)
}
// Gray draw Gray on data
func Gray(data string) string {
return paint(data, colorGray)
}

160
app/tool/bgr/log/log.go Normal file
View File

@@ -0,0 +1,160 @@
package log
import (
"fmt"
"io"
"os"
"strings"
"sync"
"go-common/app/tool/bgr/log/color"
"golang.org/x/crypto/ssh/terminal"
)
// FDWriter interface extends io.Writer with file descriptor function
type FDWriter interface {
io.Writer
Fd() uintptr
}
// Logger struct definition
type Logger struct {
mu sync.RWMutex
out FDWriter
color bool
debug bool
buf strings.Builder
}
type prefix struct {
Plain string
Color string
}
const (
_plainError = "[ ERROR ] "
_plainWarn = "[ WARN ] "
_plainInfo = "[ INFO ] "
_plainDebug = "[ DEBUG ] "
_plainFatal = "[ FATAL ] "
)
var (
_prefixError = prefix{
Plain: _plainError,
Color: colorful.Red(_plainError),
}
_prefixWarn = prefix{
Plain: _plainWarn,
Color: colorful.Orange(_plainWarn),
}
_prefixInfo = prefix{
Plain: _plainInfo,
Color: colorful.Green(_plainInfo),
}
_prefixDebug = prefix{
Plain: _plainDebug,
Color: colorful.Purple(_plainDebug),
}
_prefixFatal = prefix{
Plain: _plainFatal,
Color: colorful.Gray(_plainFatal),
}
)
// New returns new Logger instance with predefined writer output and
// automatically detect terminal coloring support
func New(out FDWriter, debug bool) *Logger {
return &Logger{
color: terminal.IsTerminal(int(out.Fd())),
out: out,
debug: debug,
buf: strings.Builder{},
}
}
func (l *Logger) output(prefix prefix, data string) (err error) {
l.mu.Lock()
defer l.mu.Unlock()
l.buf.Reset()
if l.color {
if _, err = l.buf.WriteString(prefix.Color); err != nil {
return
}
} else {
if _, err = l.buf.WriteString(prefix.Plain); err != nil {
return
}
}
if _, err = l.buf.WriteString(data); err != nil {
return
}
if data[len(data)-1] != '\n' {
l.buf.WriteString("\n")
}
_, err = l.out.Write([]byte(l.buf.String()))
return
}
// Error print error message to output
func (l *Logger) Error(v ...interface{}) {
l.output(_prefixError, fmt.Sprintln(v...))
}
// Errorf print formatted error message to output
func (l *Logger) Errorf(format string, v ...interface{}) {
l.output(_prefixError, fmt.Sprintf(format, v...))
}
// Warn print warning message to output
func (l *Logger) Warn(v ...interface{}) {
l.output(_prefixWarn, fmt.Sprintln(v...))
}
// Warnf print formatted warning message to output
func (l *Logger) Warnf(format string, v ...interface{}) {
l.output(_prefixWarn, fmt.Sprintf(format, v...))
}
// Info print informational message to output
func (l *Logger) Info(v ...interface{}) {
l.output(_prefixInfo, fmt.Sprintln(v...))
}
// Infof print formatted informational message to output
func (l *Logger) Infof(format string, v ...interface{}) {
l.output(_prefixInfo, fmt.Sprintf(format, v...))
}
// Debug print debug message to output if debug output enabled
func (l *Logger) Debug(v ...interface{}) {
if l.debug {
l.output(_prefixDebug, fmt.Sprintln(v...))
}
}
// Debugf print formatted debug message to output if debug output enabled
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.debug {
l.output(_prefixDebug, fmt.Sprintf(format, v...))
}
}
// Fatal print fatal message to output and then exit(1)
func (l *Logger) Fatal(v ...interface{}) {
l.output(_prefixFatal, fmt.Sprintln(v...))
os.Exit(1)
}
// Fatalf print formatted fatal message to output and then exit(1)
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.output(_prefixFatal, fmt.Sprintf(format, v...))
os.Exit(1)
}