Minimalist agentic harness in C
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384#include "log.h"
#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>
static const char* const level_names[] = { [LL_TRACE] = "TRACE", [LL_DEBUG] = "DEBUG", [LL_INFO] = "INFO", [LL_WARN] = "WARN", [LL_ERROR] = "ERROR", [LL_FATAL] = "FATAL",};
static const char* const level_styles[] = { [LL_TRACE] = ANSI("1;" ANSI_TRACE), [LL_DEBUG] = ANSI("1;" ANSI_DEBUG), [LL_INFO] = ANSI("1;" ANSI_INFO), [LL_WARN] = ANSI("1;" ANSI_WARN), [LL_ERROR] = ANSI("1;" ANSI_ERROR), [LL_FATAL] = ANSI("1;" ANSI_FATAL),};
enum LogLevel g_min_level = LL_DEBUG;bool g_log_colors = true;
void logInit() { char* no_color = getenv("NO_COLOR"); if ((no_color != nullptr && no_color[0] != '\0') || !isatty(STDERR_FILENO)) { g_log_colors = false; }
char* level = getenv("GOLF_LOG_LEVEL"); if (level != nullptr) { if (strcmp(level, "trace") == 0) { g_min_level = LL_TRACE; } else if (strcmp(level, "debug") == 0) { g_min_level = LL_DEBUG; } else if (strcmp(level, "info") == 0) { g_min_level = LL_INFO; } else if (strcmp(level, "warn") == 0) { g_min_level = LL_WARN; } else if (strcmp(level, "error") == 0) { g_min_level = LL_ERROR; } else if (strcmp(level, "fatal") == 0) { g_min_level = LL_FATAL; } }}
void vlogWrite(enum LogLevel level, FILE* file, const char* file_name, int line, const char* fmt, va_list ap) { if(level < g_min_level) { return; }
const char* const level_str = level_names[level];
time_t t = time(NULL); struct tm now = {0}; localtime_r(&t, &now); char time_str[32]; strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", &now);
flockfile(file); if (g_log_colors) { fprintf(file, "[%s] %s%-5s%s %s%s:%d:%s ", time_str, level_styles[level], level_str, ANSI_RESET, ANSI("1;" ANSI_GRAY), file_name, line, ANSI_RESET); } else { fprintf(file, "[%s] %-5s %s:%d: ", time_str, level_str, file_name, line); } vfprintf(file, fmt, ap); fprintf(file, "\n"); funlockfile(file);}
void logWrite(enum LogLevel level, FILE* file, const char* file_name, int line, const char* fmt, ...) { va_list ap; va_start(ap); vlogWrite(level, file, file_name, line, fmt, ap); va_end(ap);}