vendor pls
parent
513f114701
commit
1c661b4b5c
|
|
@ -0,0 +1,98 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var outfile *os.File
|
||||
var outfileB *os.File
|
||||
|
||||
var lastWasReturn = false
|
||||
|
||||
var ErrCantConfigLog = errors.New("cannot configure log")
|
||||
|
||||
func Config(path string) error {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return ErrCantConfigLog
|
||||
}
|
||||
outfile = f
|
||||
return nil
|
||||
}
|
||||
|
||||
func Logf(args ...interface{}) {
|
||||
if outfile == nil {
|
||||
outfile = os.Stderr
|
||||
}
|
||||
defer outfile.Sync()
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(outfile, "")
|
||||
return
|
||||
}
|
||||
v := fmt.Sprintf(args[0].(string), args[1:]...)
|
||||
if v[len(v)-1] == '\n' {
|
||||
v = v[:len(v)-1]
|
||||
}
|
||||
thisWasReturn := v[0] == '\r'
|
||||
if lastWasReturn && !thisWasReturn {
|
||||
fmt.Println()
|
||||
}
|
||||
if thisWasReturn {
|
||||
fmt.Fprintf(outfile, "\r%s%v", prefix(), v[1:])
|
||||
} else {
|
||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
||||
}
|
||||
lastWasReturn = thisWasReturn
|
||||
}
|
||||
|
||||
func Fatalf(args ...interface{}) {
|
||||
Logf(args...)
|
||||
if len(args) > 0 {
|
||||
panic(args[0])
|
||||
} else {
|
||||
panic("Panic from logger.fatal()")
|
||||
}
|
||||
}
|
||||
|
||||
func Fatal(args ...interface{}) {
|
||||
Log(args...)
|
||||
if len(args) > 0 {
|
||||
panic(args[0])
|
||||
} else {
|
||||
panic("Panic from logger.fatal()")
|
||||
}
|
||||
}
|
||||
|
||||
func Log(args ...interface{}) {
|
||||
if lastWasReturn {
|
||||
fmt.Println()
|
||||
}
|
||||
if outfile == nil {
|
||||
outfile = os.Stderr
|
||||
}
|
||||
v := fmt.Sprintf("%v", args)
|
||||
v = v[1 : len(v)-1]
|
||||
fmt.Fprintf(outfile, "%s%v\n", prefix(), v)
|
||||
outfile.Sync()
|
||||
lastWasReturn = false
|
||||
}
|
||||
|
||||
func LogB(args ...interface{}) {
|
||||
if outfileB == nil {
|
||||
outfileB = os.Stderr
|
||||
}
|
||||
v := fmt.Sprintf("%v", args)
|
||||
v = v[1 : len(v)-1]
|
||||
fmt.Fprintf(outfileB, "%s%v\n", prefix(), v)
|
||||
outfileB.Sync()
|
||||
}
|
||||
|
||||
func prefix() string {
|
||||
return time.Now().Format("20060102-150405") + " : "
|
||||
}
|
||||
Loading…
Reference in New Issue