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