lsquic_logger.h revision 758aff32
1/* Copyright (c) 2017 - 2020 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_STREAM, 55 LSQLM_PARSE, 56 LSQLM_CFCW, 57 LSQLM_SFCW, 58 LSQLM_SENDCTL, 59 LSQLM_ALARMSET, 60 LSQLM_CRYPTO, 61 LSQLM_HANDSHAKE, 62 LSQLM_HSK_ADAPTER, 63 LSQLM_BBR, 64 LSQLM_CUBIC, 65 LSQLM_ADAPTIVE_CC, 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_HPI, 74 LSQLM_DI, 75 LSQLM_PRQ, 76 LSQLM_PACER, 77 LSQLM_HTTP1X, 78 LSQLM_QLOG, 79 LSQLM_TRAPA, 80 LSQLM_PURGA, 81 LSQLM_HCSI_READER, 82 LSQLM_HCSO_WRITER, 83 LSQLM_QENC_HDL, 84 LSQLM_QDEC_HDL, 85 LSQLM_QPACK_ENC, 86 LSQLM_QPACK_DEC, 87 LSQLM_PRIO, 88 LSQLM_BW_SAMPLER, 89 LSQLM_PACKET_RESIZE, 90 LSQLM_CONN_STATS, 91 N_LSQUIC_LOGGER_MODULES 92}; 93 94/* Each module has its own log level. 95 */ 96extern enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES]; 97 98extern const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES]; 99 100extern const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS]; 101 102#define LSQ_LOG_ENABLED_EXT(level, module) ( \ 103 level <= LSQUIC_LOWEST_LOG_LEVEL && level <= lsq_log_levels[module]) 104 105#define LSQ_LOG_ENABLED(level) LSQ_LOG_ENABLED_EXT(level, LSQUIC_LOGGER_MODULE) 106 107struct lsquic_cid; 108 109/* The functions that perform actual logging are void. This is an 110 * optimization. In majority of cases the calls will succeed; even if 111 * they fail, there is nothing (at least, nothing simple) to be done to 112 * handle logging failure. 113 */ 114 115/* There are four levels of log functions, depending on whether they take 116 * the following arguments: 117 * 1. Logger module 118 * 2. Connection ID 119 * 3. Stream ID 120 * 121 * Each level of logging function supports one additional argument, as seen 122 * below. LSQ_LOG is set to one of LSQ_LOG0, LSQ_LOG1, LSQ_LOG2, or LSQ_LOG3. 123 * You can still use LSQ_LOG{0..3} directly. 124 */ 125 126void 127lsquic_logger_log3 (enum lsq_log_level, enum lsquic_logger_module, 128 const struct lsquic_cid *conn_id, 129 lsquic_stream_id_t stream_id, const char *format, ...) 130#if __GNUC__ 131 __attribute__((format(printf, 5, 6))) 132#endif 133; 134# define LSQ_LOG3(level, ...) do { \ 135 if (LSQ_LOG_ENABLED(level)) \ 136 lsquic_logger_log3(level, LSQUIC_LOGGER_MODULE, \ 137 LSQUIC_LOG_CONN_ID, LSQUIC_LOG_STREAM_ID, __VA_ARGS__); \ 138 } while (0) 139 140 141void 142lsquic_logger_log2 (enum lsq_log_level, enum lsquic_logger_module, 143 const struct lsquic_cid *conn_id, const char *format, ...) 144#if __GNUC__ 145 __attribute__((format(printf, 4, 5))) 146#endif 147; 148# define LSQ_LOG2(level, ...) do { \ 149 if (LSQ_LOG_ENABLED(level)) \ 150 lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE, \ 151 LSQUIC_LOG_CONN_ID, __VA_ARGS__); \ 152 } while (0) 153# define LSQ_LOG2C(level, ...) do { \ 154 if (LSQ_LOG_ENABLED(level)) \ 155 { \ 156 char cidbuf_[MAX_CID_LEN * 2 + 1]; \ 157 lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE, \ 158 LSQUIC_LOG_CONN_ID, __VA_ARGS__); \ 159 } \ 160 } while (0) 161 162void 163lsquic_logger_log1 (enum lsq_log_level, enum lsquic_logger_module, 164 const char *format, ...) 165#if __GNUC__ 166 __attribute__((format(printf, 3, 4))) 167#endif 168; 169# define LSQ_LOG1(level, ...) do { \ 170 if (LSQ_LOG_ENABLED(level)) \ 171 lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__); \ 172 } while (0) 173# define LSQ_LOG1C(level, ...) do { \ 174 if (LSQ_LOG_ENABLED(level)) \ 175 { \ 176 char cidbuf_[MAX_CID_LEN * 2 + 1]; \ 177 lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__); \ 178 } \ 179 } while (0) 180 181void 182lsquic_logger_log0 (enum lsq_log_level, const char *format, ...) 183#if __GNUC__ 184 __attribute__((format(printf, 2, 3))) 185#endif 186; 187# define LSQ_LOG0(level, ...) do { \ 188 if (LSQ_LOG_ENABLED(level)) \ 189 lsquic_logger_log0(level, __VA_ARGS__); \ 190 } while (0) 191# define LSQ_LOG0C(level, ...) do { \ 192 if (LSQ_LOG_ENABLED(level)) \ 193 { \ 194 char cidbuf_[MAX_CID_LEN * 2 + 1]; \ 195 lsquic_logger_log0(level, __VA_ARGS__); \ 196 } \ 197 } while (0) 198 199#if defined(LSQUIC_LOGGER_MODULE) 200#if defined(LSQUIC_LOG_CONN_ID) 201#if defined(LSQUIC_LOG_STREAM_ID) 202# define LSQ_LOG LSQ_LOG3 203#else 204# define LSQ_LOG LSQ_LOG2 205# define LSQ_LOGC LSQ_LOG2C 206#endif 207#else 208# define LSQ_LOG LSQ_LOG1 209# define LSQ_LOGC LSQ_LOG1C 210#endif 211#else 212# define LSQ_LOG LSQ_LOG0 213# define LSQ_LOGC LSQ_LOG0C 214# define LSQUIC_LOGGER_MODULE LSQLM_NOMODULE 215#endif 216 217#define LSQ_DEBUG(...) LSQ_LOG(LSQ_LOG_DEBUG, __VA_ARGS__) 218#define LSQ_WARN(...) LSQ_LOG(LSQ_LOG_WARN, __VA_ARGS__) 219#define LSQ_ALERT(...) LSQ_LOG(LSQ_LOG_ALERT, __VA_ARGS__) 220#define LSQ_CRIT(...) LSQ_LOG(LSQ_LOG_CRIT, __VA_ARGS__) 221#define LSQ_ERROR(...) LSQ_LOG(LSQ_LOG_ERROR, __VA_ARGS__) 222#define LSQ_NOTICE(...) LSQ_LOG(LSQ_LOG_NOTICE, __VA_ARGS__) 223#define LSQ_INFO(...) LSQ_LOG(LSQ_LOG_INFO, __VA_ARGS__) 224#define LSQ_EMERG(...) LSQ_LOG(LSQ_LOG_EMERG, __VA_ARGS__) 225 226#define LSQ_DEBUGC(...) LSQ_LOGC(LSQ_LOG_DEBUG, __VA_ARGS__) 227#define LSQ_WARNC(...) LSQ_LOGC(LSQ_LOG_WARN, __VA_ARGS__) 228#define LSQ_ALERTC(...) LSQ_LOGC(LSQ_LOG_ALERT, __VA_ARGS__) 229#define LSQ_CRITC(...) LSQ_LOGC(LSQ_LOG_CRIT, __VA_ARGS__) 230#define LSQ_ERRORC(...) LSQ_LOGC(LSQ_LOG_ERROR, __VA_ARGS__) 231#define LSQ_NOTICEC(...) LSQ_LOGC(LSQ_LOG_NOTICE, __VA_ARGS__) 232#define LSQ_INFOC(...) LSQ_LOGC(LSQ_LOG_INFO, __VA_ARGS__) 233#define LSQ_EMERGC(...) LSQ_LOGC(LSQ_LOG_EMERG, __VA_ARGS__) 234 235/* Shorthand for printing to file streams using internal lsquic_logger_if 236 */ 237void 238lsquic_log_to_fstream (FILE *, unsigned llts); 239 240enum lsquic_logger_module 241lsquic_str_to_logger_module (const char *); 242 243enum lsq_log_level 244lsquic_str_to_log_level (const char *); 245 246/* Parse and set log levels passed via -l flag. If an error is encountered, 247 * an error message is printed to stderr and negative value is returned. 248 */ 249int 250lsquic_logger_lopt (const char *optarg); 251 252#define CID_FMT ".*s" 253 254#define CID_BITS_B(cid, b) 2 * (int) (cid)->len, \ 255 (lsquic_cid2str(cid, b), b) 256 257#define CID_BITS(cid) CID_BITS_B(cid, cidbuf_) 258 259void 260lsquic_cid2str (const struct lsquic_cid *, char *out); 261 262const struct lsquic_cid * 263lsquic_conn_log_cid (const struct lsquic_conn *); 264 265#ifdef __cplusplus 266} 267#endif 268 269#endif 270