Prefix _alpm_errno_t members with ALPM
[pacman-ng.git] / lib / libalpm / log.c
blob692ff79cdc1ff1047f8071696a808710cc350a33
1 /*
2 * log.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <errno.h>
27 /* libalpm */
28 #include "log.h"
29 #include "handle.h"
30 #include "util.h"
31 #include "alpm.h"
33 /** \addtogroup alpm_log Logging Functions
34 * @brief Functions to log using libalpm
35 * @{
38 /** A printf-like function for logging.
39 * @param handle the context handle
40 * @param fmt output format
41 * @return 0 on success, -1 on error (pm_errno is set accordingly)
43 int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...)
45 int ret;
46 va_list args;
48 ASSERT(handle != NULL, return -1);
50 /* check if the logstream is open already, opening it if needed */
51 if(handle->logstream == NULL) {
52 handle->logstream = fopen(handle->logfile, "a");
53 /* if we couldn't open it, we have an issue */
54 if(handle->logstream == NULL) {
55 if(errno == EACCES) {
56 handle->pm_errno = ALPM_ERR_BADPERMS;
57 } else if(errno == ENOENT) {
58 handle->pm_errno = ALPM_ERR_NOT_A_DIR;
59 } else {
60 handle->pm_errno = ALPM_ERR_SYSTEM;
62 return -1;
66 va_start(args, fmt);
67 ret = _alpm_logaction(handle, fmt, args);
68 va_end(args);
70 /* TODO We should add a prefix to log strings depending on who called us.
71 * If logaction was called by the frontend:
72 * USER: <the frontend log>
73 * and if called internally:
74 * ALPM: <the library log>
75 * Moreover, the frontend should be able to choose its prefix
76 * (USER by default?):
77 * pacman: "PACMAN"
78 * kpacman: "KPACMAN"
79 * This would allow us to share the log file between several frontends
80 * and know who does what */
81 return ret;
84 /** @} */
86 void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...)
88 va_list args;
90 if(handle == NULL || handle->logcb == NULL) {
91 return;
94 va_start(args, fmt);
95 handle->logcb(flag, fmt, args);
96 va_end(args);
99 /* vim: set ts=2 sw=2 noet: */