lsquic_logger.c revision b93f59be
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov/* 350aadb33SDmitri Tikhonov * LSQUIC Logger implementation. 450aadb33SDmitri Tikhonov */ 550aadb33SDmitri Tikhonov 650aadb33SDmitri Tikhonov#include <errno.h> 750aadb33SDmitri Tikhonov#include <inttypes.h> 850aadb33SDmitri Tikhonov#include <stdarg.h> 950aadb33SDmitri Tikhonov#include <stdio.h> 1050aadb33SDmitri Tikhonov#include <stdlib.h> 1150aadb33SDmitri Tikhonov#include <string.h> 12461e84d8SAmol Deshpande#ifndef WIN32 1350aadb33SDmitri Tikhonov#include <sys/time.h> 14461e84d8SAmol Deshpande#endif 1550aadb33SDmitri Tikhonov#include <time.h> 1650aadb33SDmitri Tikhonov 1750aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_LOGGER /* Quis custodiet ipsos custodes? */ 1850aadb33SDmitri Tikhonov#include "lsquic_logger.h" 1950aadb33SDmitri Tikhonov#include "lsquic.h" 2050aadb33SDmitri Tikhonov 2150aadb33SDmitri Tikhonovstatic enum lsquic_logger_timestamp_style g_llts = LLTS_NONE; 2250aadb33SDmitri Tikhonov 2350aadb33SDmitri Tikhonovstatic int 2450aadb33SDmitri Tikhonovnull_vprintf (void *ctx, const char *fmt, va_list ap) 2550aadb33SDmitri Tikhonov{ 2650aadb33SDmitri Tikhonov return 0; 2750aadb33SDmitri Tikhonov} 2850aadb33SDmitri Tikhonov 2950aadb33SDmitri Tikhonov 3050aadb33SDmitri Tikhonovstatic int 3150aadb33SDmitri Tikhonovfile_vprintf (void *ctx, const char *fmt, va_list ap) 3250aadb33SDmitri Tikhonov{ 3350aadb33SDmitri Tikhonov return vfprintf((FILE *) ctx, fmt, ap); 3450aadb33SDmitri Tikhonov} 3550aadb33SDmitri Tikhonov 3650aadb33SDmitri Tikhonov 3750aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if file_logger_if = { 3850aadb33SDmitri Tikhonov .vprintf = file_vprintf, 3950aadb33SDmitri Tikhonov}; 4050aadb33SDmitri Tikhonov 4150aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if null_logger_if = { 4250aadb33SDmitri Tikhonov .vprintf = null_vprintf, 4350aadb33SDmitri Tikhonov}; 4450aadb33SDmitri Tikhonov 4550aadb33SDmitri Tikhonovstatic void *logger_ctx = NULL; 4650aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if *logger_if = &null_logger_if; 4750aadb33SDmitri Tikhonov 4850aadb33SDmitri Tikhonovenum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES] = { 4950aadb33SDmitri Tikhonov [LSQLM_NOMODULE] = LSQ_LOG_WARN, 5050aadb33SDmitri Tikhonov [LSQLM_LOGGER] = LSQ_LOG_WARN, 5150aadb33SDmitri Tikhonov [LSQLM_EVENT] = LSQ_LOG_WARN, 5250aadb33SDmitri Tikhonov [LSQLM_ENGINE] = LSQ_LOG_WARN, 5350aadb33SDmitri Tikhonov [LSQLM_CONN] = LSQ_LOG_WARN, 5450aadb33SDmitri Tikhonov [LSQLM_RECHIST] = LSQ_LOG_WARN, 5550aadb33SDmitri Tikhonov [LSQLM_STREAM] = LSQ_LOG_WARN, 5650aadb33SDmitri Tikhonov [LSQLM_PARSE] = LSQ_LOG_WARN, 5750aadb33SDmitri Tikhonov [LSQLM_CFCW] = LSQ_LOG_WARN, 5850aadb33SDmitri Tikhonov [LSQLM_SFCW] = LSQ_LOG_WARN, 5950aadb33SDmitri Tikhonov [LSQLM_SENDCTL] = LSQ_LOG_WARN, 6050aadb33SDmitri Tikhonov [LSQLM_ALARMSET] = LSQ_LOG_WARN, 6150aadb33SDmitri Tikhonov [LSQLM_CRYPTO] = LSQ_LOG_WARN, 6250aadb33SDmitri Tikhonov [LSQLM_HANDSHAKE] = LSQ_LOG_WARN, 6350aadb33SDmitri Tikhonov [LSQLM_HSK_ADAPTER] = LSQ_LOG_WARN, 6450aadb33SDmitri Tikhonov [LSQLM_CUBIC] = LSQ_LOG_WARN, 6550aadb33SDmitri Tikhonov [LSQLM_HEADERS] = LSQ_LOG_WARN, 6650aadb33SDmitri Tikhonov [LSQLM_FRAME_READER]= LSQ_LOG_WARN, 6750aadb33SDmitri Tikhonov [LSQLM_FRAME_WRITER]= LSQ_LOG_WARN, 6850aadb33SDmitri Tikhonov [LSQLM_CONN_HASH] = LSQ_LOG_WARN, 6950aadb33SDmitri Tikhonov [LSQLM_ENG_HIST] = LSQ_LOG_WARN, 7050aadb33SDmitri Tikhonov [LSQLM_SPI] = LSQ_LOG_WARN, 7150aadb33SDmitri Tikhonov [LSQLM_DI] = LSQ_LOG_WARN, 7250aadb33SDmitri Tikhonov [LSQLM_PACER] = LSQ_LOG_WARN, 7350aadb33SDmitri Tikhonov}; 7450aadb33SDmitri Tikhonov 7550aadb33SDmitri Tikhonovconst char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES] = { 7650aadb33SDmitri Tikhonov [LSQLM_NOMODULE] = "", 7750aadb33SDmitri Tikhonov [LSQLM_LOGGER] = "logger", 7850aadb33SDmitri Tikhonov [LSQLM_EVENT] = "event", 7950aadb33SDmitri Tikhonov [LSQLM_ENGINE] = "engine", 8050aadb33SDmitri Tikhonov [LSQLM_CONN] = "conn", 8150aadb33SDmitri Tikhonov [LSQLM_RECHIST] = "rechist", 8250aadb33SDmitri Tikhonov [LSQLM_STREAM] = "stream", 8350aadb33SDmitri Tikhonov [LSQLM_PARSE] = "parse", 8450aadb33SDmitri Tikhonov [LSQLM_CFCW] = "cfcw", 8550aadb33SDmitri Tikhonov [LSQLM_SFCW] = "sfcw", 8650aadb33SDmitri Tikhonov [LSQLM_SENDCTL] = "sendctl", 8750aadb33SDmitri Tikhonov [LSQLM_ALARMSET] = "alarmset", 8850aadb33SDmitri Tikhonov [LSQLM_CRYPTO] = "crypto", 8950aadb33SDmitri Tikhonov [LSQLM_HANDSHAKE] = "handshake", 9050aadb33SDmitri Tikhonov [LSQLM_HSK_ADAPTER] = "hsk-adapter", 9150aadb33SDmitri Tikhonov [LSQLM_CUBIC] = "cubic", 9250aadb33SDmitri Tikhonov [LSQLM_HEADERS] = "headers", 9350aadb33SDmitri Tikhonov [LSQLM_FRAME_READER]= "frame-reader", 9450aadb33SDmitri Tikhonov [LSQLM_FRAME_WRITER]= "frame-writer", 9550aadb33SDmitri Tikhonov [LSQLM_CONN_HASH] = "conn-hash", 9650aadb33SDmitri Tikhonov [LSQLM_ENG_HIST] = "eng-hist", 9750aadb33SDmitri Tikhonov [LSQLM_SPI] = "spi", 9850aadb33SDmitri Tikhonov [LSQLM_DI] = "di", 9950aadb33SDmitri Tikhonov [LSQLM_PACER] = "pacer", 10050aadb33SDmitri Tikhonov}; 10150aadb33SDmitri Tikhonov 10250aadb33SDmitri Tikhonovconst char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS] = { 10350aadb33SDmitri Tikhonov [LSQ_LOG_ALERT] = "ALERT", 10450aadb33SDmitri Tikhonov [LSQ_LOG_CRIT] = "CRIT", 10550aadb33SDmitri Tikhonov [LSQ_LOG_DEBUG] = "DEBUG", 10650aadb33SDmitri Tikhonov [LSQ_LOG_EMERG] = "EMERG", 10750aadb33SDmitri Tikhonov [LSQ_LOG_ERROR] = "ERROR", 10850aadb33SDmitri Tikhonov [LSQ_LOG_INFO] = "INFO", 10950aadb33SDmitri Tikhonov [LSQ_LOG_NOTICE] = "NOTICE", 11050aadb33SDmitri Tikhonov [LSQ_LOG_WARN] = "WARN", 11150aadb33SDmitri Tikhonov}; 11250aadb33SDmitri Tikhonov 11350aadb33SDmitri Tikhonov 11450aadb33SDmitri Tikhonovstatic void 11550aadb33SDmitri Tikhonovlsquic_printf (const char *fmt, ...) 11650aadb33SDmitri Tikhonov{ 11750aadb33SDmitri Tikhonov va_list ap; 11850aadb33SDmitri Tikhonov va_start(ap, fmt); 11950aadb33SDmitri Tikhonov logger_if->vprintf(logger_ctx, fmt, ap); 12050aadb33SDmitri Tikhonov va_end(ap); 12150aadb33SDmitri Tikhonov} 12250aadb33SDmitri Tikhonov 12350aadb33SDmitri Tikhonov 124b93f59beSBob Perper#ifdef WIN32 125b93f59beSBob Perperstruct timezone 126b93f59beSBob Perper{ 127b93f59beSBob Perper int tz_minuteswest; /* minutes W of Greenwich */ 128b93f59beSBob Perper int tz_dsttime; /* type of dst correction */ 129b93f59beSBob Perper}; 130b93f59beSBob Perper 131b93f59beSBob Perperstatic int 132b93f59beSBob Perpergettimeofday (struct timeval *tv, struct timezone *tz) 133b93f59beSBob Perper{ 134b93f59beSBob Perper FILETIME ft; 135b93f59beSBob Perper unsigned __int64 tmpres = 0; 136b93f59beSBob Perper static int tzflag; 137b93f59beSBob Perper 138b93f59beSBob Perper if (NULL != tv) 139b93f59beSBob Perper { 140b93f59beSBob Perper GetSystemTimeAsFileTime(&ft); 141b93f59beSBob Perper 142b93f59beSBob Perper tmpres |= ft.dwHighDateTime; 143b93f59beSBob Perper tmpres <<= 32; 144b93f59beSBob Perper tmpres |= ft.dwLowDateTime; 145b93f59beSBob Perper 146b93f59beSBob Perper /*converting file time to unix epoch */ 147b93f59beSBob Perper tmpres -= DELTA_EPOCH_IN_MICROSECS; 148b93f59beSBob Perper tmpres /= 10; /*convert into microseconds */ 149b93f59beSBob Perper tv->tv_sec = (long) (tmpres / 1000000UL); 150b93f59beSBob Perper tv->tv_usec = (long) (tmpres % 1000000UL); 151b93f59beSBob Perper } 152b93f59beSBob Perper 153b93f59beSBob Perper if (NULL != tz) 154b93f59beSBob Perper { 155b93f59beSBob Perper if (!tzflag) 156b93f59beSBob Perper { 157b93f59beSBob Perper _tzset(); 158b93f59beSBob Perper tzflag++; 159b93f59beSBob Perper } 160b93f59beSBob Perper tz->tz_minuteswest = _timezone / 60; 161b93f59beSBob Perper tz->tz_dsttime = _daylight; 162b93f59beSBob Perper } 163b93f59beSBob Perper 164b93f59beSBob Perper return 0; 165b93f59beSBob Perper} 166b93f59beSBob Perper 167b93f59beSBob Perper 168b93f59beSBob Perper#endif 169b93f59beSBob Perper 170b93f59beSBob Perper 17150aadb33SDmitri Tikhonovstatic void 17250aadb33SDmitri Tikhonovprint_timestamp (void) 17350aadb33SDmitri Tikhonov{ 17450aadb33SDmitri Tikhonov struct tm tm; 17550aadb33SDmitri Tikhonov struct timeval tv; 17650aadb33SDmitri Tikhonov gettimeofday(&tv, NULL); 17750aadb33SDmitri Tikhonov localtime_r(&tv.tv_sec, &tm); 178bfc7bfd8SDmitri Tikhonov if (g_llts == LLTS_YYYYMMDD_HHMMSSUS) 179bfc7bfd8SDmitri Tikhonov lsquic_printf("%04d-%02d-%02d %02d:%02d:%02d.%06d ", 180bfc7bfd8SDmitri Tikhonov tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 181bfc7bfd8SDmitri Tikhonov tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec)); 182bfc7bfd8SDmitri Tikhonov else if (g_llts == LLTS_YYYYMMDD_HHMMSSMS) 18350aadb33SDmitri Tikhonov lsquic_printf("%04d-%02d-%02d %02d:%02d:%02d.%03d ", 18450aadb33SDmitri Tikhonov tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 18550aadb33SDmitri Tikhonov tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000)); 18650aadb33SDmitri Tikhonov else if (g_llts == LLTS_HHMMSSMS) 18750aadb33SDmitri Tikhonov lsquic_printf("%02d:%02d:%02d.%03d ", tm.tm_hour, tm.tm_min, 18850aadb33SDmitri Tikhonov tm.tm_sec, (int) (tv.tv_usec / 1000)); 18950aadb33SDmitri Tikhonov else if (g_llts == LLTS_HHMMSSUS) 19050aadb33SDmitri Tikhonov lsquic_printf("%02d:%02d:%02d.%06d ", tm.tm_hour, tm.tm_min, 19150aadb33SDmitri Tikhonov tm.tm_sec, (int) tv.tv_usec); 19250aadb33SDmitri Tikhonov else if (g_llts == LLTS_CHROMELIKE) 19350aadb33SDmitri Tikhonov lsquic_printf("%02d%02d/%02d%02d%02d.%06d ", tm.tm_mon + 1, 19450aadb33SDmitri Tikhonov tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec); 19550aadb33SDmitri Tikhonov} 19650aadb33SDmitri Tikhonov 19750aadb33SDmitri Tikhonov 19850aadb33SDmitri Tikhonovvoid 19950aadb33SDmitri Tikhonovlsquic_logger_log3 (enum lsq_log_level log_level, 20050aadb33SDmitri Tikhonov enum lsquic_logger_module module, 20150aadb33SDmitri Tikhonov uint64_t conn_id, uint32_t stream_id, const char *fmt, ...) 20250aadb33SDmitri Tikhonov{ 20350aadb33SDmitri Tikhonov const int saved_errno = errno; 20450aadb33SDmitri Tikhonov 20550aadb33SDmitri Tikhonov if (g_llts != LLTS_NONE) 20650aadb33SDmitri Tikhonov print_timestamp(); 20750aadb33SDmitri Tikhonov 20850aadb33SDmitri Tikhonov lsquic_printf("[%s] [QUIC:%"PRIu64"-%"PRIu32"] %s: ", 20950aadb33SDmitri Tikhonov lsq_loglevel2str[log_level], conn_id, stream_id, lsqlm_to_str[module]); 21050aadb33SDmitri Tikhonov va_list ap; 21150aadb33SDmitri Tikhonov va_start(ap, fmt); 21250aadb33SDmitri Tikhonov logger_if->vprintf(logger_ctx, fmt, ap); 21350aadb33SDmitri Tikhonov va_end(ap); 21450aadb33SDmitri Tikhonov lsquic_printf("\n"); 21550aadb33SDmitri Tikhonov errno = saved_errno; 21650aadb33SDmitri Tikhonov} 21750aadb33SDmitri Tikhonov 21850aadb33SDmitri Tikhonov 21950aadb33SDmitri Tikhonovvoid 22050aadb33SDmitri Tikhonovlsquic_logger_log2 (enum lsq_log_level log_level, 22150aadb33SDmitri Tikhonov enum lsquic_logger_module module, 22250aadb33SDmitri Tikhonov uint64_t conn_id, const char *fmt, ...) 22350aadb33SDmitri Tikhonov{ 22450aadb33SDmitri Tikhonov const int saved_errno = errno; 22550aadb33SDmitri Tikhonov 22650aadb33SDmitri Tikhonov if (g_llts != LLTS_NONE) 22750aadb33SDmitri Tikhonov print_timestamp(); 22850aadb33SDmitri Tikhonov 22950aadb33SDmitri Tikhonov lsquic_printf("[%s] [QUIC:%"PRIu64"] %s: ", 23050aadb33SDmitri Tikhonov lsq_loglevel2str[log_level], conn_id, lsqlm_to_str[module]); 23150aadb33SDmitri Tikhonov va_list ap; 23250aadb33SDmitri Tikhonov va_start(ap, fmt); 23350aadb33SDmitri Tikhonov logger_if->vprintf(logger_ctx, fmt, ap); 23450aadb33SDmitri Tikhonov va_end(ap); 23550aadb33SDmitri Tikhonov lsquic_printf("\n"); 23650aadb33SDmitri Tikhonov errno = saved_errno; 23750aadb33SDmitri Tikhonov} 23850aadb33SDmitri Tikhonov 23950aadb33SDmitri Tikhonov 24050aadb33SDmitri Tikhonovvoid 24150aadb33SDmitri Tikhonovlsquic_logger_log1 (enum lsq_log_level log_level, 24250aadb33SDmitri Tikhonov enum lsquic_logger_module module, 24350aadb33SDmitri Tikhonov const char *fmt, ...) 24450aadb33SDmitri Tikhonov{ 24550aadb33SDmitri Tikhonov const int saved_errno = errno; 24650aadb33SDmitri Tikhonov 24750aadb33SDmitri Tikhonov if (g_llts != LLTS_NONE) 24850aadb33SDmitri Tikhonov print_timestamp(); 24950aadb33SDmitri Tikhonov 25050aadb33SDmitri Tikhonov lsquic_printf("[%s] %s: ", lsq_loglevel2str[log_level], 25150aadb33SDmitri Tikhonov lsqlm_to_str[module]); 25250aadb33SDmitri Tikhonov va_list ap; 25350aadb33SDmitri Tikhonov va_start(ap, fmt); 25450aadb33SDmitri Tikhonov logger_if->vprintf(logger_ctx, fmt, ap); 25550aadb33SDmitri Tikhonov va_end(ap); 25650aadb33SDmitri Tikhonov lsquic_printf("\n"); 25750aadb33SDmitri Tikhonov errno = saved_errno; 25850aadb33SDmitri Tikhonov} 25950aadb33SDmitri Tikhonov 26050aadb33SDmitri Tikhonov 26150aadb33SDmitri Tikhonovvoid 26250aadb33SDmitri Tikhonovlsquic_logger_log0 (enum lsq_log_level log_level, const char *fmt, ...) 26350aadb33SDmitri Tikhonov{ 26450aadb33SDmitri Tikhonov const int saved_errno = errno; 26550aadb33SDmitri Tikhonov 26650aadb33SDmitri Tikhonov if (g_llts != LLTS_NONE) 26750aadb33SDmitri Tikhonov print_timestamp(); 26850aadb33SDmitri Tikhonov 26950aadb33SDmitri Tikhonov lsquic_printf("[%s] ", lsq_loglevel2str[log_level]); 27050aadb33SDmitri Tikhonov va_list ap; 27150aadb33SDmitri Tikhonov va_start(ap, fmt); 27250aadb33SDmitri Tikhonov logger_if->vprintf(logger_ctx, fmt, ap); 27350aadb33SDmitri Tikhonov va_end(ap); 27450aadb33SDmitri Tikhonov lsquic_printf("\n"); 27550aadb33SDmitri Tikhonov errno = saved_errno; 27650aadb33SDmitri Tikhonov} 27750aadb33SDmitri Tikhonov 27850aadb33SDmitri Tikhonov 27950aadb33SDmitri Tikhonovvoid 28050aadb33SDmitri Tikhonovlsquic_logger_init (const struct lsquic_logger_if *lif, void *lctx, 28150aadb33SDmitri Tikhonov unsigned llts) 28250aadb33SDmitri Tikhonov{ 28350aadb33SDmitri Tikhonov logger_if = lif; 28450aadb33SDmitri Tikhonov logger_ctx = lctx; 28550aadb33SDmitri Tikhonov if (llts < N_LLTS) 28650aadb33SDmitri Tikhonov g_llts = llts; 28750aadb33SDmitri Tikhonov LSQ_DEBUG("%s called", __func__); 28850aadb33SDmitri Tikhonov} 28950aadb33SDmitri Tikhonov 29050aadb33SDmitri Tikhonov 29150aadb33SDmitri Tikhonovenum lsquic_logger_module 29250aadb33SDmitri Tikhonovlsquic_str_to_logger_module (const char *str) 29350aadb33SDmitri Tikhonov{ 29450aadb33SDmitri Tikhonov enum lsquic_logger_module i; 29550aadb33SDmitri Tikhonov for (i = 0; i < sizeof(lsqlm_to_str) / sizeof(lsqlm_to_str[0]); ++i) 29650aadb33SDmitri Tikhonov if (0 == strcasecmp(lsqlm_to_str[i], str)) 29750aadb33SDmitri Tikhonov return i; 29850aadb33SDmitri Tikhonov return -1; 29950aadb33SDmitri Tikhonov} 30050aadb33SDmitri Tikhonov 30150aadb33SDmitri Tikhonov 30250aadb33SDmitri Tikhonovenum lsq_log_level 30350aadb33SDmitri Tikhonovlsquic_str_to_log_level (const char *str) 30450aadb33SDmitri Tikhonov{ 30550aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "emerg")) 30650aadb33SDmitri Tikhonov return LSQ_LOG_EMERG; 30750aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "alert")) 30850aadb33SDmitri Tikhonov return LSQ_LOG_ALERT; 30950aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "crit")) 31050aadb33SDmitri Tikhonov return LSQ_LOG_CRIT; 31150aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "error")) 31250aadb33SDmitri Tikhonov return LSQ_LOG_ERROR; 31350aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "warn")) 31450aadb33SDmitri Tikhonov return LSQ_LOG_WARN; 31550aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "notice")) 31650aadb33SDmitri Tikhonov return LSQ_LOG_NOTICE; 31750aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "info")) 31850aadb33SDmitri Tikhonov return LSQ_LOG_INFO; 31950aadb33SDmitri Tikhonov if (0 == strcasecmp(str, "debug")) 32050aadb33SDmitri Tikhonov return LSQ_LOG_DEBUG; 32150aadb33SDmitri Tikhonov return -1; 32250aadb33SDmitri Tikhonov} 32350aadb33SDmitri Tikhonov 32450aadb33SDmitri Tikhonov 32550aadb33SDmitri Tikhonovvoid 32650aadb33SDmitri Tikhonovlsquic_log_to_fstream (FILE *file, unsigned llts) 32750aadb33SDmitri Tikhonov{ 32850aadb33SDmitri Tikhonov lsquic_logger_init(&file_logger_if, file, llts); 32950aadb33SDmitri Tikhonov} 33050aadb33SDmitri Tikhonov 33150aadb33SDmitri Tikhonov 33250aadb33SDmitri Tikhonovint 33350aadb33SDmitri Tikhonovlsquic_logger_lopt (const char *optarg_orig) 33450aadb33SDmitri Tikhonov{ 33550aadb33SDmitri Tikhonov char *const optarg = strdup(optarg_orig); 33650aadb33SDmitri Tikhonov char *mod_str; 33750aadb33SDmitri Tikhonov int i; 33850aadb33SDmitri Tikhonov for (i = 0; (mod_str = strtok(i ? NULL : optarg, ",")); ++i) { 33950aadb33SDmitri Tikhonov char *level_str = strchr(mod_str, '='); 34050aadb33SDmitri Tikhonov if (!level_str) { 34150aadb33SDmitri Tikhonov fprintf(stderr, "Invalid module specification `%s'\n", mod_str); 34250aadb33SDmitri Tikhonov break; 34350aadb33SDmitri Tikhonov } 34450aadb33SDmitri Tikhonov *level_str = '\0'; 34550aadb33SDmitri Tikhonov ++level_str; 34650aadb33SDmitri Tikhonov enum lsquic_logger_module mod = lsquic_str_to_logger_module(mod_str); 34750aadb33SDmitri Tikhonov if (-1 == (int) mod) { 34850aadb33SDmitri Tikhonov fprintf(stderr, "`%s' is not a valid module name\n", mod_str); 34950aadb33SDmitri Tikhonov break; 35050aadb33SDmitri Tikhonov } 35150aadb33SDmitri Tikhonov enum lsq_log_level level = lsquic_str_to_log_level(level_str); 35250aadb33SDmitri Tikhonov if (-1 == (int) level) { 35350aadb33SDmitri Tikhonov fprintf(stderr, "`%s' is not a valid level\n", level_str); 35450aadb33SDmitri Tikhonov break; 35550aadb33SDmitri Tikhonov } 35650aadb33SDmitri Tikhonov lsq_log_levels[mod] = level; 35750aadb33SDmitri Tikhonov LSQ_INFO("set %s to %s", mod_str, level_str); 35850aadb33SDmitri Tikhonov } 35950aadb33SDmitri Tikhonov free(optarg); 36050aadb33SDmitri Tikhonov return mod_str == NULL ? 0 : -1; 36150aadb33SDmitri Tikhonov} 36250aadb33SDmitri Tikhonov 36350aadb33SDmitri Tikhonov 36450aadb33SDmitri Tikhonovint 36550aadb33SDmitri Tikhonovlsquic_set_log_level (const char *level_str) 36650aadb33SDmitri Tikhonov{ 36750aadb33SDmitri Tikhonov enum lsq_log_level level; 36850aadb33SDmitri Tikhonov unsigned i; 36950aadb33SDmitri Tikhonov 37050aadb33SDmitri Tikhonov level = lsquic_str_to_log_level(level_str); 37150aadb33SDmitri Tikhonov if ((int) level >= 0) 37250aadb33SDmitri Tikhonov { 37350aadb33SDmitri Tikhonov for (i = 0; i < sizeof(lsq_log_levels) / sizeof(lsq_log_levels[0]); ++i) 37450aadb33SDmitri Tikhonov lsq_log_levels[i] = level; 37550aadb33SDmitri Tikhonov return 0; 37650aadb33SDmitri Tikhonov } 37750aadb33SDmitri Tikhonov else 37850aadb33SDmitri Tikhonov return -1; 37950aadb33SDmitri Tikhonov} 38050aadb33SDmitri Tikhonov 38150aadb33SDmitri Tikhonov 382