Writer returns an io.Writer that logs all writes in a separate goroutine. It is the responsibility of the caller to call the returned function to stop the goroutine.
(logf Func)
| 51 | // It is the responsibility of the caller to call the returned |
| 52 | // function to stop the goroutine. |
| 53 | func Writer(logf Func) (io.Writer, func()) { |
| 54 | pipeReader, pipeWriter := io.Pipe() |
| 55 | doneCh := make(chan struct{}) |
| 56 | go func() { |
| 57 | defer pipeWriter.Close() |
| 58 | defer pipeReader.Close() |
| 59 | scanner := bufio.NewScanner(pipeReader) |
| 60 | for { |
| 61 | select { |
| 62 | case <-doneCh: |
| 63 | return |
| 64 | default: |
| 65 | if !scanner.Scan() { |
| 66 | return |
| 67 | } |
| 68 | logf(LevelInfo, "%s", scanner.Text()) |
| 69 | } |
| 70 | } |
| 71 | }() |
| 72 | closer := func() { |
| 73 | close(doneCh) |
| 74 | } |
| 75 | return pipeWriter, closer |
| 76 | } |
no test coverage detected