Scripts testsuite output consistency
[pacman-ng.git] / lib / libalpm / log.c
blob8486716e2a30ec371316390ff1604214b50ddf43
1 /*
2 * log.c
4 * Copyright (c) 2006-2012 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 <stdio.h>
22 #include <stdarg.h>
23 #include <errno.h>
25 /* libalpm */
26 #include "log.h"
27 #include "handle.h"
28 #include "util.h"
29 #include "alpm.h"
31 /** \addtogroup alpm_log Logging Functions
32 * @brief Functions to log using libalpm
33 * @{
36 /** A printf-like function for logging.
37 * @param handle the context handle
38 * @param fmt output format
39 * @return 0 on success, -1 on error (pm_errno is set accordingly)
41 int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...)
43 int ret;
44 va_list args;
46 ASSERT(handle != NULL, return -1);
48 /* check if the logstream is open already, opening it if needed */
49 if(handle->logstream == NULL) {
50 handle->logstream = fopen(handle->logfile, "a");
51 /* if we couldn't open it, we have an issue */
52 if(handle->logstream == NULL) {
53 if(errno == EACCES) {
54 handle->pm_errno = ALPM_ERR_BADPERMS;
55 } else if(errno == ENOENT) {
56 handle->pm_errno = ALPM_ERR_NOT_A_DIR;
57 } else {
58 handle->pm_errno = ALPM_ERR_SYSTEM;
60 return -1;
64 va_start(args, fmt);
65 ret = _alpm_logaction(handle, fmt, args);
66 va_end(args);
68 /* TODO We should add a prefix to log strings depending on who called us.
69 * If logaction was called by the frontend:
70 * USER: <the frontend log>
71 * and if called internally:
72 * ALPM: <the library log>
73 * Moreover, the frontend should be able to choose its prefix
74 * (USER by default?):
75 * pacman: "PACMAN"
76 * kpacman: "KPACMAN"
77 * This would allow us to share the log file between several frontends
78 * and know who does what */
79 return ret;
82 /** @} */
84 void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...)
86 va_list args;
88 if(handle == NULL || handle->logcb == NULL) {
89 return;
92 va_start(args, fmt);
93 handle->logcb(flag, fmt, args);
94 va_end(args);
97 /* vim: set ts=2 sw=2 noet: */