lsquic_logger.h revision a137764b
1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * lsquic_logger.h -- logging functions and macros.
4 *
5 * Usage (this assumes MY_MODULE) is a part of enum lsquic_logger_module):
6 *   #define LSQUIC_LOGGER_MODULE MY_MODULE
7 *   #include "lsquic_logger.h"
8 *   LSQ_INFO("info message");
9 *
10 * If you want log messages from your module to contain connection ID, #define
11 * LSQUIC_LOG_CONN_ID so that it evaluates to connection ID.  If, in addition,
12 * you want stream ID to be logged, #define LSQUIC_LOG_STREAM_ID similarly.
13 * See existing code for examples.
14 *
15 * To add a module:
16 *   1. Add entry to enum lsquic_logger_module.
17 *   2. Update lsqlm_to_str.
18 *   3. Update lsq_log_levels.
19 */
20
21#ifndef LSQUIC_LOGGER_H
22#define LSQUIC_LOGGER_H
23
24#include <stdint.h>
25#include <stdio.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#ifndef LSQUIC_LOWEST_LOG_LEVEL
32#   define LSQUIC_LOWEST_LOG_LEVEL LSQ_LOG_DEBUG
33#endif
34
35/* Same levels as in sys/syslog.h: */
36enum lsq_log_level {
37    LSQ_LOG_EMERG,
38    LSQ_LOG_ALERT,
39    LSQ_LOG_CRIT,
40    LSQ_LOG_ERROR,
41    LSQ_LOG_WARN,
42    LSQ_LOG_NOTICE,
43    LSQ_LOG_INFO,
44    LSQ_LOG_DEBUG,
45    N_LSQUIC_LOG_LEVELS
46};
47
48enum lsquic_logger_module {
49    LSQLM_NOMODULE,
50    LSQLM_LOGGER,
51    LSQLM_EVENT,
52    LSQLM_ENGINE,
53    LSQLM_CONN,
54    LSQLM_RECHIST,
55    LSQLM_STREAM,
56    LSQLM_PARSE,
57    LSQLM_CFCW,
58    LSQLM_SFCW,
59    LSQLM_SENDCTL,
60    LSQLM_ALARMSET,
61    LSQLM_CRYPTO,
62    LSQLM_HANDSHAKE,
63    LSQLM_HSK_ADAPTER,
64    LSQLM_BBR,
65    LSQLM_CUBIC,
66    LSQLM_HEADERS,
67    LSQLM_FRAME_WRITER,
68    LSQLM_FRAME_READER,
69    LSQLM_MINI_CONN,
70    LSQLM_TOKGEN,
71    LSQLM_ENG_HIST,
72    LSQLM_SPI,
73    LSQLM_DI,
74    LSQLM_PRQ,
75    LSQLM_PACER,
76    LSQLM_HTTP1X,
77    LSQLM_QLOG,
78    LSQLM_TRAPA,
79    LSQLM_PURGA,
80    LSQLM_HCSI_READER,
81    LSQLM_HCSO_WRITER,
82    LSQLM_QENC_HDL,
83    LSQLM_QDEC_HDL,
84    LSQLM_QPACK_ENC,
85    LSQLM_QPACK_DEC,
86    LSQLM_PRIO,
87    LSQLM_BW_SAMPLER,
88    N_LSQUIC_LOGGER_MODULES
89};
90
91/* Each module has its own log level.
92 */
93extern enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES];
94
95extern const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES];
96
97extern const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS];
98
99#define LSQ_LOG_ENABLED_EXT(level, module) (                            \
100    level <= LSQUIC_LOWEST_LOG_LEVEL && level <= lsq_log_levels[module])
101
102#define LSQ_LOG_ENABLED(level) LSQ_LOG_ENABLED_EXT(level, LSQUIC_LOGGER_MODULE)
103
104struct lsquic_cid;
105
106/* The functions that perform actual logging are void.  This is an
107 * optimization.  In majority of cases the calls will succeed; even if
108 * they fail, there is nothing (at least, nothing simple) to be done to
109 * handle logging failure.
110 */
111
112/* There are four levels of log functions, depending on whether they take
113 * the following arguments:
114 *  1. Logger module
115 *  2. Connection ID
116 *  3. Stream ID
117 *
118 * Each level of logging function supports one additional argument, as seen
119 * below.  LSQ_LOG is set to one of LSQ_LOG0, LSQ_LOG1, LSQ_LOG2, or LSQ_LOG3.
120 * You can still use LSQ_LOG{0..3} directly.
121 */
122
123void
124lsquic_logger_log3 (enum lsq_log_level, enum lsquic_logger_module,
125                    const struct lsquic_cid *conn_id,
126                    lsquic_stream_id_t stream_id, const char *format, ...)
127#if __GNUC__
128            __attribute__((format(printf, 5, 6)))
129#endif
130;
131#   define LSQ_LOG3(level, ...) do {                                         \
132        if (LSQ_LOG_ENABLED(level))                                          \
133            lsquic_logger_log3(level, LSQUIC_LOGGER_MODULE,                  \
134                    LSQUIC_LOG_CONN_ID, LSQUIC_LOG_STREAM_ID, __VA_ARGS__);  \
135    } while (0)
136
137
138void
139lsquic_logger_log2 (enum lsq_log_level, enum lsquic_logger_module,
140                    const struct lsquic_cid *conn_id, const char *format, ...)
141#if __GNUC__
142            __attribute__((format(printf, 4, 5)))
143#endif
144;
145#   define LSQ_LOG2(level, ...) do {                                         \
146        if (LSQ_LOG_ENABLED(level))                                          \
147            lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE,                  \
148                                       LSQUIC_LOG_CONN_ID, __VA_ARGS__);     \
149    } while (0)
150#   define LSQ_LOG2C(level, ...) do {                                        \
151        if (LSQ_LOG_ENABLED(level))                                          \
152        {                                                                    \
153            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
154            lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE,                  \
155                                       LSQUIC_LOG_CONN_ID, __VA_ARGS__);     \
156        }                                                                    \
157    } while (0)
158
159void
160lsquic_logger_log1 (enum lsq_log_level, enum lsquic_logger_module,
161                    const char *format, ...)
162#if __GNUC__
163            __attribute__((format(printf, 3, 4)))
164#endif
165;
166#   define LSQ_LOG1(level, ...) do {                                         \
167        if (LSQ_LOG_ENABLED(level))                                          \
168            lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__);    \
169    } while (0)
170#   define LSQ_LOG1C(level, ...) do {                                        \
171        if (LSQ_LOG_ENABLED(level))                                          \
172        {                                                                    \
173            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
174            lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__);    \
175        }                                                                    \
176    } while (0)
177
178void
179lsquic_logger_log0 (enum lsq_log_level, const char *format, ...)
180#if __GNUC__
181            __attribute__((format(printf, 2, 3)))
182#endif
183;
184#   define LSQ_LOG0(level, ...) do {                                         \
185        if (LSQ_LOG_ENABLED(level))                                          \
186            lsquic_logger_log0(level, __VA_ARGS__);                          \
187    } while (0)
188#   define LSQ_LOG0C(level, ...) do {                                        \
189        if (LSQ_LOG_ENABLED(level))                                          \
190        {                                                                    \
191            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
192            lsquic_logger_log0(level, __VA_ARGS__);                          \
193        }                                                                    \
194    } while (0)
195
196#if defined(LSQUIC_LOGGER_MODULE)
197#if defined(LSQUIC_LOG_CONN_ID)
198#if defined(LSQUIC_LOG_STREAM_ID)
199#       define LSQ_LOG LSQ_LOG3
200#else
201#       define LSQ_LOG LSQ_LOG2
202#       define LSQ_LOGC LSQ_LOG2C
203#endif
204#else
205#       define LSQ_LOG LSQ_LOG1
206#       define LSQ_LOGC LSQ_LOG1C
207#endif
208#else
209#       define LSQ_LOG LSQ_LOG0
210#       define LSQ_LOGC LSQ_LOG0C
211#       define LSQUIC_LOGGER_MODULE LSQLM_NOMODULE
212#endif
213
214#define LSQ_DEBUG(...)   LSQ_LOG(LSQ_LOG_DEBUG,  __VA_ARGS__)
215#define LSQ_WARN(...)    LSQ_LOG(LSQ_LOG_WARN,   __VA_ARGS__)
216#define LSQ_ALERT(...)   LSQ_LOG(LSQ_LOG_ALERT,  __VA_ARGS__)
217#define LSQ_CRIT(...)    LSQ_LOG(LSQ_LOG_CRIT,   __VA_ARGS__)
218#define LSQ_ERROR(...)   LSQ_LOG(LSQ_LOG_ERROR,  __VA_ARGS__)
219#define LSQ_NOTICE(...)  LSQ_LOG(LSQ_LOG_NOTICE, __VA_ARGS__)
220#define LSQ_INFO(...)    LSQ_LOG(LSQ_LOG_INFO,   __VA_ARGS__)
221#define LSQ_EMERG(...)   LSQ_LOG(LSQ_LOG_EMERG,  __VA_ARGS__)
222
223#define LSQ_DEBUGC(...)   LSQ_LOGC(LSQ_LOG_DEBUG,  __VA_ARGS__)
224#define LSQ_WARNC(...)    LSQ_LOGC(LSQ_LOG_WARN,   __VA_ARGS__)
225#define LSQ_ALERTC(...)   LSQ_LOGC(LSQ_LOG_ALERT,  __VA_ARGS__)
226#define LSQ_CRITC(...)    LSQ_LOGC(LSQ_LOG_CRIT,   __VA_ARGS__)
227#define LSQ_ERRORC(...)   LSQ_LOGC(LSQ_LOG_ERROR,  __VA_ARGS__)
228#define LSQ_NOTICEC(...)  LSQ_LOGC(LSQ_LOG_NOTICE, __VA_ARGS__)
229#define LSQ_INFOC(...)    LSQ_LOGC(LSQ_LOG_INFO,   __VA_ARGS__)
230#define LSQ_EMERGC(...)   LSQ_LOGC(LSQ_LOG_EMERG,  __VA_ARGS__)
231
232/* Shorthand for printing to file streams using internal lsquic_logger_if
233 */
234void
235lsquic_log_to_fstream (FILE *, unsigned llts);
236
237enum lsquic_logger_module
238lsquic_str_to_logger_module (const char *);
239
240enum lsq_log_level
241lsquic_str_to_log_level (const char *);
242
243/* Parse and set log levels passed via -l flag.  If an error is encountered,
244 * an error message is printed to stderr and negative value is returned.
245 */
246int
247lsquic_logger_lopt (const char *optarg);
248
249#define CID_FMT ".*s"
250
251#define CID_BITS(cid) 2 * (int) (cid)->len, \
252                                    (lsquic_cid2str(cid, cidbuf_), cidbuf_)
253
254void
255lsquic_cid2str (const struct lsquic_cid *, char *out);
256
257const struct lsquic_cid *
258lsquic_conn_log_cid (const struct lsquic_conn *);
259
260#ifdef __cplusplus
261}
262#endif
263
264#endif
265