@@ -27,8 +27,7 @@ import (
2727 "syscall"
2828 "time"
2929
30- "github.com/rs/zerolog"
31- "github.com/rs/zerolog/log"
30+ "github.com/containerd/nerdctl/mod/tigron/internal/logger"
3231)
3332
3433const (
4342 // binary missing).
4443 ErrFailedStarting = errors .New ("command failed starting" )
4544 // ErrSignaled is returned by Wait() if a signal was sent to the command while running.
46- ErrSignaled = errors .New ("command execution signalled " )
45+ ErrSignaled = errors .New ("command execution signaled " )
4746 // ErrExecutionFailed is returned by Wait() when a command executes but returns a non-zero error
4847 // code.
4948 ErrExecutionFailed = errors .New ("command returned a non-zero exit code" )
6160 errExecutionCancelled = errors .New ("command execution cancelled" )
6261)
6362
63+ type contextKey string
64+
65+ // LoggerKey defines the key to attach a logger to on the context.
66+ const LoggerKey = contextKey ("logger" )
67+
6468// Result carries the resulting output of a command once it has finished.
6569type Result struct {
6670 Environ []string
@@ -76,7 +80,7 @@ type execution struct {
7680 cancel context.CancelFunc
7781 command * exec.Cmd
7882 pipes * stdPipes
79- log zerolog .Logger
83+ log logger .Logger
8084 err error
8185}
8286
@@ -193,17 +197,30 @@ func (gc *Command) Run(parentCtx context.Context) error {
193197
194198 // Create a contextual command, set the logger
195199 cmd = gc .buildCommand (ctx )
196- logg := log .Logger .With ().Ctx (ctx ).Str ("module" , "com" ).Str ("command" , cmd .String ()).Logger ()
200+
201+ // Get a debug-logger from the context
202+ var (
203+ log logger.Logger
204+ ok bool
205+ )
206+
207+ if log , ok = parentCtx .Value (LoggerKey ).(logger.Logger ); ! ok {
208+ log = nil
209+ }
210+
211+ // FIXME: this is manual silencing - should be possible to enable this with some debug flag
212+ conLog := logger .NewLogger (log ).Set ("command" , cmd .String ())
213+ emLog := logger .NewLogger (nil ).Set ("command" , cmd .String ())
197214
198215 gc .exec = & execution {
199216 context : ctx ,
200217 cancel : ctxCancel ,
201218 command : cmd ,
202- log : logg ,
219+ log : conLog ,
203220 }
204221
205222 // Prepare pipes
206- pipes , err = newStdPipes (ctx , logg , gc .ptyStdout , gc .ptyStderr , gc .ptyStdin , gc .writers )
223+ pipes , err = newStdPipes (ctx , emLog , gc .ptyStdout , gc .ptyStderr , gc .ptyStdin , gc .writers )
207224 if err != nil {
208225 ctxCancel ()
209226
@@ -223,7 +240,7 @@ func (gc *Command) Run(parentCtx context.Context) error {
223240 // Start it
224241 if err = cmd .Start (); err != nil {
225242 // On failure, can the context, wrap whatever we have and return
226- gc .exec .log .Warn (). Err ( err ). Msg ( "start failed" )
243+ gc .exec .log .Log ( "start failed" , err )
227244
228245 gc .exec .err = errors .Join (ErrFailedStarting , err )
229246
@@ -239,13 +256,11 @@ func (gc *Command) Run(parentCtx context.Context) error {
239256 // There is no good reason for this to happen, so, log it
240257 err = gc .wrap ()
241258
242- gc .exec .log .Error ().
243- Err (ctx .Err ()).
244- Err (err ).
245- Str ("stdout" , gc .result .Stdout ).
246- Str ("stderr" , gc .result .Stderr ).
247- Int ("exit" , gc .result .ExitCode ).
248- Send ()
259+ gc .exec .log .Log ("stdout" , gc .result .Stdout )
260+ gc .exec .log .Log ("stderr" , gc .result .Stderr )
261+ gc .exec .log .Log ("exitcode" , gc .result .ExitCode )
262+ gc .exec .log .Log ("err" , err )
263+ gc .exec .log .Log ("ctxerr" , ctx .Err ())
249264
250265 return err
251266 default :
@@ -328,7 +343,7 @@ func (gc *Command) wrap() error {
328343 if cmd .ProcessState != nil {
329344 var ok bool
330345 if status , ok = cmd .ProcessState .Sys ().(syscall.WaitStatus ); ! ok {
331- log . Panic (). Msg ("failed casting process state sys" )
346+ panic ("failed casting process state sys" )
332347 }
333348
334349 if status .Signaled () {
@@ -415,7 +430,7 @@ func (gc *Command) buildCommand(ctx context.Context) *exec.Cmd {
415430 // Attach platform ProcAttr and get optional custom cancellation routine
416431 if cancellation := addAttr (cmd ); cancellation != nil {
417432 cmd .Cancel = func () error {
418- gc .exec .log .Trace (). Msg ("command cancelled" )
433+ gc .exec .log .Log ("command cancelled" )
419434
420435 // Call the platform dependent cancellation routine
421436 return cancellation ()
0 commit comments