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-2025 OpenVPN Inc <sales@openvpn.net>
6 * Copyright (C) 2018-2025 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef ERROR_H
22#define ERROR_H
23
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stdlib.h>
27
28/*
29 * These globals should not be accessed directly,
30 * but rather through macros or inline functions defined below.
31 */
32extern unsigned int x_debug_level;
33extern int x_msg_line_num;
34
35/* msg() flags */
36
37#define M_DEBUG_LEVEL (0x0F) /* debug level mask */
38
39#define M_FATAL (1 << 4) /* exit program */
40#define M_NONFATAL (1 << 5) /* non-fatal error */
41#define M_WARN (1 << 6) /* call syslog with LOG_WARNING */
42#define M_DEBUG (1 << 7)
43
44#define M_ERRNO (1 << 8) /* show errno description */
45
46#define M_NOMUTE (1 << 11) /* don't do mute processing */
47#define M_NOPREFIX (1 << 12) /* don't show date/time prefix */
48#define M_USAGE_SMALL (1 << 13) /* fatal options error, call usage_small */
49#define M_MSG_VIRT_OUT (1 << 14) /* output message through msg_status_output callback */
50#define M_OPTERR (1 << 15) /* print "Options error:" prefix */
51#define M_NOLF (1 << 16) /* don't print new line */
52#define M_NOIPREFIX (1 << 17) /* don't print instance prefix */
53
54/* flag combinations which are frequently used */
55#define M_ERR (M_FATAL | M_ERRNO)
56#define M_USAGE (M_USAGE_SMALL | M_NOPREFIX | M_OPTERR)
57#define M_CLIENT (M_MSG_VIRT_OUT | M_NOMUTE | M_NOIPREFIX)
58
59
61bool dont_mute(unsigned int flags);
62
63/* Macro to ensure (and teach static analysis tools) we exit on fatal errors */
64#ifdef _MSC_VER
65#pragma warning(disable : 4127) /* EXIT_FATAL(flags) macro raises "warning C4127: conditional \
66 expression is constant" on each non M_FATAL invocation. */
67#endif
68#define EXIT_FATAL(flags) \
69 do \
70 { \
71 if ((flags) & M_FATAL) \
72 { \
73 _exit(1); \
74 } \
75 } while (false)
76
77#define msg(flags, ...) \
78 do \
79 { \
80 if (msg_test(flags)) \
81 { \
82 x_msg((flags), __VA_ARGS__); \
83 } \
84 EXIT_FATAL(flags); \
85 } while (false)
86#ifdef ENABLE_DEBUG
87#define dmsg(flags, ...) \
88 do \
89 { \
90 if (msg_test(flags)) \
91 { \
92 x_msg((flags), __VA_ARGS__); \
93 } \
94 EXIT_FATAL(flags); \
95 } while (false)
96#else
97#define dmsg(flags, ...)
98#endif
99
100void x_msg(const unsigned int flags, const char *format, ...); /* should be called via msg above */
101
102void x_msg_va(const unsigned int flags, const char *format, va_list arglist);
103
104/* Inline functions */
105
106static inline bool
107check_debug_level(unsigned int level)
108{
109 return (level & M_DEBUG_LEVEL) <= x_debug_level;
110}
111
113static inline bool
114msg_test(unsigned int flags)
115{
116 return check_debug_level(flags) && dont_mute(flags);
117}
118
119#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:264
unsigned int x_debug_level
Definition error.c:51
#define M_DEBUG_LEVEL
Definition error.h:86
bool dont_mute(unsigned int flags)
Check muting filter.
Definition error.c:388
int x_msg_line_num
Definition error.c:214
void x_msg(const unsigned int flags, const char *format,...)
Definition error.c:217
static bool check_debug_level(unsigned int level)
Definition error.h:257
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition error.c:238