log: log to stderr, not stdout

This commit is contained in:
Daniel Eklöf 2020-02-04 18:27:54 +01:00
parent ebc7511706
commit ef98df1a95
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

18
log.c
View file

@ -16,7 +16,7 @@ static bool colorize = false;
static void __attribute__((constructor)) static void __attribute__((constructor))
init(void) init(void)
{ {
colorize = isatty(STDOUT_FILENO); colorize = isatty(STDERR_FILENO);
openlog(NULL, /*LOG_PID*/0, LOG_USER); openlog(NULL, /*LOG_PID*/0, LOG_USER);
setlogmask(LOG_UPTO(LOG_WARNING)); setlogmask(LOG_UPTO(LOG_WARNING));
} }
@ -42,24 +42,24 @@ _log(enum log_class log_class, const char *module, const char *file, int lineno,
char clr[16]; char clr[16];
snprintf(clr, sizeof(clr), "\e[%dm", class_clr); snprintf(clr, sizeof(clr), "\e[%dm", class_clr);
printf("%s%s%s: ", colorize ? clr : "", class, colorize ? "\e[0m" : ""); fprintf(stderr, "%s%s%s: ", colorize ? clr : "", class, colorize ? "\e[0m" : "");
if (colorize) if (colorize)
printf("\e[2m"); fprintf(stderr, "\e[2m");
#if defined(_DEBUG) #if defined(_DEBUG)
printf("%s:%d: ", file, lineno); fprintf(stderr, "%s:%d: ", file, lineno);
#else #else
printf("%s: ", module); fprintf(stderr, "%s: ", module);
#endif #endif
if (colorize) if (colorize)
printf("\e[0m"); fprintf(stderr, "\e[0m");
vprintf(fmt, va); vfprintf(stderr, fmt, va);
if (sys_errno != 0) if (sys_errno != 0)
printf(": %s", strerror(sys_errno)); fprintf(stderr, ": %s", strerror(sys_errno));
printf("\n"); fprintf(stderr, "\n");
} }
static void static void