XBPS Library API 20250624
The X Binary Package System
log.c
1/*-
2 * Copyright (c) 2011-2015 Juan Romero Pardines.
3 * Copyright (c) 2014 Enno Boland.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <stdio.h>
28#include <errno.h>
29#include <stdarg.h>
30
31#include "xbps_api_impl.h"
32
33#ifdef __clang__
34#pragma clang diagnostic ignored "-Wformat-nonliteral"
35#endif
36
37int xbps_debug_level = 0;
38int xbps_verbose_level = 0;
39
40/**
41 * @file lib/log.c
42 * @brief Logging functions
43 * @defgroup log Logging functions
44 *
45 * Use these functions to log errors, warnings and debug messages.
46 */
47
48static void
49common_printf(FILE *f, const char *msg, const char *fmt, va_list ap)
50{
51 if (msg != NULL)
52 fprintf(f, "%s", msg);
53
54 vfprintf(f, fmt, ap);
55}
56
57void
58xbps_dbg_printf_append(const char *fmt, ...)
59{
60 va_list ap;
61
62 if (xbps_debug_level == 0)
63 return;
64
65 va_start(ap, fmt);
66 common_printf(stderr, NULL, fmt, ap);
67 va_end(ap);
68}
69
70void
71xbps_dbg_printf(const char *fmt, ...)
72{
73 va_list ap;
74
75 if (xbps_debug_level == 0)
76 return;
77
78 va_start(ap, fmt);
79 common_printf(stderr, "[DEBUG] ", fmt, ap);
80 va_end(ap);
81}
82
83void
84xbps_verbose_printf(const char *fmt, ...)
85{
86 va_list ap;
87
88 if (xbps_verbose_level == 0)
89 return;
90
91 va_start(ap, fmt);
92 common_printf(stderr, NULL, fmt, ap);
93 va_end(ap);
94}
95
96void
97xbps_error_printf(const char *fmt, ...)
98{
99 va_list ap;
100
101 va_start(ap, fmt);
102 common_printf(stderr, "ERROR: ", fmt, ap);
103 va_end(ap);
104}
105
106void
107xbps_warn_printf(const char *fmt, ...)
108{
109 va_list ap;
110
111 va_start(ap, fmt);
112 common_printf(stderr, "WARNING: ", fmt, ap);
113 va_end(ap);
114}