From 71adf42ce6801074211caabc5c1618d1ca1c3bcb Mon Sep 17 00:00:00 2001 From: s-over-4 Date: Tue, 13 Jun 2023 11:59:39 -0400 Subject: util --- src/main.c | 4 +--- src/util.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index ceefadd..0c2755a 100644 --- a/src/main.c +++ b/src/main.c @@ -2,10 +2,8 @@ #include -#include "include/log.h" +#include "include/util.h" #include "include/lexer.h" -// #include "include/tree.h" -// #include "include/parser.h" int main(int argc, char* argv[]) { diff --git a/src/util.c b/src/util.c index 32cef99..31878ed 100644 --- a/src/util.c +++ b/src/util.c @@ -1,9 +1,31 @@ #include "include/util.h" +void die(const char* fmt, ...) { + va_list ap; + + fprintf(stderr, "== FATAL ERROR ==\n"); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + fprintf(stderr, "\n"); + + exit(1); +} void log_inf(const char* fmt, ...) { va_list ap; + fprintf(stderr, "== INFO ==\n"); + va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); @@ -15,5 +37,43 @@ void log_inf(const char* fmt, ...) { fputc('\n', stderr); } - exit(1); + fprintf(stderr, "\n"); +} + +void log_war(const char* fmt, ...) { + va_list ap; + + fprintf(stderr, "== WARNING ==\n"); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + fprintf(stderr, "\n"); +} + +void log_err(const char* fmt, ...) { + va_list ap; + + fprintf(stderr, "== ERROR =="); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { + fputc(' ', stderr); + perror(NULL); + } else { + fputc('\n', stderr); + } + + fprintf(stderr, "\n"); } -- cgit v1.2.3