MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_tail_log

Function cbm_tail_log

src/foundation/subprocess.c:125–219  ·  view source on GitHub ↗

Tail one bounded batch from the child log. While the owned tree can still * write, a partial final line remains buffered. Once the tree is quiescent, * final=true delivers that last fragment exactly once. */

Source from the content-addressed store, hash-verified

123 * write, a partial final line remains buffered. Once the tree is quiescent,
124 * final=true delivers that last fragment exactly once. */
125static cbm_tail_result_t cbm_tail_log(const char *log_file, long *tail_pos, cbm_proc_log_cb cb,
126 void *ud, bool final) {
127 cbm_tail_result_t result = {.status = CBM_TAIL_ERROR, .progressed = false};
128 if (!log_file || !tail_pos) {
129 return result;
130 }
131#ifdef _WIN32
132 FILE *lf = cbm_fopen(log_file, "r");
133#else
134 int open_flags = O_RDONLY | O_NONBLOCK;
135#ifdef O_CLOEXEC
136 open_flags |= O_CLOEXEC;
137#endif
138#ifdef O_NOFOLLOW
139 open_flags |= O_NOFOLLOW;
140#endif
141 int log_fd = open(log_file, open_flags);
142 if (log_fd < 0) {
143 return result;
144 }
145 struct stat log_status;
146 if (fstat(log_fd, &log_status) != 0 || !S_ISREG(log_status.st_mode)) {
147 (void)close(log_fd);
148 return result;
149 }
150 FILE *lf = fdopen(log_fd, "r");
151#endif
152 if (!lf) {
153#ifndef _WIN32
154 (void)close(log_fd);
155#endif
156 return result;
157 }
158 if (fseek(lf, *tail_pos, SEEK_SET) == 0) {
159 char line[1024];
160 size_t delivered_lines = 0;
161 size_t delivered_bytes = 0;
162 enum {
163 CBM_TAIL_MAX_LINES_PER_POLL = 64,
164 CBM_TAIL_MAX_BYTES_PER_POLL = 64 * 1024,
165 };
166 while (delivered_lines < CBM_TAIL_MAX_LINES_PER_POLL &&
167 delivered_bytes < CBM_TAIL_MAX_BYTES_PER_POLL) {
168 long before = ftell(lf);
169 if (!fgets(line, sizeof(line), lf)) {
170 result.status = ferror(lf) ? CBM_TAIL_ERROR : CBM_TAIL_CAUGHT_UP;
171 break;
172 }
173 size_t l = strlen(line);
174 delivered_bytes += l;
175 bool complete = (l > 0 && line[l - 1] == '\n');
176 if (complete) {
177 line[l - 1] = '\0';
178 *tail_pos = ftell(lf);
179 result.progressed = true;
180 delivered_lines++;
181 if (line[0] && cb) {
182 cb(line, ud);

Callers 1

cbm_subprocess_poll_logFunction · 0.85

Calls 3

cbm_fopenFunction · 0.85
openFunction · 0.70
closeFunction · 0.50

Tested by

no test coverage detected