forked from docker/model-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.go
More file actions
227 lines (204 loc) · 5.86 KB
/
logs.go
File metadata and controls
227 lines (204 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package commands
import (
"bufio"
"context"
"errors"
"fmt"
"github.com/docker/model-runner/cmd/cli/pkg/types"
"io"
"os"
"os/signal"
"path/filepath"
"regexp"
"runtime"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/model-runner/cmd/cli/commands/completion"
"github.com/docker/model-runner/cmd/cli/desktop"
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
"github.com/nxadm/tail"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)
func newLogsCmd() *cobra.Command {
var follow, noEngines bool
c := &cobra.Command{
Use: "logs [OPTIONS]",
Short: "Fetch the Docker Model Runner logs",
RunE: func(cmd *cobra.Command, args []string) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
// If we're running in standalone mode, then print the container
// logs.
engineKind := modelRunner.EngineKind()
useStandaloneLogs := engineKind == types.ModelRunnerEngineKindMoby ||
engineKind == types.ModelRunnerEngineKindCloud
if useStandaloneLogs {
dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext())
if err != nil {
return fmt.Errorf("failed to create Docker client: %w", err)
}
ctrID, _, _, err := standalone.FindControllerContainer(cmd.Context(), dockerClient)
if err != nil {
return fmt.Errorf("unable to identify Model Runner container: %w", err)
} else if ctrID == "" {
return errors.New("unable to identify Model Runner container")
}
log, err := dockerClient.ContainerLogs(cmd.Context(), ctrID, container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: follow,
})
if err != nil {
return fmt.Errorf("unable to query Model Runner container logs: %w", err)
}
defer log.Close()
_, err = stdcopy.StdCopy(os.Stdout, os.Stderr, log)
return err
}
var serviceLogPath string
var runtimeLogPath string
switch {
case runtime.GOOS == "darwin":
serviceLogPath = filepath.Join(homeDir, "Library/Containers/com.docker.docker/Data/log/host/inference.log")
runtimeLogPath = filepath.Join(homeDir, "Library/Containers/com.docker.docker/Data/log/host/inference-llama.cpp-server.log")
case runtime.GOOS == "windows":
serviceLogPath = filepath.Join(homeDir, "AppData/Local/Docker/log/host/inference.log")
runtimeLogPath = filepath.Join(homeDir, "AppData/Local/Docker/log/host/inference-llama.cpp-server.log")
default:
return fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
if noEngines {
err = printMergedLog(serviceLogPath, "")
if err != nil {
return err
}
} else {
err = printMergedLog(serviceLogPath, runtimeLogPath)
if err != nil {
return err
}
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
defer cancel()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
t, err := tail.TailFile(
serviceLogPath, tail.Config{Location: &tail.SeekInfo{Offset: 0, Whence: io.SeekEnd}, Follow: follow, ReOpen: follow},
)
if err != nil {
return err
}
for {
select {
case line, ok := <-t.Lines:
if !ok {
return nil
}
fmt.Println(line.Text)
case <-ctx.Done():
return t.Stop()
}
}
})
if follow && !noEngines {
// Show inference engines logs if `follow` is enabled
// and the engines logs have not been skipped by setting `--no-engines`.
g.Go(func() error {
t, err := tail.TailFile(
runtimeLogPath, tail.Config{Location: &tail.SeekInfo{Offset: 0, Whence: io.SeekEnd}, Follow: follow, ReOpen: follow},
)
if err != nil {
return err
}
for {
select {
case line, ok := <-t.Lines:
if !ok {
return nil
}
fmt.Println(line.Text)
case <-ctx.Done():
return t.Stop()
}
}
})
}
return g.Wait()
},
ValidArgsFunction: completion.NoComplete,
}
c.Flags().BoolVarP(&follow, "follow", "f", false, "View logs with real-time streaming")
c.Flags().BoolVar(&noEngines, "no-engines", false, "Exclude inference engine logs from the output")
return c
}
var timestampRe = regexp.MustCompile(`\[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z)\].*`)
const timeFmt = "2006-01-02T15:04:05.000000000Z"
func printTillFirstTimestamp(logScanner *bufio.Scanner) (time.Time, string) {
if logScanner == nil {
return time.Time{}, ""
}
for logScanner.Scan() {
text := logScanner.Text()
match := timestampRe.FindStringSubmatch(text)
if len(match) == 2 {
timestamp, err := time.Parse(timeFmt, match[1])
if err != nil {
println(text)
continue
}
return timestamp, text
} else {
println(text)
}
}
return time.Time{}, ""
}
func printMergedLog(logPath1, logPath2 string) error {
var logScanner1 *bufio.Scanner
if logPath1 != "" {
logFile1, err := os.Open(logPath1)
if err == nil {
defer logFile1.Close()
logScanner1 = bufio.NewScanner(logFile1)
}
}
var logScanner2 *bufio.Scanner
if logPath2 != "" {
logFile2, err := os.Open(logPath2)
if err == nil {
defer logFile2.Close()
logScanner2 = bufio.NewScanner(logFile2)
}
}
var timestamp1 time.Time
var timestamp2 time.Time
var log1Line string
var log2Name string
timestamp1, log1Line = printTillFirstTimestamp(logScanner1)
timestamp2, log2Name = printTillFirstTimestamp(logScanner2)
for log1Line != "" && log2Name != "" {
for log1Line != "" && timestamp1.Before(timestamp2) {
println(log1Line)
timestamp1, log1Line = printTillFirstTimestamp(logScanner1)
}
for log2Name != "" && timestamp2.Before(timestamp1) {
println(log2Name)
timestamp2, log2Name = printTillFirstTimestamp(logScanner2)
}
}
if log1Line != "" {
for logScanner1.Scan() {
println(logScanner1.Text())
}
}
if log2Name != "" {
for logScanner2.Scan() {
println(logScanner2.Text())
}
}
return nil
}