feat: add process ID to logger

This commit is contained in:
Siyuan 2025-11-18 11:19:18 +00:00
parent 61dcd77313
commit 6dee8a3e24
2 changed files with 17 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package native
import (
"os"
"sync"
"time"
@ -40,8 +41,9 @@ type NativeOptions struct {
}
func NewNative(opts NativeOptions) *Native {
nativeSubLogger := nativeLogger.With().Str("scope", "native").Logger()
displaySubLogger := displayLogger.With().Str("scope", "native").Logger()
pid := os.Getpid()
nativeSubLogger := nativeLogger.With().Int("pid", pid).Str("scope", "native").Logger()
displaySubLogger := displayLogger.With().Int("pid", pid).Str("scope", "native").Logger()
onVideoStateChange := opts.OnVideoStateChange
if onVideoStateChange == nil {

View File

@ -57,9 +57,7 @@ func (n *NativeOptions) toProxyOptions() *nativeProxyOptions {
// random 16 bytes hex string
handshakeMessage := randomId(16)
maxRestartAttempts := defaultMaxRestartAttempts
// though it's unlikely to be less than 0 as it's uint, we'll add it just in case
// until we have a proper unit test for all things :-(
if n.MaxRestartAttempts <= 0 {
if n.MaxRestartAttempts > 0 {
maxRestartAttempts = n.MaxRestartAttempts
}
return &nativeProxyOptions{
@ -416,6 +414,14 @@ func (p *NativeProxy) restartProcess() error {
p.restartMu.Lock()
defer p.restartMu.Unlock()
p.restarts++
logger := p.logger.With().Uint("attempt", p.restarts).Uint("maxAttempts", p.options.MaxRestartAttempts).Logger()
if p.restarts >= p.options.MaxRestartAttempts {
logger.Fatal().Msg("max restart attempts reached, exiting")
return fmt.Errorf("max restart attempts reached")
}
if p.stopped {
return fmt.Errorf("proxy is stopped")
}
@ -424,19 +430,14 @@ func (p *NativeProxy) restartProcess() error {
p.clientMu.Lock()
if p.client != nil {
if err := p.client.Close(); err != nil {
p.logger.Warn().Err(err).Msg("failed to close gRPC client")
logger.Warn().Err(err).Msg("failed to close gRPC client")
}
p.client = nil // set to nil to avoid closing it again
}
p.clientMu.Unlock()
logger.Info().Msg("gRPC client closed")
p.restarts++
if p.restarts >= p.options.MaxRestartAttempts {
p.logger.Fatal().Msg("max restart attempts reached, exiting")
return fmt.Errorf("max restart attempts reached")
}
logger := p.logger.With().Uint("attempt", p.restarts).Uint("maxAttempts", p.options.MaxRestartAttempts).Logger()
logger.Info().Msg("attempting to restart native process")
if err := p.start(); err != nil {
logger.Error().Err(err).Msg("failed to start native process")
return fmt.Errorf("failed to start native process: %w", err)