2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* log.c
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2011-01-05 23:45:15 -05:00
|
|
|
* Copyright (c) 2006-2011 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 "config.h"
|
2007-03-05 17:13:33 -05:00
|
|
|
|
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
|
2007-06-03 23:57:38 -04:00
|
|
|
* @param fmt output format
|
|
|
|
* @return 0 on success, -1 on error (pm_errno is set accordingly)
|
|
|
|
*/
|
2011-06-07 13:41:33 -04:00
|
|
|
int SYMEXPORT alpm_logaction(pmhandle_t *handle, 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) {
|
|
|
|
handle->logstream = fopen(handle->logfile, "a");
|
|
|
|
/* if we couldn't open it, we have an issue */
|
|
|
|
if(handle->logstream == NULL) {
|
|
|
|
if(errno == EACCES) {
|
2011-06-07 17:06:16 -04:00
|
|
|
handle->pm_errno = PM_ERR_BADPERMS;
|
2007-11-04 17:38:59 -05:00
|
|
|
} else if(errno == ENOENT) {
|
2011-06-07 17:06:16 -04:00
|
|
|
handle->pm_errno = PM_ERR_NOT_A_DIR;
|
2007-11-04 17:38:59 -05:00
|
|
|
} else {
|
2011-06-07 17:06:16 -04:00
|
|
|
handle->pm_errno = PM_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);
|
2011-06-07 13:41:33 -04:00
|
|
|
ret = _alpm_logaction(handle, fmt, args);
|
2007-06-03 23:57:38 -04:00
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
/* TODO We should add a prefix to log strings depending on who called us.
|
|
|
|
* If logaction was called by the frontend:
|
|
|
|
* USER: <the frontend log>
|
|
|
|
* and if called internally:
|
|
|
|
* ALPM: <the library log>
|
|
|
|
* Moreover, the frontend should be able to choose its prefix
|
|
|
|
* (USER by default?):
|
|
|
|
* pacman: "PACMAN"
|
|
|
|
* kpacman: "KPACMAN"
|
|
|
|
* This would allow us to share the log file between several frontends
|
|
|
|
* and know who does what */
|
2011-03-20 20:45:57 -04:00
|
|
|
return ret;
|
2007-06-03 23:57:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2011-06-07 17:13:58 -04:00
|
|
|
void _alpm_log(pmhandle_t *handle, pmloglevel_t flag, const char *fmt, ...)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2007-06-07 20:55:13 -04:00
|
|
|
va_list args;
|
|
|
|
|
2011-06-18 21:44:49 -04:00
|
|
|
if(handle == NULL || handle->logcb == NULL) {
|
2005-03-14 20:51:43 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-07 20:55:13 -04:00
|
|
|
va_start(args, fmt);
|
2011-06-18 21:44:49 -04:00
|
|
|
handle->logcb(flag, fmt, args);
|
2007-06-07 20:55:13 -04:00
|
|
|
va_end(args);
|
2005-03-14 20:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=2 sw=2 noet: */
|