github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/log.go (about)

     1  package calcium
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  
     7  	enginetypes "github.com/projecteru2/core/engine/types"
     8  	"github.com/projecteru2/core/log"
     9  	"github.com/projecteru2/core/types"
    10  )
    11  
    12  // LogStream log stream for one workload
    13  func (c *Calcium) LogStream(ctx context.Context, opts *types.LogStreamOptions) (chan *types.LogStreamMessage, error) {
    14  	logger := log.WithFunc("calcium.LogStream").WithField("opts", opts)
    15  	ch := make(chan *types.LogStreamMessage)
    16  	_ = c.pool.Invoke(func() {
    17  		defer close(ch)
    18  		workload, err := c.GetWorkload(ctx, opts.ID)
    19  		if err != nil {
    20  			logger.Error(ctx, err)
    21  			ch <- &types.LogStreamMessage{ID: opts.ID, Error: err}
    22  			return
    23  		}
    24  
    25  		stdout, stderr, err := workload.Engine.VirtualizationLogs(ctx, &enginetypes.VirtualizationLogStreamOptions{
    26  			ID:     opts.ID,
    27  			Tail:   opts.Tail,
    28  			Since:  opts.Since,
    29  			Until:  opts.Until,
    30  			Follow: opts.Follow,
    31  			Stdout: true,
    32  			Stderr: true,
    33  		})
    34  		logger.Error(ctx, err)
    35  		if err != nil {
    36  			ch <- &types.LogStreamMessage{ID: opts.ID, Error: err}
    37  			return
    38  		}
    39  
    40  		for m := range c.processStdStream(ctx, stdout, stderr, bufio.ScanLines, byte('\n')) {
    41  			ch <- &types.LogStreamMessage{ID: opts.ID, Data: m.Data, StdStreamType: m.StdStreamType}
    42  		}
    43  	})
    44  
    45  	return ch, nil
    46  }