Skip to content

Commit eb6a477

Browse files
committed
emit time
1 parent f25c5bc commit eb6a477

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

include/picotls.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,13 +1397,13 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
13971397
ptls_decode_assert_block_close((src), end); \
13981398
} while (0)
13991399

1400-
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, block) \
1400+
#define PTLS_LOG__DO_LOG(module, name, conn_state, get_sni, get_sni_arg, add_time, block) \
14011401
do { \
14021402
int ptlslog_skip = 0, ptlslog_include_appdata = 0; \
14031403
do { \
14041404
char smallbuf[128]; \
14051405
ptls_buffer_t ptlslogbuf; \
1406-
ptls_log__do_write_start(&logpoint, &ptlslogbuf, smallbuf, sizeof(smallbuf)); \
1406+
ptls_log__do_write_start(&logpoint, &ptlslogbuf, smallbuf, sizeof(smallbuf), (add_time)); \
14071407
do { \
14081408
block \
14091409
} while (0); \
@@ -1420,7 +1420,7 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
14201420
PTLS_LOG_DEFINE_POINT(module, name, logpoint); \
14211421
if (ptls_log_point_maybe_active(&logpoint) == 0) \
14221422
break; \
1423-
PTLS_LOG__DO_LOG(module, name, NULL, NULL, NULL, {block}); \
1423+
PTLS_LOG__DO_LOG(module, name, NULL, NULL, NULL, 1, {block}); \
14241424
} while (0)
14251425

14261426
#define PTLS_LOG_CONN(name, tls, block) \
@@ -1434,7 +1434,7 @@ uint64_t ptls_decode_quicint(const uint8_t **src, const uint8_t *end);
14341434
active &= ptls_log_conn_maybe_active(conn_state, (const char *(*)(void *))ptls_get_server_name, _tls); \
14351435
if (active == 0) \
14361436
break; \
1437-
PTLS_LOG__DO_LOG(picotls, name, conn_state, (const char *(*)(void *))ptls_get_server_name, _tls, { \
1437+
PTLS_LOG__DO_LOG(picotls, name, conn_state, (const char *(*)(void *))ptls_get_server_name, _tls, 1, { \
14381438
PTLS_LOG_ELEMENT_PTR(tls, _tls); \
14391439
do { \
14401440
block \
@@ -1636,7 +1636,8 @@ int ptls_log__do_push_signed32(ptls_buffer_t *buf, int32_t v);
16361636
int ptls_log__do_push_signed64(ptls_buffer_t *buf, int64_t v);
16371637
int ptls_log__do_push_unsigned32(ptls_buffer_t *buf, uint32_t v);
16381638
int ptls_log__do_push_unsigned64(ptls_buffer_t *buf, uint64_t v);
1639-
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize);
1639+
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize,
1640+
int add_time);
16401641
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 *),
16411642
void *get_sni_arg, ptls_buffer_t *buf, int includes_appdata);
16421643

lib/picotls.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7105,39 +7105,53 @@ int ptls_log_add_fd(int fd, float sample_ratio, const char *_points, const char
71057105
#endif
71067106
}
71077107

7108-
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize)
7108+
void ptls_log__do_write_start(struct st_ptls_log_point_t *point, ptls_buffer_t *buf, void *smallbuf, size_t smallbufsize,
7109+
int add_time)
71097110
{
71107111
#if defined(__linux__) || defined(__APPLE__)
71117112
static PTLS_THREADLOCAL char tid[sizeof(",\"tid\":-9223372036854775808")];
7112-
static PTLS_THREADLOCAL int tid_ready;
7113-
if (!tid_ready) {
7113+
static PTLS_THREADLOCAL size_t tid_len;
7114+
if (tid_len == 0) {
71147115
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
71157116
pthread_mutex_lock(&mutex);
7116-
if (!tid_ready) {
7117+
if (tid_len == 0) {
71177118
#if defined(__linux__)
71187119
sprintf(tid, ",\"tid\":%" PRId64, (int64_t)syscall(SYS_gettid));
71197120
#elif defined(__APPLE__)
71207121
uint64_t t = 0;
71217122
(void)pthread_threadid_np(NULL, &t);
7122-
sprintf(tid, ",\"tid\":%" PRIu64, t);
7123+
int l = sprintf(tid, ",\"tid\":%" PRIu64, t);
71237124
#else
71247125
#error "unexpected platform"
71257126
#endif
71267127
__sync_synchronize();
7127-
tid_ready = 1;
7128+
tid_len = (size_t)l;
71287129
}
71297130
pthread_mutex_unlock(&mutex);
71307131
}
71317132
#else
7132-
const char *tid = "";
7133+
const char *tid = NULL;
7134+
const size_t tid_len = 0;
71337135
#endif
71347136
const char *colon_at = strchr(point->name, ':');
71357137

71367138
ptls_buffer_init(buf, smallbuf, smallbufsize);
71377139

7138-
int written = snprintf((char *)buf->base, buf->capacity, "{\"module\":\"%.*s\",\"type\":\"%s\"%s",
7139-
(int)(colon_at - point->name), point->name, colon_at + 1, tid);
7140+
int written = snprintf((char *)buf->base, buf->capacity, "{\"module\":\"%.*s\",\"type\":\"%s\"", (int)(colon_at - point->name),
7141+
point->name, colon_at + 1);
7142+
if (tid != NULL) {
7143+
assert(written > 0 && written + tid_len < buf->capacity);
7144+
memcpy((char *)buf->base + written, tid, tid_len + 1);
7145+
written += tid_len;
7146+
}
7147+
if (add_time != 0) {
7148+
struct timeval tv;
7149+
gettimeofday(&tv, NULL);
7150+
written += snprintf((char *)buf->base + written, buf->capacity - written, ",\"time\":%" PRIu64,
7151+
(uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000);
7152+
}
71407153
assert(written > 0 && written < buf->capacity && "caller MUST provide smallbuf suffient to emit the prefix");
7154+
71417155
buf->off = (size_t)written;
71427156
}
71437157

0 commit comments

Comments
 (0)