lsquic_logger.c revision 355db7c6
1/* Copyright (c) 2017 - 2018 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_logger.h"
20#include "lsquic.h"
21
22static enum lsquic_logger_timestamp_style g_llts = LLTS_NONE;
23
24static int
25null_vprintf (void *ctx, const char *fmt, va_list ap)
26{
27    return 0;
28}
29
30
31static int
32file_vprintf (void *ctx, const char *fmt, va_list ap)
33{
34    return vfprintf((FILE *) ctx, fmt, ap);
35}
36
37
38static const struct lsquic_logger_if file_logger_if = {
39    .vprintf    = file_vprintf,
40};
41
42static const struct lsquic_logger_if null_logger_if = {
43    .vprintf    = null_vprintf,
44};
45
46static void *logger_ctx = NULL;
47static const struct lsquic_logger_if *logger_if = &null_logger_if;
48
49enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES] = {
50    [LSQLM_NOMODULE]    = LSQ_LOG_WARN,
51    [LSQLM_LOGGER]      = LSQ_LOG_WARN,
52    [LSQLM_EVENT]       = LSQ_LOG_WARN,
53    [LSQLM_ENGINE]      = LSQ_LOG_WARN,
54    [LSQLM_CONN]        = LSQ_LOG_WARN,
55    [LSQLM_RECHIST]     = LSQ_LOG_WARN,
56    [LSQLM_STREAM]      = LSQ_LOG_WARN,
57    [LSQLM_PARSE]       = LSQ_LOG_WARN,
58    [LSQLM_CFCW]        = LSQ_LOG_WARN,
59    [LSQLM_SFCW]        = LSQ_LOG_WARN,
60    [LSQLM_SENDCTL]     = LSQ_LOG_WARN,
61    [LSQLM_ALARMSET]    = LSQ_LOG_WARN,
62    [LSQLM_CRYPTO]      = LSQ_LOG_WARN,
63    [LSQLM_HANDSHAKE]   = LSQ_LOG_WARN,
64    [LSQLM_HSK_ADAPTER] = LSQ_LOG_WARN,
65    [LSQLM_CUBIC]       = LSQ_LOG_WARN,
66    [LSQLM_HEADERS]     = LSQ_LOG_WARN,
67    [LSQLM_FRAME_READER]= LSQ_LOG_WARN,
68    [LSQLM_FRAME_WRITER]= LSQ_LOG_WARN,
69    [LSQLM_CONN_HASH]   = LSQ_LOG_WARN,
70    [LSQLM_ENG_HIST]    = LSQ_LOG_WARN,
71    [LSQLM_SPI]         = LSQ_LOG_WARN,
72    [LSQLM_DI]          = LSQ_LOG_WARN,
73    [LSQLM_PACER]       = LSQ_LOG_WARN,
74    [LSQLM_MIN_HEAP]    = LSQ_LOG_WARN,
75};
76
77const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES] = {
78    [LSQLM_NOMODULE]    = "",
79    [LSQLM_LOGGER]      = "logger",
80    [LSQLM_EVENT]       = "event",
81    [LSQLM_ENGINE]      = "engine",
82    [LSQLM_CONN]        = "conn",
83    [LSQLM_RECHIST]     = "rechist",
84    [LSQLM_STREAM]      = "stream",
85    [LSQLM_PARSE]       = "parse",
86    [LSQLM_CFCW]        = "cfcw",
87    [LSQLM_SFCW]        = "sfcw",
88    [LSQLM_SENDCTL]     = "sendctl",
89    [LSQLM_ALARMSET]    = "alarmset",
90    [LSQLM_CRYPTO]      = "crypto",
91    [LSQLM_HANDSHAKE]   = "handshake",
92    [LSQLM_HSK_ADAPTER] = "hsk-adapter",
93    [LSQLM_CUBIC]       = "cubic",
94    [LSQLM_HEADERS]     = "headers",
95    [LSQLM_FRAME_READER]= "frame-reader",
96    [LSQLM_FRAME_WRITER]= "frame-writer",
97    [LSQLM_CONN_HASH]   = "conn-hash",
98    [LSQLM_ENG_HIST]    = "eng-hist",
99    [LSQLM_SPI]         = "spi",
100    [LSQLM_DI]          = "di",
101    [LSQLM_PACER]       = "pacer",
102    [LSQLM_MIN_HEAP]    = "min-heap",
103};
104
105const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS] = {
106    [LSQ_LOG_ALERT]   =  "ALERT",
107    [LSQ_LOG_CRIT]    =  "CRIT",
108    [LSQ_LOG_DEBUG]   =  "DEBUG",
109    [LSQ_LOG_EMERG]   =  "EMERG",
110    [LSQ_LOG_ERROR]   =  "ERROR",
111    [LSQ_LOG_INFO]    =  "INFO",
112    [LSQ_LOG_NOTICE]  =  "NOTICE",
113    [LSQ_LOG_WARN]    =  "WARN",
114};
115
116
117static void
118lsquic_printf (const char *fmt, ...)
119{
120    va_list ap;
121    va_start(ap, fmt);
122    logger_if->vprintf(logger_ctx, fmt, ap);
123    va_end(ap);
124}
125
126
127#ifdef WIN32
128#define DELTA_EPOCH_IN_TICKS  116444736000000000Ui64
129struct timezone
130{
131    time_t tz_minuteswest;         /* minutes W of Greenwich */
132    time_t tz_dsttime;             /* type of dst correction */
133};
134
135static int
136gettimeofday (struct timeval *tv, struct timezone *tz)
137{
138    FILETIME ft;
139    uint64_t tmpres;
140    static int tzflag;
141
142    if (NULL != tv)
143    {
144        GetSystemTimeAsFileTime(&ft);
145
146        tmpres = ((uint64_t) ft.dwHighDateTime << 32)
147               | (ft.dwLowDateTime);
148
149        tmpres -= DELTA_EPOCH_IN_TICKS;
150        tv->tv_sec = tmpres / 10000000;
151        tv->tv_usec = tmpres % 1000000;
152    }
153
154    if (NULL != tz)
155    {
156        if (!tzflag)
157        {
158            _tzset();
159            tzflag++;
160        }
161        tz->tz_minuteswest = _timezone / 60;
162        tz->tz_dsttime = _daylight;
163    }
164
165    return 0;
166}
167
168
169#endif
170
171
172static void
173print_timestamp (void)
174{
175    struct tm tm;
176    struct timeval tv;
177    gettimeofday(&tv, NULL);
178#ifdef WIN32
179    {
180        time_t t = tv.tv_sec;
181#ifndef NDEBUG
182        errno_t e =
183#endif
184	localtime_s(&tm, &t);
185	assert(!e);
186    }
187#else
188    localtime_r(&tv.tv_sec, &tm);
189#endif
190    if (g_llts == LLTS_YYYYMMDD_HHMMSSUS)
191        lsquic_printf("%04d-%02d-%02d %02d:%02d:%02d.%06d ",
192            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
193            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec));
194    else if (g_llts == LLTS_YYYYMMDD_HHMMSSMS)
195        lsquic_printf("%04d-%02d-%02d %02d:%02d:%02d.%03d ",
196            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
197            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
198    else if (g_llts == LLTS_HHMMSSMS)
199        lsquic_printf("%02d:%02d:%02d.%03d ", tm.tm_hour, tm.tm_min,
200                                    tm.tm_sec, (int) (tv.tv_usec / 1000));
201    else if (g_llts == LLTS_HHMMSSUS)
202        lsquic_printf("%02d:%02d:%02d.%06d ", tm.tm_hour, tm.tm_min,
203                                    tm.tm_sec, (int) tv.tv_usec);
204    else if (g_llts == LLTS_CHROMELIKE)
205        lsquic_printf("%02d%02d/%02d%02d%02d.%06d ", tm.tm_mon + 1,
206            tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
207}
208
209
210void
211lsquic_logger_log3 (enum lsq_log_level log_level,
212                    enum lsquic_logger_module module,
213                    uint64_t conn_id, uint32_t stream_id, const char *fmt, ...)
214{
215    const int saved_errno = errno;
216
217    if (g_llts != LLTS_NONE)
218        print_timestamp();
219
220    lsquic_printf("[%s] [QUIC:%"PRIu64"-%"PRIu32"] %s: ",
221        lsq_loglevel2str[log_level], conn_id, stream_id, lsqlm_to_str[module]);
222    va_list ap;
223    va_start(ap, fmt);
224    logger_if->vprintf(logger_ctx, fmt, ap);
225    va_end(ap);
226    lsquic_printf("\n");
227    errno = saved_errno;
228}
229
230
231void
232lsquic_logger_log2 (enum lsq_log_level log_level,
233                    enum lsquic_logger_module module,
234                    uint64_t conn_id, const char *fmt, ...)
235{
236    const int saved_errno = errno;
237
238    if (g_llts != LLTS_NONE)
239        print_timestamp();
240
241    lsquic_printf("[%s] [QUIC:%"PRIu64"] %s: ",
242        lsq_loglevel2str[log_level], conn_id, lsqlm_to_str[module]);
243    va_list ap;
244    va_start(ap, fmt);
245    logger_if->vprintf(logger_ctx, fmt, ap);
246    va_end(ap);
247    lsquic_printf("\n");
248    errno = saved_errno;
249}
250
251
252void
253lsquic_logger_log1 (enum lsq_log_level log_level,
254                    enum lsquic_logger_module module,
255                    const char *fmt, ...)
256{
257    const int saved_errno = errno;
258
259    if (g_llts != LLTS_NONE)
260        print_timestamp();
261
262    lsquic_printf("[%s] %s: ", lsq_loglevel2str[log_level],
263                                                lsqlm_to_str[module]);
264    va_list ap;
265    va_start(ap, fmt);
266    logger_if->vprintf(logger_ctx, fmt, ap);
267    va_end(ap);
268    lsquic_printf("\n");
269    errno = saved_errno;
270}
271
272
273void
274lsquic_logger_log0 (enum lsq_log_level log_level, const char *fmt, ...)
275{
276    const int saved_errno = errno;
277
278    if (g_llts != LLTS_NONE)
279        print_timestamp();
280
281    lsquic_printf("[%s] ", lsq_loglevel2str[log_level]);
282    va_list ap;
283    va_start(ap, fmt);
284    logger_if->vprintf(logger_ctx, fmt, ap);
285    va_end(ap);
286    lsquic_printf("\n");
287    errno = saved_errno;
288}
289
290
291void
292lsquic_logger_init (const struct lsquic_logger_if *lif, void *lctx,
293                    unsigned llts)
294{
295    logger_if  = lif;
296    logger_ctx = lctx;
297    if (llts < N_LLTS)
298        g_llts = llts;
299    LSQ_DEBUG("%s called", __func__);
300}
301
302
303enum lsquic_logger_module
304lsquic_str_to_logger_module (const char *str)
305{
306    enum lsquic_logger_module i;
307    for (i = 0; i < sizeof(lsqlm_to_str) / sizeof(lsqlm_to_str[0]); ++i)
308        if (0 == strcasecmp(lsqlm_to_str[i], str))
309            return i;
310    return -1;
311}
312
313
314enum lsq_log_level
315lsquic_str_to_log_level (const char *str)
316{
317    if (0 == strcasecmp(str, "emerg"))
318        return LSQ_LOG_EMERG;
319    if (0 == strcasecmp(str, "alert"))
320        return LSQ_LOG_ALERT;
321    if (0 == strcasecmp(str, "crit"))
322        return LSQ_LOG_CRIT;
323    if (0 == strcasecmp(str, "error"))
324        return LSQ_LOG_ERROR;
325    if (0 == strcasecmp(str, "warn"))
326        return LSQ_LOG_WARN;
327    if (0 == strcasecmp(str, "notice"))
328        return LSQ_LOG_NOTICE;
329    if (0 == strcasecmp(str, "info"))
330        return LSQ_LOG_INFO;
331    if (0 == strcasecmp(str, "debug"))
332        return LSQ_LOG_DEBUG;
333    return -1;
334}
335
336
337void
338lsquic_log_to_fstream (FILE *file, unsigned llts)
339{
340    lsquic_logger_init(&file_logger_if, file, llts);
341}
342
343
344int
345lsquic_logger_lopt (const char *optarg_orig)
346{
347    char *const optarg = strdup(optarg_orig);
348    char *mod_str;
349    int i;
350    for (i = 0; (mod_str = strtok(i ? NULL : optarg, ",")); ++i) {
351        char *level_str = strchr(mod_str, '=');
352        if (!level_str) {
353            fprintf(stderr, "Invalid module specification `%s'\n", mod_str);
354            break;
355        }
356        *level_str = '\0';
357        ++level_str;
358        enum lsquic_logger_module mod = lsquic_str_to_logger_module(mod_str);
359        if (-1 == (int) mod) {
360            fprintf(stderr, "`%s' is not a valid module name\n", mod_str);
361            break;
362        }
363        enum lsq_log_level level = lsquic_str_to_log_level(level_str);
364        if (-1 == (int) level) {
365            fprintf(stderr, "`%s' is not a valid level\n", level_str);
366            break;
367        }
368        lsq_log_levels[mod] = level;
369        LSQ_INFO("set %s to %s", mod_str, level_str);
370    }
371    free(optarg);
372    return mod_str == NULL ? 0 : -1;
373}
374
375
376int
377lsquic_set_log_level (const char *level_str)
378{
379    enum lsq_log_level level;
380    unsigned i;
381
382    level = lsquic_str_to_log_level(level_str);
383    if ((int) level >= 0)
384    {
385        for (i = 0; i < sizeof(lsq_log_levels) / sizeof(lsq_log_levels[0]); ++i)
386            lsq_log_levels[i] = level;
387        return 0;
388    }
389    else
390        return -1;
391}
392