|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/git-lfs/git-lfs/tools" |
| 11 | + "github.com/olekukonko/ts" |
| 12 | +) |
| 13 | + |
| 14 | +// Logger logs a series of tasks to an io.Writer, processing each task in order |
| 15 | +// until completion . |
| 16 | +type Logger struct { |
| 17 | + // sink is the writer to write to. |
| 18 | + sink io.Writer |
| 19 | + |
| 20 | + // widthFn is a function that returns the width of the terminal that |
| 21 | + // this logger is running within. |
| 22 | + widthFn func() int |
| 23 | + |
| 24 | + // queue is the incoming, unbuffered queue of tasks to enqueue. |
| 25 | + queue chan Task |
| 26 | + // tasks is the set of tasks to process. |
| 27 | + tasks chan Task |
| 28 | + // wg is a WaitGroup that is incremented when new tasks are enqueued, |
| 29 | + // and decremented when tasks finish. |
| 30 | + wg *sync.WaitGroup |
| 31 | +} |
| 32 | + |
| 33 | +// NewLogger retuns a new *Logger instance that logs to "sink" and uses the |
| 34 | +// current terminal width as the width of the line. |
| 35 | +func NewLogger(sink io.Writer) *Logger { |
| 36 | + if sink == nil { |
| 37 | + sink = ioutil.Discard |
| 38 | + } |
| 39 | + |
| 40 | + l := &Logger{ |
| 41 | + sink: sink, |
| 42 | + widthFn: func() int { |
| 43 | + size, err := ts.GetSize() |
| 44 | + if err != nil { |
| 45 | + return 80 |
| 46 | + } |
| 47 | + return size.Col() |
| 48 | + }, |
| 49 | + queue: make(chan Task), |
| 50 | + tasks: make(chan Task), |
| 51 | + wg: new(sync.WaitGroup), |
| 52 | + } |
| 53 | + |
| 54 | + go l.consume() |
| 55 | + |
| 56 | + return l |
| 57 | +} |
| 58 | + |
| 59 | +// Close closes the queue and does not allow new Tasks to be `enqueue()`'d. It |
| 60 | +// waits until the currently running Task has completed. |
| 61 | +func (l *Logger) Close() { |
| 62 | + close(l.queue) |
| 63 | + |
| 64 | + l.wg.Wait() |
| 65 | +} |
| 66 | + |
| 67 | +// Waitier creates and enqueues a new *WaitingTask. |
| 68 | +func (l *Logger) Waiter(msg string) *WaitingTask { |
| 69 | + t := NewWaitingTask(msg) |
| 70 | + l.enqueue(t) |
| 71 | + |
| 72 | + return t |
| 73 | +} |
| 74 | + |
| 75 | +// Percentage creates and enqueues a new *PercentageTask. |
| 76 | +func (l *Logger) Percentage(msg string, total uint64) *PercentageTask { |
| 77 | + t := NewPercentageTask(msg, total) |
| 78 | + l.enqueue(t) |
| 79 | + |
| 80 | + return t |
| 81 | +} |
| 82 | + |
| 83 | +// enqueue enqueues the given Tasks "ts". |
| 84 | +func (l *Logger) enqueue(ts ...Task) { |
| 85 | + if l == nil { |
| 86 | + for _, t := range ts { |
| 87 | + go func() { |
| 88 | + for range <-t.Updates() { |
| 89 | + // Discard all updates. |
| 90 | + } |
| 91 | + }() |
| 92 | + } |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + l.wg.Add(len(ts)) |
| 97 | + for _, t := range ts { |
| 98 | + l.queue <- t |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// consume creates a pseudo-infinte buffer between the incoming set of tasks and |
| 103 | +// the queue of tasks to work on. |
| 104 | +func (l *Logger) consume() { |
| 105 | + go func() { |
| 106 | + // Process the single next task in sequence until completion, |
| 107 | + // then consume the next task. |
| 108 | + for task := range l.tasks { |
| 109 | + l.logTask(task) |
| 110 | + } |
| 111 | + }() |
| 112 | + |
| 113 | + pending := make([]Task, 0) |
| 114 | + |
| 115 | +L: |
| 116 | + for { |
| 117 | + // If there is a pending task, "peek" it off of the set of |
| 118 | + // pending tasks. |
| 119 | + var next Task |
| 120 | + if len(pending) > 0 { |
| 121 | + next = pending[0] |
| 122 | + } |
| 123 | + |
| 124 | + if next == nil { |
| 125 | + // If there was no pending task, wait for either a) |
| 126 | + // l.queue to close, or b) a new task to be submitted. |
| 127 | + task, ok := <-l.queue |
| 128 | + if !ok { |
| 129 | + // If the queue is closed, no more new tasks may |
| 130 | + // be added. |
| 131 | + break L |
| 132 | + } |
| 133 | + |
| 134 | + // Otherwise, add a new task to the set of tasks to |
| 135 | + // process immediately, since there is no current |
| 136 | + // buffer. |
| 137 | + l.tasks <- task |
| 138 | + } else { |
| 139 | + // If there is a pending task, wait for either a) a |
| 140 | + // write to process the task to become non-blocking, or |
| 141 | + // b) a new task to enter the queue. |
| 142 | + select { |
| 143 | + case task, ok := <-l.queue: |
| 144 | + if !ok { |
| 145 | + // If the queue is closed, no more tasks |
| 146 | + // may be added. |
| 147 | + break L |
| 148 | + } |
| 149 | + // Otherwise, add the next task to the set of |
| 150 | + // pending, active tasks. |
| 151 | + pending = append(pending, task) |
| 152 | + case l.tasks <- next: |
| 153 | + // Or "pop" the peeked task off of the pending |
| 154 | + // set. |
| 155 | + pending = pending[1:] |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + close(l.tasks) |
| 161 | +} |
| 162 | + |
| 163 | +// logTask logs the set of updates from a given task to the sink, then logs a |
| 164 | +// "done" message, and then marks the task as done. |
| 165 | +func (l *Logger) logTask(task Task) { |
| 166 | + defer l.wg.Done() |
| 167 | + |
| 168 | + var last string |
| 169 | + for last = range task.Updates() { |
| 170 | + l.logLine(last) |
| 171 | + } |
| 172 | + |
| 173 | + l.log(fmt.Sprintf("%s, done\n", last)) |
| 174 | +} |
| 175 | + |
| 176 | +// logLine writes a complete line and moves the cursor to the beginning of the |
| 177 | +// line. |
| 178 | +// |
| 179 | +// It returns the number of bytes "n" written to the sink and the error "err", |
| 180 | +// if one was encountered. |
| 181 | +func (l *Logger) logLine(str string) (n int, err error) { |
| 182 | + padding := strings.Repeat(" ", tools.MaxInt(0, l.widthFn()-len(str))) |
| 183 | + |
| 184 | + return l.log(str + padding + "\r") |
| 185 | +} |
| 186 | + |
| 187 | +// log writes a string verbatim to the sink. |
| 188 | +// |
| 189 | +// It returns the number of bytes "n" written to the sink and the error "err", |
| 190 | +// if one was encountered. |
| 191 | +func (l *Logger) log(str string) (n int, err error) { |
| 192 | + return fmt.Fprint(l.sink, str) |
| 193 | +} |
0 commit comments