github.com/vmware/govmomi@v0.51.0/object/diagnostic_log.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package object
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"io"
    11  	"math"
    12  )
    13  
    14  // DiagnosticLog wraps DiagnosticManager.BrowseLog
    15  type DiagnosticLog struct {
    16  	m DiagnosticManager
    17  
    18  	Key  string
    19  	Host *HostSystem
    20  
    21  	Start int32
    22  }
    23  
    24  // Seek to log position starting at the last nlines of the log
    25  func (l *DiagnosticLog) Seek(ctx context.Context, nlines int32) error {
    26  	h, err := l.m.BrowseLog(ctx, l.Host, l.Key, math.MaxInt32, 0)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	l.Start = h.LineEnd - nlines
    32  
    33  	return nil
    34  }
    35  
    36  // Copy log starting from l.Start to the given io.Writer
    37  // Returns on error or when end of log is reached.
    38  func (l *DiagnosticLog) Copy(ctx context.Context, w io.Writer) (int, error) {
    39  	const max = 500 // VC max == 500, ESX max == 1000
    40  	written := 0
    41  
    42  	for {
    43  		h, err := l.m.BrowseLog(ctx, l.Host, l.Key, l.Start, max)
    44  		if err != nil {
    45  			return 0, err
    46  		}
    47  
    48  		for _, line := range h.LineText {
    49  			n, err := fmt.Fprintln(w, line)
    50  			written += n
    51  			if err != nil {
    52  				return written, err
    53  			}
    54  		}
    55  
    56  		l.Start += int32(len(h.LineText))
    57  
    58  		if l.Start >= h.LineEnd {
    59  			break
    60  		}
    61  	}
    62  
    63  	return written, nil
    64  }