lsquic_logger.c revision a0e1aeee
1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/*
350aadb33SDmitri Tikhonov * LSQUIC Logger implementation.
450aadb33SDmitri Tikhonov */
550aadb33SDmitri Tikhonov
6560db45bSDmitri Tikhonov#include <assert.h>
750aadb33SDmitri Tikhonov#include <errno.h>
850aadb33SDmitri Tikhonov#include <inttypes.h>
950aadb33SDmitri Tikhonov#include <stdarg.h>
1050aadb33SDmitri Tikhonov#include <stdio.h>
1150aadb33SDmitri Tikhonov#include <stdlib.h>
1250aadb33SDmitri Tikhonov#include <string.h>
13461e84d8SAmol Deshpande#ifndef WIN32
1450aadb33SDmitri Tikhonov#include <sys/time.h>
15461e84d8SAmol Deshpande#endif
1650aadb33SDmitri Tikhonov#include <time.h>
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonov#define LSQUIC_LOGGER_MODULE LSQLM_LOGGER /* Quis custodiet ipsos custodes? */
1950aadb33SDmitri Tikhonov#include "lsquic.h"
205392f7a3SLiteSpeed Tech#include "lsquic_logger.h"
2150aadb33SDmitri Tikhonov
225392f7a3SLiteSpeed Tech#define MAX_LINE_LEN 8192
235392f7a3SLiteSpeed Tech#define FORMAT_PROBLEM(lb, len, max) (((ssize_t)lb < 0) || ((ssize_t)lb + (ssize_t)len >= (ssize_t)max))
245392f7a3SLiteSpeed Tech
255392f7a3SLiteSpeed Tech/* TODO: display GQUIC CIDs in Chrome-compatible format */
263229dd11SDmitri Tikhonov
2750aadb33SDmitri Tikhonovstatic enum lsquic_logger_timestamp_style g_llts = LLTS_NONE;
2850aadb33SDmitri Tikhonov
2950aadb33SDmitri Tikhonovstatic int
305392f7a3SLiteSpeed Technull_log_buf (void *ctx, const char *buf, size_t len)
3150aadb33SDmitri Tikhonov{
3250aadb33SDmitri Tikhonov    return 0;
3350aadb33SDmitri Tikhonov}
3450aadb33SDmitri Tikhonov
3550aadb33SDmitri Tikhonovstatic int
365392f7a3SLiteSpeed Techfile_log_buf (void *ctx, const char *buf, size_t len)
3750aadb33SDmitri Tikhonov{
385392f7a3SLiteSpeed Tech    return (int)fwrite(buf, sizeof(char), len, (FILE *) ctx);
3950aadb33SDmitri Tikhonov}
4050aadb33SDmitri Tikhonov
4150aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if file_logger_if = {
425392f7a3SLiteSpeed Tech    .log_buf    = file_log_buf,
4350aadb33SDmitri Tikhonov};
4450aadb33SDmitri Tikhonov
4550aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if null_logger_if = {
465392f7a3SLiteSpeed Tech    .log_buf    = null_log_buf,
4750aadb33SDmitri Tikhonov};
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonovstatic void *logger_ctx = NULL;
5050aadb33SDmitri Tikhonovstatic const struct lsquic_logger_if *logger_if = &null_logger_if;
5150aadb33SDmitri Tikhonov
5250aadb33SDmitri Tikhonovenum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES] = {
5350aadb33SDmitri Tikhonov    [LSQLM_NOMODULE]    = LSQ_LOG_WARN,
5450aadb33SDmitri Tikhonov    [LSQLM_LOGGER]      = LSQ_LOG_WARN,
5550aadb33SDmitri Tikhonov    [LSQLM_EVENT]       = LSQ_LOG_WARN,
5650aadb33SDmitri Tikhonov    [LSQLM_ENGINE]      = LSQ_LOG_WARN,
5750aadb33SDmitri Tikhonov    [LSQLM_CONN]        = LSQ_LOG_WARN,
5850aadb33SDmitri Tikhonov    [LSQLM_RECHIST]     = LSQ_LOG_WARN,
5950aadb33SDmitri Tikhonov    [LSQLM_STREAM]      = LSQ_LOG_WARN,
6050aadb33SDmitri Tikhonov    [LSQLM_PARSE]       = LSQ_LOG_WARN,
6150aadb33SDmitri Tikhonov    [LSQLM_CFCW]        = LSQ_LOG_WARN,
6250aadb33SDmitri Tikhonov    [LSQLM_SFCW]        = LSQ_LOG_WARN,
6350aadb33SDmitri Tikhonov    [LSQLM_SENDCTL]     = LSQ_LOG_WARN,
6450aadb33SDmitri Tikhonov    [LSQLM_ALARMSET]    = LSQ_LOG_WARN,
6550aadb33SDmitri Tikhonov    [LSQLM_CRYPTO]      = LSQ_LOG_WARN,
6650aadb33SDmitri Tikhonov    [LSQLM_HANDSHAKE]   = LSQ_LOG_WARN,
6750aadb33SDmitri Tikhonov    [LSQLM_HSK_ADAPTER] = LSQ_LOG_WARN,
685392f7a3SLiteSpeed Tech    [LSQLM_BBR]         = LSQ_LOG_WARN,
6950aadb33SDmitri Tikhonov    [LSQLM_CUBIC]       = LSQ_LOG_WARN,
7050aadb33SDmitri Tikhonov    [LSQLM_HEADERS]     = LSQ_LOG_WARN,
7150aadb33SDmitri Tikhonov    [LSQLM_FRAME_READER]= LSQ_LOG_WARN,
7250aadb33SDmitri Tikhonov    [LSQLM_FRAME_WRITER]= LSQ_LOG_WARN,
735392f7a3SLiteSpeed Tech    [LSQLM_MINI_CONN]   = LSQ_LOG_WARN,
745392f7a3SLiteSpeed Tech    [LSQLM_TOKGEN]      = LSQ_LOG_WARN,
7550aadb33SDmitri Tikhonov    [LSQLM_ENG_HIST]    = LSQ_LOG_WARN,
7650aadb33SDmitri Tikhonov    [LSQLM_SPI]         = LSQ_LOG_WARN,
7750aadb33SDmitri Tikhonov    [LSQLM_DI]          = LSQ_LOG_WARN,
785392f7a3SLiteSpeed Tech    [LSQLM_PRQ]         = LSQ_LOG_WARN,
7950aadb33SDmitri Tikhonov    [LSQLM_PACER]       = LSQ_LOG_WARN,
80e8bd737dSDmitri Tikhonov    [LSQLM_MIN_HEAP]    = LSQ_LOG_WARN,
813b55e6aeSDmitri Tikhonov    [LSQLM_HTTP1X]      = LSQ_LOG_WARN,
8255cd0b38SDmitri Tikhonov    [LSQLM_QLOG]        = LSQ_LOG_WARN,
835392f7a3SLiteSpeed Tech    [LSQLM_TRAPA]       = LSQ_LOG_WARN,
845392f7a3SLiteSpeed Tech    [LSQLM_PURGA]       = LSQ_LOG_WARN,
855392f7a3SLiteSpeed Tech    [LSQLM_HCSI_READER] = LSQ_LOG_WARN,
865392f7a3SLiteSpeed Tech    [LSQLM_HCSO_WRITER] = LSQ_LOG_WARN,
875392f7a3SLiteSpeed Tech    [LSQLM_QENC_HDL]    = LSQ_LOG_WARN,
885392f7a3SLiteSpeed Tech    [LSQLM_QDEC_HDL]    = LSQ_LOG_WARN,
895392f7a3SLiteSpeed Tech    [LSQLM_QPACK_ENC]    = LSQ_LOG_WARN,
905392f7a3SLiteSpeed Tech    [LSQLM_QPACK_DEC]    = LSQ_LOG_WARN,
915392f7a3SLiteSpeed Tech    [LSQLM_PRIO]        = LSQ_LOG_WARN,
925392f7a3SLiteSpeed Tech    [LSQLM_BW_SAMPLER]  = LSQ_LOG_WARN,
9350aadb33SDmitri Tikhonov};
9450aadb33SDmitri Tikhonov
9550aadb33SDmitri Tikhonovconst char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES] = {
9650aadb33SDmitri Tikhonov    [LSQLM_NOMODULE]    = "",
9750aadb33SDmitri Tikhonov    [LSQLM_LOGGER]      = "logger",
9850aadb33SDmitri Tikhonov    [LSQLM_EVENT]       = "event",
9950aadb33SDmitri Tikhonov    [LSQLM_ENGINE]      = "engine",
10050aadb33SDmitri Tikhonov    [LSQLM_CONN]        = "conn",
10150aadb33SDmitri Tikhonov    [LSQLM_RECHIST]     = "rechist",
10250aadb33SDmitri Tikhonov    [LSQLM_STREAM]      = "stream",
10350aadb33SDmitri Tikhonov    [LSQLM_PARSE]       = "parse",
10450aadb33SDmitri Tikhonov    [LSQLM_CFCW]        = "cfcw",
10550aadb33SDmitri Tikhonov    [LSQLM_SFCW]        = "sfcw",
10650aadb33SDmitri Tikhonov    [LSQLM_SENDCTL]     = "sendctl",
10750aadb33SDmitri Tikhonov    [LSQLM_ALARMSET]    = "alarmset",
10850aadb33SDmitri Tikhonov    [LSQLM_CRYPTO]      = "crypto",
10950aadb33SDmitri Tikhonov    [LSQLM_HANDSHAKE]   = "handshake",
11050aadb33SDmitri Tikhonov    [LSQLM_HSK_ADAPTER] = "hsk-adapter",
1115392f7a3SLiteSpeed Tech    [LSQLM_BBR]         = "bbr",
11250aadb33SDmitri Tikhonov    [LSQLM_CUBIC]       = "cubic",
11350aadb33SDmitri Tikhonov    [LSQLM_HEADERS]     = "headers",
11450aadb33SDmitri Tikhonov    [LSQLM_FRAME_READER]= "frame-reader",
11550aadb33SDmitri Tikhonov    [LSQLM_FRAME_WRITER]= "frame-writer",
1165392f7a3SLiteSpeed Tech    [LSQLM_MINI_CONN]   = "mini-conn",
1175392f7a3SLiteSpeed Tech    [LSQLM_TOKGEN]      = "tokgen",
11850aadb33SDmitri Tikhonov    [LSQLM_ENG_HIST]    = "eng-hist",
11950aadb33SDmitri Tikhonov    [LSQLM_SPI]         = "spi",
12050aadb33SDmitri Tikhonov    [LSQLM_DI]          = "di",
1215392f7a3SLiteSpeed Tech    [LSQLM_PRQ]         = "prq",
12250aadb33SDmitri Tikhonov    [LSQLM_PACER]       = "pacer",
123e8bd737dSDmitri Tikhonov    [LSQLM_MIN_HEAP]    = "min-heap",
1243b55e6aeSDmitri Tikhonov    [LSQLM_HTTP1X]      = "http1x",
12555cd0b38SDmitri Tikhonov    [LSQLM_QLOG]        = "qlog",
1265392f7a3SLiteSpeed Tech    [LSQLM_TRAPA]       = "trapa",
1275392f7a3SLiteSpeed Tech    [LSQLM_PURGA]       = "purga",
1285392f7a3SLiteSpeed Tech    [LSQLM_HCSI_READER] = "hcsi-reader",
1295392f7a3SLiteSpeed Tech    [LSQLM_HCSO_WRITER] = "hcso-writer",
1305392f7a3SLiteSpeed Tech    [LSQLM_QENC_HDL]    = "qenc-hdl",
1315392f7a3SLiteSpeed Tech    [LSQLM_QDEC_HDL]    = "qdec-hdl",
1325392f7a3SLiteSpeed Tech    [LSQLM_QPACK_ENC]    = "qpack-enc",
1335392f7a3SLiteSpeed Tech    [LSQLM_QPACK_DEC]    = "qpack-dec",
1345392f7a3SLiteSpeed Tech    [LSQLM_PRIO]        = "prio",
1355392f7a3SLiteSpeed Tech    [LSQLM_BW_SAMPLER]  = "bw-sampler",
13650aadb33SDmitri Tikhonov};
13750aadb33SDmitri Tikhonov
13850aadb33SDmitri Tikhonovconst char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS] = {
13950aadb33SDmitri Tikhonov    [LSQ_LOG_ALERT]   =  "ALERT",
14050aadb33SDmitri Tikhonov    [LSQ_LOG_CRIT]    =  "CRIT",
14150aadb33SDmitri Tikhonov    [LSQ_LOG_DEBUG]   =  "DEBUG",
14250aadb33SDmitri Tikhonov    [LSQ_LOG_EMERG]   =  "EMERG",
14350aadb33SDmitri Tikhonov    [LSQ_LOG_ERROR]   =  "ERROR",
14450aadb33SDmitri Tikhonov    [LSQ_LOG_INFO]    =  "INFO",
14550aadb33SDmitri Tikhonov    [LSQ_LOG_NOTICE]  =  "NOTICE",
14650aadb33SDmitri Tikhonov    [LSQ_LOG_WARN]    =  "WARN",
14750aadb33SDmitri Tikhonov};
14850aadb33SDmitri Tikhonov
14950aadb33SDmitri Tikhonov
150b93f59beSBob Perper#ifdef WIN32
151560db45bSDmitri Tikhonov#define DELTA_EPOCH_IN_TICKS  116444736000000000Ui64
152b93f59beSBob Perperstruct timezone
153b93f59beSBob Perper{
154ab5c8df2SBob Perper    time_t tz_minuteswest;         /* minutes W of Greenwich */
155ab5c8df2SBob Perper    time_t tz_dsttime;             /* type of dst correction */
156b93f59beSBob Perper};
157b93f59beSBob Perper
158b93f59beSBob Perperstatic int
159b93f59beSBob Perpergettimeofday (struct timeval *tv, struct timezone *tz)
160b93f59beSBob Perper{
161b93f59beSBob Perper    FILETIME ft;
162560db45bSDmitri Tikhonov    uint64_t tmpres;
163b93f59beSBob Perper    static int tzflag;
164b93f59beSBob Perper
165b93f59beSBob Perper    if (NULL != tv)
166b93f59beSBob Perper    {
167b93f59beSBob Perper        GetSystemTimeAsFileTime(&ft);
168b93f59beSBob Perper
169560db45bSDmitri Tikhonov        tmpres = ((uint64_t) ft.dwHighDateTime << 32)
170560db45bSDmitri Tikhonov               | (ft.dwLowDateTime);
171b93f59beSBob Perper
172560db45bSDmitri Tikhonov        tmpres -= DELTA_EPOCH_IN_TICKS;
173560db45bSDmitri Tikhonov        tv->tv_sec = tmpres / 10000000;
174560db45bSDmitri Tikhonov        tv->tv_usec = tmpres % 1000000;
175b93f59beSBob Perper    }
176b93f59beSBob Perper
177b93f59beSBob Perper    if (NULL != tz)
178b93f59beSBob Perper    {
179b93f59beSBob Perper        if (!tzflag)
180b93f59beSBob Perper        {
181b93f59beSBob Perper            _tzset();
182b93f59beSBob Perper            tzflag++;
183b93f59beSBob Perper        }
184b93f59beSBob Perper        tz->tz_minuteswest = _timezone / 60;
185b93f59beSBob Perper        tz->tz_dsttime = _daylight;
186b93f59beSBob Perper    }
187b93f59beSBob Perper
188b93f59beSBob Perper    return 0;
189b93f59beSBob Perper}
190b93f59beSBob Perper#endif
191b93f59beSBob Perper
192b93f59beSBob Perper
1935392f7a3SLiteSpeed Techstatic size_t
1945392f7a3SLiteSpeed Techprint_timestamp (char *buf, size_t max)
19550aadb33SDmitri Tikhonov{
19650aadb33SDmitri Tikhonov    struct tm tm;
19750aadb33SDmitri Tikhonov    struct timeval tv;
1985392f7a3SLiteSpeed Tech    size_t len = 0;
1995392f7a3SLiteSpeed Tech
20050aadb33SDmitri Tikhonov    gettimeofday(&tv, NULL);
201ab5c8df2SBob Perper#ifdef WIN32
202ab5c8df2SBob Perper    {
2037b1586ddSDmitri Tikhonov        time_t t = tv.tv_sec;
204560db45bSDmitri Tikhonov#ifndef NDEBUG
2057b1586ddSDmitri Tikhonov        errno_t e =
206560db45bSDmitri Tikhonov#endif
2077b1586ddSDmitri Tikhonov	localtime_s(&tm, &t);
208560db45bSDmitri Tikhonov	assert(!e);
209ab5c8df2SBob Perper    }
210ab5c8df2SBob Perper#else
21150aadb33SDmitri Tikhonov    localtime_r(&tv.tv_sec, &tm);
212ab5c8df2SBob Perper#endif
213bfc7bfd8SDmitri Tikhonov    if (g_llts == LLTS_YYYYMMDD_HHMMSSUS)
2145392f7a3SLiteSpeed Tech        len = snprintf(buf, max, "%04d-%02d-%02d %02d:%02d:%02d.%06d ",
215bfc7bfd8SDmitri Tikhonov            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
216bfc7bfd8SDmitri Tikhonov            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec));
217bfc7bfd8SDmitri Tikhonov    else if (g_llts == LLTS_YYYYMMDD_HHMMSSMS)
2185392f7a3SLiteSpeed Tech        len = snprintf(buf, max, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
21950aadb33SDmitri Tikhonov            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
22050aadb33SDmitri Tikhonov            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
22150aadb33SDmitri Tikhonov    else if (g_llts == LLTS_HHMMSSMS)
2225392f7a3SLiteSpeed Tech        len = snprintf(buf, max, "%02d:%02d:%02d.%03d ",
2235392f7a3SLiteSpeed Tech            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
22450aadb33SDmitri Tikhonov    else if (g_llts == LLTS_HHMMSSUS)
2255392f7a3SLiteSpeed Tech        len = snprintf(buf, max, "%02d:%02d:%02d.%06d ",
2265392f7a3SLiteSpeed Tech            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
22750aadb33SDmitri Tikhonov    else if (g_llts == LLTS_CHROMELIKE)
2285392f7a3SLiteSpeed Tech        len = snprintf(buf, max, "%02d%02d/%02d%02d%02d.%06d ",
2295392f7a3SLiteSpeed Tech            tm.tm_mon + 1, tm.tm_mday,tm.tm_hour, tm.tm_min,
2305392f7a3SLiteSpeed Tech            tm.tm_sec, (int) tv.tv_usec);
2315392f7a3SLiteSpeed Tech    return len;
23250aadb33SDmitri Tikhonov}
23350aadb33SDmitri Tikhonov
23450aadb33SDmitri Tikhonov
23550aadb33SDmitri Tikhonovvoid
23650aadb33SDmitri Tikhonovlsquic_logger_log3 (enum lsq_log_level log_level,
23750aadb33SDmitri Tikhonov                    enum lsquic_logger_module module,
2385392f7a3SLiteSpeed Tech                    const lsquic_cid_t *conn_id, lsquic_stream_id_t stream_id,
2395392f7a3SLiteSpeed Tech                    const char *fmt, ...)
24050aadb33SDmitri Tikhonov{
24150aadb33SDmitri Tikhonov    const int saved_errno = errno;
2425392f7a3SLiteSpeed Tech    char cidbuf_[MAX_CID_LEN * 2 + 1];
2435392f7a3SLiteSpeed Tech    size_t len = 0;
2445392f7a3SLiteSpeed Tech    size_t lb;
2455392f7a3SLiteSpeed Tech    size_t max = MAX_LINE_LEN;
2465392f7a3SLiteSpeed Tech    char buf[MAX_LINE_LEN];
24750aadb33SDmitri Tikhonov
24850aadb33SDmitri Tikhonov    if (g_llts != LLTS_NONE)
2495392f7a3SLiteSpeed Tech    {
2505392f7a3SLiteSpeed Tech        lb = print_timestamp(buf, max);
2515392f7a3SLiteSpeed Tech        if (FORMAT_PROBLEM(lb, len, max))
2525392f7a3SLiteSpeed Tech            goto end;
2535392f7a3SLiteSpeed Tech        len += lb;
2545392f7a3SLiteSpeed Tech    }
2555392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "[%s] [QUIC:%"CID_FMT"-%"PRIu64"] %s: ",
2565392f7a3SLiteSpeed Tech        lsq_loglevel2str[log_level], CID_BITS(conn_id),
2575392f7a3SLiteSpeed Tech        stream_id, lsqlm_to_str[module]);
2585392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
2595392f7a3SLiteSpeed Tech        goto end;
2605392f7a3SLiteSpeed Tech    len += lb;
26150aadb33SDmitri Tikhonov    va_list ap;
26250aadb33SDmitri Tikhonov    va_start(ap, fmt);
2635392f7a3SLiteSpeed Tech    lb = vsnprintf(buf + len, max - len, fmt, ap);
264a0e1aeeeSDmitri Tikhonov    va_end(ap);
2655392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
2665392f7a3SLiteSpeed Tech        goto end;
2675392f7a3SLiteSpeed Tech    len += lb;
2685392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "\n");
2695392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
2705392f7a3SLiteSpeed Tech        goto end;
2715392f7a3SLiteSpeed Tech    len += lb;
2725392f7a3SLiteSpeed Tech    logger_if->log_buf(logger_ctx, buf, len);
2735392f7a3SLiteSpeed Techend:
27450aadb33SDmitri Tikhonov    errno = saved_errno;
27550aadb33SDmitri Tikhonov}
27650aadb33SDmitri Tikhonov
27750aadb33SDmitri Tikhonov
27850aadb33SDmitri Tikhonovvoid
27950aadb33SDmitri Tikhonovlsquic_logger_log2 (enum lsq_log_level log_level,
28050aadb33SDmitri Tikhonov                    enum lsquic_logger_module module,
2815392f7a3SLiteSpeed Tech                    const struct lsquic_cid *conn_id, const char *fmt, ...)
28250aadb33SDmitri Tikhonov{
28350aadb33SDmitri Tikhonov    const int saved_errno = errno;
2845392f7a3SLiteSpeed Tech    char cidbuf_[MAX_CID_LEN * 2 + 1];
2855392f7a3SLiteSpeed Tech    size_t len = 0;
2865392f7a3SLiteSpeed Tech    size_t lb;
2875392f7a3SLiteSpeed Tech    size_t max = MAX_LINE_LEN;
2885392f7a3SLiteSpeed Tech    char buf[MAX_LINE_LEN];
28950aadb33SDmitri Tikhonov
29050aadb33SDmitri Tikhonov    if (g_llts != LLTS_NONE)
2915392f7a3SLiteSpeed Tech    {
2925392f7a3SLiteSpeed Tech        lb = print_timestamp(buf, max);
2935392f7a3SLiteSpeed Tech        if (FORMAT_PROBLEM(lb, len, max))
2945392f7a3SLiteSpeed Tech            goto end;
2955392f7a3SLiteSpeed Tech        len += lb;
2965392f7a3SLiteSpeed Tech    }
29750aadb33SDmitri Tikhonov
2985392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "[%s] [QUIC:%"CID_FMT"] %s: ",
2995392f7a3SLiteSpeed Tech        lsq_loglevel2str[log_level], CID_BITS(conn_id), lsqlm_to_str[module]);
3005392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3015392f7a3SLiteSpeed Tech        goto end;
3025392f7a3SLiteSpeed Tech    len += lb;
30350aadb33SDmitri Tikhonov    va_list ap;
30450aadb33SDmitri Tikhonov    va_start(ap, fmt);
3055392f7a3SLiteSpeed Tech    lb = vsnprintf(buf + len, max - len, fmt, ap);
306a0e1aeeeSDmitri Tikhonov    va_end(ap);
3075392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3085392f7a3SLiteSpeed Tech        goto end;
3095392f7a3SLiteSpeed Tech    len += lb;
3105392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "\n");
3115392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3125392f7a3SLiteSpeed Tech        goto end;
3135392f7a3SLiteSpeed Tech    len += lb;
3145392f7a3SLiteSpeed Tech    logger_if->log_buf(logger_ctx, buf, len);
3155392f7a3SLiteSpeed Techend:
31650aadb33SDmitri Tikhonov    errno = saved_errno;
31750aadb33SDmitri Tikhonov}
31850aadb33SDmitri Tikhonov
31950aadb33SDmitri Tikhonov
32050aadb33SDmitri Tikhonovvoid
32150aadb33SDmitri Tikhonovlsquic_logger_log1 (enum lsq_log_level log_level,
32250aadb33SDmitri Tikhonov                    enum lsquic_logger_module module,
32350aadb33SDmitri Tikhonov                    const char *fmt, ...)
32450aadb33SDmitri Tikhonov{
32550aadb33SDmitri Tikhonov    const int saved_errno = errno;
3265392f7a3SLiteSpeed Tech    size_t len = 0;
3275392f7a3SLiteSpeed Tech    size_t lb;
3285392f7a3SLiteSpeed Tech    size_t max = MAX_LINE_LEN;
3295392f7a3SLiteSpeed Tech    char buf[MAX_LINE_LEN];
33050aadb33SDmitri Tikhonov
33150aadb33SDmitri Tikhonov    if (g_llts != LLTS_NONE)
3325392f7a3SLiteSpeed Tech    {
3335392f7a3SLiteSpeed Tech        lb = print_timestamp(buf, max);
3345392f7a3SLiteSpeed Tech        if (FORMAT_PROBLEM(lb, len, max))
3355392f7a3SLiteSpeed Tech            goto end;
3365392f7a3SLiteSpeed Tech        len += lb;
3375392f7a3SLiteSpeed Tech    }
3385392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "[%s] %s: ", lsq_loglevel2str[log_level],
33950aadb33SDmitri Tikhonov                                                lsqlm_to_str[module]);
3405392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3415392f7a3SLiteSpeed Tech        goto end;
3425392f7a3SLiteSpeed Tech    len += lb;
34350aadb33SDmitri Tikhonov    va_list ap;
34450aadb33SDmitri Tikhonov    va_start(ap, fmt);
3455392f7a3SLiteSpeed Tech    lb = vsnprintf(buf + len, max - len, fmt, ap);
346a0e1aeeeSDmitri Tikhonov    va_end(ap);
3475392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3485392f7a3SLiteSpeed Tech        goto end;
3495392f7a3SLiteSpeed Tech    len += lb;
3505392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "\n");
3515392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3525392f7a3SLiteSpeed Tech        goto end;
3535392f7a3SLiteSpeed Tech    len += lb;
3545392f7a3SLiteSpeed Tech    logger_if->log_buf(logger_ctx, buf, len);
3555392f7a3SLiteSpeed Techend:
35650aadb33SDmitri Tikhonov    errno = saved_errno;
35750aadb33SDmitri Tikhonov}
35850aadb33SDmitri Tikhonov
35950aadb33SDmitri Tikhonov
36050aadb33SDmitri Tikhonovvoid
36150aadb33SDmitri Tikhonovlsquic_logger_log0 (enum lsq_log_level log_level, const char *fmt, ...)
36250aadb33SDmitri Tikhonov{
36350aadb33SDmitri Tikhonov    const int saved_errno = errno;
3645392f7a3SLiteSpeed Tech    size_t len = 0;
3655392f7a3SLiteSpeed Tech    size_t lb;
3665392f7a3SLiteSpeed Tech    size_t max = MAX_LINE_LEN;
3675392f7a3SLiteSpeed Tech    char buf[MAX_LINE_LEN];
36850aadb33SDmitri Tikhonov
36950aadb33SDmitri Tikhonov    if (g_llts != LLTS_NONE)
3705392f7a3SLiteSpeed Tech    {
3715392f7a3SLiteSpeed Tech        lb = print_timestamp(buf, max);
3725392f7a3SLiteSpeed Tech        if (FORMAT_PROBLEM(lb, len, max))
3735392f7a3SLiteSpeed Tech            goto end;
3745392f7a3SLiteSpeed Tech        len += lb;
3755392f7a3SLiteSpeed Tech    }
37650aadb33SDmitri Tikhonov
3775392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "[%s] ", lsq_loglevel2str[log_level]);
3785392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3795392f7a3SLiteSpeed Tech        goto end;
3805392f7a3SLiteSpeed Tech    len += lb;
38150aadb33SDmitri Tikhonov    va_list ap;
38250aadb33SDmitri Tikhonov    va_start(ap, fmt);
3835392f7a3SLiteSpeed Tech    lb = vsnprintf(buf + len, max - len, fmt, ap);
38450aadb33SDmitri Tikhonov    va_end(ap);
3855392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3865392f7a3SLiteSpeed Tech        goto end;
3875392f7a3SLiteSpeed Tech    len += lb;
3885392f7a3SLiteSpeed Tech    lb = snprintf(buf + len, max - len, "\n");
3895392f7a3SLiteSpeed Tech    if (FORMAT_PROBLEM(lb, len, max))
3905392f7a3SLiteSpeed Tech        goto end;
3915392f7a3SLiteSpeed Tech    len += lb;
3925392f7a3SLiteSpeed Tech    logger_if->log_buf(logger_ctx, buf, len);
3935392f7a3SLiteSpeed Techend:
39450aadb33SDmitri Tikhonov    errno = saved_errno;
39550aadb33SDmitri Tikhonov}
39650aadb33SDmitri Tikhonov
39750aadb33SDmitri Tikhonov
39850aadb33SDmitri Tikhonovvoid
39950aadb33SDmitri Tikhonovlsquic_logger_init (const struct lsquic_logger_if *lif, void *lctx,
40050aadb33SDmitri Tikhonov                    unsigned llts)
40150aadb33SDmitri Tikhonov{
40250aadb33SDmitri Tikhonov    logger_if  = lif;
40350aadb33SDmitri Tikhonov    logger_ctx = lctx;
40450aadb33SDmitri Tikhonov    if (llts < N_LLTS)
40550aadb33SDmitri Tikhonov        g_llts = llts;
40650aadb33SDmitri Tikhonov    LSQ_DEBUG("%s called", __func__);
40750aadb33SDmitri Tikhonov}
40850aadb33SDmitri Tikhonov
40950aadb33SDmitri Tikhonov
41050aadb33SDmitri Tikhonovenum lsquic_logger_module
41150aadb33SDmitri Tikhonovlsquic_str_to_logger_module (const char *str)
41250aadb33SDmitri Tikhonov{
41350aadb33SDmitri Tikhonov    enum lsquic_logger_module i;
41450aadb33SDmitri Tikhonov    for (i = 0; i < sizeof(lsqlm_to_str) / sizeof(lsqlm_to_str[0]); ++i)
41550aadb33SDmitri Tikhonov        if (0 == strcasecmp(lsqlm_to_str[i], str))
41650aadb33SDmitri Tikhonov            return i;
41750aadb33SDmitri Tikhonov    return -1;
41850aadb33SDmitri Tikhonov}
41950aadb33SDmitri Tikhonov
42050aadb33SDmitri Tikhonov
42150aadb33SDmitri Tikhonovenum lsq_log_level
42250aadb33SDmitri Tikhonovlsquic_str_to_log_level (const char *str)
42350aadb33SDmitri Tikhonov{
42450aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "emerg"))
42550aadb33SDmitri Tikhonov        return LSQ_LOG_EMERG;
42650aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "alert"))
42750aadb33SDmitri Tikhonov        return LSQ_LOG_ALERT;
42850aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "crit"))
42950aadb33SDmitri Tikhonov        return LSQ_LOG_CRIT;
43050aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "error"))
43150aadb33SDmitri Tikhonov        return LSQ_LOG_ERROR;
43250aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "warn"))
43350aadb33SDmitri Tikhonov        return LSQ_LOG_WARN;
43450aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "notice"))
43550aadb33SDmitri Tikhonov        return LSQ_LOG_NOTICE;
43650aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "info"))
43750aadb33SDmitri Tikhonov        return LSQ_LOG_INFO;
43850aadb33SDmitri Tikhonov    if (0 == strcasecmp(str, "debug"))
43950aadb33SDmitri Tikhonov        return LSQ_LOG_DEBUG;
44050aadb33SDmitri Tikhonov    return -1;
44150aadb33SDmitri Tikhonov}
44250aadb33SDmitri Tikhonov
44350aadb33SDmitri Tikhonov
44450aadb33SDmitri Tikhonovvoid
44550aadb33SDmitri Tikhonovlsquic_log_to_fstream (FILE *file, unsigned llts)
44650aadb33SDmitri Tikhonov{
44750aadb33SDmitri Tikhonov    lsquic_logger_init(&file_logger_if, file, llts);
44850aadb33SDmitri Tikhonov}
44950aadb33SDmitri Tikhonov
45050aadb33SDmitri Tikhonov
45150aadb33SDmitri Tikhonovint
45250aadb33SDmitri Tikhonovlsquic_logger_lopt (const char *optarg_orig)
45350aadb33SDmitri Tikhonov{
45450aadb33SDmitri Tikhonov    char *const optarg = strdup(optarg_orig);
45550aadb33SDmitri Tikhonov    char *mod_str;
45650aadb33SDmitri Tikhonov    int i;
45750aadb33SDmitri Tikhonov    for (i = 0; (mod_str = strtok(i ? NULL : optarg, ",")); ++i) {
45850aadb33SDmitri Tikhonov        char *level_str = strchr(mod_str, '=');
45950aadb33SDmitri Tikhonov        if (!level_str) {
46050aadb33SDmitri Tikhonov            fprintf(stderr, "Invalid module specification `%s'\n", mod_str);
46150aadb33SDmitri Tikhonov            break;
46250aadb33SDmitri Tikhonov        }
46350aadb33SDmitri Tikhonov        *level_str = '\0';
46450aadb33SDmitri Tikhonov        ++level_str;
46550aadb33SDmitri Tikhonov        enum lsquic_logger_module mod = lsquic_str_to_logger_module(mod_str);
46650aadb33SDmitri Tikhonov        if (-1 == (int) mod) {
46750aadb33SDmitri Tikhonov            fprintf(stderr, "`%s' is not a valid module name\n", mod_str);
46850aadb33SDmitri Tikhonov            break;
46950aadb33SDmitri Tikhonov        }
47050aadb33SDmitri Tikhonov        enum lsq_log_level level = lsquic_str_to_log_level(level_str);
47150aadb33SDmitri Tikhonov        if (-1 == (int) level) {
47250aadb33SDmitri Tikhonov            fprintf(stderr, "`%s' is not a valid level\n", level_str);
47350aadb33SDmitri Tikhonov            break;
47450aadb33SDmitri Tikhonov        }
47550aadb33SDmitri Tikhonov        lsq_log_levels[mod] = level;
47650aadb33SDmitri Tikhonov        LSQ_INFO("set %s to %s", mod_str, level_str);
47750aadb33SDmitri Tikhonov    }
47850aadb33SDmitri Tikhonov    free(optarg);
47950aadb33SDmitri Tikhonov    return mod_str == NULL ? 0 : -1;
48050aadb33SDmitri Tikhonov}
48150aadb33SDmitri Tikhonov
48250aadb33SDmitri Tikhonov
48350aadb33SDmitri Tikhonovint
48450aadb33SDmitri Tikhonovlsquic_set_log_level (const char *level_str)
48550aadb33SDmitri Tikhonov{
48650aadb33SDmitri Tikhonov    enum lsq_log_level level;
48750aadb33SDmitri Tikhonov    unsigned i;
48850aadb33SDmitri Tikhonov
48950aadb33SDmitri Tikhonov    level = lsquic_str_to_log_level(level_str);
49050aadb33SDmitri Tikhonov    if ((int) level >= 0)
49150aadb33SDmitri Tikhonov    {
49250aadb33SDmitri Tikhonov        for (i = 0; i < sizeof(lsq_log_levels) / sizeof(lsq_log_levels[0]); ++i)
49350aadb33SDmitri Tikhonov            lsq_log_levels[i] = level;
49450aadb33SDmitri Tikhonov        return 0;
49550aadb33SDmitri Tikhonov    }
49650aadb33SDmitri Tikhonov    else
49750aadb33SDmitri Tikhonov        return -1;
49850aadb33SDmitri Tikhonov}
4995392f7a3SLiteSpeed Tech
5005392f7a3SLiteSpeed Tech
5015392f7a3SLiteSpeed Tech/* `out' must be at least MAX_CID_LEN * 2 + 1 characters long */
5025392f7a3SLiteSpeed Techvoid
5035392f7a3SLiteSpeed Techlsquic_cid2str (const lsquic_cid_t *cid, char *out)
5045392f7a3SLiteSpeed Tech{
5055392f7a3SLiteSpeed Tech    static const char hex[] = "0123456789ABCDEF";
5065392f7a3SLiteSpeed Tech    int i;
5075392f7a3SLiteSpeed Tech
5085392f7a3SLiteSpeed Tech    for (i = 0; i < (int) cid->len; ++i)
5095392f7a3SLiteSpeed Tech    {
5105392f7a3SLiteSpeed Tech        *out++ = hex[ cid->idbuf[i] >> 4 ];
5115392f7a3SLiteSpeed Tech        *out++ = hex[ cid->idbuf[i] & 0xF ];
5125392f7a3SLiteSpeed Tech    }
5135392f7a3SLiteSpeed Tech    *out = '\0';
5145392f7a3SLiteSpeed Tech}
515