lsquic_logger.c revision fbc6cc04
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * LSQUIC Logger implementation.
4 */
5
6#include <assert.h>
7#include <errno.h>
8#include <inttypes.h>
9#include <stdarg.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#ifndef WIN32
14#include <sys/time.h>
15#endif
16#include <time.h>
17
18#define LSQUIC_LOGGER_MODULE LSQLM_LOGGER /* Quis custodiet ipsos custodes? */
19#include "lsquic.h"
20#include "lsquic_logger.h"
21
22#define MAX_LINE_LEN 8192
23/* Expanded TRUNC_FMT should not exceed TRUNC_SZ bytes.  At the same time,
24 * TRUNC_SZ should be significantly smaller than MAX_LINE_LEN.
25 */
26#define TRUNC_FMT "<truncated, need %d bytes>"
27#define TRUNC_SZ 40
28#define FORMAT_PROBLEM(lb, len, max) ((lb < 0) || (lb + len >= max))
29
30/* TODO: display GQUIC CIDs in Chrome-compatible format */
31
32static enum lsquic_logger_timestamp_style g_llts = LLTS_NONE;
33
34static int
35null_log_buf (void *ctx, const char *buf, size_t len)
36{
37    return 0;
38}
39
40static int
41file_log_buf (void *ctx, const char *buf, size_t len)
42{
43    return (int)fwrite(buf, sizeof(char), len, (FILE *) ctx);
44}
45
46static const struct lsquic_logger_if file_logger_if = {
47    .log_buf    = file_log_buf,
48};
49
50static const struct lsquic_logger_if null_logger_if = {
51    .log_buf    = null_log_buf,
52};
53
54static void *logger_ctx = NULL;
55static const struct lsquic_logger_if *logger_if = &null_logger_if;
56
57enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES] = {
58    [LSQLM_NOMODULE]    = LSQ_LOG_WARN,
59    [LSQLM_LOGGER]      = LSQ_LOG_WARN,
60    [LSQLM_EVENT]       = LSQ_LOG_WARN,
61    [LSQLM_ENGINE]      = LSQ_LOG_WARN,
62    [LSQLM_CONN]        = LSQ_LOG_WARN,
63    [LSQLM_STREAM]      = LSQ_LOG_WARN,
64    [LSQLM_PARSE]       = LSQ_LOG_WARN,
65    [LSQLM_CFCW]        = LSQ_LOG_WARN,
66    [LSQLM_SFCW]        = LSQ_LOG_WARN,
67    [LSQLM_SENDCTL]     = LSQ_LOG_WARN,
68    [LSQLM_ALARMSET]    = LSQ_LOG_WARN,
69    [LSQLM_CRYPTO]      = LSQ_LOG_WARN,
70    [LSQLM_HANDSHAKE]   = LSQ_LOG_WARN,
71    [LSQLM_HSK_ADAPTER] = LSQ_LOG_WARN,
72    [LSQLM_BBR]         = LSQ_LOG_WARN,
73    [LSQLM_CUBIC]       = LSQ_LOG_WARN,
74    [LSQLM_ADAPTIVE_CC] = LSQ_LOG_WARN,
75    [LSQLM_HEADERS]     = LSQ_LOG_WARN,
76    [LSQLM_FRAME_READER]= LSQ_LOG_WARN,
77    [LSQLM_FRAME_WRITER]= LSQ_LOG_WARN,
78    [LSQLM_MINI_CONN]   = LSQ_LOG_WARN,
79    [LSQLM_TOKGEN]      = LSQ_LOG_WARN,
80    [LSQLM_ENG_HIST]    = LSQ_LOG_WARN,
81    [LSQLM_SPI]         = LSQ_LOG_WARN,
82    [LSQLM_HPI]         = LSQ_LOG_WARN,
83    [LSQLM_DI]          = LSQ_LOG_WARN,
84    [LSQLM_PRQ]         = LSQ_LOG_WARN,
85    [LSQLM_PACER]       = LSQ_LOG_WARN,
86    [LSQLM_HTTP1X]      = LSQ_LOG_WARN,
87    [LSQLM_QLOG]        = LSQ_LOG_WARN,
88    [LSQLM_TRAPA]       = LSQ_LOG_WARN,
89    [LSQLM_PURGA]       = LSQ_LOG_WARN,
90    [LSQLM_HCSI_READER] = LSQ_LOG_WARN,
91    [LSQLM_HCSO_WRITER] = LSQ_LOG_WARN,
92    [LSQLM_QENC_HDL]    = LSQ_LOG_WARN,
93    [LSQLM_QDEC_HDL]    = LSQ_LOG_WARN,
94    [LSQLM_QPACK_ENC]    = LSQ_LOG_WARN,
95    [LSQLM_QPACK_DEC]    = LSQ_LOG_WARN,
96    [LSQLM_PRIO]        = LSQ_LOG_WARN,
97    [LSQLM_BW_SAMPLER]  = LSQ_LOG_WARN,
98    [LSQLM_PACKET_RESIZE] = LSQ_LOG_WARN,
99};
100
101const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES] = {
102    [LSQLM_NOMODULE]    = "",
103    [LSQLM_LOGGER]      = "logger",
104    [LSQLM_EVENT]       = "event",
105    [LSQLM_ENGINE]      = "engine",
106    [LSQLM_CONN]        = "conn",
107    [LSQLM_STREAM]      = "stream",
108    [LSQLM_PARSE]       = "parse",
109    [LSQLM_CFCW]        = "cfcw",
110    [LSQLM_SFCW]        = "sfcw",
111    [LSQLM_SENDCTL]     = "sendctl",
112    [LSQLM_ALARMSET]    = "alarmset",
113    [LSQLM_CRYPTO]      = "crypto",
114    [LSQLM_HANDSHAKE]   = "handshake",
115    [LSQLM_HSK_ADAPTER] = "hsk-adapter",
116    [LSQLM_BBR]         = "bbr",
117    [LSQLM_CUBIC]       = "cubic",
118    [LSQLM_ADAPTIVE_CC] = "adaptive-cc",
119    [LSQLM_HEADERS]     = "headers",
120    [LSQLM_FRAME_READER]= "frame-reader",
121    [LSQLM_FRAME_WRITER]= "frame-writer",
122    [LSQLM_MINI_CONN]   = "mini-conn",
123    [LSQLM_TOKGEN]      = "tokgen",
124    [LSQLM_ENG_HIST]    = "eng-hist",
125    [LSQLM_SPI]         = "spi",
126    [LSQLM_HPI]         = "hpi",
127    [LSQLM_DI]          = "di",
128    [LSQLM_PRQ]         = "prq",
129    [LSQLM_PACER]       = "pacer",
130    [LSQLM_HTTP1X]      = "http1x",
131    [LSQLM_QLOG]        = "qlog",
132    [LSQLM_TRAPA]       = "trapa",
133    [LSQLM_PURGA]       = "purga",
134    [LSQLM_HCSI_READER] = "hcsi-reader",
135    [LSQLM_HCSO_WRITER] = "hcso-writer",
136    [LSQLM_QENC_HDL]    = "qenc-hdl",
137    [LSQLM_QDEC_HDL]    = "qdec-hdl",
138    [LSQLM_QPACK_ENC]    = "qpack-enc",
139    [LSQLM_QPACK_DEC]    = "qpack-dec",
140    [LSQLM_PRIO]        = "prio",
141    [LSQLM_BW_SAMPLER]  = "bw-sampler",
142    [LSQLM_PACKET_RESIZE] = "packet-resize",
143};
144
145const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS] = {
146    [LSQ_LOG_ALERT]   =  "ALERT",
147    [LSQ_LOG_CRIT]    =  "CRIT",
148    [LSQ_LOG_DEBUG]   =  "DEBUG",
149    [LSQ_LOG_EMERG]   =  "EMERG",
150    [LSQ_LOG_ERROR]   =  "ERROR",
151    [LSQ_LOG_INFO]    =  "INFO",
152    [LSQ_LOG_NOTICE]  =  "NOTICE",
153    [LSQ_LOG_WARN]    =  "WARN",
154};
155
156
157#ifdef WIN32
158#define DELTA_EPOCH_IN_TICKS  116444736000000000Ui64
159struct timezone
160{
161    time_t tz_minuteswest;         /* minutes W of Greenwich */
162    time_t tz_dsttime;             /* type of dst correction */
163};
164
165static int
166gettimeofday (struct timeval *tv, struct timezone *tz)
167{
168    FILETIME ft;
169    uint64_t tmpres;
170    static int tzflag;
171
172    if (NULL != tv)
173    {
174        GetSystemTimeAsFileTime(&ft);
175
176        tmpres = ((uint64_t) ft.dwHighDateTime << 32)
177               | (ft.dwLowDateTime);
178
179        tmpres -= DELTA_EPOCH_IN_TICKS;
180        tv->tv_sec = tmpres / 10000000;
181        tv->tv_usec = tmpres % 1000000;
182    }
183
184    if (NULL != tz)
185    {
186        if (!tzflag)
187        {
188            _tzset();
189            tzflag++;
190        }
191        tz->tz_minuteswest = _timezone / 60;
192        tz->tz_dsttime = _daylight;
193    }
194
195    return 0;
196}
197#endif
198
199
200static size_t
201print_timestamp (char *buf, size_t max)
202{
203    struct tm tm;
204    struct timeval tv;
205    size_t len = 0;
206
207    gettimeofday(&tv, NULL);
208#ifdef WIN32
209    {
210        time_t t = tv.tv_sec;
211#ifndef NDEBUG
212        errno_t e =
213#endif
214	localtime_s(&tm, &t);
215	assert(!e);
216    }
217#else
218    localtime_r(&tv.tv_sec, &tm);
219#endif
220    if (g_llts == LLTS_YYYYMMDD_HHMMSSUS)
221        len = snprintf(buf, max, "%04d-%02d-%02d %02d:%02d:%02d.%06d ",
222            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
223            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec));
224    else if (g_llts == LLTS_YYYYMMDD_HHMMSSMS)
225        len = snprintf(buf, max, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
226            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
227            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
228    else if (g_llts == LLTS_HHMMSSMS)
229        len = snprintf(buf, max, "%02d:%02d:%02d.%03d ",
230            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
231    else if (g_llts == LLTS_HHMMSSUS)
232        len = snprintf(buf, max, "%02d:%02d:%02d.%06d ",
233            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
234    else if (g_llts == LLTS_CHROMELIKE)
235        len = snprintf(buf, max, "%02d%02d/%02d%02d%02d.%06d ",
236            tm.tm_mon + 1, tm.tm_mday,tm.tm_hour, tm.tm_min,
237            tm.tm_sec, (int) tv.tv_usec);
238    return len;
239}
240
241
242void
243lsquic_logger_log3 (enum lsq_log_level log_level,
244                    enum lsquic_logger_module module,
245                    const lsquic_cid_t *conn_id, lsquic_stream_id_t stream_id,
246                    const char *fmt, ...)
247{
248    const int saved_errno = errno;
249    char cidbuf_[MAX_CID_LEN * 2 + 1];
250    size_t len = 0;
251    int lb;
252    size_t max = MAX_LINE_LEN;
253    char buf[MAX_LINE_LEN];
254
255    if (g_llts != LLTS_NONE)
256    {
257        lb = print_timestamp(buf, max);
258        if (FORMAT_PROBLEM(lb, len, max))
259            goto end;
260        len += lb;
261    }
262    lb = snprintf(buf + len, max - len, "[%s] [QUIC:%"CID_FMT"-%"PRIu64"] %s: ",
263        lsq_loglevel2str[log_level], CID_BITS(conn_id),
264        stream_id, lsqlm_to_str[module]);
265    if (FORMAT_PROBLEM(lb, len, max))
266        goto end;
267    len += lb;
268    va_list ap;
269    va_start(ap, fmt);
270    lb = vsnprintf(buf + len, max - len, fmt, ap);
271    va_end(ap);
272    if (lb > 0 && (size_t) lb >= max - len && max - len >= TRUNC_SZ)
273    {
274        len = max - TRUNC_SZ;
275        lb = snprintf(buf + max - TRUNC_SZ, TRUNC_SZ, TRUNC_FMT, lb);
276    }
277    if (FORMAT_PROBLEM(lb, len, max))
278        goto end;
279    len += lb;
280    lb = snprintf(buf + len, max - len, "\n");
281    if (FORMAT_PROBLEM(lb, len, max))
282        goto end;
283    len += lb;
284    logger_if->log_buf(logger_ctx, buf, len);
285end:
286    errno = saved_errno;
287}
288
289
290void
291lsquic_logger_log2 (enum lsq_log_level log_level,
292                    enum lsquic_logger_module module,
293                    const struct lsquic_cid *conn_id, const char *fmt, ...)
294{
295    const int saved_errno = errno;
296    char cidbuf_[MAX_CID_LEN * 2 + 1];
297    size_t len = 0;
298    int lb;
299    size_t max = MAX_LINE_LEN;
300    char buf[MAX_LINE_LEN];
301
302    if (g_llts != LLTS_NONE)
303    {
304        lb = print_timestamp(buf, max);
305        if (FORMAT_PROBLEM(lb, len, max))
306            goto end;
307        len += lb;
308    }
309
310    lb = snprintf(buf + len, max - len, "[%s] [QUIC:%"CID_FMT"] %s: ",
311        lsq_loglevel2str[log_level], CID_BITS(conn_id), lsqlm_to_str[module]);
312    if (FORMAT_PROBLEM(lb, len, max))
313        goto end;
314    len += lb;
315    va_list ap;
316    va_start(ap, fmt);
317    lb = vsnprintf(buf + len, max - len, fmt, ap);
318    va_end(ap);
319    if (lb > 0 && (size_t) lb >= max - len && max - len >= TRUNC_SZ)
320    {
321        len = max - TRUNC_SZ;
322        lb = snprintf(buf + max - TRUNC_SZ, TRUNC_SZ, TRUNC_FMT, lb);
323    }
324    if (FORMAT_PROBLEM(lb, len, max))
325        goto end;
326    len += lb;
327    lb = snprintf(buf + len, max - len, "\n");
328    if (FORMAT_PROBLEM(lb, len, max))
329        goto end;
330    len += lb;
331    logger_if->log_buf(logger_ctx, buf, len);
332end:
333    errno = saved_errno;
334}
335
336
337void
338lsquic_logger_log1 (enum lsq_log_level log_level,
339                    enum lsquic_logger_module module,
340                    const char *fmt, ...)
341{
342    const int saved_errno = errno;
343    size_t len = 0;
344    int lb;
345    size_t max = MAX_LINE_LEN;
346    char buf[MAX_LINE_LEN];
347
348    if (g_llts != LLTS_NONE)
349    {
350        lb = print_timestamp(buf, max);
351        if (FORMAT_PROBLEM(lb, len, max))
352            goto end;
353        len += lb;
354    }
355    lb = snprintf(buf + len, max - len, "[%s] %s: ", lsq_loglevel2str[log_level],
356                                                lsqlm_to_str[module]);
357    if (FORMAT_PROBLEM(lb, len, max))
358        goto end;
359    len += lb;
360    va_list ap;
361    va_start(ap, fmt);
362    lb = vsnprintf(buf + len, max - len, fmt, ap);
363    va_end(ap);
364    if (lb > 0 && (size_t) lb >= max - len && max - len >= TRUNC_SZ)
365    {
366        len = max - TRUNC_SZ;
367        lb = snprintf(buf + max - TRUNC_SZ, TRUNC_SZ, TRUNC_FMT, lb);
368    }
369    if (FORMAT_PROBLEM(lb, len, max))
370        goto end;
371    len += lb;
372    lb = snprintf(buf + len, max - len, "\n");
373    if (FORMAT_PROBLEM(lb, len, max))
374        goto end;
375    len += lb;
376    logger_if->log_buf(logger_ctx, buf, len);
377end:
378    errno = saved_errno;
379}
380
381
382void
383lsquic_logger_log0 (enum lsq_log_level log_level, const char *fmt, ...)
384{
385    const int saved_errno = errno;
386    size_t len = 0;
387    int lb;
388    size_t max = MAX_LINE_LEN;
389    char buf[MAX_LINE_LEN];
390
391    if (g_llts != LLTS_NONE)
392    {
393        lb = print_timestamp(buf, max);
394        if (FORMAT_PROBLEM(lb, len, max))
395            goto end;
396        len += lb;
397    }
398
399    lb = snprintf(buf + len, max - len, "[%s] ", lsq_loglevel2str[log_level]);
400    if (FORMAT_PROBLEM(lb, len, max))
401        goto end;
402    len += lb;
403    va_list ap;
404    va_start(ap, fmt);
405    lb = vsnprintf(buf + len, max - len, fmt, ap);
406    va_end(ap);
407    if (lb > 0 && (size_t) lb >= max - len && max - len >= TRUNC_SZ)
408    {
409        len = max - TRUNC_SZ;
410        lb = snprintf(buf + max - TRUNC_SZ, TRUNC_SZ, TRUNC_FMT, lb);
411    }
412    if (FORMAT_PROBLEM(lb, len, max))
413        goto end;
414    len += lb;
415    lb = snprintf(buf + len, max - len, "\n");
416    if (FORMAT_PROBLEM(lb, len, max))
417        goto end;
418    len += lb;
419    logger_if->log_buf(logger_ctx, buf, len);
420end:
421    errno = saved_errno;
422}
423
424
425void
426lsquic_logger_init (const struct lsquic_logger_if *lif, void *lctx,
427                    unsigned llts)
428{
429    logger_if  = lif;
430    logger_ctx = lctx;
431    if (llts < N_LLTS)
432        g_llts = llts;
433    LSQ_DEBUG("%s called", __func__);
434}
435
436
437enum lsquic_logger_module
438lsquic_str_to_logger_module (const char *str)
439{
440    enum lsquic_logger_module i;
441    for (i = 0; i < sizeof(lsqlm_to_str) / sizeof(lsqlm_to_str[0]); ++i)
442        if (0 == strcasecmp(lsqlm_to_str[i], str))
443            return i;
444    return -1;
445}
446
447
448enum lsq_log_level
449lsquic_str_to_log_level (const char *str)
450{
451    if (0 == strcasecmp(str, "emerg"))
452        return LSQ_LOG_EMERG;
453    if (0 == strcasecmp(str, "alert"))
454        return LSQ_LOG_ALERT;
455    if (0 == strcasecmp(str, "crit"))
456        return LSQ_LOG_CRIT;
457    if (0 == strcasecmp(str, "error"))
458        return LSQ_LOG_ERROR;
459    if (0 == strcasecmp(str, "warn"))
460        return LSQ_LOG_WARN;
461    if (0 == strcasecmp(str, "notice"))
462        return LSQ_LOG_NOTICE;
463    if (0 == strcasecmp(str, "info"))
464        return LSQ_LOG_INFO;
465    if (0 == strcasecmp(str, "debug"))
466        return LSQ_LOG_DEBUG;
467    return -1;
468}
469
470
471void
472lsquic_log_to_fstream (FILE *file, unsigned llts)
473{
474    lsquic_logger_init(&file_logger_if, file, llts);
475}
476
477
478int
479lsquic_logger_lopt (const char *optarg_orig)
480{
481    char *const optarg = strdup(optarg_orig);
482    char *mod_str;
483    int i;
484    for (i = 0; (mod_str = strtok(i ? NULL : optarg, ",")); ++i) {
485        char *level_str = strchr(mod_str, '=');
486        if (!level_str) {
487            fprintf(stderr, "Invalid module specification `%s'\n", mod_str);
488            break;
489        }
490        *level_str = '\0';
491        ++level_str;
492        enum lsquic_logger_module mod = lsquic_str_to_logger_module(mod_str);
493        if (-1 == (int) mod) {
494            fprintf(stderr, "`%s' is not a valid module name\n", mod_str);
495            break;
496        }
497        enum lsq_log_level level = lsquic_str_to_log_level(level_str);
498        if (-1 == (int) level) {
499            fprintf(stderr, "`%s' is not a valid level\n", level_str);
500            break;
501        }
502        lsq_log_levels[mod] = level;
503        LSQ_INFO("set %s to %s", mod_str, level_str);
504    }
505    free(optarg);
506    return mod_str == NULL ? 0 : -1;
507}
508
509
510int
511lsquic_set_log_level (const char *level_str)
512{
513    enum lsq_log_level level;
514    unsigned i;
515
516    level = lsquic_str_to_log_level(level_str);
517    if ((int) level >= 0)
518    {
519        for (i = 0; i < sizeof(lsq_log_levels) / sizeof(lsq_log_levels[0]); ++i)
520            lsq_log_levels[i] = level;
521        return 0;
522    }
523    else
524        return -1;
525}
526
527
528/* `out' must be at least MAX_CID_LEN * 2 + 1 characters long */
529void
530lsquic_cid2str (const lsquic_cid_t *cid, char *out)
531{
532    static const char hex[] = "0123456789ABCDEF";
533    int i;
534
535    for (i = 0; i < (int) cid->len; ++i)
536    {
537        *out++ = hex[ cid->idbuf[i] >> 4 ];
538        *out++ = hex[ cid->idbuf[i] & 0xF ];
539    }
540    *out = '\0';
541}
542