Skip to content

Commit

Permalink
Update log
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Nov 21, 2020
1 parent 5f06875 commit 06647ed
Show file tree
Hide file tree
Showing 53 changed files with 453 additions and 501 deletions.
60 changes: 34 additions & 26 deletions cmd/pipe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"path/filepath"

_ "github.com/pipeproxy/pipe/init"
_ "github.com/pipeproxy/pipe/internal/log"

"github.com/pipeproxy/pipe"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/pipeproxy/pipe/internal/notify"
"github.com/spf13/pflag"
"github.com/wzshiming/lockfile"
"github.com/wzshiming/logger"
)

var signal string
Expand All @@ -39,65 +40,68 @@ func init() {

func main() {

log := logger.Log
lf, err := lockfile.NewLockfile(pidfile)
if err != nil {
logger.Fatalf("lockfile error: %s", err)
log.Error(err, "lockfile")
return
}

if signal == "" {
logger.Infof("Start pipe")
log.Info("Start pipe")
} else {
logger.Infof("Send signal %s to pipe", signal)
log = log.WithName(signal).WithValues("signal", signal)
log.Info("send signal to pipe")
}
switch signal {
case "":
err := lf.Lock()
if err != nil {
logger.Fatalln("start error:", err)
log.Error(err, "lock pidfile")
return
}
start(conf)
ctx := logger.WithContext(context.Background(), log)
start(ctx, log, conf)
err = lf.Unlock()
if err != nil {
logger.Fatalln("end error:", err)
log.Error(err, "unlock pidfile")
return
}
case "reload":
pid, err := lf.Get()
if err != nil {
logger.Fatalln("reload error:", err)
log.Error(err, "reload")
return
}
err = notify.Kill(pid, notify.Reload)
if err != nil {
logger.Fatalln("send error:", err)
log.Error(err, "send signal reload")
return
}
case "stop":
pid, err := lf.Get()
if err != nil {
logger.Fatalln("stop error:", err)
log.Error(err, "stop")
return
}
err = notify.Kill(pid, notify.Stop)
if err != nil {
logger.Fatalln("send error:", err)
log.Error(err, "send signal stop")
return
}
case "reopen":
pid, err := lf.Get()
if err != nil {
logger.Fatalln("reopen error:", err)
log.Error(err, "reopen")
return
}
err = notify.Kill(pid, notify.Reopen)
if err != nil {
logger.Fatalln("send error:", err)
log.Error(err, "send signal reopen")
return
}
default:
logger.Fatalf("not defined signal %s", signal)
log.V(-2).Info("not defined signal")
return
}
}
Expand All @@ -116,50 +120,54 @@ func getConfig(conf string) ([]byte, error) {
return c, nil
}

func start(conf string) {
func start(ctx context.Context, log logger.Logger, conf string) {

c, err := getConfig(conf)
if err != nil {
logger.Errorf("read config file %q error: %s", conf, err)
log.Error(err, "read config file",
"config", conf,
)
return
}

svc, err := pipe.NewPipeWithConfig(context.Background(), c)
svc, err := pipe.NewPipeWithConfig(ctx, c)
if err != nil {
logger.Errorf("configure config error: %s", err)
log.Error(err, "configure config")
return
}

notify.On(notify.Stop, func() {
logger.Info("Closing")
log.Info("Closing")

err := svc.Close()
if svc == nil {
logger.Errorf("service close error: %s", err)
if err != nil {
log.Error(err, "service close")
return
}
})
notify.On(notify.Reload, func() {
logger.Info("Reloading")
log.Info("Reloading")

c, err := getConfig(conf)
if err != nil {
logger.Errorf("read config file %q error: %s", conf, err)
log.Error(err, "read config file",
"config", conf,
)
return
}

err = svc.Reload(c)
if err != nil {
logger.Errorf("reload error: %s", err)
log.Error(err, "reload")
return
}
})

err = svc.Run()
if err != nil {
logger.Errorf("start error: %s", err)
log.Error(err, "start")
return
}

logger.Info("exit pipe")
log.Info("Exit pipe")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/once"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func OnceGet(ctx context.Context, name string, defaults once.Once) once.Once {
if defaults != nil {
return defaults
}
logger.Warnf("once.Once %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("once.Once is not defined", "name", name)
return OnceNone
}

Expand All @@ -77,7 +77,7 @@ func newOnceNone() once.Once {
}

func (_OnceNone) Do(_ context.Context) (error error) {
logger.Warn("this is none of once.Once")
logger.Log.V(-1).Info("this is none of once.Once")

error = fmt.Errorf("error once.Once is none")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/packet"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func HandlerGet(ctx context.Context, name string, defaults packet.Handler) packe
if defaults != nil {
return defaults
}
logger.Warnf("packet.Handler %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("packet.Handler is not defined", "name", name)
return HandlerNone
}

Expand All @@ -77,7 +77,7 @@ func newHandlerNone() packet.Handler {
}

func (_HandlerNone) ServePacket(_ context.Context, _ net.PacketConn) {
logger.Warn("this is none of packet.Handler")
logger.Log.V(-1).Info("this is none of packet.Handler")

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/packet"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -65,7 +65,7 @@ func ListenConfigGet(ctx context.Context, name string, defaults packet.ListenCon
if defaults != nil {
return defaults
}
logger.Warnf("packet.ListenConfig %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("packet.ListenConfig is not defined", "name", name)
return ListenConfigNone
}

Expand All @@ -78,7 +78,7 @@ func newListenConfigNone() packet.ListenConfig {
}

func (_ListenConfigNone) ListenPacket(_ context.Context) (_ net.PacketConn, error error) {
logger.Warn("this is none of packet.ListenConfig")
logger.Log.V(-1).Info("this is none of packet.ListenConfig")

error = fmt.Errorf("error packet.ListenConfig is none")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/protocol"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -63,7 +63,7 @@ func HandlerGet(ctx context.Context, name string, defaults protocol.Handler) pro
if defaults != nil {
return defaults
}
logger.Warnf("protocol.Handler %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("protocol.Handler is not defined", "name", name)
return HandlerNone
}

Expand All @@ -76,7 +76,7 @@ func newHandlerNone() protocol.Handler {
}

func (_HandlerNone) ServeProtocol(_ context.Context, _ protocol.Protocol) {
logger.Warn("this is none of protocol.Handler")
logger.Log.V(-1).Info("this is none of protocol.Handler")

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/service"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func ServiceGet(ctx context.Context, name string, defaults service.Service) serv
if defaults != nil {
return defaults
}
logger.Warnf("service.Service %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("service.Service is not defined", "name", name)
return ServiceNone
}

Expand All @@ -77,15 +77,15 @@ func newServiceNone() service.Service {
}

func (_ServiceNone) Close() (error error) {
logger.Warn("this is none of service.Service")
logger.Log.V(-1).Info("this is none of service.Service")

error = fmt.Errorf("error service.Service is none")

return
}

func (_ServiceNone) Run(_ context.Context) (error error) {
logger.Warn("this is none of service.Service")
logger.Log.V(-1).Info("this is none of service.Service")

error = fmt.Errorf("error service.Service is none")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/stream"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -65,7 +65,7 @@ func DialerGet(ctx context.Context, name string, defaults stream.Dialer) stream.
if defaults != nil {
return defaults
}
logger.Warnf("stream.Dialer %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("stream.Dialer is not defined", "name", name)
return DialerNone
}

Expand All @@ -78,7 +78,7 @@ func newDialerNone() stream.Dialer {
}

func (_DialerNone) DialStream(_ context.Context) (_ net.Conn, error error) {
logger.Warn("this is none of stream.Dialer")
logger.Log.V(-1).Info("this is none of stream.Dialer")

error = fmt.Errorf("error stream.Dialer is none")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/stream"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func HandlerGet(ctx context.Context, name string, defaults stream.Handler) strea
if defaults != nil {
return defaults
}
logger.Warnf("stream.Handler %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("stream.Handler is not defined", "name", name)
return HandlerNone
}

Expand All @@ -77,7 +77,7 @@ func newHandlerNone() stream.Handler {
}

func (_HandlerNone) ServeStream(_ context.Context, _ net.Conn) {
logger.Warn("this is none of stream.Handler")
logger.Log.V(-1).Info("this is none of stream.Handler")

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pipeproxy/pipe/components/common/register"
"github.com/pipeproxy/pipe/components/stream"
"github.com/pipeproxy/pipe/internal/ctxcache"
"github.com/pipeproxy/pipe/internal/logger"
"github.com/wzshiming/logger"
)

func init() {
Expand Down Expand Up @@ -65,7 +65,7 @@ func ListenConfigGet(ctx context.Context, name string, defaults stream.ListenCon
if defaults != nil {
return defaults
}
logger.Warnf("stream.ListenConfig %q is not defined", name)
logger.FromContext(ctx).V(-1).Info("stream.ListenConfig is not defined", "name", name)
return ListenConfigNone
}

Expand All @@ -78,7 +78,7 @@ func newListenConfigNone() stream.ListenConfig {
}

func (_ListenConfigNone) ListenStream(_ context.Context) (_ net.Listener, error error) {
logger.Warn("this is none of stream.ListenConfig")
logger.Log.V(-1).Info("this is none of stream.ListenConfig")

error = fmt.Errorf("error stream.ListenConfig is none")

Expand Down
Loading

0 comments on commit 06647ed

Please sign in to comment.