reduce amount of code generated for each logpoint (also prepare for tid)
diff --git a/include/picotls.h b/include/picotls.h index 2b4105a..f6cdfc7 100644 --- a/include/picotls.h +++ b/include/picotls.h
@@ -1403,17 +1403,13 @@ do { \ char smallbuf[128]; \ ptls_buffer_t ptlslogbuf; \ - ptls_buffer_init(&ptlslogbuf, smallbuf, sizeof(smallbuf)); \ - PTLS_LOG__DO_PUSH_SAFESTR("{\"module\":\"" PTLS_TO_STR(module) "\",\"type\":\"" PTLS_TO_STR(name) "\""); \ + ptls_log__do_write_start(&logpoint, &ptlslogbuf, smallbuf, sizeof(smallbuf)); \ do { \ block \ } while (0); \ - PTLS_LOG__DO_PUSH_SAFESTR("}\n"); \ - if (!ptlslog_skip) \ - ptlslog_include_appdata = \ - ptls_log__do_write(&logpoint, (conn_state), (get_sni), (get_sni_arg), &ptlslogbuf, ptlslog_include_appdata); \ - ptls_buffer_dispose(&ptlslogbuf); \ - } while (!ptlslog_skip && ptlslog_include_appdata); \ + ptlslog_include_appdata = ptls_log__do_write_end(ptlslog_skip ? NULL : &logpoint, (conn_state), (get_sni), \ + (get_sni_arg), &ptlslogbuf, ptlslog_include_appdata); \ + } while (ptlslog_include_appdata); \ } while (0) #define PTLS_LOG_DEFINE_POINT(_module, _name, _var) \ @@ -1640,8 +1636,9 @@ int ptls_log__do_push_signed64(ptls_buffer_t *buf, int64_t v); int ptls_log__do_push_unsigned32(ptls_buffer_t *buf, uint32_t v); int ptls_log__do_push_unsigned64(ptls_buffer_t *buf, uint64_t v); -int ptls_log__do_write(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), - void *get_sni_arg, const ptls_buffer_t *buf, int includes_appdata); +void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize); +int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), + void *get_sni_arg, ptls_buffer_t *buf, int includes_appdata); /** * create a client object to handle new TLS connection
diff --git a/lib/picotls.c b/lib/picotls.c index 37151e6..81661db 100644 --- a/lib/picotls.c +++ b/lib/picotls.c
@@ -7102,12 +7102,31 @@ #endif } -int ptls_log__do_write(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), - void *get_sni_arg, const ptls_buffer_t *buf, int includes_appdata) +void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize) { + const char *colon_at = strchr(point->name, ':'); + + ptls_buffer_init(buf, smallbuf, smallbufsize); + + int written = snprintf((char *)buf->base, buf->capacity, "{\"module\":\"%.*s\",\"type\":\"%s\"", (int)(colon_at - point->name), + point->name, colon_at + 1); + assert(written > 0 && written < buf->capacity && "caller MUST provide smallbuf suffient to emit the prefix"); + buf->off = (size_t)written; +} + +int ptls_log__do_write_end(struct st_ptls_log_point_t *point, struct st_ptls_log_conn_state_t *conn, const char *(*get_sni)(void *), + void *get_sni_arg, ptls_buffer_t *buf, int includes_appdata) +{ + int needs_appdata = 0; + #if PTLS_HAVE_LOG uint32_t active; - int needs_appdata = 0; + + /* point == NULL indicates skip */ + if (point == NULL || ptls_buffer_reserve(buf, 2) != 0) + goto Exit; + buf->base[buf->off++] = '}'; + buf->base[buf->off++] = '\n'; pthread_mutex_lock(&logctx.mutex); @@ -7152,9 +7171,9 @@ if (includes_appdata) assert(!needs_appdata); - - return needs_appdata; -#else - return 0; #endif + +Exit: + ptls_buffer_dispose(buf); + return needs_appdata; }