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