From 2f193e64c8bfd650bd047cafd67430fc7ff9e497 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 23 Oct 2024 15:37:28 -0400 Subject: [PATCH] ssb: Show muxrpc command names when possible in verbose logging. --- src/ssb.c | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/ssb.c b/src/ssb.c index 9ee3aad0..b0f0b1b6 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -698,7 +698,8 @@ static void _tf_ssb_request_activity_timer(uv_timer_t* timer) } } -static bool _tf_ssb_connection_get_request_callback(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t** out_callback, void** out_user_data) +static bool _tf_ssb_connection_get_request_callback( + tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t** out_callback, void** out_user_data, const char** out_name) { if (!connection->requests) { @@ -715,6 +716,10 @@ static bool _tf_ssb_connection_get_request_callback(tf_ssb_connection_t* connect { *out_user_data = request->user_data; } + if (out_name) + { + *out_name = request->name; + } request->last_active = uv_now(connection->ssb->loop); if (uv_timer_get_due_in(&connection->ssb->request_activity_timer) == 0) { @@ -838,6 +843,7 @@ void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t r bool tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const char* new_request_name, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data) { + const char* request_name = ""; if (!connection) { if (cleanup) @@ -849,10 +855,10 @@ bool tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, if (flags & k_ssb_rpc_flag_new_request) { assert(request_number > 0); - assert(!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL)); + assert(!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL, NULL)); assert(new_request_name); } - else if (!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL)) + else if (!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL, &request_name)) { if (flags & k_ssb_rpc_flag_binary) { @@ -874,9 +880,9 @@ bool tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, memcpy(combined + 1 + 2 * sizeof(uint32_t), message, size); if (connection->ssb->verbose) { - tf_printf(MAGENTA "%s RPC SEND" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B] %.*s\n", connection->name, (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", - (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], request_number, size, - (flags & k_ssb_rpc_mask_type) == k_ssb_rpc_flag_binary ? 0 : (int)size, message); + tf_printf(MAGENTA "%s RPC SEND[%s]" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B] %.*s\n", connection->name, request_name, + (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], + request_number, size, (flags & k_ssb_rpc_mask_type) == k_ssb_rpc_flag_binary ? 0 : (int)size, message); } _tf_ssb_connection_add_debug_message(connection, true, flags & k_ssb_rpc_mask_send, request_number, message, size); _tf_ssb_connection_box_stream_send(connection, combined, 1 + 2 * sizeof(uint32_t) + size); @@ -1613,21 +1619,22 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t } else if (flags & k_ssb_rpc_flag_json) { - char id[k_id_base64_len] = ""; - tf_ssb_id_bin_to_str(id, sizeof(id), connection->serverpub); + tf_ssb_rpc_callback_t* callback = NULL; + void* user_data = NULL; + const char* request_name = ""; + bool have_request = _tf_ssb_connection_get_request_callback(connection, -request_number, &callback, &user_data, &request_name); if (connection->ssb->verbose) { - tf_printf(CYAN "%s RPC RECV" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B] %.*s\n", connection->name, (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", - (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], request_number, size, (int)size, message); + tf_printf(CYAN "%s RPC RECV[%s]" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B] %.*s\n", connection->name, request_name, + (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], + request_number, size, (int)size, message); } JSContext* context = connection->ssb->context; JSValue val = JS_ParseJSON(context, (const char*)message, size, NULL); if (!JS_IsUndefined(val)) { - tf_ssb_rpc_callback_t* callback = NULL; - void* user_data = NULL; - if (_tf_ssb_connection_get_request_callback(connection, -request_number, &callback, &user_data)) + if (have_request) { if (callback) { @@ -1680,14 +1687,17 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t } else if ((flags & k_ssb_rpc_mask_type) == k_ssb_rpc_flag_binary) { - if (connection->ssb->verbose) - { - tf_printf(CYAN "%s RPC RECV" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B]\n", connection->name, (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", - (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], request_number, size); - } tf_ssb_rpc_callback_t* callback = NULL; void* user_data = NULL; - if (_tf_ssb_connection_get_request_callback(connection, -request_number, &callback, &user_data)) + const char* request_name = ""; + bool have_request = _tf_ssb_connection_get_request_callback(connection, -request_number, &callback, &user_data, &request_name); + if (connection->ssb->verbose) + { + tf_printf(CYAN "%s RPC RECV[%s]" RESET " end/error=%s stream=%s type=%s RN=%d: [%zd B]\n", connection->name, request_name, + (flags & k_ssb_rpc_flag_end_error) ? "true" : "false", (flags & k_ssb_rpc_flag_stream) ? "true" : "false", k_ssb_type_names[flags & k_ssb_rpc_mask_type], + request_number, size); + } + if (have_request) { if (callback) {