logger.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. package esxi
  15. import (
  16. "github.com/vmware/govmomi/vim25/progress"
  17. "yunion.io/x/log"
  18. )
  19. type logger struct {
  20. name string
  21. chid chan progress.Report
  22. over chan struct{}
  23. }
  24. func (l *logger) Sink() chan<- progress.Report {
  25. return l.chid
  26. }
  27. func (l *logger) End() {
  28. close(l.over)
  29. }
  30. func newLeaseLogger(name string, cap int) *logger {
  31. return &logger{
  32. name: name,
  33. chid: make(chan progress.Report, cap),
  34. over: make(chan struct{}),
  35. }
  36. }
  37. func (l *logger) Log() {
  38. go func() {
  39. var pre float32 = 0
  40. log.Debugf("logger.Log...")
  41. Loop:
  42. for {
  43. select {
  44. case r, ok := <-l.chid:
  45. if !ok {
  46. break Loop
  47. }
  48. if r.Error() != nil {
  49. log.Errorf("%s report, error: %s", l.name, r.Error())
  50. break Loop
  51. }
  52. if r.Percentage() >= pre {
  53. log.Debugf("%s report: speed: %s, percentage: %f%%", l.name, r.Detail(), pre)
  54. pre += 10
  55. }
  56. if r.Percentage() >= 110 {
  57. break Loop
  58. }
  59. case <-l.over:
  60. break Loop
  61. }
  62. }
  63. }()
  64. }