2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* log.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2014-01-01 05:24:48 -05:00
|
|
|
* Copyright (c) 2006-2014 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>
|
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
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** 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
|
|
|
{
|
2007-06-07 20:55:13 -04:00
|
|
|
int ret;
|
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
|
|
|
|
2007-11-04 17:38:59 -05:00
|
|
|
/* check if the logstream is open already, opening it if needed */
|
|
|
|
if(handle->logstream == NULL) {
|
2014-01-02 13:37:12 -05:00
|
|
|
int fd;
|
|
|
|
do {
|
|
|
|
fd = open(handle->logfile, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC,
|
|
|
|
0000);
|
|
|
|
} while(fd == -1 && errno == EINTR);
|
|
|
|
if(fd >= 0) {
|
|
|
|
handle->logstream = fdopen(fd, "a");
|
|
|
|
}
|
2007-11-04 17:38:59 -05:00
|
|
|
/* if we couldn't open it, we have an issue */
|
2014-01-02 13:37:12 -05:00
|
|
|
if(fd < 0 || handle->logstream == 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
|
|
|
}
|
2011-06-07 17:06:16 -04:00
|
|
|
return -1;
|
2007-11-04 17:38:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-03 23:57:38 -04:00
|
|
|
va_start(args, fmt);
|
2013-01-18 20:42:21 -05:00
|
|
|
ret = _alpm_logaction(handle, prefix, fmt, args);
|
2007-06-03 23:57:38 -04:00
|
|
|
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-01-10 10:25:15 -05:00
|
|
|
alpm_event_log_t event = {
|
|
|
|
.type = ALPM_EVENT_LOG,
|
|
|
|
.level = flag,
|
|
|
|
.fmt = fmt
|
|
|
|
};
|
2007-06-07 20:55:13 -04:00
|
|
|
|
2014-01-10 10:25:15 -05:00
|
|
|
if(handle == NULL || handle->eventcb == NULL) {
|
2005-03-14 20:51:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-10 10:25:15 -05:00
|
|
|
va_start(event.args, fmt);
|
|
|
|
EVENT(handle, &event);
|
|
|
|
va_end(event.args);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
/* vim: set noet: */
|