_alpm_checkconflicts split
[pacman.git] / lib / libalpm / log.c
blobf666a099d2520548e572784a3ae0367c064e6bc6
1 /*
2 * log.c
4 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <errno.h>
29 #include <time.h>
31 /* libalpm */
32 #include "log.h"
33 #include "handle.h"
34 #include "util.h"
35 #include "error.h"
36 #include "alpm.h"
38 /** \addtogroup alpm_log Logging Functions
39 * @brief Functions to log using libalpm
40 * @{
43 /** A printf-like function for logging.
44 * @param fmt output format
45 * @return 0 on success, -1 on error (pm_errno is set accordingly)
47 int SYMEXPORT alpm_logaction(char *fmt, ...)
49 int ret;
50 va_list args;
52 ALPM_LOG_FUNC;
54 /* Sanity checks */
55 ASSERT(handle != NULL, RET_ERR(PM_ERR_HANDLE_NULL, -1));
57 /* check if the logstream is open already, opening it if needed */
58 if(handle->logstream == NULL) {
59 handle->logstream = fopen(handle->logfile, "a");
60 /* if we couldn't open it, we have an issue */
61 if(handle->logstream == NULL) {
62 if(errno == EACCES) {
63 pm_errno = PM_ERR_BADPERMS;
64 } else if(errno == ENOENT) {
65 pm_errno = PM_ERR_NOT_A_DIR;
66 } else {
67 pm_errno = PM_ERR_SYSTEM;
69 return(-1);
73 va_start(args, fmt);
74 ret = _alpm_logaction(handle->usesyslog, handle->logstream, fmt, args);
75 va_end(args);
77 /* TODO We should add a prefix to log strings depending on who called us.
78 * If logaction was called by the frontend:
79 * USER: <the frontend log>
80 * and if called internally:
81 * ALPM: <the library log>
82 * Moreover, the frontend should be able to choose its prefix
83 * (USER by default?):
84 * pacman: "PACMAN"
85 * kpacman: "KPACMAN"
86 * This would allow us to share the log file between several frontends
87 * and know who does what */
88 return(ret);
91 /** @} */
93 void _alpm_log(pmloglevel_t flag, char *fmt, ...)
95 va_list args;
96 alpm_cb_log logcb = alpm_option_get_logcb();
98 if(logcb == NULL) {
99 return;
102 va_start(args, fmt);
103 logcb(flag, fmt, args);
104 va_end(args);
107 /* vim: set ts=2 sw=2 noet: */