MCPcopy
hub / github.com/wavetermdev/waveterm / tailLogFile

Function tailLogFile

cmd/wsh/cmd/wshcmd-wavepath.go:86–141  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

84}
85
86func tailLogFile(path string) error {
87 file, err := os.Open(path)
88 if err != nil {
89 return fmt.Errorf("opening log file: %w", err)
90 }
91 defer file.Close()
92
93 // Get file size
94 stat, err := file.Stat()
95 if err != nil {
96 return fmt.Errorf("getting file stats: %w", err)
97 }
98
99 // Read last 16KB or whole file if smaller
100 readSize := int64(16 * 1024)
101 var offset int64
102 if stat.Size() > readSize {
103 offset = stat.Size() - readSize
104 }
105
106 _, err = file.Seek(offset, 0)
107 if err != nil {
108 return fmt.Errorf("seeking file: %w", err)
109 }
110
111 buf := make([]byte, readSize)
112 n, err := file.Read(buf)
113 if err != nil && err != io.EOF {
114 return fmt.Errorf("reading file: %w", err)
115 }
116 buf = buf[:n]
117
118 // Skip partial line at start if we're not at beginning of file
119 if offset > 0 {
120 idx := bytes.IndexByte(buf, '\n')
121 if idx >= 0 {
122 buf = buf[idx+1:]
123 }
124 }
125
126 // Split into lines
127 lines := bytes.Split(buf, []byte{'\n'})
128
129 // Take last 100 lines if we have more
130 startIdx := 0
131 if len(lines) > 100 {
132 startIdx = len(lines) - 100
133 }
134
135 // Print lines
136 for _, line := range lines[startIdx:] {
137 WriteStdout("%s\n", string(line))
138 }
139
140 return nil
141}

Callers 1

wavepathRunFunction · 0.85

Calls 6

WriteStdoutFunction · 0.85
OpenMethod · 0.80
StatMethod · 0.80
CloseMethod · 0.65
SizeMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected