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