XBPS Library API 20240111
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;
38
39/**
40 * @file lib/log.c
41 * @brief Logging functions
42 * @defgroup log Logging functions
43 *
44 * Use these functions to log errors, warnings and debug messages.
45 */
46
47static void
48common_printf(FILE *f, const char *msg, const char *fmt, va_list ap)
49{
50 if (msg != NULL)
51 fprintf(f, "%s", msg);
52
53 vfprintf(f, fmt, ap);
54}
55
56void
57xbps_dbg_printf_append(const char *fmt, ...)
58{
59 va_list ap;
60
61 if (xbps_debug_level == 0)
62 return;
63
64 va_start(ap, fmt);
65 common_printf(stderr, NULL, fmt, ap);
66 va_end(ap);
67}
68
69void
70xbps_dbg_printf(const char *fmt, ...)
71{
72 va_list ap;
73
74 if (xbps_debug_level == 0)
75 return;
76
77 va_start(ap, fmt);
78 common_printf(stderr, "[DEBUG] ", fmt, ap);
79 va_end(ap);
80}
81
82void
83xbps_error_printf(const char *fmt, ...)
84{
85 va_list ap;
86
87 va_start(ap, fmt);
88 common_printf(stderr, "ERROR: ", fmt, ap);
89 va_end(ap);
90}
91
92void
93xbps_warn_printf(const char *fmt, ...)
94{
95 va_list ap;
96
97 va_start(ap, fmt);
98 common_printf(stderr, "WARNING: ", fmt, ap);
99 va_end(ap);
100}