Skip to content

Commit 0a1f463

Browse files
committed
More modernizing logging
Ported block_device/nbd Made the native CGO return distinct errors in set_edid Made the cgo_linux (go code) report C-level error return codes Use defer for server and listener cleanup. Try doing a timeboxed GracefulStop of the server
1 parent 240e948 commit 0a1f463

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+991
-897
lines changed

block_device.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/jetkvm/kvm/internal/logging"
1111
"github.com/pojntfx/go-nbd/pkg/server"
12-
"github.com/rs/zerolog"
1312
)
1413

1514
type remoteImageBackend struct {
@@ -63,14 +62,18 @@ type NBDDevice struct {
6362
serverConn net.Conn
6463
clientConn net.Conn
6564
dev *os.File
66-
67-
l *zerolog.Logger
6865
}
6966

7067
func NewNBDDevice() *NBDDevice {
7168
return &NBDDevice{}
7269
}
7370

71+
func (d *NBDDevice) getLogger() *logging.Context {
72+
return logging.GetSubsystemLogger("nbd").
73+
Str("socket_path", nbdSocketPath).
74+
Str("device_path", nbdDevicePath)
75+
}
76+
7477
func (d *NBDDevice) Start() error {
7578
var err error
7679

@@ -83,19 +86,10 @@ func (d *NBDDevice) Start() error {
8386
return err
8487
}
8588

86-
if d.l == nil {
87-
scopedLogger := logging.GetSubsystemLogger("nbd").
88-
With().
89-
Str("socket_path", nbdSocketPath).
90-
Str("device_path", nbdDevicePath).
91-
Logger()
92-
d.l = &scopedLogger
93-
}
94-
9589
// Remove the socket file if it already exists
9690
if _, err := os.Stat(nbdSocketPath); err == nil {
9791
if err := os.Remove(nbdSocketPath); err != nil {
98-
d.l.Error().Err(err).Msg("failed to remove existing socket file")
92+
d.getLogger().Error().Err(err).Msg("failed to remove existing socket file")
9993
os.Exit(1)
10094
}
10195
}
@@ -137,5 +131,5 @@ func (d *NBDDevice) runServerConn() {
137131
SupportsMultiConn: false,
138132
})
139133

140-
d.l.Info().Err(err).Msg("nbd server exited")
134+
d.getLogger().Info().Err(err).Msg("nbd server exited")
141135
}

block_device_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ func (d *NBDDevice) runClientConn() {
1111
ExportName: "jetkvm",
1212
BlockSize: uint32(4 * 1024),
1313
})
14-
d.l.Info().Err(err).Msg("nbd client exited")
14+
d.getLogger().Info().Err(err).Msg("nbd client exited")
1515
}
1616

1717
func (d *NBDDevice) Close() {
1818
if d.dev != nil {
1919
err := client.Disconnect(d.dev)
2020
if err != nil {
21-
d.l.Warn().Err(err).Msg("error disconnecting nbd client")
21+
d.getLogger().Warn().Err(err).Msg("error disconnecting nbd client")
2222
}
2323
_ = d.dev.Close()
2424
}

block_device_notlinux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
)
88

99
func (d *NBDDevice) runClientConn() {
10-
d.l.Error().Msg("platform not supported")
10+
d.getLogger().Error().Msg("platform not supported")
1111
os.Exit(1)
1212
}
1313

1414
func (d *NBDDevice) Close() {
15-
d.l.Error().Msg("platform not supported")
15+
d.getLogger().Error().Msg("platform not supported")
1616
os.Exit(1)
1717
}

cloud.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/coder/websocket"
2323
"github.com/gin-gonic/gin"
24-
"github.com/rs/zerolog"
2524
)
2625

2726
type CloudRegisterRequest struct {
@@ -325,19 +324,13 @@ func runWebsocketClient() error {
325324
header.Set("Authorization", "Bearer "+config.CloudToken)
326325
dialCtx, cancelDial := context.WithTimeout(context.Background(), CloudWebSocketConnectTimeout)
327326

328-
l := logging.GetSubsystemLogger("websocket").
329-
With().
330-
Str("source", wsURL.Host).
331-
Str("sourceType", "cloud").
332-
Logger()
333-
334-
scopedLogger := &l
327+
logger := logging.GetSubsystemLogger("websocket").Str("source", wsURL.Host).Str("sourceType", "cloud")
335328

336329
defer cancelDial()
337330
c, resp, err := websocket.Dial(dialCtx, wsURL.String(), &websocket.DialOptions{
338331
HTTPHeader: header,
339332
OnPingReceived: func(ctx context.Context, payload []byte) bool {
340-
scopedLogger.Debug().Bytes("payload", payload).Int("length", len(payload)).Msg("ping frame received")
333+
logger.Debug().Bytes("payload", payload).Int("length", len(payload)).Msg("ping frame received")
341334

342335
metricConnectionTotalPingReceivedCount.WithLabelValues("cloud", wsURL.Host).Inc()
343336
metricConnectionLastPingReceivedTimestamp.WithLabelValues("cloud", wsURL.Host).SetToCurrentTime()
@@ -359,15 +352,12 @@ func runWebsocketClient() error {
359352

360353
if connectionId == "" {
361354
connectionId = uuid.New().String()
362-
scopedLogger.Warn().
355+
logger.Warn().
363356
Str("connectionId", connectionId).
364357
Msg("no connection id received from the server, generating a new one")
365358
}
366359

367-
lWithConnectionId := scopedLogger.With().
368-
Str("connectionID", connectionId).
369-
Logger()
370-
scopedLogger = &lWithConnectionId
360+
logger = logger.With().Str("connectionID", connectionId)
371361

372362
cloudLogger := logging.GetSubsystemLogger("cloud")
373363

@@ -391,7 +381,7 @@ func runWebsocketClient() error {
391381
wsResetMetrics(true, "cloud", wsURL.Host)
392382

393383
// we don't have a source for the cloud connection
394-
return handleWebRTCSignalWsMessages(c, true, wsURL.Host, connectionId, scopedLogger)
384+
return handleWebRTCSignalWsMessages(c, true, wsURL.Host, connectionId)
395385
}
396386

397387
func authenticateSession(ctx context.Context, c *websocket.Conn, req WebRTCSessionRequest) error {
@@ -431,7 +421,6 @@ func handleSessionRequest(
431421
req WebRTCSessionRequest,
432422
isCloudConnection bool,
433423
source string,
434-
scopedLogger *zerolog.Logger,
435424
) error {
436425
var sourceType string
437426
if isCloudConnection {

display.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ func requestDisplayUpdate(shouldWakeDisplay bool, reason string) {
193193
if shouldWakeDisplay {
194194
wakeDisplay(false, reason)
195195
}
196-
logging.GetSubsystemLogger("display").Debug().Msg("display updating")
197196
// TODO: only run once regardless how many pending updates
198197
updateDisplay()
199198
}()

internal/hidrpc/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
)
66

77
func GetHidRpcLoggingContext() *logging.Context {
8-
return logging.NewContext(logging.GetSubsystemLogger("hidrpc"))
8+
return logging.GetSubsystemLogger("hidrpc")
99
}

0 commit comments

Comments
 (0)