diff --git a/src/drw.c b/src/drw.c index 730037c..8c84f76 100644 --- a/src/drw.c +++ b/src/drw.c @@ -132,7 +132,7 @@ xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern) return NULL; } } else { - die("no font specified."); + fatal("no font specified."); } /* Do not allow using color fonts. This is a workaround for a BadLength @@ -204,7 +204,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname) if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen), DefaultColormap(drw->dpy, drw->screen), clrname, dest)) - die("error, cannot allocate color '%s'", clrname); + fatal("error, cannot allocate color '%s'", clrname); } /* Wrapper to create color schemes. The caller has to call free(3) on the @@ -347,7 +347,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp if (!drw->fonts->pattern) { /* Refer to the comment in xfont_create for more information. */ - die("the first font in the cache must be loaded from a font string."); + fatal("the first font in the cache must be loaded from a font string."); } fcpattern = FcPatternDuplicate(drw->fonts->pattern); diff --git a/src/dwm.c b/src/dwm.c index 0b38b62..ad91cf3 100644 --- a/src/dwm.c +++ b/src/dwm.c @@ -282,18 +282,18 @@ static void (*handler[LASTEvent])(XEvent*) = { int dwm_main() { if (!(dpy = XOpenDisplay(NULL))) { - die("polytreewm: cannot open display"); + fatal("cannot open display"); } checkotherwm(); if (!setup()) { - die("polytreewm: cannot setup"); + fatal("cannot setup"); } #ifdef __OpenBSD__ if (pledge("stdio rpath proc exec", NULL) == -1) { - die("pledge"); + fatal("pledge"); } #endif /* __OpenBSD__ */ @@ -1641,11 +1641,11 @@ bool setup() root = RootWindow(dpy, screen.x_screen); drw = drw_create(dpy, screen.x_screen, root, screen.sizes.w, screen.sizes.h); if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) - die("no fonts could be loaded."); + fatal("no fonts could be loaded."); updategeom(); /* init atoms */ atoms = atoms_create(dpy); - if (atoms == NULL) die("polytreewm: fatal: cannot allocate atoms"); + if (atoms == NULL) fatal("cannot allocate atoms"); /* init cursors */ cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); cursor[CurResize] = drw_cur_create(drw, XC_sizing); @@ -1760,7 +1760,7 @@ void showhide(Client *c) void sigchld(__attribute__((unused)) int unused) { if (signal(SIGCHLD, sigchld) == SIG_ERR) - die("can't install SIGCHLD handler:"); + fatal_perror("can't install SIGCHLD handler"); while (0 < waitpid(-1, NULL, WNOHANG)); } diff --git a/src/dwm/xerror.c b/src/dwm/xerror.c index 73c9cae..82f6c03 100644 --- a/src/dwm/xerror.c +++ b/src/dwm/xerror.c @@ -19,9 +19,8 @@ int xerror(Display *const dpy, XErrorEvent *const ee) return 0; } - fprintf( - stderr, - "polytreewm: fatal error: request code=%d, error code=%d\n", + fatal_nodie( + "request code=%d, error code=%d\n", ee->request_code, ee->error_code ); @@ -42,7 +41,7 @@ int xerrorstart( __attribute__((unused)) Display *const dpy, __attribute__((unused)) XErrorEvent *const ee ) { - die("polytreewm: another window manager is already running"); + fatal("another window manager is already running"); return -1; } diff --git a/src/main.c b/src/main.c index 7c14dae..547fb7c 100644 --- a/src/main.c +++ b/src/main.c @@ -8,6 +8,9 @@ #include #include +static void logger(const char *level, const char *fmt, ...); +static void logger_perror(const char *level, const char *fmt, ...); + int main(int argc, char *argv[]) { if (argc == 2 && strcmp(argv[1], "-v") == 0) { @@ -16,7 +19,8 @@ int main(int argc, char *argv[]) } if (argc != 1) { - die("usage: polytreewm [-v]"); + fprintf(stderr, "usage: polytreewm [-v]\n"); + exit(EXIT_FAILURE); } if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) { @@ -26,20 +30,22 @@ int main(int argc, char *argv[]) return dwm_main(argc, argv); } -void die(const char *const fmt, ...) +void logger(const char *const level, const char *const fmt, ...) { + fprintf(stderr, "%s: ", level); + va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fputc('\n', stderr); - - exit(EXIT_FAILURE); } -void die_perror(const char *const fmt, ...) +void logger_perror(const char *const level, const char *const fmt, ...) { + fprintf(stderr, "%s: ", level); + va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -47,18 +53,56 @@ void die_perror(const char *const fmt, ...) fprintf(stderr, ": "); perror(NULL); +} + +void fatal(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("ERROR", fmt, ap); + va_end(ap); exit(EXIT_FAILURE); } -void warning(const char *const fmt, ...) +void fatal_perror(const char* const fmt, ...) { - fputs("WARN: ", stderr); - va_list ap; va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + logger_perror("ERROR", fmt, ap); va_end(ap); - fputc('\n', stderr); + exit(EXIT_FAILURE); +} + +void fatal_nodie(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("ERROR", fmt, ap); + va_end(ap); +} + +void fatal_perror_nodie(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger_perror("ERROR", fmt, ap); + va_end(ap); +} + +void warning(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger("WARN", fmt, ap); + va_end(ap); +} + +void warning_perror(const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + logger_perror("WARN", fmt, ap); + va_end(ap); } diff --git a/src/main.h b/src/main.h index f9a0d82..6cfeb33 100644 --- a/src/main.h +++ b/src/main.h @@ -1,8 +1,13 @@ #ifndef _MAIN_H #define _MAIN_H -void die(const char *fmt, ...); -void die_perror(const char *fmt, ...); +__attribute__((noreturn)) +void fatal(const char *fmt, ...); +__attribute__((noreturn)) +void fatal_perror(const char *fmt, ...); +void fatal_nodie(const char *fmt, ...); +void fatal_perror_nodie(const char *fmt, ...); void warning(const char *fmt, ...); +void warning_perror(const char *fmt, ...); #endif // _MAIN_H diff --git a/src/util.c b/src/util.c index bcd96d9..b2dacb5 100644 --- a/src/util.c +++ b/src/util.c @@ -10,6 +10,6 @@ void *ecalloc(size_t nmemb, size_t size) { void *p = calloc(nmemb, size); - if (!p) die_perror("calloc"); + if (!p) fatal_perror("calloc"); return p; }