improve logging from scripts

This commit is contained in:
asrael 2025-12-03 09:41:33 -06:00
parent 3313c800a9
commit 4587ba7266
7 changed files with 99 additions and 60 deletions

View file

@ -22,6 +22,7 @@ struct pxl8_repl {
char commands[PXL8_REPL_QUEUE_SIZE][PXL8_MAX_REPL_COMMAND_SIZE];
atomic_uint cmd_write_idx;
atomic_uint cmd_read_idx;
atomic_bool cmd_complete;
char logs[PXL8_REPL_QUEUE_SIZE][PXL8_MAX_REPL_COMMAND_SIZE];
atomic_uint log_write_idx;
@ -113,7 +114,6 @@ static void* pxl8_repl_thread(void* arg) {
struct linenoiseState ls;
char input_buf[PXL8_MAX_REPL_COMMAND_SIZE];
bool editing = false;
bool stopped_for_logs = false;
struct pollfd pfd = {
.fd = STDIN_FILENO,
@ -125,28 +125,23 @@ static void* pxl8_repl_thread(void* arg) {
u32 log_write_idx = atomic_load(&repl->log_write_idx);
if (log_read_idx != log_write_idx) {
printf("\r\033[K");
if (editing) {
linenoiseEditStop(&ls);
editing = false;
stopped_for_logs = true;
printf("\033[A\r\033[K");
}
while (log_read_idx != log_write_idx) {
printf("%s", repl->logs[log_read_idx]);
fflush(stdout);
atomic_store(&repl->log_read_idx, (log_read_idx + 1) % PXL8_REPL_QUEUE_SIZE);
log_read_idx = atomic_load(&repl->log_read_idx);
log_write_idx = atomic_load(&repl->log_write_idx);
}
fflush(stdout);
continue;
}
if (!editing && !atomic_load(&repl->should_quit)) {
if (stopped_for_logs) {
struct timespec ts = {.tv_sec = 0, .tv_nsec = 5000000};
nanosleep(&ts, NULL);
stopped_for_logs = false;
continue;
}
const char* prompt = (repl->accumulator[0] != '\0') ? ".. " : ">> ";
if (linenoiseEditStart(&ls, STDIN_FILENO, STDOUT_FILENO, input_buf, sizeof(input_buf), prompt) == -1) {
atomic_store(&repl->should_quit, true);
@ -197,10 +192,22 @@ static void* pxl8_repl_thread(void* arg) {
linenoiseFree(line);
while (atomic_load(&repl->cmd_write_idx) != atomic_load(&repl->cmd_read_idx)) {
while (!atomic_load(&repl->should_quit) &&
(atomic_load(&repl->cmd_write_idx) != atomic_load(&repl->cmd_read_idx) ||
!atomic_load(&repl->cmd_complete))) {
u32 lr = atomic_load(&repl->log_read_idx);
u32 lw = atomic_load(&repl->log_write_idx);
while (lr != lw) {
printf("%s", repl->logs[lr]);
atomic_store(&repl->log_read_idx, (lr + 1) % PXL8_REPL_QUEUE_SIZE);
lr = atomic_load(&repl->log_read_idx);
lw = atomic_load(&repl->log_write_idx);
}
fflush(stdout);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 1000000};
nanosleep(&ts, NULL);
}
atomic_store(&repl->cmd_complete, false);
}
if (editing) linenoiseEditStop(&ls);
@ -217,6 +224,7 @@ pxl8_repl* pxl8_repl_create(void) {
repl->accumulator[0] = '\0';
atomic_store(&repl->cmd_write_idx, 0);
atomic_store(&repl->cmd_read_idx, 0);
atomic_store(&repl->cmd_complete, true);
atomic_store(&repl->log_write_idx, 0);
atomic_store(&repl->log_read_idx, 0);
atomic_store(&repl->should_quit, false);
@ -301,3 +309,8 @@ void pxl8_repl_clear_accumulator(pxl8_repl* repl) {
if (!repl) return;
repl->accumulator[0] = '\0';
}
void pxl8_repl_signal_complete(pxl8_repl* repl) {
if (!repl) return;
atomic_store(&repl->cmd_complete, true);
}