File: verbose.h

package info (click to toggle)
libreswan 5.2-2.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,644 kB
  • sloc: ansic: 129,988; sh: 32,018; xml: 20,646; python: 10,303; makefile: 3,022; javascript: 1,506; sed: 574; yacc: 511; perl: 264; awk: 52
file content (129 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* verbose wrapper around logger
 *
 * Copyright (C) 2024  Andrew Cagney
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <https://www.gnu.org/licenses/gpl2.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 */

#ifndef VERBOSE_H
#define VERBOSE_H

/*
 * Pass-by-value wrapper around logger to make it easy to generate
 * indented debug/verbose logs.
 *
 * Standalone tools, such as <<ipsec showroute>>, can enable more
 * verbose logging when --verbose is specified, vis:
 *
 *	struct verbose verbose = {
 *		.logger = logger,
 *		.rc_flags = (verbose ? LOG_STREAM : 0),
 *	};
 *
 * While pluto, internally, enables more verbose debug logging:
 *
 *	struct verbose verbose = {
 *		.logger = logger,
 *		.rc_flags = (DBGP(DBG_BASE) ? DEBUG_STREAM : 0),
 *	};
 *
 * Functions then pass verbose by value, and increment .level as
 * needed.
 */

#include "lset.h"

struct verbose {
	const struct logger *logger;
	lset_t rc_flags;
	int level;
	const char *prefix;
};

/*
 * vlog() output indented logs
 * verbose() outputs indented logs
 * vdbg() outputs indented debug logs when DBG_BASE
 */
#define VERBOSE_LOG(LOGGER, MESSAGE, ...)		\
	struct verbose verbose = {			\
		.logger = (LOGGER),			\
		.rc_flags = RC_LOG,			\
		.prefix = "",				\
	};						\
	verbose(MESSAGE, ##__VA_ARGS__);		\
	verbose.level++;

/*
 * vlog() outputs flat logs
 * verbose() outputs indented debug logs when DBG_BASE
 * vdbg() outputs indented debug logs when DBG_BASE
 */
#define VERBOSE_DBGP(COND, LOGGER, MESSAGE, ...)			\
	struct verbose verbose = {					\
		.logger = (LOGGER),					\
		.rc_flags = (LDBGP(COND, LOGGER) ? DEBUG_STREAM : 0),	\
		.prefix = "",						\
	};								\
	verbose(MESSAGE, ##__VA_ARGS__);				\
	verbose.level++;

#define PRI_VERBOSE "%s%*s"
#define pri_verbose "", (verbose.level * 2), ""

/*
 * Normal logging: the message is always logged (no indentation); just
 * a wrapper around llog(verbose.logger)
 */
#define vlog(FMT, ...)					\
	llog(RC_LOG, verbose.logger, FMT, ##__VA_ARGS__);

/*
 * Debug-logging: when the logger has debugging enabled, the message is
 * logged, prefixed by indentation.
 */

#define vdbg(FMT, ...)							\
	{								\
		if (LDBGP(DBG_BASE, verbose.logger)) {			\
			llog(DEBUG_STREAM, verbose.logger,		\
			     PRI_VERBOSE""FMT,				\
			     pri_verbose, ##__VA_ARGS__);		\
		}							\
	}

/*
 * Informational log: when verbose.rc_info is non-zero, the message is
 * logged, prefixed by indentation.
 *
 * Use this for messages that, depending on the caller, should be
 * supressed, pretty-logged or pretty-debug-logged.
 */

#define verbose(FMT, ...)							\
	{								\
		if (verbose.rc_flags != 0) {				\
			llog(verbose.rc_flags, verbose.logger,		\
			     PRI_VERBOSE""FMT,				\
			     pri_verbose, ##__VA_ARGS__);		\
		}							\
	}

#define vbad(BAD) PBAD(verbose.logger, BAD)

#define vexpect(EXPECT) PEXPECT(verbose.logger, EXPECT)
#define vassert(ASSERT) PASSERT(verbose.logger, ASSERT)

#define vexpect_where(WHERE, EXPECT) PEXPECT_WHERE(verbose.logger, WHERE, EXPECT)
#define vassert_where(WHERE, ASSERT) PASSERT_WHERE(verbose.logger, WHERE, ASSERT)

#endif