Skip to content

Commit 8d7e643

Browse files
committed
Log local port to make it easier to correlate logs with the server
Change-Id: Ic641bd5e784148fbfa4f91c5b0eabc4c341d0f96 Reviewed-on: https://review.couchbase.org/c/libcouchbase/+/232671 Tested-by: Build Bot <build@couchbase.com> Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
1 parent 4d3635e commit 8d7e643

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

src/ctx-log-inl.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
/* small utility for retrieving host/port information from the CTX */
19-
static const lcb_host_t *get_ctx_host(const lcbio_CTX *ctx)
19+
static const lcb_host_t *get_ctx_host_remote(const lcbio_CTX *ctx)
2020
{
2121
static lcb_host_t host = {"NOHOST", "NOPORT", 0};
2222
if (!ctx) {
@@ -31,10 +31,26 @@ static const lcb_host_t *get_ctx_host(const lcbio_CTX *ctx)
3131
return &ctx->sock->info->ep_remote;
3232
}
3333

34-
#define CTX_LOGFMT_PRE "<" LCB_LOG_SPEC("%s%s%s:%s") "> (CTX=%p,%s"
34+
static const lcb_host_t *get_ctx_host_local(const lcbio_CTX *ctx)
35+
{
36+
static lcb_host_t host = {"NOHOST", "NOPORT", 0};
37+
if (!ctx) {
38+
return &host;
39+
}
40+
if (!ctx->sock) {
41+
return &host;
42+
}
43+
if (!ctx->sock->info) {
44+
return &host;
45+
}
46+
return &ctx->sock->info->ep_local;
47+
}
48+
49+
#define CTX_LOGFMT_PRE "<" LCB_LOG_SPEC("%s:%s%s%s:%s") "> (CTX=%p,%s"
3550
#define CTX_LOGFMT CTX_LOGFMT_PRE ") "
3651
#define CTX_LOGID(ctx) \
37-
(ctx && ctx->sock && ctx->sock->settings->log_redaction) ? LCB_LOG_SD_OTAG : "", \
38-
(get_ctx_host(ctx)->ipv6 ? "[" : ""), get_ctx_host(ctx)->host, (get_ctx_host(ctx)->ipv6 ? "]" : ""), \
39-
get_ctx_host(ctx)->port, (ctx && ctx->sock && ctx->sock->settings->log_redaction) ? LCB_LOG_SD_CTAG : "", \
40-
(void *)ctx, ctx ? ctx->subsys : ""
52+
(ctx && ctx->sock && ctx->sock->settings->log_redaction) ? LCB_LOG_SD_OTAG : "", get_ctx_host_local(ctx)->port, \
53+
(get_ctx_host_remote(ctx)->ipv6 ? "[" : ""), get_ctx_host_remote(ctx)->host, \
54+
(get_ctx_host_remote(ctx)->ipv6 ? "]" : ""), get_ctx_host_remote(ctx)->port, \
55+
(ctx && ctx->sock && ctx->sock->settings->log_redaction) ? LCB_LOG_SD_CTAG : "", (void *)ctx, \
56+
ctx ? ctx->subsys : ""

src/lcbio/connect.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using namespace lcb::io;
3030
#define EAI_SYSTEM 0
3131
#endif
3232
#define LOGARGS(conn, lvl) conn->settings, "connection", LCB_LOG_##lvl, __FILE__, __LINE__
33-
static const lcb_host_t *get_loghost(lcbio_SOCKET *s)
33+
static const lcb_host_t *get_loghost_remote(lcbio_SOCKET *s)
3434
{
3535
static lcb_host_t host = {"NOHOST", "NOPORT", 0};
3636
if (!s) {
@@ -41,13 +41,25 @@ static const lcb_host_t *get_loghost(lcbio_SOCKET *s)
4141
}
4242
return &s->info->ep_remote;
4343
}
44+
static const lcb_host_t *get_loghost_local(lcbio_SOCKET *s)
45+
{
46+
static lcb_host_t host = {"NOHOST", "NOPORT", 0};
47+
if (!s) {
48+
return &host;
49+
}
50+
if (!s->info) {
51+
return &host;
52+
}
53+
return &s->info->ep_local;
54+
}
4455

4556
/** Format string arguments for %p%s:%s */
4657
#define CSLOGID(sock) \
47-
sock->settings->log_redaction ? LCB_LOG_SD_OTAG : "", get_loghost(sock)->ipv6 ? "[" : "", get_loghost(sock)->host, \
48-
get_loghost(sock)->ipv6 ? "]" : "", get_loghost(sock)->port, \
58+
sock->settings->log_redaction ? LCB_LOG_SD_OTAG : "", get_loghost_local(sock)->port, \
59+
get_loghost_remote(sock)->ipv6 ? "[" : "", get_loghost_remote(sock)->host, \
60+
get_loghost_remote(sock)->ipv6 ? "]" : "", get_loghost_remote(sock)->port, \
4961
sock->settings->log_redaction ? LCB_LOG_SD_CTAG : "", sock->id
50-
#define CSLOGFMT "<" LCB_LOG_SPEC("%s%s%s:%s") "> (SOCK=%016" PRIx64 ") "
62+
#define CSLOGFMT "<" LCB_LOG_SPEC("%s:%s%s%s:%s") "> (SOCK=%016" PRIx64 ") "
5163

5264
#define LOGARGS_T(lvl) LOGARGS(this->sock, lvl)
5365
#define CSLOGID_T() CSLOGID(this->sock)

0 commit comments

Comments
 (0)