lsquic_logger.h revision b8fa6195
17d09751dSDmitri Tikhonov/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * lsquic_logger.h -- logging functions and macros.
450aadb33SDmitri Tikhonov *
550aadb33SDmitri Tikhonov * Usage (this assumes MY_MODULE) is a part of enum lsquic_logger_module):
650aadb33SDmitri Tikhonov *   #define LSQUIC_LOGGER_MODULE MY_MODULE
750aadb33SDmitri Tikhonov *   #include "lsquic_logger.h"
850aadb33SDmitri Tikhonov *   LSQ_INFO("info message");
950aadb33SDmitri Tikhonov *
1050aadb33SDmitri Tikhonov * If you want log messages from your module to contain connection ID, #define
1150aadb33SDmitri Tikhonov * LSQUIC_LOG_CONN_ID so that it evaluates to connection ID.  If, in addition,
1250aadb33SDmitri Tikhonov * you want stream ID to be logged, #define LSQUIC_LOG_STREAM_ID similarly.
1350aadb33SDmitri Tikhonov * See existing code for examples.
1450aadb33SDmitri Tikhonov *
1550aadb33SDmitri Tikhonov * To add a module:
1650aadb33SDmitri Tikhonov *   1. Add entry to enum lsquic_logger_module.
1750aadb33SDmitri Tikhonov *   2. Update lsqlm_to_str.
1850aadb33SDmitri Tikhonov *   3. Update lsq_log_levels.
1950aadb33SDmitri Tikhonov */
2050aadb33SDmitri Tikhonov
2150aadb33SDmitri Tikhonov#ifndef LSQUIC_LOGGER_H
2250aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_H
2350aadb33SDmitri Tikhonov
2450aadb33SDmitri Tikhonov#include <stdint.h>
2550aadb33SDmitri Tikhonov#include <stdio.h>
2650aadb33SDmitri Tikhonov
2750aadb33SDmitri Tikhonov#ifdef __cplusplus
2850aadb33SDmitri Tikhonovextern "C" {
2950aadb33SDmitri Tikhonov#endif
3050aadb33SDmitri Tikhonov
3150aadb33SDmitri Tikhonov#ifndef LSQUIC_LOWEST_LOG_LEVEL
3250aadb33SDmitri Tikhonov#   define LSQUIC_LOWEST_LOG_LEVEL LSQ_LOG_DEBUG
3350aadb33SDmitri Tikhonov#endif
3450aadb33SDmitri Tikhonov
3550aadb33SDmitri Tikhonov/* Same levels as in sys/syslog.h: */
3650aadb33SDmitri Tikhonovenum lsq_log_level {
3750aadb33SDmitri Tikhonov    LSQ_LOG_EMERG,
3850aadb33SDmitri Tikhonov    LSQ_LOG_ALERT,
3950aadb33SDmitri Tikhonov    LSQ_LOG_CRIT,
4050aadb33SDmitri Tikhonov    LSQ_LOG_ERROR,
4150aadb33SDmitri Tikhonov    LSQ_LOG_WARN,
4250aadb33SDmitri Tikhonov    LSQ_LOG_NOTICE,
4350aadb33SDmitri Tikhonov    LSQ_LOG_INFO,
4450aadb33SDmitri Tikhonov    LSQ_LOG_DEBUG,
4550aadb33SDmitri Tikhonov    N_LSQUIC_LOG_LEVELS
4650aadb33SDmitri Tikhonov};
4750aadb33SDmitri Tikhonov
4850aadb33SDmitri Tikhonovenum lsquic_logger_module {
4950aadb33SDmitri Tikhonov    LSQLM_NOMODULE,
5050aadb33SDmitri Tikhonov    LSQLM_LOGGER,
5150aadb33SDmitri Tikhonov    LSQLM_EVENT,
5250aadb33SDmitri Tikhonov    LSQLM_ENGINE,
5350aadb33SDmitri Tikhonov    LSQLM_CONN,
5450aadb33SDmitri Tikhonov    LSQLM_RECHIST,
5550aadb33SDmitri Tikhonov    LSQLM_STREAM,
5650aadb33SDmitri Tikhonov    LSQLM_PARSE,
5750aadb33SDmitri Tikhonov    LSQLM_CFCW,
5850aadb33SDmitri Tikhonov    LSQLM_SFCW,
5950aadb33SDmitri Tikhonov    LSQLM_SENDCTL,
6050aadb33SDmitri Tikhonov    LSQLM_ALARMSET,
6150aadb33SDmitri Tikhonov    LSQLM_CRYPTO,
6250aadb33SDmitri Tikhonov    LSQLM_HANDSHAKE,
6350aadb33SDmitri Tikhonov    LSQLM_HSK_ADAPTER,
645392f7a3SLiteSpeed Tech    LSQLM_BBR,
6550aadb33SDmitri Tikhonov    LSQLM_CUBIC,
6650aadb33SDmitri Tikhonov    LSQLM_HEADERS,
6750aadb33SDmitri Tikhonov    LSQLM_FRAME_WRITER,
6850aadb33SDmitri Tikhonov    LSQLM_FRAME_READER,
695392f7a3SLiteSpeed Tech    LSQLM_MINI_CONN,
705392f7a3SLiteSpeed Tech    LSQLM_TOKGEN,
7150aadb33SDmitri Tikhonov    LSQLM_ENG_HIST,
7250aadb33SDmitri Tikhonov    LSQLM_SPI,
7350aadb33SDmitri Tikhonov    LSQLM_DI,
745392f7a3SLiteSpeed Tech    LSQLM_PRQ,
7550aadb33SDmitri Tikhonov    LSQLM_PACER,
763b55e6aeSDmitri Tikhonov    LSQLM_HTTP1X,
7755cd0b38SDmitri Tikhonov    LSQLM_QLOG,
785392f7a3SLiteSpeed Tech    LSQLM_TRAPA,
795392f7a3SLiteSpeed Tech    LSQLM_PURGA,
805392f7a3SLiteSpeed Tech    LSQLM_HCSI_READER,
815392f7a3SLiteSpeed Tech    LSQLM_HCSO_WRITER,
825392f7a3SLiteSpeed Tech    LSQLM_QENC_HDL,
835392f7a3SLiteSpeed Tech    LSQLM_QDEC_HDL,
845392f7a3SLiteSpeed Tech    LSQLM_QPACK_ENC,
855392f7a3SLiteSpeed Tech    LSQLM_QPACK_DEC,
865392f7a3SLiteSpeed Tech    LSQLM_PRIO,
875392f7a3SLiteSpeed Tech    LSQLM_BW_SAMPLER,
88b8fa6195SDmitri Tikhonov    LSQLM_PACKET_RESIZE,
8950aadb33SDmitri Tikhonov    N_LSQUIC_LOGGER_MODULES
9050aadb33SDmitri Tikhonov};
9150aadb33SDmitri Tikhonov
9250aadb33SDmitri Tikhonov/* Each module has its own log level.
9350aadb33SDmitri Tikhonov */
9450aadb33SDmitri Tikhonovextern enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES];
9550aadb33SDmitri Tikhonov
9650aadb33SDmitri Tikhonovextern const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES];
9750aadb33SDmitri Tikhonov
9850aadb33SDmitri Tikhonovextern const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS];
9950aadb33SDmitri Tikhonov
10050aadb33SDmitri Tikhonov#define LSQ_LOG_ENABLED_EXT(level, module) (                            \
10150aadb33SDmitri Tikhonov    level <= LSQUIC_LOWEST_LOG_LEVEL && level <= lsq_log_levels[module])
10250aadb33SDmitri Tikhonov
10350aadb33SDmitri Tikhonov#define LSQ_LOG_ENABLED(level) LSQ_LOG_ENABLED_EXT(level, LSQUIC_LOGGER_MODULE)
10450aadb33SDmitri Tikhonov
1055392f7a3SLiteSpeed Techstruct lsquic_cid;
1065392f7a3SLiteSpeed Tech
10750aadb33SDmitri Tikhonov/* The functions that perform actual logging are void.  This is an
10850aadb33SDmitri Tikhonov * optimization.  In majority of cases the calls will succeed; even if
10950aadb33SDmitri Tikhonov * they fail, there is nothing (at least, nothing simple) to be done to
11050aadb33SDmitri Tikhonov * handle logging failure.
11150aadb33SDmitri Tikhonov */
11250aadb33SDmitri Tikhonov
11350aadb33SDmitri Tikhonov/* There are four levels of log functions, depending on whether they take
11450aadb33SDmitri Tikhonov * the following arguments:
11550aadb33SDmitri Tikhonov *  1. Logger module
11650aadb33SDmitri Tikhonov *  2. Connection ID
11750aadb33SDmitri Tikhonov *  3. Stream ID
11850aadb33SDmitri Tikhonov *
11950aadb33SDmitri Tikhonov * Each level of logging function supports one additional argument, as seen
12050aadb33SDmitri Tikhonov * below.  LSQ_LOG is set to one of LSQ_LOG0, LSQ_LOG1, LSQ_LOG2, or LSQ_LOG3.
12150aadb33SDmitri Tikhonov * You can still use LSQ_LOG{0..3} directly.
12250aadb33SDmitri Tikhonov */
12350aadb33SDmitri Tikhonov
12450aadb33SDmitri Tikhonovvoid
12550aadb33SDmitri Tikhonovlsquic_logger_log3 (enum lsq_log_level, enum lsquic_logger_module,
1265392f7a3SLiteSpeed Tech                    const struct lsquic_cid *conn_id,
1275392f7a3SLiteSpeed Tech                    lsquic_stream_id_t stream_id, const char *format, ...)
12850aadb33SDmitri Tikhonov#if __GNUC__
12950aadb33SDmitri Tikhonov            __attribute__((format(printf, 5, 6)))
13050aadb33SDmitri Tikhonov#endif
13150aadb33SDmitri Tikhonov;
13250aadb33SDmitri Tikhonov#   define LSQ_LOG3(level, ...) do {                                         \
13350aadb33SDmitri Tikhonov        if (LSQ_LOG_ENABLED(level))                                          \
13450aadb33SDmitri Tikhonov            lsquic_logger_log3(level, LSQUIC_LOGGER_MODULE,                  \
1355392f7a3SLiteSpeed Tech                    LSQUIC_LOG_CONN_ID, LSQUIC_LOG_STREAM_ID, __VA_ARGS__);  \
13650aadb33SDmitri Tikhonov    } while (0)
13750aadb33SDmitri Tikhonov
1385392f7a3SLiteSpeed Tech
13950aadb33SDmitri Tikhonovvoid
14050aadb33SDmitri Tikhonovlsquic_logger_log2 (enum lsq_log_level, enum lsquic_logger_module,
1415392f7a3SLiteSpeed Tech                    const struct lsquic_cid *conn_id, const char *format, ...)
14250aadb33SDmitri Tikhonov#if __GNUC__
14350aadb33SDmitri Tikhonov            __attribute__((format(printf, 4, 5)))
14450aadb33SDmitri Tikhonov#endif
14550aadb33SDmitri Tikhonov;
14650aadb33SDmitri Tikhonov#   define LSQ_LOG2(level, ...) do {                                         \
14750aadb33SDmitri Tikhonov        if (LSQ_LOG_ENABLED(level))                                          \
14850aadb33SDmitri Tikhonov            lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE,                  \
1495392f7a3SLiteSpeed Tech                                       LSQUIC_LOG_CONN_ID, __VA_ARGS__);     \
1505392f7a3SLiteSpeed Tech    } while (0)
1515392f7a3SLiteSpeed Tech#   define LSQ_LOG2C(level, ...) do {                                        \
1525392f7a3SLiteSpeed Tech        if (LSQ_LOG_ENABLED(level))                                          \
1535392f7a3SLiteSpeed Tech        {                                                                    \
1545392f7a3SLiteSpeed Tech            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
1555392f7a3SLiteSpeed Tech            lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE,                  \
1565392f7a3SLiteSpeed Tech                                       LSQUIC_LOG_CONN_ID, __VA_ARGS__);     \
1575392f7a3SLiteSpeed Tech        }                                                                    \
15850aadb33SDmitri Tikhonov    } while (0)
15950aadb33SDmitri Tikhonov
16050aadb33SDmitri Tikhonovvoid
16150aadb33SDmitri Tikhonovlsquic_logger_log1 (enum lsq_log_level, enum lsquic_logger_module,
16250aadb33SDmitri Tikhonov                    const char *format, ...)
16350aadb33SDmitri Tikhonov#if __GNUC__
16450aadb33SDmitri Tikhonov            __attribute__((format(printf, 3, 4)))
16550aadb33SDmitri Tikhonov#endif
16650aadb33SDmitri Tikhonov;
16750aadb33SDmitri Tikhonov#   define LSQ_LOG1(level, ...) do {                                         \
16850aadb33SDmitri Tikhonov        if (LSQ_LOG_ENABLED(level))                                          \
16950aadb33SDmitri Tikhonov            lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__);    \
17050aadb33SDmitri Tikhonov    } while (0)
1715392f7a3SLiteSpeed Tech#   define LSQ_LOG1C(level, ...) do {                                        \
1725392f7a3SLiteSpeed Tech        if (LSQ_LOG_ENABLED(level))                                          \
1735392f7a3SLiteSpeed Tech        {                                                                    \
1745392f7a3SLiteSpeed Tech            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
1755392f7a3SLiteSpeed Tech            lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__);    \
1765392f7a3SLiteSpeed Tech        }                                                                    \
1775392f7a3SLiteSpeed Tech    } while (0)
17850aadb33SDmitri Tikhonov
17950aadb33SDmitri Tikhonovvoid
18050aadb33SDmitri Tikhonovlsquic_logger_log0 (enum lsq_log_level, const char *format, ...)
18150aadb33SDmitri Tikhonov#if __GNUC__
18250aadb33SDmitri Tikhonov            __attribute__((format(printf, 2, 3)))
18350aadb33SDmitri Tikhonov#endif
18450aadb33SDmitri Tikhonov;
18550aadb33SDmitri Tikhonov#   define LSQ_LOG0(level, ...) do {                                         \
18650aadb33SDmitri Tikhonov        if (LSQ_LOG_ENABLED(level))                                          \
18750aadb33SDmitri Tikhonov            lsquic_logger_log0(level, __VA_ARGS__);                          \
18850aadb33SDmitri Tikhonov    } while (0)
1895392f7a3SLiteSpeed Tech#   define LSQ_LOG0C(level, ...) do {                                        \
1905392f7a3SLiteSpeed Tech        if (LSQ_LOG_ENABLED(level))                                          \
1915392f7a3SLiteSpeed Tech        {                                                                    \
1925392f7a3SLiteSpeed Tech            char cidbuf_[MAX_CID_LEN * 2 + 1];                               \
1935392f7a3SLiteSpeed Tech            lsquic_logger_log0(level, __VA_ARGS__);                          \
1945392f7a3SLiteSpeed Tech        }                                                                    \
1955392f7a3SLiteSpeed Tech    } while (0)
19650aadb33SDmitri Tikhonov
19750aadb33SDmitri Tikhonov#if defined(LSQUIC_LOGGER_MODULE)
19850aadb33SDmitri Tikhonov#if defined(LSQUIC_LOG_CONN_ID)
19950aadb33SDmitri Tikhonov#if defined(LSQUIC_LOG_STREAM_ID)
20050aadb33SDmitri Tikhonov#       define LSQ_LOG LSQ_LOG3
20150aadb33SDmitri Tikhonov#else
20250aadb33SDmitri Tikhonov#       define LSQ_LOG LSQ_LOG2
2035392f7a3SLiteSpeed Tech#       define LSQ_LOGC LSQ_LOG2C
20450aadb33SDmitri Tikhonov#endif
20550aadb33SDmitri Tikhonov#else
20650aadb33SDmitri Tikhonov#       define LSQ_LOG LSQ_LOG1
2075392f7a3SLiteSpeed Tech#       define LSQ_LOGC LSQ_LOG1C
20850aadb33SDmitri Tikhonov#endif
20950aadb33SDmitri Tikhonov#else
21050aadb33SDmitri Tikhonov#       define LSQ_LOG LSQ_LOG0
2115392f7a3SLiteSpeed Tech#       define LSQ_LOGC LSQ_LOG0C
21250aadb33SDmitri Tikhonov#       define LSQUIC_LOGGER_MODULE LSQLM_NOMODULE
21350aadb33SDmitri Tikhonov#endif
21450aadb33SDmitri Tikhonov
21550aadb33SDmitri Tikhonov#define LSQ_DEBUG(...)   LSQ_LOG(LSQ_LOG_DEBUG,  __VA_ARGS__)
21650aadb33SDmitri Tikhonov#define LSQ_WARN(...)    LSQ_LOG(LSQ_LOG_WARN,   __VA_ARGS__)
21750aadb33SDmitri Tikhonov#define LSQ_ALERT(...)   LSQ_LOG(LSQ_LOG_ALERT,  __VA_ARGS__)
21850aadb33SDmitri Tikhonov#define LSQ_CRIT(...)    LSQ_LOG(LSQ_LOG_CRIT,   __VA_ARGS__)
21950aadb33SDmitri Tikhonov#define LSQ_ERROR(...)   LSQ_LOG(LSQ_LOG_ERROR,  __VA_ARGS__)
22050aadb33SDmitri Tikhonov#define LSQ_NOTICE(...)  LSQ_LOG(LSQ_LOG_NOTICE, __VA_ARGS__)
22150aadb33SDmitri Tikhonov#define LSQ_INFO(...)    LSQ_LOG(LSQ_LOG_INFO,   __VA_ARGS__)
22250aadb33SDmitri Tikhonov#define LSQ_EMERG(...)   LSQ_LOG(LSQ_LOG_EMERG,  __VA_ARGS__)
22350aadb33SDmitri Tikhonov
2245392f7a3SLiteSpeed Tech#define LSQ_DEBUGC(...)   LSQ_LOGC(LSQ_LOG_DEBUG,  __VA_ARGS__)
2255392f7a3SLiteSpeed Tech#define LSQ_WARNC(...)    LSQ_LOGC(LSQ_LOG_WARN,   __VA_ARGS__)
2265392f7a3SLiteSpeed Tech#define LSQ_ALERTC(...)   LSQ_LOGC(LSQ_LOG_ALERT,  __VA_ARGS__)
2275392f7a3SLiteSpeed Tech#define LSQ_CRITC(...)    LSQ_LOGC(LSQ_LOG_CRIT,   __VA_ARGS__)
2285392f7a3SLiteSpeed Tech#define LSQ_ERRORC(...)   LSQ_LOGC(LSQ_LOG_ERROR,  __VA_ARGS__)
2295392f7a3SLiteSpeed Tech#define LSQ_NOTICEC(...)  LSQ_LOGC(LSQ_LOG_NOTICE, __VA_ARGS__)
2305392f7a3SLiteSpeed Tech#define LSQ_INFOC(...)    LSQ_LOGC(LSQ_LOG_INFO,   __VA_ARGS__)
2315392f7a3SLiteSpeed Tech#define LSQ_EMERGC(...)   LSQ_LOGC(LSQ_LOG_EMERG,  __VA_ARGS__)
2325392f7a3SLiteSpeed Tech
23350aadb33SDmitri Tikhonov/* Shorthand for printing to file streams using internal lsquic_logger_if
23450aadb33SDmitri Tikhonov */
23550aadb33SDmitri Tikhonovvoid
23650aadb33SDmitri Tikhonovlsquic_log_to_fstream (FILE *, unsigned llts);
23750aadb33SDmitri Tikhonov
23850aadb33SDmitri Tikhonovenum lsquic_logger_module
23950aadb33SDmitri Tikhonovlsquic_str_to_logger_module (const char *);
24050aadb33SDmitri Tikhonov
24150aadb33SDmitri Tikhonovenum lsq_log_level
24250aadb33SDmitri Tikhonovlsquic_str_to_log_level (const char *);
24350aadb33SDmitri Tikhonov
24450aadb33SDmitri Tikhonov/* Parse and set log levels passed via -l flag.  If an error is encountered,
24550aadb33SDmitri Tikhonov * an error message is printed to stderr and negative value is returned.
24650aadb33SDmitri Tikhonov */
24750aadb33SDmitri Tikhonovint
24850aadb33SDmitri Tikhonovlsquic_logger_lopt (const char *optarg);
24950aadb33SDmitri Tikhonov
2505392f7a3SLiteSpeed Tech#define CID_FMT ".*s"
2515392f7a3SLiteSpeed Tech
252fb73393fSDmitri Tikhonov#define CID_BITS_B(cid, b) 2 * (int) (cid)->len, \
253fb73393fSDmitri Tikhonov                                    (lsquic_cid2str(cid, b), b)
254fb73393fSDmitri Tikhonov
255fb73393fSDmitri Tikhonov#define CID_BITS(cid) CID_BITS_B(cid, cidbuf_)
2565392f7a3SLiteSpeed Tech
2575392f7a3SLiteSpeed Techvoid
2585392f7a3SLiteSpeed Techlsquic_cid2str (const struct lsquic_cid *, char *out);
2595392f7a3SLiteSpeed Tech
2605392f7a3SLiteSpeed Techconst struct lsquic_cid *
2615392f7a3SLiteSpeed Techlsquic_conn_log_cid (const struct lsquic_conn *);
2625392f7a3SLiteSpeed Tech
26350aadb33SDmitri Tikhonov#ifdef __cplusplus
26450aadb33SDmitri Tikhonov}
26550aadb33SDmitri Tikhonov#endif
26650aadb33SDmitri Tikhonov
26750aadb33SDmitri Tikhonov#endif
268