libalpm
Arch Linux Package Manager Library
log.c
Go to the documentation of this file.
00001 /*
00002  *  log.c
00003  *
00004  *  Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
00005  *  Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdarg.h>
00023 #include <errno.h>
00024 
00025 /* libalpm */
00026 #include "log.h"
00027 #include "handle.h"
00028 #include "util.h"
00029 #include "alpm.h"
00030 
00031 /** \addtogroup alpm_log Logging Functions
00032  * @brief Functions to log using libalpm
00033  * @{
00034  */
00035 
00036 /** A printf-like function for logging.
00037  * @param handle the context handle
00038  * @param fmt output format
00039  * @return 0 on success, -1 on error (pm_errno is set accordingly)
00040  */
00041 int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...)
00042 {
00043     int ret;
00044     va_list args;
00045 
00046     ASSERT(handle != NULL, return -1);
00047 
00048     /* check if the logstream is open already, opening it if needed */
00049     if(handle->logstream == NULL) {
00050         handle->logstream = fopen(handle->logfile, "a");
00051         /* if we couldn't open it, we have an issue */
00052         if(handle->logstream == NULL) {
00053             if(errno == EACCES) {
00054                 handle->pm_errno = ALPM_ERR_BADPERMS;
00055             } else if(errno == ENOENT) {
00056                 handle->pm_errno = ALPM_ERR_NOT_A_DIR;
00057             } else {
00058                 handle->pm_errno = ALPM_ERR_SYSTEM;
00059             }
00060             return -1;
00061         }
00062     }
00063 
00064     va_start(args, fmt);
00065     ret = _alpm_logaction(handle, fmt, args);
00066     va_end(args);
00067 
00068     /* TODO We should add a prefix to log strings depending on who called us.
00069      * If logaction was called by the frontend:
00070      *   USER: <the frontend log>
00071      * and if called internally:
00072      *   ALPM: <the library log>
00073      * Moreover, the frontend should be able to choose its prefix
00074      * (USER by default?):
00075      *   pacman: "PACMAN"
00076      *   kpacman: "KPACMAN"
00077      * This would allow us to share the log file between several frontends
00078      * and know who does what */
00079     return ret;
00080 }
00081 
00082 /** @} */
00083 
00084 void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...)
00085 {
00086     va_list args;
00087 
00088     if(handle == NULL || handle->logcb == NULL) {
00089         return;
00090     }
00091 
00092     va_start(args, fmt);
00093     handle->logcb(flag, fmt, args);
00094     va_end(args);
00095 }
00096 
00097 /* vim: set ts=2 sw=2 noet: */