[service] Move logging to the core, remove pkg level logs

This commit is contained in:
Daniel
2024-11-14 17:33:27 +01:00
parent 8b1bdc7eb1
commit f91003d077
22 changed files with 360 additions and 306 deletions

View File

@@ -8,12 +8,17 @@ import (
"runtime"
"github.com/safing/jess"
"github.com/safing/portmaster/base/log"
)
type ServiceConfig struct {
BinDir string
DataDir string
LogToStdout bool
LogDir string
LogLevel string
BinariesIndexURLs []string
IntelIndexURLs []string
VerifyBinaryUpdates jess.TrustStore
@@ -21,9 +26,10 @@ type ServiceConfig struct {
}
func (sc *ServiceConfig) Init() error {
// Check directories
// Check directories.
switch runtime.GOOS {
case "windows":
// Fall back to defaults.
if sc.BinDir == "" {
exeDir, err := getCurrentBinaryFolder() // Default: C:/Program Files/Portmaster
if err != nil {
@@ -34,6 +40,9 @@ func (sc *ServiceConfig) Init() error {
if sc.DataDir == "" {
sc.DataDir = filepath.FromSlash("$ProgramData/Portmaster")
}
if sc.LogDir == "" {
sc.LogDir = filepath.Join(sc.DataDir, "logs")
}
case "linux":
// Fall back to defaults.
@@ -43,6 +52,9 @@ func (sc *ServiceConfig) Init() error {
if sc.DataDir == "" {
sc.DataDir = "/var/lib/portmaster"
}
if sc.LogDir == "" {
sc.LogDir = "/var/log/portmaster"
}
default:
// Fail if not configured on other platforms.
@@ -52,11 +64,15 @@ func (sc *ServiceConfig) Init() error {
if sc.DataDir == "" {
return errors.New("binary directory must be configured - auto-detection not supported on this platform")
}
if !sc.LogToStdout && sc.LogDir == "" {
return errors.New("logging directory must be configured - auto-detection not supported on this platform")
}
}
// Expand path variables.
sc.BinDir = os.ExpandEnv(sc.BinDir)
sc.DataDir = os.ExpandEnv(sc.DataDir)
sc.LogDir = os.ExpandEnv(sc.LogDir)
// Apply defaults for required fields.
if len(sc.BinariesIndexURLs) == 0 {
@@ -67,6 +83,11 @@ func (sc *ServiceConfig) Init() error {
sc.IntelIndexURLs = DefaultIntelIndexURLs
}
// Check log level.
if sc.LogLevel != "" && log.ParseLevel(sc.LogLevel) == 0 {
return fmt.Errorf("invalid log level %q", sc.LogLevel)
}
return nil
}

View File

@@ -5,11 +5,12 @@ import (
"errors"
"fmt"
"log/slog"
"os"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/safing/portmaster/base/log"
)
// workerContextKey is a key used for the context key/value storage.
@@ -303,7 +304,7 @@ func (m *Manager) runWorker(w *WorkerCtx, fn func(w *WorkerCtx) error) (panicInf
// Print panic to stderr.
stackTrace := string(debug.Stack())
fmt.Fprintf(
os.Stderr,
log.GlobalWriter,
"===== PANIC =====\n%s\n\n%s===== END =====\n",
panicVal,
stackTrace,