Skip to content

Commit 5b26d82

Browse files
committed
feat: tries to fix rendering of application logs
1 parent 232520d commit 5b26d82

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

pkg/logging/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func New(opts Options) *slog.Logger {
9898
},
9999
},
100100
}
101+
101102
sl := l.Slog()
102103
if opts.SetAsDefaultLogger {
103104
slog.SetDefault(sl)

pkg/runfile/run.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runfile
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io"
@@ -61,9 +62,20 @@ type runTaskArgs struct {
6162
envOverrides map[string]string
6263
}
6364

65+
type outputWriter struct {
66+
mu sync.Mutex
67+
writer io.Writer
68+
}
69+
70+
func (ow *outputWriter) Write(p []byte) (n int, err error) {
71+
ow.mu.Lock()
72+
defer ow.mu.Unlock()
73+
return ow.writer.Write(p)
74+
}
75+
6476
func processOutput(writer io.Writer, reader io.Reader, prefix *string) {
6577
prevByte := byte('\n')
66-
msg := make([]byte, 1)
78+
msg := make([]byte, 1024)
6779
for {
6880
n, err := reader.Read(msg)
6981
if err != nil {
@@ -74,17 +86,33 @@ func processOutput(writer io.Writer, reader io.Reader, prefix *string) {
7486
}
7587
}
7688

77-
if n != 1 {
78-
continue
89+
if n > 0 {
90+
for i := 0; i < n; i++ {
91+
if prevByte == '\n' && prefix != nil {
92+
writer.Write([]byte(*prefix)) // Write prefix at the start of a line
93+
}
94+
writer.Write([]byte{msg[i]}) // Write the current byte
95+
prevByte = msg[i]
96+
}
7997
}
98+
}
99+
}
80100

81-
if prevByte == '\n' && prefix != nil {
82-
// os.Stdout.WriteString(fmt.Sprintf("HERE... msg: '%s'", msg[:n]))
83-
writer.Write([]byte(*prefix))
101+
func processOutputLineByLine(writer io.Writer, reader io.Reader, prefix *string) {
102+
r := bufio.NewReader(reader)
103+
for {
104+
b, err := r.ReadBytes('\n')
105+
if err != nil {
106+
// logger.Info("stdout", "msg", string(msg[:n]), "err", err)
107+
if errors.Is(err, io.EOF) {
108+
writer.Write([]byte(*prefix))
109+
writer.Write(b)
110+
return
111+
}
84112
}
85113

86-
writer.Write(msg[:n])
87-
prevByte = msg[0]
114+
writer.Write([]byte(*prefix))
115+
writer.Write(b)
88116
}
89117
}
90118

@@ -150,14 +178,17 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error {
150178
go func() {
151179
defer wg.Done()
152180
logPrefix := fmt.Sprintf("%s ", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", strings.Join(trail, "/"))))
153-
processOutput(os.Stdout, stdoutR, &logPrefix)
181+
// processOutput(os.Stdout, stdoutR, &logPrefix)
182+
processOutput(&outputWriter{writer: os.Stdout}, stdoutR, &logPrefix)
154183
}()
155184

156185
wg.Add(1)
157186
go func() {
158187
defer wg.Done()
159188
logPrefix := fmt.Sprintf("%s ", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", strings.Join(trail, "/"))))
160-
processOutput(os.Stderr, stderrR, &logPrefix)
189+
// processOutputLineByLine(os.Stderr, stderrR, &logPrefix)
190+
processOutput(&outputWriter{writer: os.Stderr}, stderrR, &logPrefix)
191+
// processOutputLineByLine(os.Stderr, stderrR, &logPrefix)
161192
}()
162193

163194
cmd := createCommand(ctx, cmdArgs{

0 commit comments

Comments
 (0)