mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
add LogCollector interface, docker, mock log collectors
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
|
||||
"github.com/bcicen/ctop/models"
|
||||
api "github.com/fsouza/go-dockerclient"
|
||||
)
|
||||
@@ -68,29 +65,8 @@ func (c *Docker) Stream() chan models.Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
||||
func (c *Docker) StreamLogs() (chan string, error) {
|
||||
r, w := io.Pipe()
|
||||
logCh := make(chan string)
|
||||
|
||||
opts := api.LogsOptions{
|
||||
Container: c.id,
|
||||
OutputStream: w,
|
||||
ErrorStream: w,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
Tail: "10",
|
||||
Follow: true,
|
||||
Timestamps: true,
|
||||
}
|
||||
|
||||
go tailLogs(r, logCh)
|
||||
go func() {
|
||||
err := c.client.Logs(opts)
|
||||
if err != nil {
|
||||
log.Errorf("error reading container logs: %s", err)
|
||||
}
|
||||
}()
|
||||
return logCh, nil
|
||||
func (c *Docker) Logs() LogCollector {
|
||||
return &DockerLogs{c.id, c.client, make(chan bool)}
|
||||
}
|
||||
|
||||
// Stop collector
|
||||
@@ -139,10 +115,3 @@ func (c *Docker) ReadIO(stats *api.Stats) {
|
||||
}
|
||||
c.IOBytesRead, c.IOBytesWrite = read, write
|
||||
}
|
||||
|
||||
func tailLogs(reader io.Reader, ch chan string) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
ch <- scanner.Text()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user