2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* log.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2016-01-02 22:48:43 -05:00
|
|
|
* Copyright (c) 2006-2016 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2005-03-14 20:51:43 -05:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-03-14 20:51:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2007-11-04 17:38:59 -05:00
|
|
|
#include <errno.h>
|
2015-04-12 00:01:20 -04:00
|
|
|
#include <syslog.h>
|
2007-03-05 17:13:33 -05:00
|
|
|
|
|
|
|
/* libalpm */
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "log.h"
|
2007-06-03 23:57:38 -04:00
|
|
|
#include "handle.h"
|
|
|
|
#include "util.h"
|
2007-03-05 17:13:33 -05:00
|
|
|
#include "alpm.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** \addtogroup alpm_log Logging Functions
|
|
|
|
* @brief Functions to log using libalpm
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-04-12 00:01:23 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
/** A printf-like function for logging.
|
2011-06-07 17:06:16 -04:00
|
|
|
* @param handle the context handle
|
2013-01-18 20:42:21 -05:00
|
|
|
* @param prefix caller-specific prefix for the log
|
2007-06-03 23:57:38 -04:00
|
|
|
* @param fmt output format
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2013-02-10 01:47:39 -05:00
|
|
|
int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix,
|
|
|
|
const char *fmt, ...)
|
2007-06-03 23:57:38 -04:00
|
|
|
{
|
2015-04-12 00:01:20 -04:00
|
|
|
int ret = 0;
|
2007-06-03 23:57:38 -04:00
|
|
|
va_list args;
|
|
|
|
|
2011-06-07 17:06:16 -04:00
|
|
|
ASSERT(handle != NULL, return -1);
|
2007-06-03 23:57:38 -04:00
|
|
|
|
2015-04-12 00:01:20 -04:00
|
|
|
if(!(prefix && *prefix)) {
|
|
|
|
prefix = "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2007-11-04 17:38:59 -05:00
|
|
|
/* check if the logstream is open already, opening it if needed */
|
2015-04-12 00:01:21 -04:00
|
|
|
if(handle->logstream == NULL && handle->logfile != NULL) {
|
2014-01-02 13:37:12 -05:00
|
|
|
int fd;
|
|
|
|
do {
|
|
|
|
fd = open(handle->logfile, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC,
|
2014-12-22 12:19:38 -05:00
|
|
|
0644);
|
2014-01-02 13:37:12 -05:00
|
|
|
} while(fd == -1 && errno == EINTR);
|
2007-11-04 17:38:59 -05:00
|
|
|
/* if we couldn't open it, we have an issue */
|
2015-04-12 00:01:20 -04:00
|
|
|
if(fd < 0 || (handle->logstream = fdopen(fd, "a")) == NULL) {
|
2007-11-04 17:38:59 -05:00
|
|
|
if(errno == EACCES) {
|
2011-07-01 12:01:39 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_BADPERMS;
|
2007-11-04 17:38:59 -05:00
|
|
|
} else if(errno == ENOENT) {
|
2011-07-01 12:01:39 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_NOT_A_DIR;
|
2007-11-04 17:38:59 -05:00
|
|
|
} else {
|
2011-07-01 12:01:39 -04:00
|
|
|
handle->pm_errno = ALPM_ERR_SYSTEM;
|
2007-11-04 17:38:59 -05:00
|
|
|
}
|
2015-04-12 00:01:22 -04:00
|
|
|
ret = -1;
|
2007-11-04 17:38:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
va_start(args, fmt);
|
|
|
|
|
2015-04-12 00:01:20 -04:00
|
|
|
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) {
|
2015-04-12 00:01:23 -04:00
|
|
|
if(_alpm_log_leader(handle->logstream, prefix) < 0
|
|
|
|
|| vfprintf(handle->logstream, fmt, args) < 0) {
|
|
|
|
ret = -1;
|
|
|
|
handle->pm_errno = ALPM_ERR_SYSTEM;
|
|
|
|
}
|
2015-04-12 00:01:20 -04:00
|
|
|
fflush(handle->logstream);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2011-06-28 00:41:07 -04:00
|
|
|
void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2014-09-28 17:45:35 -04:00
|
|
|
va_list args;
|
2007-06-07 20:55:13 -04:00
|
|
|
|
2014-09-28 17:45:35 -04:00
|
|
|
if(handle == NULL || handle->logcb == NULL) {
|
2005-03-14 20:51:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-28 17:45:35 -04:00
|
|
|
va_start(args, fmt);
|
|
|
|
handle->logcb(flag, fmt, args);
|
|
|
|
va_end(args);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|