lsquic_logger.c revision 50aadb33
1/* Copyright (c) 2017 LiteSpeed Technologies Inc.  See LICENSE. */
2/*
3 * LSQUIC Logger implementation.
4 */
5
6#include <errno.h>
7#include <inttypes.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/time.h>
13#include <time.h>
14
15#define LSQUIC_LOGGER_MODULE LSQLM_LOGGER /* Quis custodiet ipsos custodes? */
16#include "lsquic_logger.h"
17#include "lsquic.h"
18
19static enum lsquic_logger_timestamp_style g_llts = LLTS_NONE;
20
21static int
22null_vprintf (void *ctx, const char *fmt, va_list ap)
23{
24    return 0;
25}
26
27
28static int
29file_vprintf (void *ctx, const char *fmt, va_list ap)
30{
31    return vfprintf((FILE *) ctx, fmt, ap);
32}
33
34
35static const struct lsquic_logger_if file_logger_if = {
36    .vprintf    = file_vprintf,
37};
38
39static const struct lsquic_logger_if null_logger_if = {
40    .vprintf    = null_vprintf,
41};
42
43static void *logger_ctx = NULL;
44static const struct lsquic_logger_if *logger_if = &null_logger_if;
45
46enum lsq_log_level lsq_log_levels[N_LSQUIC_LOGGER_MODULES] = {
47    [LSQLM_NOMODULE]    = LSQ_LOG_WARN,
48    [LSQLM_LOGGER]      = LSQ_LOG_WARN,
49    [LSQLM_EVENT]       = LSQ_LOG_WARN,
50    [LSQLM_ENGINE]      = LSQ_LOG_WARN,
51    [LSQLM_CONN]        = LSQ_LOG_WARN,
52    [LSQLM_RECHIST]     = LSQ_LOG_WARN,
53    [LSQLM_STREAM]      = LSQ_LOG_WARN,
54    [LSQLM_PARSE]       = LSQ_LOG_WARN,
55    [LSQLM_CFCW]        = LSQ_LOG_WARN,
56    [LSQLM_SFCW]        = LSQ_LOG_WARN,
57    [LSQLM_SENDCTL]     = LSQ_LOG_WARN,
58    [LSQLM_ALARMSET]    = LSQ_LOG_WARN,
59    [LSQLM_CRYPTO]      = LSQ_LOG_WARN,
60    [LSQLM_HANDSHAKE]   = LSQ_LOG_WARN,
61    [LSQLM_HSK_ADAPTER] = LSQ_LOG_WARN,
62    [LSQLM_CUBIC]       = LSQ_LOG_WARN,
63    [LSQLM_HEADERS]     = LSQ_LOG_WARN,
64    [LSQLM_FRAME_READER]= LSQ_LOG_WARN,
65    [LSQLM_FRAME_WRITER]= LSQ_LOG_WARN,
66    [LSQLM_CONN_HASH]   = LSQ_LOG_WARN,
67    [LSQLM_ENG_HIST]    = LSQ_LOG_WARN,
68    [LSQLM_SPI]         = LSQ_LOG_WARN,
69    [LSQLM_DI]          = LSQ_LOG_WARN,
70    [LSQLM_PACER]       = LSQ_LOG_WARN,
71};
72
73const char *const lsqlm_to_str[N_LSQUIC_LOGGER_MODULES] = {
74    [LSQLM_NOMODULE]    = "",
75    [LSQLM_LOGGER]      = "logger",
76    [LSQLM_EVENT]       = "event",
77    [LSQLM_ENGINE]      = "engine",
78    [LSQLM_CONN]        = "conn",
79    [LSQLM_RECHIST]     = "rechist",
80    [LSQLM_STREAM]      = "stream",
81    [LSQLM_PARSE]       = "parse",
82    [LSQLM_CFCW]        = "cfcw",
83    [LSQLM_SFCW]        = "sfcw",
84    [LSQLM_SENDCTL]     = "sendctl",
85    [LSQLM_ALARMSET]    = "alarmset",
86    [LSQLM_CRYPTO]      = "crypto",
87    [LSQLM_HANDSHAKE]   = "handshake",
88    [LSQLM_HSK_ADAPTER] = "hsk-adapter",
89    [LSQLM_CUBIC]       = "cubic",
90    [LSQLM_HEADERS]     = "headers",
91    [LSQLM_FRAME_READER]= "frame-reader",
92    [LSQLM_FRAME_WRITER]= "frame-writer",
93    [LSQLM_CONN_HASH]   = "conn-hash",
94    [LSQLM_ENG_HIST]    = "eng-hist",
95    [LSQLM_SPI]         = "spi",
96    [LSQLM_DI]          = "di",
97    [LSQLM_PACER]       = "pacer",
98};
99
100const char *const lsq_loglevel2str[N_LSQUIC_LOG_LEVELS] = {
101    [LSQ_LOG_ALERT]   =  "ALERT",
102    [LSQ_LOG_CRIT]    =  "CRIT",
103    [LSQ_LOG_DEBUG]   =  "DEBUG",
104    [LSQ_LOG_EMERG]   =  "EMERG",
105    [LSQ_LOG_ERROR]   =  "ERROR",
106    [LSQ_LOG_INFO]    =  "INFO",
107    [LSQ_LOG_NOTICE]  =  "NOTICE",
108    [LSQ_LOG_WARN]    =  "WARN",
109};
110
111
112static void
113lsquic_printf (const char *fmt, ...)
114{
115    va_list ap;
116    va_start(ap, fmt);
117    logger_if->vprintf(logger_ctx, fmt, ap);
118    va_end(ap);
119}
120
121
122static void
123print_timestamp (void)
124{
125    struct tm tm;
126    struct timeval tv;
127    gettimeofday(&tv, NULL);
128    localtime_r(&tv.tv_sec, &tm);
129    if (g_llts == LLTS_YYYYMMDD_HHMMSSMS)
130        lsquic_printf("%04d-%02d-%02d %02d:%02d:%02d.%03d ",
131            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
132            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) (tv.tv_usec / 1000));
133    else if (g_llts == LLTS_HHMMSSMS)
134        lsquic_printf("%02d:%02d:%02d.%03d ", tm.tm_hour, tm.tm_min,
135                                    tm.tm_sec, (int) (tv.tv_usec / 1000));
136    else if (g_llts == LLTS_HHMMSSUS)
137        lsquic_printf("%02d:%02d:%02d.%06d ", tm.tm_hour, tm.tm_min,
138                                    tm.tm_sec, (int) tv.tv_usec);
139    else if (g_llts == LLTS_CHROMELIKE)
140        lsquic_printf("%02d%02d/%02d%02d%02d.%06d ", tm.tm_mon + 1,
141            tm.tm_mday,tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
142}
143
144
145void
146lsquic_logger_log3 (enum lsq_log_level log_level,
147                    enum lsquic_logger_module module,
148                    uint64_t conn_id, uint32_t stream_id, const char *fmt, ...)
149{
150    const int saved_errno = errno;
151
152    if (g_llts != LLTS_NONE)
153        print_timestamp();
154
155    lsquic_printf("[%s] [QUIC:%"PRIu64"-%"PRIu32"] %s: ",
156        lsq_loglevel2str[log_level], conn_id, stream_id, lsqlm_to_str[module]);
157    va_list ap;
158    va_start(ap, fmt);
159    logger_if->vprintf(logger_ctx, fmt, ap);
160    va_end(ap);
161    lsquic_printf("\n");
162    errno = saved_errno;
163}
164
165
166void
167lsquic_logger_log2 (enum lsq_log_level log_level,
168                    enum lsquic_logger_module module,
169                    uint64_t conn_id, const char *fmt, ...)
170{
171    const int saved_errno = errno;
172
173    if (g_llts != LLTS_NONE)
174        print_timestamp();
175
176    lsquic_printf("[%s] [QUIC:%"PRIu64"] %s: ",
177        lsq_loglevel2str[log_level], conn_id, lsqlm_to_str[module]);
178    va_list ap;
179    va_start(ap, fmt);
180    logger_if->vprintf(logger_ctx, fmt, ap);
181    va_end(ap);
182    lsquic_printf("\n");
183    errno = saved_errno;
184}
185
186
187void
188lsquic_logger_log1 (enum lsq_log_level log_level,
189                    enum lsquic_logger_module module,
190                    const char *fmt, ...)
191{
192    const int saved_errno = errno;
193
194    if (g_llts != LLTS_NONE)
195        print_timestamp();
196
197    lsquic_printf("[%s] %s: ", lsq_loglevel2str[log_level],
198                                                lsqlm_to_str[module]);
199    va_list ap;
200    va_start(ap, fmt);
201    logger_if->vprintf(logger_ctx, fmt, ap);
202    va_end(ap);
203    lsquic_printf("\n");
204    errno = saved_errno;
205}
206
207
208void
209lsquic_logger_log0 (enum lsq_log_level log_level, const char *fmt, ...)
210{
211    const int saved_errno = errno;
212
213    if (g_llts != LLTS_NONE)
214        print_timestamp();
215
216    lsquic_printf("[%s] ", lsq_loglevel2str[log_level]);
217    va_list ap;
218    va_start(ap, fmt);
219    logger_if->vprintf(logger_ctx, fmt, ap);
220    va_end(ap);
221    lsquic_printf("\n");
222    errno = saved_errno;
223}
224
225
226void
227lsquic_logger_init (const struct lsquic_logger_if *lif, void *lctx,
228                    unsigned llts)
229{
230    logger_if  = lif;
231    logger_ctx = lctx;
232    if (llts < N_LLTS)
233        g_llts = llts;
234    LSQ_DEBUG("%s called", __func__);
235}
236
237
238enum lsquic_logger_module
239lsquic_str_to_logger_module (const char *str)
240{
241    enum lsquic_logger_module i;
242    for (i = 0; i < sizeof(lsqlm_to_str) / sizeof(lsqlm_to_str[0]); ++i)
243        if (0 == strcasecmp(lsqlm_to_str[i], str))
244            return i;
245    return -1;
246}
247
248
249enum lsq_log_level
250lsquic_str_to_log_level (const char *str)
251{
252    if (0 == strcasecmp(str, "emerg"))
253        return LSQ_LOG_EMERG;
254    if (0 == strcasecmp(str, "alert"))
255        return LSQ_LOG_ALERT;
256    if (0 == strcasecmp(str, "crit"))
257        return LSQ_LOG_CRIT;
258    if (0 == strcasecmp(str, "error"))
259        return LSQ_LOG_ERROR;
260    if (0 == strcasecmp(str, "warn"))
261        return LSQ_LOG_WARN;
262    if (0 == strcasecmp(str, "notice"))
263        return LSQ_LOG_NOTICE;
264    if (0 == strcasecmp(str, "info"))
265        return LSQ_LOG_INFO;
266    if (0 == strcasecmp(str, "debug"))
267        return LSQ_LOG_DEBUG;
268    return -1;
269}
270
271
272void
273lsquic_log_to_fstream (FILE *file, unsigned llts)
274{
275    lsquic_logger_init(&file_logger_if, file, llts);
276}
277
278
279int
280lsquic_logger_lopt (const char *optarg_orig)
281{
282    char *const optarg = strdup(optarg_orig);
283    char *mod_str;
284    int i;
285    for (i = 0; (mod_str = strtok(i ? NULL : optarg, ",")); ++i) {
286        char *level_str = strchr(mod_str, '=');
287        if (!level_str) {
288            fprintf(stderr, "Invalid module specification `%s'\n", mod_str);
289            break;
290        }
291        *level_str = '\0';
292        ++level_str;
293        enum lsquic_logger_module mod = lsquic_str_to_logger_module(mod_str);
294        if (-1 == (int) mod) {
295            fprintf(stderr, "`%s' is not a valid module name\n", mod_str);
296            break;
297        }
298        enum lsq_log_level level = lsquic_str_to_log_level(level_str);
299        if (-1 == (int) level) {
300            fprintf(stderr, "`%s' is not a valid level\n", level_str);
301            break;
302        }
303        lsq_log_levels[mod] = level;
304        LSQ_INFO("set %s to %s", mod_str, level_str);
305    }
306    free(optarg);
307    return mod_str == NULL ? 0 : -1;
308}
309
310
311int
312lsquic_set_log_level (const char *level_str)
313{
314    enum lsq_log_level level;
315    unsigned i;
316
317    level = lsquic_str_to_log_level(level_str);
318    if ((int) level >= 0)
319    {
320        for (i = 0; i < sizeof(lsq_log_levels) / sizeof(lsq_log_levels[0]); ++i)
321            lsq_log_levels[i] = level;
322        return 0;
323    }
324    else
325        return -1;
326}
327
328
329