Skip to content

Commit

Permalink
Merge branch 'obsd-master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasAdam committed Sep 10, 2021
2 parents 3b10392 + e6b40cb commit b28fffc
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions log.c
Expand Up @@ -98,28 +98,27 @@ log_close(void)

/* Write a log message. */
static void printflike(1, 0)
log_vwrite(const char *msg, va_list ap)
log_vwrite(const char *msg, va_list ap, const char *prefix)
{
char *fmt, *out;
char *s, *out;
struct timeval tv;

if (log_file == NULL)
return;

if (vasprintf(&fmt, msg, ap) == -1)
if (vasprintf(&s, msg, ap) == -1)
return;
if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) {
free(fmt);
if (stravis(&out, s, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) {
free(s);
return;
}
free(s);

gettimeofday(&tv, NULL);
if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec,
(int)tv.tv_usec, out) != -1)
if (fprintf(log_file, "%lld.%06d %s%s\n", (long long)tv.tv_sec,
(int)tv.tv_usec, prefix, out) != -1)
fflush(log_file);

free(out);
free(fmt);
}

/* Log a debug message. */
Expand All @@ -132,36 +131,36 @@ log_debug(const char *msg, ...)
return;

va_start(ap, msg);
log_vwrite(msg, ap);
log_vwrite(msg, ap, "");
va_end(ap);
}

/* Log a critical error with error string and die. */
__dead void
fatal(const char *msg, ...)
{
char *fmt;
char tmp[256];
va_list ap;

if (snprintf(tmp, sizeof tmp, "fatal: %s: ", strerror(errno)) < 0)
exit (1);

va_start(ap, msg);
if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
exit(1);
log_vwrite(fmt, ap);
log_vwrite(msg, ap, tmp);
va_end(ap);

exit(1);
}

/* Log a critical error and die. */
__dead void
fatalx(const char *msg, ...)
{
char *fmt;
va_list ap;

va_start(ap, msg);
if (asprintf(&fmt, "fatal: %s", msg) == -1)
exit(1);
log_vwrite(fmt, ap);
log_vwrite(msg, ap, "fatal: ");
va_end(ap);

exit(1);
}

0 comments on commit b28fffc

Please sign in to comment.