1 /*
2 * log.c
3 *
4 * Copyright (c) 2006-2018 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include <errno.h>
24 #include <syslog.h>
25
26 /* libalpm */
27 #include "log.h"
28 #include "handle.h"
29 #include "util.h"
30 #include "alpm.h"
31
32 /** \addtogroup alpm_log Logging Functions
33 * @brief Functions to log using libalpm
34 * @{
35 */
36
_alpm_log_leader(FILE * f,const char * prefix)37 static int _alpm_log_leader(FILE *f, const char *prefix)
38 {
39 time_t t = time(NULL);
40 struct tm *tm = localtime(&t);
41
42 /* Use ISO-8601 date format */
43 return fprintf(f, "[%04d-%02d-%02d %02d:%02d] [%s] ",
44 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
45 tm->tm_hour, tm->tm_min, prefix);
46 }
47
48 /** A printf-like function for logging.
49 * @param handle the context handle
50 * @param prefix caller-specific prefix for the log
51 * @param fmt output format
52 * @return 0 on success, -1 on error (pm_errno is set accordingly)
53 */
alpm_logaction(alpm_handle_t * handle,const char * prefix,const char * fmt,...)54 int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix,
55 const char *fmt, ...)
56 {
57 int ret = 0;
58 va_list args;
59
60 ASSERT(handle != NULL, return -1);
61
62 if(!(prefix && *prefix)) {
63 prefix = "UNKNOWN";
64 }
65
66 /* check if the logstream is open already, opening it if needed */
67 if(handle->logstream == NULL && handle->logfile != NULL) {
68 int fd;
69 do {
70 fd = open(handle->logfile, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC,
71 0644);
72 } while(fd == -1 && errno == EINTR);
73 /* if we couldn't open it, we have an issue */
74 if(fd < 0 || (handle->logstream = fdopen(fd, "a")) == NULL) {
75 if(errno == EACCES) {
76 handle->pm_errno = ALPM_ERR_BADPERMS;
77 } else if(errno == ENOENT) {
78 handle->pm_errno = ALPM_ERR_NOT_A_DIR;
79 } else {
80 handle->pm_errno = ALPM_ERR_SYSTEM;
81 }
82 ret = -1;
83 }
84 }
85
86 va_start(args, fmt);
87
88 if(handle->usesyslog) {
89 /* we can't use a va_list more than once, so we need to copy it
90 * so we can use the original when calling vfprintf below. */
91 va_list args_syslog;
92 va_copy(args_syslog, args);
93 vsyslog(LOG_WARNING, fmt, args_syslog);
94 va_end(args_syslog);
95 }
96
97 if(handle->logstream) {
98 if(_alpm_log_leader(handle->logstream, prefix) < 0
99 || vfprintf(handle->logstream, fmt, args) < 0) {
100 ret = -1;
101 handle->pm_errno = ALPM_ERR_SYSTEM;
102 }
103 fflush(handle->logstream);
104 }
105
106 va_end(args);
107 return ret;
108 }
109
110 /** @} */
111
_alpm_log(alpm_handle_t * handle,alpm_loglevel_t flag,const char * fmt,...)112 void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...)
113 {
114 va_list args;
115
116 if(handle == NULL || handle->logcb == NULL) {
117 return;
118 }
119
120 va_start(args, fmt);
121 handle->logcb(flag, fmt, args);
122 va_end(args);
123 }
124