yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/logger.go (about)

     1  // Copyright 2019 Yunion
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package esxi
    16  
    17  import (
    18  	"github.com/vmware/govmomi/vim25/progress"
    19  
    20  	"yunion.io/x/log"
    21  )
    22  
    23  type logger struct {
    24  	name string
    25  	chid chan progress.Report
    26  	over chan struct{}
    27  }
    28  
    29  func (l *logger) Sink() chan<- progress.Report {
    30  	return l.chid
    31  }
    32  
    33  func (l *logger) End() {
    34  	close(l.over)
    35  }
    36  
    37  func newLeaseLogger(name string, cap int) *logger {
    38  	return &logger{
    39  		name: name,
    40  		chid: make(chan progress.Report, cap),
    41  		over: make(chan struct{}),
    42  	}
    43  }
    44  
    45  func (l *logger) Log() {
    46  	go func() {
    47  		var pre float32 = 0
    48  
    49  		log.Debugf("logger.Log...")
    50  	Loop:
    51  		for {
    52  			select {
    53  			case r, ok := <-l.chid:
    54  				if !ok {
    55  					break Loop
    56  				}
    57  				if r.Error() != nil {
    58  					log.Errorf("%s report, error: %s", l.name, r.Error())
    59  					break Loop
    60  				}
    61  				if r.Percentage() >= pre {
    62  					log.Debugf("%s report: speed: %s, percentage: %f%%", l.name, r.Detail(), pre)
    63  					pre += 10
    64  				}
    65  				if r.Percentage() >= 110 {
    66  					break Loop
    67  				}
    68  			case <-l.over:
    69  				break Loop
    70  			}
    71  
    72  		}
    73  	}()
    74  }