
#ifndef BEATRAT_LOG_H
#define BEATRAT_LOG_H

typedef enum {
    LOG_LEVEL_INFO,
    LOG_LEVEL_WARN,
    LOG_LEVEL_ERROR
} LogLevel;

/* Opens the log file (append mode, rotating if it's grown past
 * LOG_MAX_BYTES -- see log.c) at ~/.local/state/beatrat/<filename>
 * (respecting $XDG_STATE_HOME if set), creating that directory if it
 * doesn't exist yet. `filename` is a bare name like "beatrat.log", NOT
 * a path -- this used to be opened relative to whatever directory the
 * program happened to be launched from, which meant a run from a
 * different working directory silently started (or added to) a whole
 * separate log file instead of the one you'd expect. Safe to skip
 * checking the return value -- failure just means we fall back to
 * stderr-only logging, which log_write() already handles. */
int  log_init(const char *filename);
void log_close(void);

/* When true, log_write() skips its stderr line entirely and only
 * writes to the log file -- see main()'s -s/--silence flag. Off by
 * default (matches the original always-print-to-terminal behavior).
 * Safe to call before or after log_init(). */
void log_set_quiet(int quiet);

void log_write(LogLevel level, const char *func, const char *fmt, ...)
    __attribute__((format(printf, 3, 4)));

#define LOG_INFO(...)  log_write(LOG_LEVEL_INFO,  __func__, __VA_ARGS__)
#define LOG_WARN(...)  log_write(LOG_LEVEL_WARN,  __func__, __VA_ARGS__)
#define LOG_ERROR(...) log_write(LOG_LEVEL_ERROR, __func__, __VA_ARGS__)

#endif
