(path string)
| 84 | } |
| 85 | |
| 86 | func 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 | } |
no test coverage detected