From 61ff46690835d9e74df3e14adc18bddeb74fcd23 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Tue, 7 Mar 2023 17:50:17 +0000 Subject: [PATCH] Replace all printfs with tf_printf, which redirects to android logging. Change into the files directory so that sqlite can do its thing. Getting closer. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4203 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- Makefile | 3 +- .../unprompted/tildefriends/MainActivity.java | 4 + src/database.js.c | 3 +- src/jnitest.c | 14 ++++ src/log.h | 9 +++ src/main.c | 31 ++++++-- src/ssb.c | 75 +++++++++--------- src/ssb.connections.c | 9 ++- src/ssb.db.c | 77 ++++++++++--------- src/ssb.export.c | 15 ++-- src/ssb.import.c | 17 ++-- src/ssb.js.c | 11 +-- src/ssb.tests.c | 63 +++++++-------- src/task.c | 17 ++-- src/tests.c | 65 ++++++++-------- src/util.js.c | 13 ++-- 16 files changed, 240 insertions(+), 186 deletions(-) create mode 100644 src/log.h diff --git a/Makefile b/Makefile index b4e4a167..bbd2927f 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,8 @@ $(ANDROID_TARGETS): CFLAGS += \ -Wno-unknown-warning-option $(ANDROID_TARGETS): LDFLAGS += \ -target $(ANDROID_NDK_TARGET_TRIPLE)$(ANDROID_NDK_API_VERSION) \ - -Ldeps/openssl/android/arm64-v8a/usr/local/lib + -Ldeps/openssl/android/arm64-v8a/usr/local/lib \ + -llog ifeq ($(UNAME_M),x86_64) debug: CFLAGS += -fsanitize=address -fsanitize=undefined -fno-common diff --git a/src/android/com/unprompted/tildefriends/MainActivity.java b/src/android/com/unprompted/tildefriends/MainActivity.java index dbc5b525..50427537 100644 --- a/src/android/com/unprompted/tildefriends/MainActivity.java +++ b/src/android/com/unprompted/tildefriends/MainActivity.java @@ -1,6 +1,7 @@ package com.unprompted.tildefriends; import android.app.Activity; +import android.util.Log; import android.os.Bundle; import android.widget.TextView; @@ -10,10 +11,13 @@ public class MainActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView text = (TextView)findViewById(R.id.my_text); + Log.w("tildefriends", String.format("external files dir is %s", getFilesDir().toString())); + setFilesPath(getFilesDir().toString()); text.setText(getMessage()); } public native String getMessage(); + public native void setFilesPath(String path); static { System.loadLibrary("tildefriends"); diff --git a/src/database.js.c b/src/database.js.c index 6d7ce329..4feafe69 100644 --- a/src/database.js.c +++ b/src/database.js.c @@ -1,5 +1,6 @@ #include "database.js.h" +#include "log.h" #include "mem.h" #include @@ -40,7 +41,7 @@ void tf_database_register(JSContext* context, sqlite3* sqlite) }; if (JS_NewClass(JS_GetRuntime(context), _database_class_id, &def) != 0) { - printf("Failed to register database.\n"); + tf_printf("Failed to register database.\n"); } JSValue global = JS_GetGlobalObject(context); diff --git a/src/jnitest.c b/src/jnitest.c index d7ee5fab..57913762 100644 --- a/src/jnitest.c +++ b/src/jnitest.c @@ -1,8 +1,22 @@ #if defined(__ANDROID__) +#include "log.h" + #include +#include + +void tf_android(); JNIEXPORT jstring JNICALL Java_com_unprompted_tildefriends_MainActivity_getMessage(JNIEnv* env, jobject obj) { + tf_android(); return (*env)->NewStringUTF(env, "Hello!"); } + +JNIEXPORT void JNICALL Java_com_unprompted_tildefriends_MainActivity_setFilesPath( JNIEnv* env, jobject obj, jstring path) +{ + tf_printf("setFilesPath called.\n"); + const char* file_path = (*env)->GetStringUTFChars(obj, path, NULL); + int result = chdir(file_path); + tf_printf("chdir %s => %d\n", file_path, result); +} #endif diff --git a/src/log.h b/src/log.h new file mode 100644 index 00000000..0c88ed65 --- /dev/null +++ b/src/log.h @@ -0,0 +1,9 @@ +#pragma once + +#if defined(__ANDROID__) +#include +#define tf_printf(...) __android_log_print(ANDROID_LOG_INFO, "tildefriends", __VA_ARGS__) +#else +#include +#define tf_printf printf +#endif diff --git a/src/main.c b/src/main.c index e8d6f200..59f62c1c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include "log.h" #include "mem.h" #include "ssb.h" #include "ssb.db.h" @@ -215,13 +216,13 @@ static int _tf_command_import(const char* file, int argc, char* argv[]) { for (int i = 0; i < extra_count; i++) { - printf("Importing %s...\n", extras[i]); + tf_printf("Importing %s...\n", extras[i]); tf_ssb_import(ssb, args.user, extras[i]); } } else { - printf("Importing %s...\n", "apps"); + tf_printf("Importing %s...\n", "apps"); tf_ssb_import(ssb, args.user, "apps"); } tf_ssb_destroy(ssb); @@ -275,7 +276,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[]) { for (int i = 0; i < extra_count; i++) { - printf("Exporting %s...\n", extras[i]); + tf_printf("Exporting %s...\n", extras[i]); tf_ssb_export(ssb, extras[i]); } } @@ -295,7 +296,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[]) { char buffer[256]; snprintf(buffer, sizeof(buffer), "/~%s/%s", args.user, k_export[i]); - printf("Exporting %s...\n", buffer); + tf_printf("Exporting %s...\n", buffer); tf_ssb_export(ssb, buffer); } } @@ -651,11 +652,11 @@ xopt_help: static int _tf_command_usage(const char* file, int argc, char* argv[]) { - printf("Usage: %s command [command-options]\n", file); - printf("commands:\n"); + tf_printf("Usage: %s command [command-options]\n", file); + tf_printf("commands:\n"); for (int i = 0; i < (int)_countof(k_commands); i++) { - printf(" %s - %s\n", k_commands[i].name, k_commands[i].description); + tf_printf(" %s - %s\n", k_commands[i].name, k_commands[i].description); } return 0; } @@ -669,7 +670,7 @@ static void _do_leak_checks(int sig) static void _backtrace_error(void* data, const char* message, int errnum) { - printf("libbacktrace error %d: %s\n", errnum, message); + tf_printf("libbacktrace error %d: %s\n", errnum, message); } int main(int argc, char* argv[]) @@ -732,3 +733,17 @@ done: tf_mem_shutdown(); return result; } + +static void _tf_android_thread(void* user_data) +{ + char* args[] = { "tildefriends" }; + main(_countof(args), args); +} + +void tf_android() +{ + uv_thread_t thread_id = 0; + tf_printf("TEST: Starting a thread.\n"); + uv_thread_create(&thread_id, _tf_android_thread, NULL); + tf_printf("TEST: Post-thread start.\n"); +} diff --git a/src/ssb.c b/src/ssb.c index 7e9b428d..df58973d 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -1,5 +1,6 @@ #include "ssb.h" +#include "log.h" #include "mem.h" #include "ssb.connections.h" #include "ssb.db.h" @@ -396,7 +397,7 @@ static void _tf_ssb_connection_close(tf_ssb_connection_t* connection, const char else if (connection->state == k_tf_ssb_state_verified || connection->state == k_tf_ssb_state_server_verified) { - printf("Connection %s %p is closing: %s.\n", connection->name, connection, reason); + tf_printf("Connection %s %p is closing: %s.\n", connection->name, connection, reason); _tf_ssb_add_debug_close(connection->ssb, connection, reason); connection->state = k_tf_ssb_state_closing; _tf_ssb_connection_send_close(connection); @@ -707,7 +708,7 @@ void 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) { - printf(MAGENTA "%s RPC SEND" RESET " flags=%x RN=%d: [%zd B] %.*s\n", connection->name, flags, request_number, size, (flags & k_ssb_rpc_mask_type) == k_ssb_rpc_flag_binary ? 0 : (int)size, message); + tf_printf(MAGENTA "%s RPC SEND" RESET " flags=%x RN=%d: [%zd B] %.*s\n", connection->name, flags, 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, request_number, message, size); _tf_ssb_connection_box_stream_send(connection, combined, 1 + 2 * sizeof(uint32_t) + size); @@ -911,23 +912,23 @@ static bool _tf_ssb_verify_and_strip_signature_internal(JSContext* context, JSVa verified = r == 0; if (!verified) { - //printf("crypto_sign_verify_detached fail (r=%d)\n", r); + //tf_printf("crypto_sign_verify_detached fail (r=%d)\n", r); if (false) { - printf("val=[%.*s]\n", (int)strlen(sigstr), sigstr); - printf("sig=%.*s\n", (int)(sigkind - str), str); - printf("public key=%.*s\n", (int)(type - author_id), author_id); + tf_printf("val=[%.*s]\n", (int)strlen(sigstr), sigstr); + tf_printf("sig=%.*s\n", (int)(sigkind - str), str); + tf_printf("public key=%.*s\n", (int)(type - author_id), author_id); } } } else { - printf("base64 decode sig fail [%.*s]\n", (int)(sigkind - str), str); + tf_printf("base64 decode sig fail [%.*s]\n", (int)(sigkind - str), str); } } else { - printf("base64 decode author[%.*s] fail (%d)\n", (int)(type - author_id), author_id, r); + tf_printf("base64 decode author[%.*s] fail (%d)\n", (int)(type - author_id), author_id, r); } JS_FreeCString(context, author); @@ -1435,7 +1436,7 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t tf_ssb_id_bin_to_str(id, sizeof(id), connection->serverpub); if (connection->ssb->verbose) { - printf(CYAN "%s RPC RECV" RESET " from %s flags=%x RN=%d: [%zd B] %.*s\n", connection->name, id, flags, request_number, size, (int)size, message); + tf_printf(CYAN "%s RPC RECV" RESET " from %s flags=%x RN=%d: [%zd B] %.*s\n", connection->name, id, flags, request_number, size, (int)size, message); } JSContext* context = connection->ssb->context; JSValue val = JS_ParseJSON(context, (const char*)message, size, NULL); @@ -1480,7 +1481,7 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t } else { - printf("Failed to parse %.*s\n", (int)size, message); + tf_printf("Failed to parse %.*s\n", (int)size, message); } JS_FreeValue(context, val); @@ -1489,7 +1490,7 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t { if (connection->ssb->verbose) { - printf(CYAN "%s RPC RECV" RESET " flags=%x RN=%d: %zd bytes\n", connection->name, flags, request_number, size); + tf_printf(CYAN "%s RPC RECV" RESET " flags=%x RN=%d: %zd bytes\n", connection->name, flags, request_number, size); } tf_ssb_rpc_callback_t* callback = NULL; void* user_data = NULL; @@ -1506,7 +1507,7 @@ static void _tf_ssb_connection_rpc_recv(tf_ssb_connection_t* connection, uint8_t } else { - printf("No request callback for %p %d\n", connection, -request_number); + tf_printf("No request callback for %p %d\n", connection, -request_number); } } @@ -1674,12 +1675,12 @@ void tf_ssb_append_message_with_keys(tf_ssb_t* ssb, const char* author, const ui } else { - printf("message not stored.\n"); + tf_printf("message not stored.\n"); } } else { - printf("Failed to verify message signature.\n"); + tf_printf("Failed to verify message signature.\n"); } JS_FreeValue(context, root); @@ -1950,7 +1951,7 @@ static void _tf_ssb_connection_on_connect(uv_connect_t* connect, int status) int result = uv_read_start(connect->handle, _tf_ssb_connection_on_tcp_alloc, _tf_ssb_connection_on_tcp_recv); if (result) { - printf("uv_read_start => %s\n", uv_strerror(status)); + tf_printf("uv_read_start => %s\n", uv_strerror(status)); _tf_ssb_connection_close(connection, "uv_read_start failed"); } else @@ -2082,7 +2083,7 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path if (!_tf_ssb_load_keys(ssb)) { - printf("Generating a new keypair.\n"); + tf_printf("Generating a new keypair.\n"); tf_ssb_db_identity_create(ssb, ":admin", ssb->pub, ssb->priv); } @@ -2339,14 +2340,14 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c { char id[k_id_base64_len]; tf_ssb_id_bin_to_str(id, sizeof(id), public_key); - printf("Not connecting to %s:%d, because we are already connected to %s (state = %d).\n", host, ntohs(addr->sin_port), id, connection->state); + tf_printf("Not connecting to %s:%d, because we are already connected to %s (state = %d).\n", host, ntohs(addr->sin_port), id, connection->state); return NULL; } else if (memcmp(ssb->pub, public_key, k_id_bin_len) == 0) { char id[k_id_base64_len]; tf_ssb_id_bin_to_str(id, sizeof(id), public_key); - printf("Not connecting to %s:%d, because they appear to be ourselves %s.\n", host, ntohs(addr->sin_port), id); + tf_printf("Not connecting to %s:%d, because they appear to be ourselves %s.\n", host, ntohs(addr->sin_port), id); return NULL; } } @@ -2379,7 +2380,7 @@ tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, c int result = uv_tcp_connect(&connection->connect, &connection->tcp, (const struct sockaddr*)addr, _tf_ssb_connection_on_connect); if (result) { - printf("uv_tcp_connect(%s): %s\n", host, uv_strerror(result)); + tf_printf("uv_tcp_connect(%s): %s\n", host, uv_strerror(result)); connection->connect.data = NULL; _tf_ssb_connection_destroy(connection, "connect failed"); } @@ -2478,7 +2479,7 @@ static void _tf_on_connect_getaddrinfo(uv_getaddrinfo_t* addrinfo, int result, s } else { - printf("getaddrinfo => %s\n", uv_strerror(result)); + tf_printf("getaddrinfo => %s\n", uv_strerror(result)); } tf_free(connect); uv_freeaddrinfo(info); @@ -2501,7 +2502,7 @@ void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* ke int r = uv_getaddrinfo(ssb->loop, &connect->req, _tf_on_connect_getaddrinfo, host, NULL, &(struct addrinfo) { .ai_family = AF_INET }); if (r < 0) { - printf("uv_getaddrinfo: %s\n", uv_strerror(r)); + tf_printf("uv_getaddrinfo: %s\n", uv_strerror(r)); tf_free(connect); } } @@ -2516,7 +2517,7 @@ static void _tf_ssb_on_connection(uv_stream_t* stream, int status) tf_ssb_t* ssb = stream->data; if (status < 0) { - printf("uv_listen failed: %s\n", uv_strerror(status)); + tf_printf("uv_listen failed: %s\n", uv_strerror(status)); return; } @@ -2560,13 +2561,13 @@ static void _tf_ssb_send_broadcast(tf_ssb_t* ssb, struct sockaddr_in* address) if (uv_tcp_getsockname(&ssb->server, &server_addr, &len) != 0 || server_addr.sa_family != AF_INET) { - printf("Unable to get server's address.\n"); + tf_printf("Unable to get server's address.\n"); } char address_str[256]; if (uv_ip4_name(address, address_str, sizeof(address_str)) != 0) { - printf("Unable to convert address to string.\n"); + tf_printf("Unable to convert address to string.\n"); } char fullid[k_id_base64_len]; @@ -2584,7 +2585,7 @@ static void _tf_ssb_send_broadcast(tf_ssb_t* ssb, struct sockaddr_in* address) int r = uv_udp_try_send(&ssb->broadcast_sender, &buf, 1, (struct sockaddr*)&broadcast_addr); if (r < 0) { - printf("failed to send broadcast %d: %s\n", r, uv_strerror(r)); + tf_printf("failed to send broadcast %d: %s\n", r, uv_strerror(r)); } } @@ -2611,14 +2612,14 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port) { if (ssb->server.data) { - printf("Already listening.\n"); + tf_printf("Already listening.\n"); return; } ssb->server.data = ssb; if (uv_tcp_init(ssb->loop, &ssb->server) != 0) { - printf("uv_tcp_init failed\n"); + tf_printf("uv_tcp_init failed\n"); return; } @@ -2628,19 +2629,19 @@ void tf_ssb_server_open(tf_ssb_t* ssb, int port) addr.sin_addr.s_addr = INADDR_ANY; if (uv_tcp_bind(&ssb->server, (struct sockaddr*)&addr, 0) != 0) { - printf("uv_tcp_bind failed\n"); + tf_printf("uv_tcp_bind failed\n"); return; } int status = uv_listen((uv_stream_t*)&ssb->server, SOMAXCONN, _tf_ssb_on_connection); if (status != 0) { - printf("uv_listen failed: %s\n", uv_strerror(status)); + tf_printf("uv_listen failed: %s\n", uv_strerror(status)); /* TODO: cleanup */ return; } - printf("Starting broadcasts.\n"); + tf_printf("Starting broadcasts.\n"); uv_timer_start(&ssb->broadcast_timer, _tf_ssb_broadcast_timer, 2000, 2000); } @@ -2652,7 +2653,7 @@ void tf_ssb_server_close(tf_ssb_t* ssb) } uv_timer_stop(&ssb->broadcast_timer); - printf("Stopped broadcasts.\n"); + tf_printf("Stopped broadcasts.\n"); } bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size) @@ -2674,7 +2675,7 @@ static bool _tf_ssb_parse_broadcast(const char* in_broadcast, tf_ssb_broadcast_t } else if (strncmp(in_broadcast, "ws:", 3) == 0) { - printf("Unsupported broadcast: %s\n", in_broadcast); + tf_printf("Unsupported broadcast: %s\n", in_broadcast); } return false; } @@ -2688,7 +2689,7 @@ void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address) } else { - printf("Unable to parse: %s\n", address); + tf_printf("Unable to parse: %s\n", address); } } @@ -2749,7 +2750,7 @@ static void _tf_ssb_add_broadcast(tf_ssb_t* ssb, const tf_ssb_broadcast_t* broad char key[k_id_base64_len]; if (tf_ssb_id_bin_to_str(key, sizeof(key), broadcast->pub)) { - printf("Received new broadcast: host=%s, pub=%s.\n", broadcast->host, key); + tf_printf("Received new broadcast: host=%s, pub=%s.\n", broadcast->host, key); } } @@ -2850,13 +2851,13 @@ void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger) int result = uv_udp_bind(&ssb->broadcast_listener, (const struct sockaddr*)&addr, UV_UDP_REUSEADDR); if (result != 0) { - printf("bind: %s\n", uv_strerror(result)); + tf_printf("bind: %s\n", uv_strerror(result)); } result = uv_udp_recv_start(&ssb->broadcast_listener, _tf_ssb_on_broadcast_listener_alloc, _tf_ssb_on_broadcast_listener_recv); if (result != 0) { - printf("uv_udp_recv_start: %s\n", uv_strerror(result)); + tf_printf("uv_udp_recv_start: %s\n", uv_strerror(result)); } if (!linger) @@ -3354,7 +3355,7 @@ bool tf_ssb_verify_strip_and_store_message(tf_ssb_t* ssb, JSValue value) } else { - printf("failed to verify message\n"); + tf_printf("failed to verify message\n"); } return false; } diff --git a/src/ssb.connections.c b/src/ssb.connections.c index 7dc67f4a..d8bb30ca 100644 --- a/src/ssb.connections.c +++ b/src/ssb.connections.c @@ -1,5 +1,6 @@ #include "ssb.connections.h" +#include "log.h" #include "mem.h" #include "ssb.h" @@ -68,7 +69,7 @@ static bool _tf_ssb_connections_get_next_connection(tf_ssb_connections_t* connec } else { - printf("prepare: %s\n", sqlite3_errmsg(connections->db)); + tf_printf("prepare: %s\n", sqlite3_errmsg(connections->db)); } return result; } @@ -136,7 +137,7 @@ void tf_ssb_connections_store(tf_ssb_connections_t* connections, const char* hos int r = sqlite3_step(statement); if (r != SQLITE_DONE) { - printf("tf_ssb_connections_store: %d, %s.\n", r, sqlite3_errmsg(connections->db)); + tf_printf("tf_ssb_connections_store: %d, %s.\n", r, sqlite3_errmsg(connections->db)); } } sqlite3_finalize(statement); @@ -154,7 +155,7 @@ void tf_ssb_connections_set_attempted(tf_ssb_connections_t* connections, const c { if (sqlite3_step(statement) != SQLITE_DONE) { - printf("tf_ssb_connections_set_attempted: %s.\n", sqlite3_errmsg(connections->db)); + tf_printf("tf_ssb_connections_set_attempted: %s.\n", sqlite3_errmsg(connections->db)); } } sqlite3_finalize(statement); @@ -172,7 +173,7 @@ void tf_ssb_connections_set_succeeded(tf_ssb_connections_t* connections, const c { if (sqlite3_step(statement) != SQLITE_DONE) { - printf("tf_ssb_connections_set_succeeded: %s.\n", sqlite3_errmsg(connections->db)); + tf_printf("tf_ssb_connections_set_succeeded: %s.\n", sqlite3_errmsg(connections->db)); } } sqlite3_finalize(statement); diff --git a/src/ssb.db.c b/src/ssb.db.c index 92bd2d54..633c3dbe 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -1,5 +1,6 @@ #include "ssb.db.h" +#include "log.h" #include "mem.h" #include "ssb.h" #include "trace.h" @@ -20,7 +21,7 @@ static void _tf_ssb_db_exec(sqlite3* db, const char* statement) int result = sqlite3_exec(db, statement, NULL, NULL, &error); if (result != SQLITE_OK) { - printf("Error running '%s': %s.\n", statement, error); + tf_printf("Error running '%s': %s.\n", statement, error); abort(); } } @@ -38,14 +39,14 @@ static bool _tf_ssb_db_has_rows(sqlite3* db, const char* query) } if (result != SQLITE_DONE) { - printf("%s\n", sqlite3_errmsg(db)); + tf_printf("%s\n", sqlite3_errmsg(db)); abort(); } sqlite3_finalize(statement); } else { - printf("%s\n", sqlite3_errmsg(db)); + tf_printf("%s\n", sqlite3_errmsg(db)); abort(); } return found; @@ -123,19 +124,19 @@ void tf_ssb_db_init(tf_ssb_t* ssb) if (!populate_fts && /* HACK */ false) { - printf("Checking FTS5 integrity...\n"); + tf_printf("Checking FTS5 integrity...\n"); if (sqlite3_exec(db, "INSERT INTO messages_fts(messages_fts, rank) VALUES ('integrity-check', 0)", NULL, NULL, NULL) == SQLITE_CORRUPT_VTAB) { populate_fts = true; } - printf("Done.\n"); + tf_printf("Done.\n"); } if (populate_fts) { - printf("Populating full-text search...\n"); + tf_printf("Populating full-text search...\n"); _tf_ssb_db_exec(db, "INSERT INTO messages_fts (rowid, content) SELECT rowid, content FROM messages"); - printf("Done.\n"); + tf_printf("Done.\n"); } _tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGIN INSERT INTO messages_fts(rowid, content) VALUES (new.rowid, new.content); END"); @@ -149,14 +150,14 @@ void tf_ssb_db_init(tf_ssb_t* ssb) " ref TEXT, " " UNIQUE(message, ref)" ")"); - printf("Populating messages_refs...\n"); + tf_printf("Populating messages_refs...\n"); _tf_ssb_db_exec(db, "INSERT INTO messages_refs(message, ref) " "SELECT messages.id, j.value FROM messages, json_tree(messages.content) as j WHERE " "j.value LIKE '&%.sha256' OR " "j.value LIKE '%%%.sha256' OR " "j.value LIKE '@%.ed25519' " "ON CONFLICT DO NOTHING"); - printf("Done.\n"); + tf_printf("Done.\n"); } _tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS messages_ai_refs"); @@ -205,7 +206,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb) if (need_convert_timestamp_to_real) { - printf("Converting timestamp column from INTEGER to REAL.\n"); + tf_printf("Converting timestamp column from INTEGER to REAL.\n"); _tf_ssb_db_exec(db, "BEGIN TRANSACTION"); _tf_ssb_db_exec(db, "DROP INDEX IF EXISTS messages_author_timestamp_index"); _tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN timestamp_real REAL"); @@ -217,7 +218,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb) } if (need_add_sequence_before_author) { - printf("Adding sequence_before_author column.\n"); + tf_printf("Adding sequence_before_author column.\n"); _tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN sequence_before_author INTEGER"); } } @@ -291,7 +292,7 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, int r = sqlite3_step(statement); if (r != SQLITE_DONE) { - printf("%s\n", sqlite3_errmsg(db)); + tf_printf("%s\n", sqlite3_errmsg(db)); } stored = r == SQLITE_DONE && sqlite3_changes(db) != 0; if (stored) @@ -301,18 +302,18 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, } else { - printf("bind failed\n"); + tf_printf("bind failed\n"); } sqlite3_finalize(statement); } else { - printf("prepare failed: %s\n", sqlite3_errmsg(db)); + tf_printf("prepare failed: %s\n", sqlite3_errmsg(db)); } } else { - printf("Previous message doesn't exist.\n"); + tf_printf("Previous message doesn't exist.\n"); } if (last_row_id != -1) @@ -330,14 +331,14 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id, } if (r != SQLITE_DONE) { - printf("%s\n", sqlite3_errmsg(db)); + tf_printf("%s\n", sqlite3_errmsg(db)); } } sqlite3_finalize(statement); } else { - printf("prepare failed: %s\n", sqlite3_errmsg(db)); + tf_printf("prepare failed: %s\n", sqlite3_errmsg(db)); } } @@ -456,13 +457,13 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* } else { - printf("bind failed: %s\n", sqlite3_errmsg(db)); + tf_printf("bind failed: %s\n", sqlite3_errmsg(db)); } sqlite3_finalize(statement); } else { - printf("prepare failed: %s\n", sqlite3_errmsg(db)); + tf_printf("prepare failed: %s\n", sqlite3_errmsg(db)); } if (rows) @@ -470,7 +471,7 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char* tf_ssb_notify_blob_stored(ssb, id); if (!out_new) { - printf("blob stored %s %zd => %d\n", id, size, result); + tf_printf("blob stored %s %zd => %d\n", id, size, result); } } @@ -514,7 +515,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut } else { - printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb))); + tf_printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb))); } return found; } @@ -543,7 +544,7 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i } else { - printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb))); + tf_printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb))); } return found; } @@ -671,7 +672,7 @@ static int _tf_ssb_sqlite_authorizer(void* user_data, int action_code, const cha } if (result != SQLITE_OK) { - printf("Denying sqlite access to %d %s %s %s %s\n", action_code, arg0, arg1, arg2, arg3); + tf_printf("Denying sqlite access to %d %s %s %s %s\n", action_code, arg0, arg1, arg2, arg3); fflush(stdout); } return result; @@ -796,12 +797,12 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) bool delete_following = false; if (strcmp(author, previous_author)) { - printf("%s\n", author); + tf_printf("%s\n", author); } if (strcmp(author, previous_author) == 0 && sequence != previous_sequence + 1) { - printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence); + tf_printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence); delete_following = true; } else @@ -810,30 +811,30 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) { if (previous && strcmp(previous, previous_id)) { - printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous); + tf_printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous); } if (strcmp(id, actual_id)) { if (_tf_ssb_update_message_id(db, id, actual_id)) { - printf("updated %s to %s\n", id, actual_id); + tf_printf("updated %s to %s\n", id, actual_id); } else { - printf("failed to update %s to %s\n", id, actual_id); + tf_printf("failed to update %s to %s\n", id, actual_id); } } } else { - printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv); + tf_printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv); delete_following = true; } } if (delete_following) { - printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); + tf_printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); sqlite3_stmt* delete_statement = NULL; if (sqlite3_prepare(db, "DELETE FROM messages WHERE author = ? AND sequence >= ?", -1, &delete_statement, NULL) == SQLITE_OK) { @@ -842,7 +843,7 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author) { if (sqlite3_step(delete_statement) != SQLITE_DONE) { - printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); + tf_printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence); } } sqlite3_finalize(delete_statement); @@ -918,7 +919,7 @@ bool tf_ssb_db_identity_add(tf_ssb_t* ssb, const char* user, const char* public_ sqlite3_changes(db) != 0; if (!added) { - printf("Unable to add identity: %s.\n", sqlite3_errmsg(db)); + tf_printf("Unable to add identity: %s.\n", sqlite3_errmsg(db)); } } sqlite3_finalize(statement); @@ -1171,20 +1172,20 @@ static void _test_private(sqlite3* db, const uint8_t* private_key) uint8_t* key = out + 1; if (crypto_secretbox_open_easy(result, body, body_size, nonce, key) != -1) { - printf("%.*s\n", (int)body_size, result); + tf_printf("%.*s\n", (int)body_size, result); } } } } else { - printf("scalarmult failed\n"); + tf_printf("scalarmult failed\n"); } } } else { - printf("base64 failed\n"); + tf_printf("base64 failed\n"); } } sqlite3_finalize(statement); @@ -1199,14 +1200,14 @@ void tf_ssb_db_private(sqlite3* db) while (sqlite3_step(statement) == SQLITE_ROW) { uint8_t private_key[crypto_sign_SECRETKEYBYTES] = { 0 }; - printf("-> %s\n", sqlite3_column_text(statement, 0)); + tf_printf("-> %s\n", sqlite3_column_text(statement, 0)); int r = tf_base64_decode((const char*)sqlite3_column_text(statement, 1), sqlite3_column_bytes(statement, 1) - strlen(".ed25519"), private_key, sizeof(private_key)); if (r == sizeof(private_key)) { uint8_t key[crypto_sign_SECRETKEYBYTES] = { 0 }; if (crypto_sign_ed25519_sk_to_curve25519(key, private_key) != 0) { - printf("key convert failed\n"); + tf_printf("key convert failed\n"); } else { @@ -1298,7 +1299,7 @@ void tf_ssb_db_forget_stored_connection(tf_ssb_t* ssb, const char* address, int sqlite3_bind_text(statement, 3, pubkey, -1, NULL) != SQLITE_OK || sqlite3_step(statement) != SQLITE_DONE) { - printf("Delete stored connection: %s.\n", sqlite3_errmsg(db)); + tf_printf("Delete stored connection: %s.\n", sqlite3_errmsg(db)); } sqlite3_finalize(statement); } diff --git a/src/ssb.export.c b/src/ssb.export.c index d9986c19..cb514d45 100644 --- a/src/ssb.export.c +++ b/src/ssb.export.c @@ -1,5 +1,6 @@ #include "ssb.export.h" +#include "log.h" #include "mem.h" #include "ssb.db.h" #include "ssb.h" @@ -19,7 +20,7 @@ static void _write_file(const char* path, const void* blob, size_t size) } else { - printf("Failed to open %s for write: %s.\n", path, strerror(errno)); + tf_printf("Failed to open %s for write: %s.\n", path, strerror(errno)); } } @@ -31,7 +32,7 @@ static void _make_dir(const char* path) if (mkdir(path, 0755) && errno != EEXIST) #endif { - printf("Failed to create directory %s: %s.\n", path, strerror(errno)); + tf_printf("Failed to create directory %s: %s.\n", path, strerror(errno)); } } @@ -63,7 +64,7 @@ static void _tf_ssb_export_scandir(uv_fs_t* req) int r = uv_fs_unlink(tf_ssb_get_loop(export->ssb), &req, path, NULL); if (r) { - printf("Failed to unlink %s: %s.", path, uv_strerror(r)); + tf_printf("Failed to unlink %s: %s.", path, uv_strerror(r)); } uv_fs_req_cleanup(&req); tf_free(path); @@ -80,7 +81,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) char path[256] = { 0 }; if (sscanf(key, "/~%255[^/]/%255s", user, path) != 2) { - printf("Unable to export %s.\n", key); + tf_printf("Unable to export %s.\n", key); return; } @@ -106,7 +107,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) if (!*app_blob_id) { - printf("Did not find app blob ID for %s.\n", key); + tf_printf("Did not find app blob ID for %s.\n", key); return; } @@ -114,7 +115,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) size_t size = 0; if (!tf_ssb_db_blob_get(ssb, app_blob_id, &blob, &size)) { - printf("Did not find blob for %s: %s.\n", key, app_blob_id); + tf_printf("Did not find blob for %s: %s.\n", key, app_blob_id); return; } char file_path[1024]; @@ -191,7 +192,7 @@ void tf_ssb_export(tf_ssb_t* ssb, const char* key) int r = uv_fs_scandir(tf_ssb_get_loop(ssb), &export.req, file_path, 0, _tf_ssb_export_scandir); if (r) { - printf("Failed to scan directory %s: %s.", file_path, uv_strerror(r)); + tf_printf("Failed to scan directory %s: %s.", file_path, uv_strerror(r)); } while (!export.done) { diff --git a/src/ssb.import.c b/src/ssb.import.c index 8d8fcd39..9a1c730e 100644 --- a/src/ssb.import.c +++ b/src/ssb.import.c @@ -1,5 +1,6 @@ #include "ssb.import.h" +#include "log.h" #include "mem.h" #include "ssb.db.h" #include "ssb.h" @@ -98,7 +99,7 @@ static char* _tf_ssb_import_read_file(uv_loop_t* loop, const char* path, size_t* } else { - printf("Failed to read %s: %s.\n", path, uv_strerror(r)); + tf_printf("Failed to read %s: %s.\n", path, uv_strerror(r)); } uv_fs_req_cleanup(&read_req); @@ -106,12 +107,12 @@ static char* _tf_ssb_import_read_file(uv_loop_t* loop, const char* path, size_t* r = uv_fs_close(loop, &close_req, handle, NULL); if (r) { - printf("Failed to close %s: %s.\n", path, uv_strerror(r)); + tf_printf("Failed to close %s: %s.\n", path, uv_strerror(r)); } } else { - printf("Failed to open %s: %s.\n", path, uv_strerror(handle)); + tf_printf("Failed to open %s: %s.\n", path, uv_strerror(handle)); } uv_fs_req_cleanup(&req); return data; @@ -138,7 +139,7 @@ static void _tf_ssb_import_recursive_add_files(tf_ssb_t* ssb, uv_loop_t* loop, J bool is_new = false; if (tf_ssb_db_blob_store(ssb, (const uint8_t*)blob, size, id, sizeof(id), &is_new) && is_new) { - printf("Stored %s as %s.\n", full_path, id); + tf_printf("Stored %s as %s.\n", full_path, id); } JS_SetPropertyStr(context, files, full_path + strlen(root) + 1, JS_NewString(context, id)); @@ -149,7 +150,7 @@ static void _tf_ssb_import_recursive_add_files(tf_ssb_t* ssb, uv_loop_t* loop, J } else { - printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); + tf_printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); } uv_fs_req_cleanup(&req); } @@ -203,7 +204,7 @@ static void _tf_ssb_import_app_json(tf_ssb_t* ssb, uv_loop_t* loop, JSContext* c if (_tf_ssb_register_app(ssb, user, app, id)) { - printf("Registered %s path:%s as %s.\n", user, app, id); + tf_printf("Registered %s path:%s as %s.\n", user, app, id); _tf_ssb_import_add_app(ssb, user, app); } } @@ -218,7 +219,7 @@ static void _tf_ssb_import_app_json(tf_ssb_t* ssb, uv_loop_t* loop, JSContext* c } else { - printf("Failed to open %s: %s.\n", path, uv_strerror(r)); + tf_printf("Failed to open %s: %s.\n", path, uv_strerror(r)); } uv_fs_req_cleanup(&req); } @@ -245,7 +246,7 @@ void tf_ssb_import(tf_ssb_t* ssb, const char* user, const char* path) } else { - printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); + tf_printf("Failed to scan directory %s: %s.", path, uv_strerror(r)); } uv_fs_req_cleanup(&req); } diff --git a/src/ssb.js.c b/src/ssb.js.c index 1b1b1669..3dac891c 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -1,6 +1,7 @@ #include "ssb.js.h" #include "database.js.h" +#include "log.h" #include "mem.h" #include "ssb.db.h" #include "ssb.h" @@ -699,7 +700,7 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar if (JS_IsString(args)) { const char* address_str = JS_ToCString(context, args); - printf("Connecting to %s\n", address_str); + tf_printf("Connecting to %s\n", address_str); tf_ssb_connect_str(ssb, address_str); JS_FreeCString(context, address_str); } @@ -714,14 +715,14 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar const char* pubkey_str = JS_ToCString(context, pubkey); if (pubkey_str) { - printf("Connecting to %s:%d\n", address_str, port_int); + tf_printf("Connecting to %s:%d\n", address_str, port_int); uint8_t pubkey_bin[k_id_bin_len]; tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str); tf_ssb_connect(ssb, address_str, port_int, pubkey_bin); } else { - printf("Not connecting to null.\n"); + tf_printf("Not connecting to null.\n"); } JS_FreeCString(context, pubkey_str); JS_FreeCString(context, address_str); @@ -858,7 +859,7 @@ void tf_ssb_run_file(JSContext* context, const char* file_name) FILE* file = fopen(file_name, "rb"); if (!file) { - printf("Unable to open %s: %s.", file_name, strerror(errno)); + tf_printf("Unable to open %s: %s.", file_name, strerror(errno)); return; } @@ -874,7 +875,7 @@ void tf_ssb_run_file(JSContext* context, const char* file_name) JSValue result = JS_Eval(context, source, file_size, file_name, 0); if (tf_util_report_error(context, result)) { - printf("Error running %s.\n", file_name); + tf_printf("Error running %s.\n", file_name); } JSRuntime* runtime = JS_GetRuntime(context); diff --git a/src/ssb.tests.c b/src/ssb.tests.c index e4780e0b..8bdd0f87 100644 --- a/src/ssb.tests.c +++ b/src/ssb.tests.c @@ -1,5 +1,6 @@ #include "ssb.h" +#include "log.h" #include "mem.h" #include "ssb.db.h" #include "ssb.js.h" @@ -17,7 +18,7 @@ void tf_ssb_test_id_conversion(const tf_test_options_t* options) { - printf("Testing id conversion.\n"); + tf_printf("Testing id conversion.\n"); uint8_t bin[k_id_bin_len] = { 0 }; char str[k_id_base64_len] = { 0 }; const char* k_id = "@bzRTe6hgOII2yZ1keGGoNoQgostjQc830trHc453crY=.ed25519"; @@ -59,20 +60,20 @@ static void _ssb_test_connections_changed(tf_ssb_t* ssb, tf_ssb_change_t change, if (ssb == test->ssb0) { - printf("callback0 change=%d connection=%p\n", change, connection); + tf_printf("callback0 change=%d connection=%p\n", change, connection); test->connection_count0 = count; } else if (ssb == test->ssb1) { - printf("callback1 change=%d connection=%p\n", change, connection); + tf_printf("callback1 change=%d connection=%p\n", change, connection); test->connection_count1 = count; } else if (ssb == test->ssb2) { - printf("callback2 change=%d connection=%p\n", change, connection); + tf_printf("callback2 change=%d connection=%p\n", change, connection); test->connection_count2 = count; } - printf("conns = %d %d %d\n", test->connection_count0, test->connection_count1, test->connection_count2); + tf_printf("conns = %d %d %d\n", test->connection_count0, test->connection_count1, test->connection_count2); } typedef struct _count_messages_t @@ -126,7 +127,7 @@ static void _ssb_test_idle(uv_idle_t* idle) void tf_ssb_test_ssb(const tf_test_options_t* options) { - printf("Testing SSB.\n"); + tf_printf("Testing SSB.\n"); uv_loop_t loop = { 0 }; uv_loop_init(&loop); @@ -162,7 +163,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) assert(b); b = tf_ssb_whoami(ssb1, id1, sizeof(id1)); assert(b); - printf("ID %s and %s\n", id0, id1); + tf_printf("ID %s and %s\n", id0, id1); char blob_id[k_id_base64_len] = { 0 }; const char* k_blob = "Hello, blob!"; @@ -199,7 +200,7 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) tf_ssb_id_str_to_bin(id0bin, id0); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin); - printf("Waiting for connection.\n"); + tf_printf("Waiting for connection.\n"); while (test.connection_count0 != 1 || test.connection_count1 != 1) { @@ -207,13 +208,13 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) } tf_ssb_server_close(ssb0); - printf("Waiting for messages.\n"); + tf_printf("Waiting for messages.\n"); while (_ssb_test_count_messages(ssb1) < 3) { uv_run(&loop, UV_RUN_ONCE); } - printf("Waiting for blob.\n"); + tf_printf("Waiting for blob.\n"); while (!tf_ssb_db_blob_get(ssb1, blob_id, NULL, NULL)) { uv_run(&loop, UV_RUN_ONCE); @@ -223,12 +224,12 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) size_t s1 = 0; b = tf_ssb_db_blob_get(ssb1, blob_id, &b1, &s1); assert(b); - printf("s1 = %zd sl = %zd\n", s1, strlen(k_blob)); + tf_printf("s1 = %zd sl = %zd\n", s1, strlen(k_blob)); assert(s1 == strlen(k_blob)); assert(memcmp(b1, k_blob, strlen(k_blob)) == 0); tf_free(b1); - printf("Waiting for message to self.\n"); + tf_printf("Waiting for message to self.\n"); int count0 = 0; int count1 = 0; tf_ssb_add_message_added_callback(ssb0, _message_added, NULL, &count0); @@ -240,13 +241,13 @@ void tf_ssb_test_ssb(const tf_test_options_t* options) } tf_ssb_remove_message_added_callback(ssb0, _message_added, &count0); - printf("Waiting for message from other.\n"); + tf_printf("Waiting for message from other.\n"); while (count1 == 0) { uv_run(&loop, UV_RUN_ONCE); } tf_ssb_remove_message_added_callback(ssb1, _message_added, &count1); - printf("done\n"); + tf_printf("done\n"); tf_ssb_send_close(ssb1); @@ -285,12 +286,12 @@ static void _broadcasts_changed(tf_ssb_t* ssb, void* user_data) } *count = 0; tf_ssb_visit_broadcasts(ssb, _broadcasts_visit, count); - printf("BROADCASTS %d %d %d\n", test->broadcast_count0, test->broadcast_count1, test->broadcast_count2); + tf_printf("BROADCASTS %d %d %d\n", test->broadcast_count0, test->broadcast_count1, test->broadcast_count2); } void tf_ssb_test_rooms(const tf_test_options_t* options) { - printf("Testing Rooms.\n"); + tf_printf("Testing Rooms.\n"); uv_loop_t loop = { 0 }; uv_loop_init(&loop); @@ -343,7 +344,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) assert(b); b = tf_ssb_whoami(ssb2, id2, sizeof(id2)); assert(b); - printf("ID %s, %s, %s\n", id0, id1, id2); + tf_printf("ID %s, %s, %s\n", id0, id1, id2); tf_ssb_server_open(ssb0, 12347); @@ -352,7 +353,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin); tf_ssb_connect(ssb2, "127.0.0.1", 12347, id0bin); - printf("Waiting for connection.\n"); + tf_printf("Waiting for connection.\n"); while (test.connection_count0 != 2 || test.connection_count1 != 1 || test.connection_count2 != 1) @@ -361,7 +362,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) } tf_ssb_server_close(ssb0); - printf("Waiting for broadcasts.\n"); + tf_printf("Waiting for broadcasts.\n"); while (test.broadcast_count1 != 1 || test.broadcast_count2 != 1) { @@ -400,9 +401,9 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) JS_FreeValue(context, message); tf_ssb_connection_t* tun0 = tf_ssb_connection_tunnel_create(ssb1, id0, tunnel_request_number, id2); - printf("tun0 = %p\n", tun0); + tf_printf("tun0 = %p\n", tun0); - printf("Done.\n"); + tf_printf("Done.\n"); while (test.connection_count0 != 2 || test.connection_count1 != 2 || @@ -411,7 +412,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) uv_run(&loop, UV_RUN_ONCE); } - printf("Done.\n"); + tf_printf("Done.\n"); uv_run(&loop, UV_RUN_NOWAIT); @@ -444,7 +445,7 @@ void tf_ssb_test_rooms(const tf_test_options_t* options) void tf_ssb_test_following(const tf_test_options_t* options) { - printf("Testing following.\n"); + tf_printf("Testing following.\n"); uv_loop_t loop = { 0 }; uv_loop_init(&loop); @@ -485,14 +486,14 @@ void tf_ssb_test_following(const tf_test_options_t* options) #define DUMP(id, depth) \ do \ { \ - printf("following %d:\n", depth); \ + tf_printf("following %d:\n", depth); \ const char** tf_ssb_get_following_deep(tf_ssb_t* ssb_param, const char** ids, int depth_param); \ const char** f = tf_ssb_get_following_deep(ssb0, (const char*[]) { id, NULL }, depth); \ for (const char** p = f; p && *p; p++) \ { \ - printf("* %s\n", *p); \ + tf_printf("* %s\n", *p); \ } \ - printf("\n"); \ + tf_printf("\n"); \ tf_free(f); \ } \ while (0) @@ -527,7 +528,7 @@ void tf_ssb_test_following(const tf_test_options_t* options) void tf_ssb_test_bench(const tf_test_options_t* options) { - printf("Testing following.\n"); + tf_printf("Testing following.\n"); uv_loop_t loop = { 0 }; uv_loop_init(&loop); @@ -547,7 +548,7 @@ void tf_ssb_test_bench(const tf_test_options_t* options) tf_ssb_append_post(ssb0, "Hello, world!"); } clock_gettime(CLOCK_REALTIME, &end_time); - printf("insert = %f seconds\n", (float)(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9f); + tf_printf("insert = %f seconds\n", (float)(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9f); tf_ssb_t* ssb1 = tf_ssb_create(&loop, NULL, ":memory:"); tf_ssb_generate_keys(ssb1); @@ -568,15 +569,15 @@ void tf_ssb_test_bench(const tf_test_options_t* options) tf_ssb_server_open(ssb0, 12347); tf_ssb_connect(ssb1, "127.0.0.1", 12347, id0bin); - printf("Waiting for messages.\n"); + tf_printf("Waiting for messages.\n"); clock_gettime(CLOCK_REALTIME, &start_time); while (_ssb_test_count_messages(ssb1) < k_messages) { uv_run(&loop, UV_RUN_ONCE); } clock_gettime(CLOCK_REALTIME, &end_time); - printf("Done.\n"); - printf("replicate = %f seconds\n", (float)(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9f); + tf_printf("Done.\n"); + tf_printf("replicate = %f seconds\n", (float)(end_time.tv_sec - start_time.tv_sec) + (end_time.tv_nsec - start_time.tv_nsec) / 1e9f); tf_ssb_send_close(ssb1); tf_ssb_server_close(ssb0); diff --git a/src/task.c b/src/task.c index e306a75a..9d90e446 100644 --- a/src/task.c +++ b/src/task.c @@ -3,6 +3,7 @@ #include "bcrypt.js.h" #include "database.js.h" #include "file.js.h" +#include "log.h" #include "mem.h" #include "packetstream.h" #include "serialize.h" @@ -338,7 +339,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName) tf_trace_begin(task->_trace, "tf_task_execute"); const char* source = _task_loadFile(fileName); - printf("Running script %s\n", fileName); + tf_printf("Running script %s\n", fileName); if (!*task->_scriptName) { strncpy(task->_scriptName, fileName, sizeof(task->_scriptName) - 1); @@ -364,7 +365,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName) JSValue result = JS_Eval(task->_context, source, strlen(source), fileName, JS_EVAL_TYPE_MODULE); if (tf_util_report_error(task->_context, result)) { - printf("Reported an error.\n"); + tf_printf("Reported an error.\n"); } if (!JS_IsError(task->_context, result) && !JS_IsException(result)) { @@ -375,7 +376,7 @@ int tf_task_execute(tf_task_t* task, const char* fileName) } else { - printf("Failed to load file: %s.\n", fileName); + tf_printf("Failed to load file: %s.\n", fileName); } tf_trace_end(task->_trace); return executed; @@ -494,7 +495,7 @@ JSValue _task_invokeExport_internal(tf_taskstub_t* from, tf_task_t* to, exportid } else { - printf("%s: That's not an export we have (exportId=%d, exports=%d)\n", to->_scriptName, exportId, to->_export_count); + tf_printf("%s: That's not an export we have (exportId=%d, exports=%d)\n", to->_scriptName, exportId, to->_export_count); } tf_packetstream_send(tf_taskstub_get_stream(from), kReleaseImport, (void*)&exportId, sizeof(exportId)); return result; @@ -600,7 +601,7 @@ void tf_task_send_promise_message(tf_task_t* from, tf_taskstub_t* to, tf_task_me } else { - printf("Sending to a NULL task.\n"); + tf_printf("Sending to a NULL task.\n"); } } @@ -1240,7 +1241,7 @@ void tf_task_resolve_promise(tf_task_t* task, promiseid_t promise, JSValue value } else { - printf("Didn't find promise %d to resolve.\n", promise); + tf_printf("Didn't find promise %d to resolve.\n", promise); abort(); } } @@ -1270,7 +1271,7 @@ void tf_task_reject_promise(tf_task_t* task, promiseid_t promise, JSValue value) } else { - printf("Didn't find promise %d to reject.\n", promise); + tf_printf("Didn't find promise %d to reject.\n", promise); abort(); } } @@ -1591,7 +1592,7 @@ void tf_task_activate(tf_task_t* task) } else { - printf("Assignment missing '=': %s.\n", assignment); + tf_printf("Assignment missing '=': %s.\n", assignment); exit(1); } } diff --git a/src/tests.c b/src/tests.c index 9722522e..98e7b4d7 100644 --- a/src/tests.c +++ b/src/tests.c @@ -1,5 +1,6 @@ #include "tests.h" +#include "log.h" #include "mem.h" #include "ssb.tests.h" @@ -23,7 +24,7 @@ static void _test_nop(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); (void)result; assert(WIFEXITED(result)); @@ -54,7 +55,7 @@ static void _test_child(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); (void)result; assert(WIFEXITED(result)); @@ -96,7 +97,7 @@ static void _test_promise(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); (void)result; assert(WIFEXITED(result)); @@ -142,7 +143,7 @@ static void _test_promise_remote_throw(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); (void)result; assert(WIFEXITED(result)); @@ -190,7 +191,7 @@ static void _test_promise_remote_reject(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); (void)result; assert(WIFEXITED(result)); @@ -236,9 +237,9 @@ static void _test_database(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: --db-path=out/testdb.sqlite -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -257,9 +258,9 @@ static void _test_this(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -290,9 +291,9 @@ static void _test_await(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -326,16 +327,16 @@ static void _test_import(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/bad.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -356,9 +357,9 @@ static void _test_exit(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -376,9 +377,9 @@ static void _test_icu(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -428,9 +429,9 @@ static void _test_uint8array(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -517,9 +518,9 @@ static void _test_socket(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -576,9 +577,9 @@ static void _test_file(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -608,9 +609,9 @@ static void _test_sign(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -633,9 +634,9 @@ static void _test_b64(const tf_test_options_t* options) char command[256]; snprintf(command, sizeof(command), "%s run --ssb-port=0 --db-path=:memory: -s out/test.js", options->exe_path); - printf("%s\n", command); + tf_printf("%s\n", command); int result = system(command); - printf("returned %d\n", WEXITSTATUS(result)); + tf_printf("returned %d\n", WEXITSTATUS(result)); assert(WIFEXITED(result)); assert(WEXITSTATUS(result) == 0); @@ -667,9 +668,9 @@ static void _tf_test_run(const tf_test_options_t* options, const char* name, voi #define MAGENTA "\e[1;35m" #define CYAN "\e[1;36m" #define RESET "\e[0m" - printf(CYAN "== running test " MAGENTA "%s" CYAN " ==\n" RESET, name); + tf_printf(CYAN "== running test " MAGENTA "%s" CYAN " ==\n" RESET, name); test(options); - printf("[" GREEN "pass" RESET "] %s\n", name); + tf_printf("[" GREEN "pass" RESET "] %s\n", name); #undef GREEN #undef MAGENTA #undef CYAN @@ -700,5 +701,5 @@ void tf_tests(const tf_test_options_t* options) _tf_test_run(options, "b64", _test_b64); _tf_test_run(options, "rooms", tf_ssb_test_rooms); _tf_test_run(options, "bench", tf_ssb_test_bench); - printf("Tests completed.\n"); + tf_printf("Tests completed.\n"); } diff --git a/src/util.js.c b/src/util.js.c index f9e3ee79..3eb5bca0 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -1,5 +1,6 @@ #include "util.js.h" +#include "log.h" #include "mem.h" #include "task.h" #include "trace.h" @@ -136,23 +137,23 @@ JSValue _util_print(JSContext* context, JSValueConst this_val, int argc, JSValue tf_task_t* task = JS_GetContextOpaque(context); if (task) { - printf("Task[%p:%s]>", task, tf_task_get_name(task)); + tf_printf("Task[%p:%s]>", task, tf_task_get_name(task)); tf_task_print(task, argc, argv); } for (int i = 0; i < argc; ++i) { if (JS_IsNull(argv[i])) { - printf(" null"); + tf_printf(" null"); } else { const char* value = JS_ToCString(context, argv[i]); - printf(" %s", value); + tf_printf(" %s", value); JS_FreeCString(context, value); } } - printf("\n"); + tf_printf("\n"); return JS_NULL; } @@ -162,14 +163,14 @@ bool tf_util_report_error(JSContext* context, JSValue value) if (JS_IsError(context, value)) { const char* string = JS_ToCString(context, value); - printf("ERROR: %s\n", string); + tf_printf("ERROR: %s\n", string); JS_FreeCString(context, string); JSValue stack = JS_GetPropertyStr(context, value, "stack"); if (!JS_IsUndefined(stack)) { const char* stack_str = JS_ToCString(context, stack); - printf("%s\n", stack_str); + tf_printf("%s\n", stack_str); JS_FreeCString(context, stack_str); } JS_FreeValue(context, stack);