(root string, maxLines int)
| 220 | } |
| 221 | |
| 222 | func capMetricsLog(root string, maxLines int) error { |
| 223 | if maxLines <= 0 { |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | path := MetricsPath(root) |
| 228 | data, err := os.ReadFile(path) |
| 229 | if err != nil { |
| 230 | if os.IsNotExist(err) { |
| 231 | return nil |
| 232 | } |
| 233 | return err |
| 234 | } |
| 235 | data = bytes.TrimSpace(data) |
| 236 | if len(data) == 0 { |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | lines := bytes.Split(data, []byte("\n")) |
| 241 | if len(lines) <= maxLines { |
| 242 | return nil |
| 243 | } |
| 244 | |
| 245 | trimmed := bytes.Join(lines[len(lines)-maxLines:], []byte("\n")) |
| 246 | trimmed = append(trimmed, '\n') |
| 247 | tmpPath := path + ".tmp" |
| 248 | if err := os.WriteFile(tmpPath, trimmed, 0644); err != nil { |
| 249 | return err |
| 250 | } |
| 251 | return os.Rename(tmpPath, path) |
| 252 | } |
no test coverage detected