XBPS Library API 20250719
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/macro.h"
32#include "xbps_api_impl.h"
33
34#ifdef __clang__
35#pragma clang diagnostic ignored "-Wformat-nonliteral"
36#endif
37
39int xbps_verbose_level = 0;
40
41/**
42 * @file lib/log.c
43 * @brief Logging functions
44 * @defgroup log Logging functions
45 *
46 * Use these functions to log errors, warnings and debug messages.
47 */
48
49static void
50common_printf(FILE *f, const char *msg, const char *fmt, va_list ap)
51{
52 if (msg != NULL)
53 fprintf(f, "%s", msg);
54
55 vfprintf(f, fmt, ap);
56}
57
58void
59xbps_dbg_printf_append(const char *fmt, ...)
60{
61 va_list ap;
62
63 if (xbps_debug_level == 0)
64 return;
65
66 va_start(ap, fmt);
67 common_printf(stderr, NULL, fmt, ap);
68 va_end(ap);
69}
70
71void
72xbps_dbg_printf(const char *fmt, ...)
73{
74 va_list ap;
75
76 if (xbps_debug_level == 0)
77 return;
78
79 va_start(ap, fmt);
80 common_printf(stderr, "[DEBUG] ", fmt, ap);
81 va_end(ap);
82}
83
84void
85xbps_verbose_printf(const char *fmt, ...)
86{
87 va_list ap;
88
89 if (xbps_verbose_level == 0)
90 return;
91
92 va_start(ap, fmt);
93 common_printf(stderr, NULL, fmt, ap);
94 va_end(ap);
95}
96
97void
98xbps_error_printf(const char *fmt, ...)
99{
100 va_list ap;
101
102 va_start(ap, fmt);
103 common_printf(stderr, "ERROR: ", fmt, ap);
104 va_end(ap);
105}
106
107void
108xbps_warn_printf(const char *fmt, ...)
109{
110 va_list ap;
111
112 va_start(ap, fmt);
113 common_printf(stderr, "WARNING: ", fmt, ap);
114 va_end(ap);
115}
116
117int
118xbps_error_errno(int r, const char *fmt, ...)
119{
120 va_list ap;
121
122 va_start(ap, fmt);
123 common_printf(stderr, "ERROR: ", fmt, ap);
124 va_end(ap);
125
126 return -ABS(r);
127}
void xbps_verbose_printf(const char *,...) __attribute__((format(printf
Prints messages to stderr if verbosity is enabled.
int xbps_debug_level
The Debug level.
Definition log.c:38
int xbps_error_errno(int r, const char *fmt,...)
Prints formatted log message to stderr and returns error.
Definition log.c:118
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:72
void xbps_error_printf(const char *fmt,...)
Prints error messages to stderr.
Definition log.c:98
void xbps_dbg_printf_append(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:59
void xbps_warn_printf(const char *fmt,...)
Prints warning messages to stderr.
Definition log.c:108