|
| 1 | +/* |
| 2 | + * progress_sink.c — Human-readable progress for --progress CLI flag. |
| 3 | + * |
| 4 | + * Maps structured log events (msg=pass.timing, msg=pipeline.done, etc.) |
| 5 | + * to phase labels on stderr. When installed, replaces default log output. |
| 6 | + * |
| 7 | + * Thread-safe: fprintf has per-FILE* locking on POSIX. |
| 8 | + */ |
| 9 | +#include "progress_sink.h" |
| 10 | +#include "foundation/constants.h" |
| 11 | +#include "foundation/log.h" |
| 12 | + |
| 13 | +#include <stdatomic.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | + |
| 18 | +enum { PERCENT = 100, NOT_SET = -1 }; |
| 19 | + |
| 20 | +static FILE *s_out; |
| 21 | +static atomic_int s_needs_newline; |
| 22 | +static int s_gbuf_nodes = NOT_SET; |
| 23 | +static int s_gbuf_edges = NOT_SET; |
| 24 | + |
| 25 | +/* Extract value of "key=VALUE" from a structured log line. */ |
| 26 | +static const char *extract_kv(const char *line, const char *key, char *buf, int buf_len) { |
| 27 | + if (!line || !key || !buf || buf_len <= 0) { |
| 28 | + return NULL; |
| 29 | + } |
| 30 | + size_t klen = strlen(key); |
| 31 | + const char *p = line; |
| 32 | + while (*p) { |
| 33 | + if ((p == line || p[-SKIP_ONE] == ' ') && strncmp(p, key, klen) == 0 && p[klen] == '=') { |
| 34 | + const char *val = p + klen + SKIP_ONE; |
| 35 | + int i = 0; |
| 36 | + while (val[i] && val[i] != ' ' && i < buf_len - SKIP_ONE) { |
| 37 | + buf[i] = val[i]; |
| 38 | + i++; |
| 39 | + } |
| 40 | + buf[i] = '\0'; |
| 41 | + return buf; |
| 42 | + } |
| 43 | + p++; |
| 44 | + } |
| 45 | + return NULL; |
| 46 | +} |
| 47 | + |
| 48 | +void cbm_progress_sink_init(FILE *out) { |
| 49 | + s_out = out ? out : stderr; |
| 50 | + atomic_store(&s_needs_newline, 0); |
| 51 | + s_gbuf_nodes = NOT_SET; |
| 52 | + s_gbuf_edges = NOT_SET; |
| 53 | + cbm_log_set_sink(cbm_progress_sink_fn); |
| 54 | +} |
| 55 | + |
| 56 | +void cbm_progress_sink_fini(void) { |
| 57 | + if (atomic_load(&s_needs_newline) && s_out) { |
| 58 | + (void)fprintf(s_out, "\n"); |
| 59 | + (void)fflush(s_out); |
| 60 | + } |
| 61 | + cbm_log_set_sink(NULL); |
| 62 | + s_out = NULL; |
| 63 | +} |
| 64 | + |
| 65 | +/* Phase label table: maps pass names to display labels. */ |
| 66 | +typedef struct { |
| 67 | + const char *pass; |
| 68 | + const char *label; |
| 69 | +} phase_t; |
| 70 | + |
| 71 | +static const phase_t phases[] = { |
| 72 | + {"parallel_extract", "[2/9] Extracting definitions"}, |
| 73 | + {"registry_build", "[3/9] Building registry"}, |
| 74 | + {"parallel_resolve", "[4/9] Resolving calls & edges"}, |
| 75 | + {"tests", "[5/9] Detecting tests"}, |
| 76 | + {"httplinks", "[6/9] Scanning HTTP links"}, |
| 77 | + {"githistory_compute", "[7/9] Analyzing git history"}, |
| 78 | + {"configlink", "[8/9] Linking config files"}, |
| 79 | + {"dump", "[9/9] Writing database"}, |
| 80 | +}; |
| 81 | + |
| 82 | +enum { PHASE_COUNT = sizeof(phases) / sizeof(phases[0]) }; |
| 83 | + |
| 84 | +/* Flush pending \r line if needed. */ |
| 85 | +static void flush_carriage(void) { |
| 86 | + if (atomic_load(&s_needs_newline)) { |
| 87 | + (void)fprintf(s_out, "\n"); |
| 88 | + atomic_store(&s_needs_newline, 0); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/* Handle pipeline.discover event. */ |
| 93 | +static void on_discover(const char *line) { |
| 94 | + char files[CBM_SZ_32] = {0}; |
| 95 | + if (extract_kv(line, "files", files, (int)sizeof(files))) { |
| 96 | + (void)fprintf(s_out, " Discovering files (%s found)\n", files); |
| 97 | + } else { |
| 98 | + (void)fprintf(s_out, " Discovering files...\n"); |
| 99 | + } |
| 100 | + (void)fflush(s_out); |
| 101 | +} |
| 102 | + |
| 103 | +/* Handle pipeline.route event. */ |
| 104 | +static void on_route(const char *line) { |
| 105 | + char val[CBM_SZ_32] = {0}; |
| 106 | + const char *path = extract_kv(line, "path", val, (int)sizeof(val)); |
| 107 | + if (path && strcmp(path, "incremental") == 0) { |
| 108 | + (void)fprintf(s_out, " Starting incremental index\n"); |
| 109 | + } else { |
| 110 | + (void)fprintf(s_out, " Starting full index\n"); |
| 111 | + } |
| 112 | + (void)fflush(s_out); |
| 113 | +} |
| 114 | + |
| 115 | +/* Handle pass.start event. */ |
| 116 | +static void on_pass_start(const char *line) { |
| 117 | + char val[CBM_SZ_64] = {0}; |
| 118 | + const char *pass = extract_kv(line, "pass", val, (int)sizeof(val)); |
| 119 | + if (pass && strcmp(pass, "structure") == 0) { |
| 120 | + (void)fprintf(s_out, "[1/9] Building file structure\n"); |
| 121 | + (void)fflush(s_out); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/* Handle pass.timing event. */ |
| 126 | +static void on_pass_timing(const char *line) { |
| 127 | + char val[CBM_SZ_64] = {0}; |
| 128 | + const char *pass = extract_kv(line, "pass", val, (int)sizeof(val)); |
| 129 | + if (!pass) { |
| 130 | + return; |
| 131 | + } |
| 132 | + flush_carriage(); |
| 133 | + for (int i = 0; i < PHASE_COUNT; i++) { |
| 134 | + if (strcmp(pass, phases[i].pass) == 0) { |
| 135 | + (void)fprintf(s_out, "%s\n", phases[i].label); |
| 136 | + (void)fflush(s_out); |
| 137 | + return; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/* Handle gbuf.dump event — capture node/edge counts. */ |
| 143 | +static void on_gbuf_dump(const char *line) { |
| 144 | + char n[CBM_SZ_32] = {0}; |
| 145 | + char e[CBM_SZ_32] = {0}; |
| 146 | + if (extract_kv(line, "nodes", n, (int)sizeof(n))) { |
| 147 | + s_gbuf_nodes = (int)strtol(n, NULL, CBM_DECIMAL_BASE); |
| 148 | + } |
| 149 | + if (extract_kv(line, "edges", e, (int)sizeof(e))) { |
| 150 | + s_gbuf_edges = (int)strtol(e, NULL, CBM_DECIMAL_BASE); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +/* Handle pipeline.done event. */ |
| 155 | +static void on_done(const char *line) { |
| 156 | + flush_carriage(); |
| 157 | + char ms[CBM_SZ_32] = {0}; |
| 158 | + const char *elapsed = extract_kv(line, "elapsed_ms", ms, (int)sizeof(ms)); |
| 159 | + if (s_gbuf_nodes >= 0 && s_gbuf_edges >= 0 && elapsed) { |
| 160 | + (void)fprintf(s_out, "Done: %d nodes, %d edges (%s ms)\n", s_gbuf_nodes, s_gbuf_edges, |
| 161 | + elapsed); |
| 162 | + } else if (s_gbuf_nodes >= 0 && s_gbuf_edges >= 0) { |
| 163 | + (void)fprintf(s_out, "Done: %d nodes, %d edges\n", s_gbuf_nodes, s_gbuf_edges); |
| 164 | + } else { |
| 165 | + (void)fprintf(s_out, "Done.\n"); |
| 166 | + } |
| 167 | + (void)fflush(s_out); |
| 168 | +} |
| 169 | + |
| 170 | +/* Handle parallel.extract.progress event — in-place counter. */ |
| 171 | +static void on_extract_progress(const char *line) { |
| 172 | + char done[CBM_SZ_32] = {0}; |
| 173 | + char total[CBM_SZ_32] = {0}; |
| 174 | + if (extract_kv(line, "done", done, (int)sizeof(done)) && |
| 175 | + extract_kv(line, "total", total, (int)sizeof(total))) { |
| 176 | + long d = strtol(done, NULL, CBM_DECIMAL_BASE); |
| 177 | + long t = strtol(total, NULL, CBM_DECIMAL_BASE); |
| 178 | + int pct = (t > 0) ? (int)((d * PERCENT) / t) : 0; |
| 179 | + (void)fprintf(s_out, "\r Extracting: %ld/%ld files (%d%%)", d, t, pct); |
| 180 | + (void)fflush(s_out); |
| 181 | + atomic_store(&s_needs_newline, SKIP_ONE); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/* Event dispatch table. */ |
| 186 | +typedef struct { |
| 187 | + const char *msg; |
| 188 | + void (*handler)(const char *line); |
| 189 | +} event_handler_t; |
| 190 | + |
| 191 | +static const event_handler_t handlers[] = { |
| 192 | + {"pipeline.discover", on_discover}, |
| 193 | + {"pipeline.route", on_route}, |
| 194 | + {"pass.start", on_pass_start}, |
| 195 | + {"pass.timing", on_pass_timing}, |
| 196 | + {"gbuf.dump", on_gbuf_dump}, |
| 197 | + {"pipeline.done", on_done}, |
| 198 | + {"parallel.extract.progress", on_extract_progress}, |
| 199 | +}; |
| 200 | + |
| 201 | +enum { HANDLER_COUNT = sizeof(handlers) / sizeof(handlers[0]) }; |
| 202 | + |
| 203 | +void cbm_progress_sink_fn(const char *line) { |
| 204 | + if (!line || !s_out) { |
| 205 | + return; |
| 206 | + } |
| 207 | + char msg[CBM_SZ_64] = {0}; |
| 208 | + if (!extract_kv(line, "msg", msg, (int)sizeof(msg))) { |
| 209 | + return; |
| 210 | + } |
| 211 | + for (int i = 0; i < HANDLER_COUNT; i++) { |
| 212 | + if (strcmp(msg, handlers[i].msg) == 0) { |
| 213 | + handlers[i].handler(line); |
| 214 | + return; |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments