Skip to content

Commit

Permalink
Fix: log more information about Chromium's run
Browse files Browse the repository at this point in the history
Regardless of whether there was an error or not, log some information
about the run (resources, exit code, etc).

Signed-off-by: Marcelo E. Magallon <[email protected]>
  • Loading branch information
mem committed Nov 1, 2024
1 parent 9272bc5 commit 9c20d0e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions crocochrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,25 +312,33 @@ func (s *Supervisor) launch(ctx context.Context, sessionID string) error {
}()

err = cmd.Run()

attrs := make([]slog.Attr, 0, 7)

if err != nil {
attrs = append(attrs, slog.Attr{Key: "err", Value: slog.AnyValue(err)})
}

if cmd.ProcessState != nil {
attrs = append(attrs,
slog.Attr{Key: "pid", Value: slog.IntValue(cmd.ProcessState.Pid())},
slog.Attr{Key: "exit_code", Value: slog.IntValue(cmd.ProcessState.ExitCode())},
slog.Attr{Key: "process_state", Value: slog.StringValue(cmd.ProcessState.String())},
slog.Attr{Key: "system_time", Value: slog.DurationValue(cmd.ProcessState.SystemTime())},
slog.Attr{Key: "user_time", Value: slog.DurationValue(cmd.ProcessState.UserTime())},
slog.Attr{Key: "sys_usage", Value: slog.AnyValue(cmd.ProcessState.SysUsage())},
)
}

if err != nil && !errors.Is(ctx.Err(), context.Canceled) {
logger.Error("running chromium", "err", err)
logger.Error("chromium output", "stdout", stdout.String())
logger.Error("chromium output", "stderr", stderr.String())

if ps := cmd.ProcessState; ps != nil {
logger.Debug(
"chromium process finished",
"state", ps.String(),
"exitCode", ps.ExitCode(),
"rss", ps.SysUsage().(*syscall.Rusage).Maxrss,
)
}
logger.LogAttrs(ctx, slog.LevelError, "chromium process finished", attrs...)
logger.Error("chromium output", "stdout", stdout.String(), "stderr", stderr.String())

return err
}

logger.Debug("chromium output", "stdout", stdout.String())
logger.Debug("chromium output", "stderr", stderr.String())
logger.LogAttrs(ctx, slog.LevelInfo, "chromium process finished", attrs...)
logger.Debug("chromium output", "stdout", stdout.String(), "stderr", stderr.String())

return nil
}
Expand Down

0 comments on commit 9c20d0e

Please sign in to comment.