index : pacman | |
Archlinux32 fork of pacman | gitolite user |
summaryrefslogtreecommitdiff |
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2015-04-12 00:01:20 -0400 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2015-05-12 14:00:54 +1000 |
commit | ae7067440c20c49b3d11c63844d583381282c6b2 (patch) | |
tree | a8649744ff7c3d6cc2e408d2745c787b82e6b563 /lib/libalpm/log.c | |
parent | f9423cfa5d5b9f2041b70676438082faad1cd1ee (diff) |
-rw-r--r-- | lib/libalpm/log.c | 36 |
diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index 8ee61df5..fceb96f4 100644 --- a/lib/libalpm/log.c +++ b/lib/libalpm/log.c @@ -21,6 +21,7 @@ #include <stdio.h> #include <stdarg.h> #include <errno.h> +#include <syslog.h> /* libalpm */ #include "log.h" @@ -42,11 +43,15 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, const char *fmt, ...) { - int ret; + int ret = 0; va_list args; ASSERT(handle != NULL, return -1); + if(!(prefix && *prefix)) { + prefix = "UNKNOWN"; + } + /* check if the logstream is open already, opening it if needed */ if(handle->logstream == NULL) { int fd; @@ -54,11 +59,8 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, fd = open(handle->logfile, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0644); } while(fd == -1 && errno == EINTR); - if(fd >= 0) { - handle->logstream = fdopen(fd, "a"); - } /* if we couldn't open it, we have an issue */ - if(fd < 0 || handle->logstream == NULL) { + if(fd < 0 || (handle->logstream = fdopen(fd, "a")) == NULL) { if(errno == EACCES) { handle->pm_errno = ALPM_ERR_BADPERMS; } else if(errno == ENOENT) { @@ -71,9 +73,29 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, } va_start(args, fmt); - ret = _alpm_logaction(handle, prefix, fmt, args); - va_end(args); + if(handle->usesyslog) { + /* we can't use a va_list more than once, so we need to copy it + * so we can use the original when calling vfprintf below. */ + va_list args_syslog; + va_copy(args_syslog, args); + vsyslog(LOG_WARNING, fmt, args_syslog); + va_end(args_syslog); + } + + if(handle->logstream) { + time_t t = time(NULL); + struct tm *tm = localtime(&t); + + /* Use ISO-8601 date format */ + fprintf(handle->logstream, "[%04d-%02d-%02d %02d:%02d] [%s] ", + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, prefix); + ret = vfprintf(handle->logstream, fmt, args); + fflush(handle->logstream); + } + + va_end(args); return ret; } |