Improve dying and logging

This commit is contained in:
Alex Kotov 2021-11-21 07:25:19 +05:00
parent bc7eff3c4f
commit 256a49ee6c
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
6 changed files with 74 additions and 26 deletions

View File

@ -132,7 +132,7 @@ xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
return NULL; return NULL;
} }
} else { } else {
die("no font specified."); fatal("no font specified.");
} }
/* Do not allow using color fonts. This is a workaround for a BadLength /* 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), if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
DefaultColormap(drw->dpy, drw->screen), DefaultColormap(drw->dpy, drw->screen),
clrname, dest)) 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 /* 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) { if (!drw->fonts->pattern) {
/* Refer to the comment in xfont_create for more information. */ /* 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); fcpattern = FcPatternDuplicate(drw->fonts->pattern);

View File

@ -282,18 +282,18 @@ static void (*handler[LASTEvent])(XEvent*) = {
int dwm_main() int dwm_main()
{ {
if (!(dpy = XOpenDisplay(NULL))) { if (!(dpy = XOpenDisplay(NULL))) {
die("polytreewm: cannot open display"); fatal("cannot open display");
} }
checkotherwm(); checkotherwm();
if (!setup()) { if (!setup()) {
die("polytreewm: cannot setup"); fatal("cannot setup");
} }
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (pledge("stdio rpath proc exec", NULL) == -1) { if (pledge("stdio rpath proc exec", NULL) == -1) {
die("pledge"); fatal("pledge");
} }
#endif /* __OpenBSD__ */ #endif /* __OpenBSD__ */
@ -1641,11 +1641,11 @@ bool setup()
root = RootWindow(dpy, screen.x_screen); root = RootWindow(dpy, screen.x_screen);
drw = drw_create(dpy, screen.x_screen, root, screen.sizes.w, screen.sizes.h); drw = drw_create(dpy, screen.x_screen, root, screen.sizes.w, screen.sizes.h);
if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
die("no fonts could be loaded."); fatal("no fonts could be loaded.");
updategeom(); updategeom();
/* init atoms */ /* init atoms */
atoms = atoms_create(dpy); atoms = atoms_create(dpy);
if (atoms == NULL) die("polytreewm: fatal: cannot allocate atoms"); if (atoms == NULL) fatal("cannot allocate atoms");
/* init cursors */ /* init cursors */
cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
cursor[CurResize] = drw_cur_create(drw, XC_sizing); cursor[CurResize] = drw_cur_create(drw, XC_sizing);
@ -1760,7 +1760,7 @@ void showhide(Client *c)
void sigchld(__attribute__((unused)) int unused) void sigchld(__attribute__((unused)) int unused)
{ {
if (signal(SIGCHLD, sigchld) == SIG_ERR) 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)); while (0 < waitpid(-1, NULL, WNOHANG));
} }

View File

@ -19,9 +19,8 @@ int xerror(Display *const dpy, XErrorEvent *const ee)
return 0; return 0;
} }
fprintf( fatal_nodie(
stderr, "request code=%d, error code=%d\n",
"polytreewm: fatal error: request code=%d, error code=%d\n",
ee->request_code, ee->request_code,
ee->error_code ee->error_code
); );
@ -42,7 +41,7 @@ int xerrorstart(
__attribute__((unused)) Display *const dpy, __attribute__((unused)) Display *const dpy,
__attribute__((unused)) XErrorEvent *const ee __attribute__((unused)) XErrorEvent *const ee
) { ) {
die("polytreewm: another window manager is already running"); fatal("another window manager is already running");
return -1; return -1;
} }

View File

@ -8,6 +8,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
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[]) int main(int argc, char *argv[])
{ {
if (argc == 2 && strcmp(argv[1], "-v") == 0) { if (argc == 2 && strcmp(argv[1], "-v") == 0) {
@ -16,7 +19,8 @@ int main(int argc, char *argv[])
} }
if (argc != 1) { if (argc != 1) {
die("usage: polytreewm [-v]"); fprintf(stderr, "usage: polytreewm [-v]\n");
exit(EXIT_FAILURE);
} }
if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) { if (!setlocale(LC_CTYPE, "") || !dwm_has_locale_support()) {
@ -26,20 +30,22 @@ int main(int argc, char *argv[])
return dwm_main(argc, 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_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fputc('\n', stderr); 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_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
@ -47,18 +53,56 @@ void die_perror(const char *const fmt, ...)
fprintf(stderr, ": "); fprintf(stderr, ": ");
perror(NULL); 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); exit(EXIT_FAILURE);
} }
void warning(const char *const fmt, ...) void fatal_perror(const char* const fmt, ...)
{ {
fputs("WARN: ", stderr);
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); logger_perror("ERROR", fmt, ap);
va_end(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);
} }

View File

@ -1,8 +1,13 @@
#ifndef _MAIN_H #ifndef _MAIN_H
#define _MAIN_H #define _MAIN_H
void die(const char *fmt, ...); __attribute__((noreturn))
void die_perror(const char *fmt, ...); 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(const char *fmt, ...);
void warning_perror(const char *fmt, ...);
#endif // _MAIN_H #endif // _MAIN_H

View File

@ -10,6 +10,6 @@
void *ecalloc(size_t nmemb, size_t size) void *ecalloc(size_t nmemb, size_t size)
{ {
void *p = calloc(nmemb, size); void *p = calloc(nmemb, size);
if (!p) die_perror("calloc"); if (!p) fatal_perror("calloc");
return p; return p;
} }