Last active
May 2, 2016 00:16
-
-
Save larryv/a1d7ac5cf21ed38454d1e5cb80140cb1 to your computer and use it in GitHub Desktop.
log message and exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdarg.h> | |
#include <stdio.h> | |
void die(const char *fmt, ...) | |
{ | |
va_list ap; | |
va_start(ap, fmt); | |
/* This buffer length is arbitrary. */ | |
const size_t buflen = 1024; | |
static char message[buflen] = "ERROR: "; | |
if (errno) { | |
vsnprintf(message, buflen, fmt, ap); | |
perror(message); | |
} else { | |
vsnprintf(strchr(message, '\0'), buflen - strlen(message), fmt, ap); | |
fprintf(stderr, "%s\n", message); | |
} | |
va_end(ap); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment