lsquic_logger.h revision 55cd0b38
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_CUBIC, 65 LSQLM_HEADERS, 66 LSQLM_FRAME_WRITER, 67 LSQLM_FRAME_READER, 68 LSQLM_CONN_HASH, 69 LSQLM_ENG_HIST, 70 LSQLM_SPI, 71 LSQLM_DI, 72 LSQLM_PACER, 73 LSQLM_MIN_HEAP, 74 LSQLM_HTTP1X, 75 LSQLM_QLOG, 76 N_LSQUIC_LOGGER_MODULES 77}; 78 79/* Each module has its own log level. 80 */ 81extern enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES]; 82 83extern const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES]; 84 85extern const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS]; 86 87#define LSQ_LOG_ENABLED_EXT(level, module) ( \ 88 level <= LSQUIC_LOWEST_LOG_LEVEL && level <= lsq_log_levels[module]) 89 90#define LSQ_LOG_ENABLED(level) LSQ_LOG_ENABLED_EXT(level, LSQUIC_LOGGER_MODULE) 91 92/* The functions that perform actual logging are void. This is an 93 * optimization. In majority of cases the calls will succeed; even if 94 * they fail, there is nothing (at least, nothing simple) to be done to 95 * handle logging failure. 96 */ 97 98/* There are four levels of log functions, depending on whether they take 99 * the following arguments: 100 * 1. Logger module 101 * 2. Connection ID 102 * 3. Stream ID 103 * 104 * Each level of logging function supports one additional argument, as seen 105 * below. LSQ_LOG is set to one of LSQ_LOG0, LSQ_LOG1, LSQ_LOG2, or LSQ_LOG3. 106 * You can still use LSQ_LOG{0..3} directly. 107 */ 108 109void 110lsquic_logger_log3 (enum lsq_log_level, enum lsquic_logger_module, 111 uint64_t conn_id, uint32_t stream_id, 112 const char *format, ...) 113#if __GNUC__ 114 __attribute__((format(printf, 5, 6))) 115#endif 116; 117# define LSQ_LOG3(level, ...) do { \ 118 if (LSQ_LOG_ENABLED(level)) \ 119 lsquic_logger_log3(level, LSQUIC_LOGGER_MODULE, \ 120 LSQUIC_LOG_CONN_ID, LSQUIC_LOG_STREAM_ID, __VA_ARGS__); \ 121 } while (0) 122 123void 124lsquic_logger_log2 (enum lsq_log_level, enum lsquic_logger_module, 125 uint64_t conn_id, const char *format, ...) 126#if __GNUC__ 127 __attribute__((format(printf, 4, 5))) 128#endif 129; 130# define LSQ_LOG2(level, ...) do { \ 131 if (LSQ_LOG_ENABLED(level)) \ 132 lsquic_logger_log2(level, LSQUIC_LOGGER_MODULE, \ 133 LSQUIC_LOG_CONN_ID, __VA_ARGS__); \ 134 } while (0) 135 136void 137lsquic_logger_log1 (enum lsq_log_level, enum lsquic_logger_module, 138 const char *format, ...) 139#if __GNUC__ 140 __attribute__((format(printf, 3, 4))) 141#endif 142; 143# define LSQ_LOG1(level, ...) do { \ 144 if (LSQ_LOG_ENABLED(level)) \ 145 lsquic_logger_log1(level, LSQUIC_LOGGER_MODULE, __VA_ARGS__); \ 146 } while (0) 147 148void 149lsquic_logger_log0 (enum lsq_log_level, const char *format, ...) 150#if __GNUC__ 151 __attribute__((format(printf, 2, 3))) 152#endif 153; 154# define LSQ_LOG0(level, ...) do { \ 155 if (LSQ_LOG_ENABLED(level)) \ 156 lsquic_logger_log0(level, __VA_ARGS__); \ 157 } while (0) 158 159#if defined(LSQUIC_LOGGER_MODULE) 160#if defined(LSQUIC_LOG_CONN_ID) 161#if defined(LSQUIC_LOG_STREAM_ID) 162# define LSQ_LOG LSQ_LOG3 163#else 164# define LSQ_LOG LSQ_LOG2 165#endif 166#else 167# define LSQ_LOG LSQ_LOG1 168#endif 169#else 170# define LSQ_LOG LSQ_LOG0 171# define LSQUIC_LOGGER_MODULE LSQLM_NOMODULE 172#endif 173 174#define LSQ_DEBUG(...) LSQ_LOG(LSQ_LOG_DEBUG, __VA_ARGS__) 175#define LSQ_WARN(...) LSQ_LOG(LSQ_LOG_WARN, __VA_ARGS__) 176#define LSQ_ALERT(...) LSQ_LOG(LSQ_LOG_ALERT, __VA_ARGS__) 177#define LSQ_CRIT(...) LSQ_LOG(LSQ_LOG_CRIT, __VA_ARGS__) 178#define LSQ_ERROR(...) LSQ_LOG(LSQ_LOG_ERROR, __VA_ARGS__) 179#define LSQ_NOTICE(...) LSQ_LOG(LSQ_LOG_NOTICE, __VA_ARGS__) 180#define LSQ_INFO(...) LSQ_LOG(LSQ_LOG_INFO, __VA_ARGS__) 181#define LSQ_EMERG(...) LSQ_LOG(LSQ_LOG_EMERG, __VA_ARGS__) 182 183/* Shorthand for printing to file streams using internal lsquic_logger_if 184 */ 185void 186lsquic_log_to_fstream (FILE *, unsigned llts); 187 188enum lsquic_logger_module 189lsquic_str_to_logger_module (const char *); 190 191enum lsq_log_level 192lsquic_str_to_log_level (const char *); 193 194/* Parse and set log levels passed via -l flag. If an error is encountered, 195 * an error message is printed to stderr and negative value is returned. 196 */ 197int 198lsquic_logger_lopt (const char *optarg); 199 200#ifdef __cplusplus 201} 202#endif 203 204#endif 205