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