Skip to content

Commit

Permalink
Merge pull request #83 from alexadhy/feature/windows-cmdutil
Browse files Browse the repository at this point in the history
[WIP] windows cmdutil
  • Loading branch information
jdknives authored Jun 21, 2021
2 parents c6a532c + 3627ed7 commit 7a2419d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 33 deletions.
32 changes: 1 addition & 31 deletions cmdutil/service_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"fmt"
"io"
"io/ioutil"
"log/syslog"
"os"
"strings"
"unicode"

jsoniter "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
logrussyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -132,14 +129,7 @@ func (sf *ServiceFlags) Logger() *logging.Logger {
logging.SetLevel(logLvl)

if sf.Syslog != "" {
hook, err := logrussyslog.NewSyslogHook(sf.SyslogNet, sf.Syslog, sysLvl, sf.Tag)
if err != nil {
log.WithError(err).
WithField("net", sf.SyslogNet).
WithField("addr", sf.Syslog).
Fatal("Failed to connect to syslog daemon.")
}
logging.AddHook(hook)
sf.sysLogHook(log, sysLvl)
}

return log
Expand Down Expand Up @@ -242,26 +232,6 @@ func ValidTag(tag string) error {
return nil
}

// LevelFromString returns a logrus.Level and syslog.Priority from a string identifier.
func LevelFromString(s string) (logrus.Level, syslog.Priority, error) {
switch strings.ToLower(s) {
case "debug":
return logrus.DebugLevel, syslog.LOG_DEBUG, nil
case "info", "notice":
return logrus.InfoLevel, syslog.LOG_INFO, nil
case "warn", "warning":
return logrus.WarnLevel, syslog.LOG_WARNING, nil
case "error":
return logrus.ErrorLevel, syslog.LOG_ERR, nil
case "fatal", "critical":
return logrus.FatalLevel, syslog.LOG_CRIT, nil
case "panic":
return logrus.PanicLevel, syslog.LOG_EMERG, nil
default:
return logrus.DebugLevel, syslog.LOG_DEBUG, ErrInvalidLogString
}
}

func alreadyDone(done *bool) bool {
if *done {
return true
Expand Down
4 changes: 2 additions & 2 deletions cmdutil/signal_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"os"
"os/signal"
"syscall"

"github.com/sirupsen/logrus"
)
Expand All @@ -18,7 +17,8 @@ func SignalContext(ctx context.Context, log logrus.FieldLogger) (context.Context
ctx, cancel := context.WithCancel(ctx)

ch := make(chan os.Signal)
signal.Notify(ch, []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT}...)
listenSigs := listenSignals()
signal.Notify(ch, listenSigs...)

go func() {
select {
Expand Down
13 changes: 13 additions & 0 deletions cmdutil/signal_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !windows

package cmdutil

import (
"os"

"golang.org/x/sys/unix"
)

func listenSignals() []os.Signal {
return []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGQUIT}
}
13 changes: 13 additions & 0 deletions cmdutil/signal_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build windows

package cmdutil

import (
"os"

"golang.org/x/sys/windows"
)

func listenSignals() []os.Signal {
return []os.Signal{windows.SIGINT, windows.SIGTERM, windows.SIGQUIT}
}
43 changes: 43 additions & 0 deletions cmdutil/sysloghook_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// +build !windows

package cmdutil

import (
"log/syslog"
"strings"

"github.com/sirupsen/logrus"
logrussyslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/skycoin/skycoin/src/util/logging"
)

func (sf *ServiceFlags) sysLogHook(log *logging.Logger, sysLvl int) {
hook, err := logrussyslog.NewSyslogHook(sf.SyslogNet, sf.Syslog, syslog.Priority(sysLvl), sf.Tag)
if err != nil {
log.WithError(err).
WithField("net", sf.SyslogNet).
WithField("addr", sf.Syslog).
Fatal("Failed to connect to syslog daemon.")
}
logging.AddHook(hook)
}

// LevelFromString returns a logrus.Level and syslog.Priority from a string identifier.
func LevelFromString(s string) (logrus.Level, int, error) {
switch strings.ToLower(s) {
case "debug":
return logrus.DebugLevel, int(syslog.LOG_DEBUG), nil
case "info", "notice":
return logrus.InfoLevel, int(syslog.LOG_INFO), nil
case "warn", "warning":
return logrus.WarnLevel, int(syslog.LOG_WARNING), nil
case "error":
return logrus.ErrorLevel, int(syslog.LOG_ERR), nil
case "fatal", "critical":
return logrus.FatalLevel, int(syslog.LOG_CRIT), nil
case "panic":
return logrus.PanicLevel, int(syslog.LOG_EMERG), nil
default:
return logrus.DebugLevel, int(syslog.LOG_DEBUG), ErrInvalidLogString
}
}
33 changes: 33 additions & 0 deletions cmdutil/sysloghook_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// +build windows

package cmdutil

import (
"strings"

"github.com/sirupsen/logrus"
"github.com/skycoin/skycoin/src/util/logging"
)

func (sf *ServiceFlags) sysLogHook(_ *logging.Logger, _ int) {
}

// LevelFromString returns a logrus.Level and syslog.Priority from a string identifier.
func LevelFromString(s string) (logrus.Level, int, error) {
switch strings.ToLower(s) {
case "debug":
return logrus.DebugLevel, 0, nil
case "info", "notice":
return logrus.InfoLevel, 0, nil
case "warn", "warning":
return logrus.WarnLevel, 0, nil
case "error":
return logrus.ErrorLevel, 0, nil
case "fatal", "critical":
return logrus.FatalLevel, 0, nil
case "panic":
return logrus.PanicLevel, 0, nil
default:
return logrus.DebugLevel, 0, ErrInvalidLogString
}
}

0 comments on commit 7a2419d

Please sign in to comment.