OpenVPN
error.h
Go to the documentation of this file.
1/*
2 * error -- OpenVPN compatible error reporting API
3 * https://community.openvpn.net/openvpn/wiki/Tapctl
4 *
5 * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
6 * Copyright (C) 2018-2024 Simon Rozman <simon@rozman.si>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#ifndef ERROR_H
23#define ERROR_H
24
25#include <stdarg.h>
26#include <stdbool.h>
27#include <stdlib.h>
28
29/*
30 * These globals should not be accessed directly,
31 * but rather through macros or inline functions defined below.
32 */
33extern unsigned int x_debug_level;
34extern int x_msg_line_num;
35
36/* msg() flags */
37
38#define M_DEBUG_LEVEL (0x0F) /* debug level mask */
39
40#define M_FATAL (1<<4) /* exit program */
41#define M_NONFATAL (1<<5) /* non-fatal error */
42#define M_WARN (1<<6) /* call syslog with LOG_WARNING */
43#define M_DEBUG (1<<7)
44
45#define M_ERRNO (1<<8) /* show errno description */
46
47#define M_NOMUTE (1<<11) /* don't do mute processing */
48#define M_NOPREFIX (1<<12) /* don't show date/time prefix */
49#define M_USAGE_SMALL (1<<13) /* fatal options error, call usage_small */
50#define M_MSG_VIRT_OUT (1<<14) /* output message through msg_status_output callback */
51#define M_OPTERR (1<<15) /* print "Options error:" prefix */
52#define M_NOLF (1<<16) /* don't print new line */
53#define M_NOIPREFIX (1<<17) /* don't print instance prefix */
54
55/* flag combinations which are frequently used */
56#define M_ERR (M_FATAL | M_ERRNO)
57#define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
58#define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
59
60
62bool dont_mute(unsigned int flags);
63
64/* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
65#ifdef _MSC_VER
66#pragma warning(disable: 4127) /* EXIT_FATAL(flags) macro raises "warning C4127: conditional expression is constant" on each non M_FATAL invocation. */
67#endif
68#define EXIT_FATAL(flags) do { if ((flags) & M_FATAL) {_exit(1);}} while (false)
69
70#define msg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
71#ifdef ENABLE_DEBUG
72#define dmsg(flags, ...) do { if (msg_test(flags)) {x_msg((flags), __VA_ARGS__);} EXIT_FATAL(flags); } while (false)
73#else
74#define dmsg(flags, ...)
75#endif
76
77void x_msg(const unsigned int flags, const char *format, ...); /* should be called via msg above */
78
79void x_msg_va(const unsigned int flags, const char *format, va_list arglist);
80
81/* Inline functions */
82
83static inline bool
84check_debug_level(unsigned int level)
85{
86 return (level & M_DEBUG_LEVEL) <= x_debug_level;
87}
88
90static inline bool
91msg_test(unsigned int flags)
92{
93 return check_debug_level(flags) && dont_mute(flags);
94}
95
96#endif /* ifndef ERROR_H */
static bool msg_test(unsigned int flags)
Return true if flags represent an enabled, not muted log level.
Definition error.h:227
unsigned int x_debug_level
Definition error.c:52
#define M_DEBUG_LEVEL
Definition error.h:87
bool dont_mute(unsigned int flags)
Check muting filter.
Definition error.c:407
int x_msg_line_num
Definition error.c:210
void x_msg(const unsigned int flags, const char *format,...)
Definition error.c:213
static bool check_debug_level(unsigned int level)
Definition error.h:220
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition error.c:234