diagnostic_log.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright (c) 2015 VMware, Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package object
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "math"
  19. )
  20. // DiagnosticLog wraps DiagnosticManager.BrowseLog
  21. type DiagnosticLog struct {
  22. m DiagnosticManager
  23. Key string
  24. Host *HostSystem
  25. Start int32
  26. }
  27. // Seek to log position starting at the last nlines of the log
  28. func (l *DiagnosticLog) Seek(ctx context.Context, nlines int32) error {
  29. h, err := l.m.BrowseLog(ctx, l.Host, l.Key, math.MaxInt32, 0)
  30. if err != nil {
  31. return err
  32. }
  33. l.Start = h.LineEnd - nlines
  34. return nil
  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. for {
  42. h, err := l.m.BrowseLog(ctx, l.Host, l.Key, l.Start, max)
  43. if err != nil {
  44. return 0, err
  45. }
  46. for _, line := range h.LineText {
  47. n, err := fmt.Fprintln(w, line)
  48. written += n
  49. if err != nil {
  50. return written, err
  51. }
  52. }
  53. l.Start += int32(len(h.LineText))
  54. if l.Start >= h.LineEnd {
  55. break
  56. }
  57. }
  58. return written, nil
  59. }