ssb: Suppress noisy output when running command-line actions with output intended to be parsed.

This commit is contained in:
2025-01-14 21:37:11 -05:00
parent 56c77c781a
commit 250933bf41
7 changed files with 79 additions and 27 deletions

View File

@ -287,6 +287,7 @@ static int _tf_command_import(const char* file, int argc, char* argv[])
_create_directories_for_file(db_path, 0700);
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
if (optind < argc)
{
for (int i = optind; i < argc; i++)
@ -356,6 +357,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[])
}
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
if (optind < argc)
{
for (int i = optind; i < argc; i++)
@ -474,6 +476,7 @@ static int _tf_command_publish(const char* file, int argc, char* argv[])
tf_printf("Posting %s as account %s belonging to %s...\n", content, identity, user);
_create_directories_for_file(db_path, 0700);
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
uint8_t private_key[512] = { 0 };
if (tf_ssb_db_identity_get_private_key(ssb, user, identity, private_key, sizeof(private_key)))
{
@ -579,6 +582,7 @@ static int _tf_command_private(const char* file, int argc, char* argv[])
tf_printf("Posting %s as account %s belonging to %s...\n", text, identity, user);
_create_directories_for_file(db_path, 0700);
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
uint8_t private_key[512] = { 0 };
const char* recipient_list[k_max_private_message_recipients] = { 0 };
int recipient_count = 0;
@ -728,6 +732,7 @@ static int _tf_command_store_blob(const char* file, int argc, char* argv[])
char id[256];
_create_directories_for_file(db_path, 0700);
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
if (tf_ssb_db_blob_store(ssb, (const uint8_t*)data, size, id, sizeof(id), NULL))
{
tf_printf("%s\n", id);
@ -845,6 +850,7 @@ static int _tf_command_get_sequence(const char* file, int argc, char* argv[])
}
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
int64_t sequence = -1;
int result = tf_ssb_db_get_latest_message_by_author(ssb, identity, &sequence, NULL, 0) ? EXIT_SUCCESS : EXIT_FAILURE;
tf_printf("%" PRId64 "\n", sequence);
@ -897,6 +903,7 @@ static int _tf_command_get_identity(const char* file, int argc, char* argv[])
char id[k_id_base64_len] = { 0 };
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
int result = tf_ssb_whoami(ssb, id, sizeof(id)) ? EXIT_SUCCESS : EXIT_FAILURE;
tf_printf("%s\n", id);
tf_ssb_destroy(ssb);
@ -1012,6 +1019,7 @@ static int _tf_command_get_contacts(const char* file, int argc, char* argv[])
}
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
tf_ssb_set_quiet(ssb, true);
JSContext* context = tf_ssb_get_context(ssb);
JSValue contacts = JS_NewObject(context);
JSValue follows = JS_NewObject(context);

View File

@ -197,6 +197,7 @@ typedef struct _tf_ssb_t
uint8_t priv[crypto_sign_SECRETKEYBYTES];
bool verbose;
bool quiet;
bool shutting_down;
bool shutting_down_deferred;
@ -2533,7 +2534,10 @@ static void _tf_ssb_on_timer_close(uv_handle_t* handle)
void tf_ssb_destroy(tf_ssb_t* ssb)
{
tf_printf("tf_ssb_destroy\n");
if (!ssb->quiet)
{
tf_printf("tf_ssb_destroy\n");
}
ssb->shutting_down = true;
if (ssb->broadcast_listener.data && !uv_is_closing((uv_handle_t*)&ssb->broadcast_listener))
@ -2576,7 +2580,10 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
uv_close((uv_handle_t*)&ssb->timers[i]->timer, _tf_ssb_on_timer_close);
}
tf_printf("Waiting for closes.\n");
if (!ssb->quiet)
{
tf_printf("Waiting for closes.\n");
}
while (ssb->broadcast_listener.data || ssb->broadcast_sender.data || ssb->broadcast_timer.data || ssb->broadcast_cleanup_timer.data || ssb->trace_timer.data ||
ssb->server.data || ssb->ref_count || ssb->request_activity_timer.data || ssb->timers_count)
@ -2584,7 +2591,10 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
uv_run(ssb->loop, UV_RUN_ONCE);
}
tf_printf("Waiting for rpc.\n");
if (!ssb->quiet)
{
tf_printf("Waiting for rpc.\n");
}
while (ssb->rpc)
{
@ -2643,7 +2653,10 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
tf_free(node);
}
tf_printf("Closing connections.\n");
if (!ssb->quiet)
{
tf_printf("Closing connections.\n");
}
tf_ssb_connection_t* connection = ssb->connections;
while (connection)
{
@ -2652,7 +2665,10 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
connection = next;
}
uv_run(ssb->loop, UV_RUN_NOWAIT);
tf_printf("Closed.\n");
if (!ssb->quiet)
{
tf_printf("Closed.\n");
}
if (ssb->connections_tracker)
{
@ -2664,17 +2680,26 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
if (ssb->loop == &ssb->own_loop)
{
tf_printf("uv_loop_close\n");
if (!ssb->quiet)
{
tf_printf("uv_loop_close\n");
}
int r = uv_loop_close(ssb->loop);
if (r != 0)
if (r != 0 && !ssb->quiet)
{
tf_printf("uv_loop_close: %s\n", uv_strerror(r));
}
}
tf_printf("uv loop closed.\n");
if (!ssb->quiet)
{
tf_printf("uv loop closed.\n");
}
if (ssb->own_context)
{
tf_printf("closing ssb context\n");
if (!ssb->quiet)
{
tf_printf("closing ssb context\n");
}
JS_FreeContext(ssb->context);
JS_FreeRuntime(ssb->runtime);
ssb->own_context = false;
@ -4254,6 +4279,11 @@ void tf_ssb_set_verbose(tf_ssb_t* ssb, bool verbose)
ssb->verbose = verbose;
}
void tf_ssb_set_quiet(tf_ssb_t* ssb, bool quiet)
{
ssb->quiet = quiet;
}
static void _tf_ssb_scheduled_timer(uv_timer_t* handle)
{
tf_ssb_timer_t* timer = handle->data;

View File

@ -189,6 +189,13 @@ void tf_ssb_start_periodic(tf_ssb_t* ssb);
*/
void tf_ssb_set_verbose(tf_ssb_t* ssb, bool verbose);
/**
** Reduce logging verbosity.
** @param ssb The SSB instance.
** @param quiet Disable unnecessary messages.
*/
void tf_ssb_set_quiet(tf_ssb_t* ssb, bool quiet);
/**
** Acquire an SQLite database for unrestricted reading. Release qith tf_ssb_release_db_reader().
** @param ssb The SSB instance.