[service] Move logging to the core, remove pkg level logs
This commit is contained in:
@@ -24,6 +24,10 @@ var (
|
||||
binDir string
|
||||
dataDir string
|
||||
|
||||
logToStdout bool
|
||||
logDir string
|
||||
logLevel string
|
||||
|
||||
svcCfg *service.ServiceConfig
|
||||
)
|
||||
|
||||
@@ -35,6 +39,11 @@ func init() {
|
||||
// Add persisent flags for all commands.
|
||||
rootCmd.PersistentFlags().StringVar(&binDir, "bin-dir", "", "set directory for executable binaries (rw/ro)")
|
||||
rootCmd.PersistentFlags().StringVar(&dataDir, "data-dir", "", "set directory for variable data (rw)")
|
||||
|
||||
// Add flags for service only.
|
||||
rootCmd.Flags().BoolVar(&logToStdout, "log-stdout", false, "log to stdout instead of file")
|
||||
rootCmd.Flags().StringVar(&logDir, "log-dir", "", "set directory for logs")
|
||||
rootCmd.Flags().StringVar(&logLevel, "log", "", "set log level to [trace|debug|info|warning|error|critical]")
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -56,8 +65,13 @@ func initializeGlobals(cmd *cobra.Command, args []string) {
|
||||
|
||||
// Create service config.
|
||||
svcCfg = &service.ServiceConfig{
|
||||
BinDir: binDir,
|
||||
DataDir: dataDir,
|
||||
BinDir: binDir,
|
||||
DataDir: dataDir,
|
||||
|
||||
LogToStdout: logToStdout,
|
||||
LogDir: logDir,
|
||||
LogLevel: logLevel,
|
||||
|
||||
BinariesIndexURLs: service.DefaultStableBinaryIndexURLs,
|
||||
IntelIndexURLs: service.DefaultIntelIndexURLs,
|
||||
VerifyBinaryUpdates: service.BinarySigningTrustStore,
|
||||
|
||||
@@ -75,9 +75,15 @@ func cmdRun(cmd *cobra.Command, args []string) {
|
||||
|
||||
// START
|
||||
|
||||
// Set default log level and start logging.
|
||||
log.SetLogLevel(log.WarningLevel)
|
||||
_ = log.Start()
|
||||
// FIXME: fix color and duplicate level when logging with slog
|
||||
// FIXME: check for tty for color enabling
|
||||
|
||||
// Start logging.
|
||||
err = log.Start(svcCfg.LogLevel, svcCfg.LogToStdout, svcCfg.LogDir)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err.Error())
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
// Create system service.
|
||||
service := NewSystemService(instance)
|
||||
@@ -97,10 +103,10 @@ func cmdRun(cmd *cobra.Command, args []string) {
|
||||
case <-instance.ShutdownComplete():
|
||||
// Print stack on shutdown, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stdout, "PRINTING STACK ON EXIT")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON EXIT")
|
||||
}
|
||||
case <-time.After(3 * time.Minute):
|
||||
printStackTo(os.Stderr, "PRINTING STACK - TAKING TOO LONG FOR SHUTDOWN")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK - TAKING TOO LONG FOR SHUTDOWN")
|
||||
}
|
||||
|
||||
// Check if restart was triggered and send start service command if true.
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
processInfo "github.com/shirou/gopsutil/process"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service"
|
||||
)
|
||||
|
||||
@@ -30,7 +31,7 @@ func (s *LinuxSystemService) Run() {
|
||||
|
||||
// Print stack on start failure, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON START FAILURE")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
|
||||
os.Exit(1)
|
||||
@@ -57,7 +58,7 @@ wait:
|
||||
case sig := <-signalCh:
|
||||
// Only print and continue to wait if SIGUSR1
|
||||
if sig == syscall.SIGUSR1 {
|
||||
printStackTo(os.Stdout, "PRINTING STACK ON REQUEST")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON REQUEST")
|
||||
continue wait
|
||||
} else {
|
||||
// Trigger shutdown.
|
||||
@@ -84,7 +85,7 @@ wait:
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SIGNAL: %s> again, but already shutting down - %d more to force\n", sig, forceCnt)
|
||||
} else {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,12 @@ import (
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/safing/portmaster/service"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/sys/windows/svc"
|
||||
"golang.org/x/sys/windows/svc/debug"
|
||||
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service"
|
||||
)
|
||||
|
||||
const serviceName = "PortmasterCore"
|
||||
@@ -66,7 +68,7 @@ func (s *WindowsSystemService) Execute(args []string, changeRequests <-chan svc.
|
||||
|
||||
// Print stack on start failure, if enabled.
|
||||
if printStackOnExit {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON START FAILURE")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON START FAILURE")
|
||||
}
|
||||
|
||||
// Notify service manager we stopped again.
|
||||
@@ -140,7 +142,7 @@ waitShutdown:
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SIGNAL: %s> but already shutting down - %d more to force\n", sig, forceCnt)
|
||||
} else {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -154,7 +156,7 @@ waitShutdown:
|
||||
if forceCnt > 0 {
|
||||
fmt.Printf(" <SERVICE CMD: %v> but already shutting down - %d more to force\n", serviceCmdName(c.Cmd), forceCnt)
|
||||
} else {
|
||||
printStackTo(os.Stderr, "PRINTING STACK ON FORCED EXIT")
|
||||
printStackTo(log.GlobalWriter, "PRINTING STACK ON FORCED EXIT")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,11 @@ func update(cmd *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("internal configuration error: %w", err)
|
||||
}
|
||||
// Force logging to stdout.
|
||||
svcCfg.LogToStdout = true
|
||||
|
||||
// Start logging.
|
||||
log.SetLogLevel(log.InfoLevel)
|
||||
_ = log.Start()
|
||||
_ = log.Start(svcCfg.LogLevel, svcCfg.LogToStdout, svcCfg.LogDir)
|
||||
defer log.Shutdown()
|
||||
|
||||
// Create updaters.
|
||||
|
||||
Reference in New Issue
Block a user