log.c

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

Generated on Mon Jan 14 23:53:40 2008 for libalpm by  doxygen 1.5.4