Skip to content

Commit

Permalink
Fix/con error (#27)
Browse files Browse the repository at this point in the history
* Request and response packets are not aligned
* logger
* reconnet on error
* copy stream
  • Loading branch information
chenjunbiao authored Aug 14, 2020
1 parent 0456d0d commit 79c05f5
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 33 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/yomorun/yomo
go 1.14

require (
github.com/10cella/yomo-json-codec v0.2.4
github.com/10cella/yomo-json-codec v0.2.5
github.com/lucas-clemente/quic-go v0.17.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/10cella/yomo-json-codec v0.2.3 h1:IMcV7lR8cknbXFnNIU4JB6menKKGny0XXcI
github.com/10cella/yomo-json-codec v0.2.3/go.mod h1:oApqc2PnZohrtlVJ3T1Knou9Ap9mcCJW1i3xiRtvF7c=
github.com/10cella/yomo-json-codec v0.2.4 h1:bdOInT1MnBhXDsGBHx/fYn0CiXzKxrm5/kFjfJyYdrw=
github.com/10cella/yomo-json-codec v0.2.4/go.mod h1:j4rl6bth7Bb6F8UC0SPN2XxlGGUsHGWO8LnrH5Q8RzI=
github.com/10cella/yomo-json-codec v0.2.5 h1:XTWWFVkQENaLLBTxBdXyG61UneA8SQF/YdlnyHptV/o=
github.com/10cella/yomo-json-codec v0.2.5/go.mod h1:j4rl6bth7Bb6F8UC0SPN2XxlGGUsHGWO8LnrH5Q8RzI=
github.com/10cella/yomo-txtkv-codec v1.0.5 h1:rVdtBcsff3RG3BEq7AHq2WvNiGDF6IEKlTiplypfX+c=
github.com/10cella/yomo-txtkv-codec v1.0.5/go.mod h1:nBfcD3qVq6kkSRHOgSUxK1+gMRoXI0obLF+b1eiyCtM=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down
146 changes: 146 additions & 0 deletions pkg/util/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package util

import (
"fmt"
"log"
"os"
"reflect"
"strings"
"sync"
"time"

"github.com/yomorun/yomo/pkg/env"
)

type LogLevel uint8

const (
LogLevelNothing LogLevel = iota
LogLevelError
LogLevelInfo
LogLevelDebug
)

const logEnv = "YOMO_LOG_LEVEL"

type Logger interface {
SetLogLevel(LogLevel)
SetLogTimeFormat(format string)
WithPrefix(prefix string) Logger
Debug() bool

Errorf(format string, args ...interface{})
Infof(format string, args ...interface{})
Debugf(format string, args ...interface{})
}

var DefaultLogger Logger

type defaultLogger struct {
prefix string

logLevel LogLevel
timeFormat string
}

var _ Logger = &defaultLogger{}

func (l *defaultLogger) SetLogLevel(level LogLevel) {
l.logLevel = level
}

func (l *defaultLogger) SetLogTimeFormat(format string) {
log.SetFlags(0)
l.timeFormat = format
}

func (l *defaultLogger) Debugf(format string, args ...interface{}) {
if l.logLevel == LogLevelDebug {
l.logMessage(format, args...)
}
}

func (l *defaultLogger) Infof(format string, args ...interface{}) {
if l.logLevel >= LogLevelInfo {
l.logMessage(format, args...)
}
}

func (l *defaultLogger) Errorf(format string, args ...interface{}) {
if l.logLevel >= LogLevelError {
l.logMessage(format, args...)
}
}

func (l *defaultLogger) logMessage(format string, args ...interface{}) {
var pre string

if len(l.timeFormat) > 0 {
pre = time.Now().Format(l.timeFormat) + " "
}
if len(l.prefix) > 0 {
pre += l.prefix + " "
}
log.Printf(pre+format, args...)
}

func (l *defaultLogger) WithPrefix(prefix string) Logger {
if len(l.prefix) > 0 {
prefix = l.prefix + " " + prefix
}
return &defaultLogger{
logLevel: l.logLevel,
timeFormat: l.timeFormat,
prefix: prefix,
}
}

func (l *defaultLogger) Debug() bool {
return l.logLevel == LogLevelDebug
}

func init() {
loadDefaultLogger()
}

var mux sync.Mutex

func loadDefaultLogger() {
mux.Lock()
defer mux.Unlock()
if DefaultLogger == nil {
DefaultLogger = &defaultLogger{}
DefaultLogger.SetLogLevel(readLoggingEnv())
}
}

func readLoggingEnv() LogLevel {
lvl := strings.ToLower(env.GetString(logEnv, "info"))
switch lvl {
case "":
return LogLevelNothing
case "debug":
return LogLevelDebug
case "info":
return LogLevelInfo
case "error":
return LogLevelError
default:
fmt.Fprintln(os.Stderr, "invalid log level")
return LogLevelNothing
}
}

func GetLogger(prefix string) Logger {
if DefaultLogger == nil {
loadDefaultLogger()
}
return Logger.WithPrefix(DefaultLogger, prefix)
}

func GetLoggerOf(obj interface{}) Logger {
if DefaultLogger == nil {
loadDefaultLogger()
}
return Logger.WithPrefix(DefaultLogger, reflect.TypeOf(obj).Name())
}
Loading

0 comments on commit 79c05f5

Please sign in to comment.