Skip to content

Commit

Permalink
fixup! Add session_id to endpoint logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jul 29, 2024
1 parent 49a12e5 commit 7d64e76
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 41 deletions.
38 changes: 37 additions & 1 deletion api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,34 @@ oc_cloud_set_endpoint(oc_cloud_context_t *ctx)
return success;
}

void
oc_cloud_endpoint_log(oc_endpoint_t *endpoint, const char *prefix)
{
#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(endpoint, &ep)) {
ep_str = oc_string(ep);
}
#if OC_DBG_IS_ENABLED
int64_t session_id =
(endpoint->flags & TCP) != 0 ? (int64_t)endpoint->session_id : -1;
OC_CLOUD_INFO("%s%s (session_id=%" PRId64 ")", prefix, ep_str, session_id);
#else /* !OC_DBG_IS_ENABLED */
OC_CLOUD_INFO("%s%s", prefix, ep_str);
#endif /* OC_DBG_IS_ENABLED */
#else /* !OC_INFO_IS_ENABLED */
(void)endpoint;
(void)prefix;
#endif /* OC_INFO_IS_ENABLED */
}

void
oc_cloud_close_endpoint(const oc_endpoint_t *ep)
{
if (oc_endpoint_is_empty(ep)) {
return;
}
OC_CLOUD_DBG("oc_cloud_close_endpoint");
oc_close_session(ep);
}
Expand Down Expand Up @@ -311,10 +336,21 @@ cloud_ep_session_event_handler(const oc_endpoint_t *endpoint,
oc_cloud_context_t *ctx = (oc_cloud_context_t *)user_data;
if (oc_endpoint_compare(endpoint, ctx->cloud_ep) != 0) {
OC_CLOUD_DBG("session handler skipped: endpoint does not match");
oc_endpoint_log("cloud->ep: ", ctx->cloud_ep);
oc_endpoint_log("endpoint: ", endpoint);
return;
}
OC_CLOUD_DBG("cloud_ep_session_event_handler ep_state: %d (current: %d)",
#if OC_DBG_IS_ENABLED
int64_t session_id =
(ctx->cloud_ep->flags & TCP) != 0 ? (int64_t)ctx->cloud_ep->session_id : -1;
OC_CLOUD_DBG("ep(%" PRId64 ") ep_state: %d (current: %d)", session_id,
(int)state, (int)ctx->cloud_ep_state);

#endif /* OC_DBG_IS_ENABLED */
if (ctx->cloud_ep->session_id == 0 && state == OC_SESSION_CONNECTED) {
OC_CLOUD_DBG("session_id set to %" PRIu32, endpoint->session_id);
ctx->cloud_ep->session_id = endpoint->session_id;
}
bool state_changed = ctx->cloud_ep_state != state;
if (!state_changed) {
return;
Expand Down
9 changes: 1 addition & 8 deletions api/cloud/oc_cloud_deregister.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,7 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout,
&conf)) {
goto error;
}
#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) {
ep_str = oc_string(ep);
}
OC_CLOUD_INFO("Deregistering from %s", ep_str);
#endif /* OC_INFO_IS_ENABLED */
oc_cloud_endpoint_log(ctx->cloud_ep, "Deregistering from ");
if (oc_cloud_access_deregister(
conf, oc_string(ctx->store.uid),
useAccessToken ? oc_string(ctx->store.access_token) : NULL)) {
Expand Down
3 changes: 3 additions & 0 deletions api/cloud/oc_cloud_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ void oc_cloud_update_by_resource(oc_cloud_context_t *ctx,
const oc_cloud_conf_update_t *data)
OC_NONNULL();

/** Log cloud endpoint address and session id */
void oc_cloud_endpoint_log(oc_endpoint_t *ep, const char *prefix) OC_NONNULL();

#ifdef __cplusplus
}
#endif
Expand Down
27 changes: 3 additions & 24 deletions api/cloud/oc_cloud_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,7 @@ cloud_manager_register_async(void *data)
ctx->schedule_action.timeout, &conf)) {
goto retry;
}
#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) {
ep_str = oc_string(ep);
}
OC_CLOUD_INFO("Registering to %s", ep_str);
#endif /* OC_INFO_IS_ENABLED */
oc_cloud_endpoint_log(ctx->cloud_ep, "Registering to ");
if (!oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL,
oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
Expand Down Expand Up @@ -664,14 +657,7 @@ cloud_manager_login_async(void *data)
ctx->schedule_action.timeout, &conf)) {
goto retry;
}
#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) {
ep_str = oc_string(ep);
}
OC_CLOUD_INFO("Login to %s", ep_str);
#endif /* OC_INFO_IS_ENABLED */
oc_cloud_endpoint_log(ctx->cloud_ep, "Login to ");
if (!oc_cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
OC_CLOUD_ERR("failed to sent sign in request to cloud");
Expand Down Expand Up @@ -892,14 +878,7 @@ cloud_manager_refresh_token_async(void *data)
goto retry;
}

#if OC_INFO_IS_ENABLED
const char *ep_str = "";
oc_string64_t ep = { 0 };
if (oc_endpoint_to_string64(ctx->cloud_ep, &ep)) {
ep_str = oc_string(ep);
}
OC_CLOUD_INFO("Refreshing access token for %s", ep_str);
#endif /* OC_INFO_IS_ENABLED */
oc_cloud_endpoint_log(ctx->cloud_ep, "Refreshing access token for ");
if (!oc_cloud_access_refresh_access_token(
conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid),
oc_string(ctx->store.refresh_token))) {
Expand Down
2 changes: 1 addition & 1 deletion api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ oc_do_ip_discovery_at_endpoint(const char *rt, oc_discovery_handler_t handler,
void
oc_close_session(const oc_endpoint_t *endpoint)
{
oc_endpoint_short_log("oc_close_session:", endpoint);
oc_endpoint_log("oc_close_session: ", endpoint);
#ifdef OC_SECURITY
if ((endpoint->flags & SECURED) != 0) {
oc_tls_close_connection(endpoint);
Expand Down
10 changes: 6 additions & 4 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,8 @@ oc_endpoint_is_empty(const oc_endpoint_t *endpoint)
{
oc_endpoint_t empty;
memset(&empty, 0, sizeof(oc_endpoint_t));
// ignore the next pointer
empty.next = endpoint->next;
// NOLINTNEXTLINE(bugprone-suspicious-memory-comparison)
return memcmp(&empty, endpoint, sizeof(oc_endpoint_t)) == 0;
}
Expand Down Expand Up @@ -807,19 +809,19 @@ oc_endpoint_set_local_address(oc_endpoint_t *ep, unsigned interface_index)
#endif /* OC_CLIENT */

void
oc_endpoint_short_log(const char *prefix, const oc_endpoint_t *endpoint)
oc_endpoint_log(const char *prefix, const oc_endpoint_t *endpoint)
{
#if OC_DBG_IS_ENABLED
// GCOVR_EXCL_START
#if OC_DBG_IS_ENABLED
oc_string64_t endpoint_str;
oc_endpoint_to_string64(endpoint, &endpoint_str);
int64_t session_id =
(endpoint->flags & TCP) != 0 ? (int64_t)endpoint->session_id : -1;
OC_ERR("%s endpoint(addr=%s, session_id=%" PRId64 ")", prefix,
OC_ERR("%sendpoint(addr=%s, session_id=%" PRId64 ")", prefix,
oc_string(endpoint_str), session_id);
// GCOVR_EXCL_STOP
#else /* !OC_DBG_IS_ENABLED */
(void)prefix;
(void)endpoint;
#endif /* OC_DBG_IS_ENABLED */
// GCOVR_EXCL_STOP
}
2 changes: 1 addition & 1 deletion api/oc_endpoint_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ bool oc_endpoint_to_string64(const oc_endpoint_t *endpoint,
oc_string64_t *endpoint_str);

/** Log session id and address. */
void oc_endpoint_short_log(const char *prefix, const oc_endpoint_t *endpoint)
void oc_endpoint_log(const char *prefix, const oc_endpoint_t *endpoint)
OC_NONNULL();

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions api/oc_session_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ oc_session_start_event(const oc_endpoint_t *endpoint)

// only a specific session should be connected
assert(endpoint->session_id != 0);
oc_endpoint_short_log("oc_session_start_event:", endpoint);
oc_endpoint_log("oc_session_start_event: ", endpoint);

oc_endpoint_t *ep = oc_new_endpoint();
memcpy(ep, endpoint, sizeof(oc_endpoint_t));
Expand All @@ -159,7 +159,7 @@ oc_session_end_event(const oc_endpoint_t *endpoint)

// only a specific session should be disconnected
assert(endpoint->session_id != 0);
oc_endpoint_short_log("oc_session_end_event:", endpoint);
oc_endpoint_log("oc_session_end_event: ", endpoint);

oc_endpoint_t *ep = oc_new_endpoint();
memcpy(ep, endpoint, sizeof(oc_endpoint_t));
Expand Down

0 comments on commit 7d64e76

Please sign in to comment.