Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat:] Add Log tailing #610

Merged
merged 8 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pkg/cmd/dev/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ func DevStopCommand() *cobra.Command {
}

func DevLogsCommand() *cobra.Command {
opts := &DevBaseOptions{}
opts := &DevLogsOptions{}
cmd := &cobra.Command{
Use: "logs",
Short: devLogsShortDesc,
Long: devLogsLongDesc,
Run: runLogsCommand(opts),
}
cmd.Flags().BoolVarP(&opts.tail, "tail", "t", false, "Tail the log file")
srikary12 marked this conversation as resolved.
Show resolved Hide resolved
return cmd
}

Expand Down Expand Up @@ -113,14 +114,14 @@ func runStopCommand(opts *DevBaseOptions) func(cmd *cobra.Command, args []string
}
}

func runLogsCommand(opts *DevBaseOptions) func(cmd *cobra.Command, args []string) {
func runLogsCommand(opts *DevLogsOptions) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
if err := opts.complete(cmd.Context(), args); err != nil {
output.Errorf("failed to complete options: %s", err)
return
}

err := harness.PrintLogs(opts.configHome, cmd.OutOrStdout())
err := harness.PrintLogs(opts.configHome, cmd.OutOrStdout(), opts.tail)
if err != nil {
output.Errorln(err)
os.Exit(1)
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmd/dev/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type DevBaseOptions struct {
configHome string
}

type DevLogsOptions struct {
DevBaseOptions
tail bool
}

func (opts *DevBaseOptions) complete(ctx context.Context, args []string) error {
configHome, ok := ctx.Value(constants.ConfigKey{}).(string)
if !ok {
Expand Down
26 changes: 23 additions & 3 deletions pkg/lib/harness/llm-harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package harness

import (
"bufio"
"errors"
"fmt"
"io"
Expand All @@ -28,6 +29,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"kitops/pkg/lib/constants"
"kitops/pkg/output"
Expand Down Expand Up @@ -169,7 +171,7 @@ func (harness *LLMHarness) Stop() error {
return nil
}

func PrintLogs(configHome string, w io.Writer) error {
func PrintLogs(configHome string, w io.Writer, tail bool) error {
harnessPath := constants.HarnessPath(configHome)
logPath := filepath.Join(harnessPath, constants.HarnessLogFile)
logFile, err := os.Open(logPath)
Expand All @@ -181,8 +183,26 @@ func PrintLogs(configHome string, w io.Writer) error {
return fmt.Errorf("error reading log file: %w", err)
}
defer logFile.Close()
if _, err = io.Copy(w, logFile); err != nil {
return fmt.Errorf("failed to print log file: %w", err)
if !tail {
if _, err = io.Copy(w, logFile); err != nil {
return fmt.Errorf("failed to print log file: %w", err)
}
srikary12 marked this conversation as resolved.
Show resolved Hide resolved
} else {
reader := bufio.NewReader(logFile)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
time.Sleep(1 * time.Second)
} else {
return fmt.Errorf("failed to print log file: %w", err)
}
srikary12 marked this conversation as resolved.
Show resolved Hide resolved
}
stringReader := strings.NewReader(line)
if _, err = io.Copy(w, stringReader); err != nil {
return fmt.Errorf("failed to print log file: %w", err)
srikary12 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return nil
}
Expand Down