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