Debian Codegraph @

contrib/libngtcp2/ngtcp2/lib/ngtcp2_log.h

raw symbols SCIP
1 /*
2 * ngtcp2
3 *
4 * Copyright (c) 2018 ngtcp2 contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #ifndef NGTCP2_LOG_H
26 #define NGTCP2_LOG_H
27
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif /* defined(HAVE_CONFIG_H) */
31
32 #include <ngtcp2/ngtcp2.h>
33
34 #include "ngtcp2_pkt.h"
35
36 typedef struct ngtcp2_log {
37 /* log_printf is a sink to write log. NULL means no logging
38 output. */
39 ngtcp2_printf log_printf;
40 /* events is an event filter. Only events set in this field are
41 emitted. */
42 uint8_t events;
43 /* ts is the time point used to write time delta in the log. */
44 ngtcp2_tstamp ts;
45 /* last_ts is the most recent time point that this object is
46 told. */
47 ngtcp2_tstamp last_ts;
48 /* user_data is user-defined opaque data which is passed to
49 log_pritnf. */
50 void *user_data;
51 /* scid is SCID encoded as NULL-terminated hex string. */
52 char scid[NGTCP2_MAX_CIDLEN * 2 + 1];
53 } ngtcp2_log;
54
55 /**
56 * @enum
57 *
58 * :type:`ngtcp2_log_event` defines an event of ngtcp2 library
59 * internal logger.
60 */
61 typedef enum ngtcp2_log_event {
62 /**
63 * :enum:`NGTCP2_LOG_EVENT_NONE` represents no event.
64 */
65 NGTCP2_LOG_EVENT_NONE,
66 /**
67 * :enum:`NGTCP2_LOG_EVENT_CON` is a connection (catch-all) event
68 */
69 NGTCP2_LOG_EVENT_CON = 0x1,
70 /**
71 * :enum:`NGTCP2_LOG_EVENT_PKT` is a packet event.
72 */
73 NGTCP2_LOG_EVENT_PKT = 0x2,
74 /**
75 * :enum:`NGTCP2_LOG_EVENT_FRM` is a QUIC frame event.
76 */
77 NGTCP2_LOG_EVENT_FRM = 0x4,
78 /**
79 * :enum:`NGTCP2_LOG_EVENT_LDC` is a loss detection and congestion
80 * control event.
81 */
82 NGTCP2_LOG_EVENT_LDC = 0x8,
83 /**
84 * :enum:`NGTCP2_LOG_EVENT_CRY` is a crypto event.
85 */
86 NGTCP2_LOG_EVENT_CRY = 0x10,
87 /**
88 * :enum:`NGTCP2_LOG_EVENT_PTV` is a path validation event.
89 */
90 NGTCP2_LOG_EVENT_PTV = 0x20,
91 /**
92 * :enum:`NGTCP2_LOG_EVENT_CCA` is a congestion controller algorithm
93 * event.
94 */
95 NGTCP2_LOG_EVENT_CCA = 0x40,
96 } ngtcp2_log_event;
97
98 void ngtcp2_log_init(ngtcp2_log *log, const ngtcp2_cid *scid,
99 ngtcp2_printf log_printf, ngtcp2_tstamp ts,
100 void *user_data);
101
102 void ngtcp2_log_rx_fr(ngtcp2_log *log, const ngtcp2_pkt_hd *hd,
103 const ngtcp2_frame *fr);
104 void ngtcp2_log_tx_fr(ngtcp2_log *log, const ngtcp2_pkt_hd *hd,
105 const ngtcp2_frame *fr);
106
107 void ngtcp2_log_rx_vn(ngtcp2_log *log, const ngtcp2_pkt_hd *hd,
108 const uint32_t *sv, size_t nsv);
109
110 void ngtcp2_log_rx_sr(ngtcp2_log *log, const ngtcp2_pkt_stateless_reset *sr);
111
112 void ngtcp2_log_remote_tp(ngtcp2_log *log,
113 const ngtcp2_transport_params *params);
114
115 void ngtcp2_log_pkt_lost(ngtcp2_log *log, int64_t pkt_num, uint8_t type,
116 uint8_t flags, ngtcp2_tstamp sent_ts);
117
118 void ngtcp2_log_rx_pkt_hd(ngtcp2_log *log, const ngtcp2_pkt_hd *hd);
119
120 void ngtcp2_log_tx_pkt_hd(ngtcp2_log *log, const ngtcp2_pkt_hd *hd);
121
122 void ngtcp2_log_tx_cancel(ngtcp2_log *log, const ngtcp2_pkt_hd *hd);
123
124 #define NGTCP2_LOG_HD "I%08" PRIu64 " 0x%s %s"
125
126 uint64_t ngtcp2_log_timestamp(const ngtcp2_log *log);
127
128 static inline const char *ngtcp2_log_event_str(ngtcp2_log_event ev) {
129 switch (ev) {
130 case NGTCP2_LOG_EVENT_CON:
131 return "con";
132 case NGTCP2_LOG_EVENT_PKT:
133 return "pkt";
134 case NGTCP2_LOG_EVENT_FRM:
135 return "frm";
136 case NGTCP2_LOG_EVENT_LDC:
137 return "ldc";
138 case NGTCP2_LOG_EVENT_CRY:
139 return "cry";
140 case NGTCP2_LOG_EVENT_PTV:
141 return "ptv";
142 case NGTCP2_LOG_EVENT_CCA:
143 return "cca";
144 case NGTCP2_LOG_EVENT_NONE:
145 default:
146 return "non";
147 }
148 }
149
150 #define ngtcp2_log_infof_raw(LOG, EV, FMT, ...) \
151 (LOG)->log_printf((LOG)->user_data, NGTCP2_LOG_HD " " FMT, \
152 ngtcp2_log_timestamp(LOG), (LOG)->scid, \
153 ngtcp2_log_event_str(EV), __VA_ARGS__);
154
155 /**
156 * @function
157 *
158 * `ngtcp2_log_infof` writes info level log with printf like
159 * formatting.
160 */
161 #define ngtcp2_log_infof(LOG, EV, FMT, ...) \
162 do { \
163 if (!(LOG)->log_printf || !((LOG)->events & (EV))) { \
164 break; \
165 } \
166 \
167 ngtcp2_log_infof_raw((LOG), (EV), FMT, __VA_ARGS__); \
168 } while (0)
169
170 #define ngtcp2_log_info_raw(LOG, EV, FMT) \
171 (LOG)->log_printf((LOG)->user_data, NGTCP2_LOG_HD " " FMT, \
172 ngtcp2_log_timestamp(LOG), (LOG)->scid, \
173 ngtcp2_log_event_str(EV))
174
175 /**
176 * @function
177 *
178 * `ngtcp2_log_info` writes info level log. FMT should not contain
179 * formatting directive. This function exists to workaround the issue
180 * that __VA_ARGS__ cannot be empty.
181 */
182 #define ngtcp2_log_info(LOG, EV, FMT) \
183 do { \
184 if (!(LOG)->log_printf || !((LOG)->events & (EV))) { \
185 break; \
186 } \
187 \
188 ngtcp2_log_info_raw((LOG), (EV), FMT); \
189 } while (0)
190
191 #endif /* !defined(NGTCP2_LOG_H) */