[pacman-dev] [PATCH 1/4] merge _alpm_logaction into alpm_logaction
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- lib/libalpm/log.c | 36 +++++++++++++++++++++++++++++------- lib/libalpm/util.c | 44 -------------------------------------------- lib/libalpm/util.h | 3 --- 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index 8ee61df..fceb96f 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; } diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 26d091b..66a2742 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -27,7 +27,6 @@ #include <ctype.h> #include <dirent.h> #include <time.h> -#include <syslog.h> #include <errno.h> #include <limits.h> #include <sys/wait.h> @@ -446,49 +445,6 @@ ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, return files; } -/** Write formatted message to log. - * @param handle the context handle - * @param format formatted string to write out - * @param args formatting arguments - * @return 0 or number of characters written on success, vfprintf return value - * on error - */ -int _alpm_logaction(alpm_handle_t *handle, const char *prefix, - const char *fmt, va_list args) -{ - int ret = 0; - - if(!(prefix && *prefix)) { - prefix = "UNKNOWN"; - } - - 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; - struct tm *tm; - - t = time(NULL); - 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); - } - - return ret; -} - /** Execute a command with arguments in a chroot. * @param handle the context handle * @param cmd command to execute diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 4839618..95112cf 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -119,9 +119,6 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix, ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count); -int _alpm_logaction(alpm_handle_t *handle, const char *prefix, const char *fmt, va_list args) - __attribute__((format(printf, 3, 0))); - int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]); int _alpm_ldconfig(alpm_handle_t *handle); int _alpm_str_cmp(const void *s1, const void *s2); -- 2.3.5
valgrind does not like calling open(NULL, ...). This also makes the return value 0 if logfile has not been set. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- lib/libalpm/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index fceb96f..c049bf9 100644 --- a/lib/libalpm/log.c +++ b/lib/libalpm/log.c @@ -53,7 +53,7 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, } /* check if the logstream is open already, opening it if needed */ - if(handle->logstream == NULL) { + if(handle->logstream == NULL && handle->logfile != NULL) { int fd; do { fd = open(handle->logfile, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, -- 2.3.5
Even if opening the log file fails, if a user has enabled syslog we may still be able to log to that. Set the error return value and continue instead of bailing out. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- lib/libalpm/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index c049bf9..602dd37 100644 --- a/lib/libalpm/log.c +++ b/lib/libalpm/log.c @@ -68,7 +68,7 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, } else { handle->pm_errno = ALPM_ERR_SYSTEM; } - return -1; + ret = -1; } } -- 2.3.5
"return 0 on success, -1 on error (pm_errno is set accordingly)" Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> --- lib/libalpm/log.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c index 602dd37..4383735 100644 --- a/lib/libalpm/log.c +++ b/lib/libalpm/log.c @@ -34,6 +34,17 @@ * @{ */ +static int _alpm_log_leader(FILE *f, const char *prefix) +{ + time_t t = time(NULL); + struct tm *tm = localtime(&t); + + /* Use ISO-8601 date format */ + return fprintf(f, "[%04d-%02d-%02d %02d:%02d] [%s] ", + tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, + tm->tm_hour, tm->tm_min, prefix); +} + /** A printf-like function for logging. * @param handle the context handle * @param prefix caller-specific prefix for the log @@ -84,14 +95,11 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, } 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); + if(_alpm_log_leader(handle->logstream, prefix) < 0 + || vfprintf(handle->logstream, fmt, args) < 0) { + ret = -1; + handle->pm_errno = ALPM_ERR_SYSTEM; + } fflush(handle->logstream); } -- 2.3.5
participants (1)
-
Andrew Gregory