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 6 commits
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.follow, "follow", "f", false, "Stream the log file")
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.follow)
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
follow bool
}

func (opts *DevBaseOptions) complete(ctx context.Context, args []string) error {
configHome, ok := ctx.Value(constants.ConfigKey{}).(string)
if !ok {
Expand Down
35 changes: 31 additions & 4 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, follow bool) error {
harnessPath := constants.HarnessPath(configHome)
logPath := filepath.Join(harnessPath, constants.HarnessLogFile)
logFile, err := os.Open(logPath)
Expand All @@ -181,10 +183,35 @@ 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)
reader := bufio.NewReader(logFile)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
if !follow {
return nil
}
time.Sleep(1 * time.Second)
serverStatus := checkExistence(configHome)
if serverStatus {
return fmt.Errorf("server stopped")
}
continue
srikary12 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return fmt.Errorf("failed to print log file: %w", err)
}
}
if _, err := w.Write([]byte(line)); err != nil {
return fmt.Errorf("failed to write to output: %w", err)
}
}
return nil
}

func checkExistence(configHome string) bool {
pidFile := filepath.Join(constants.HarnessPath(configHome), constants.HarnessProcessFile)

_, err := readPIDFromFile(pidFile)
return os.IsNotExist(err)
}
srikary12 marked this conversation as resolved.
Show resolved Hide resolved

func isProcessRunning(pid int) bool {
Expand Down