diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index c4ea79b08d..6d8f5df457 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -53,7 +53,7 @@ static void test_basic(void) ck_assert(mem != nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); logger_callback_log(logger, print_debug_logger, nullptr, nullptr); // Attempt to create a new TCP_Server instance. @@ -312,7 +312,7 @@ static void test_some(void) ck_assert(mem != nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -506,7 +506,7 @@ static void test_client(void) const Memory *mem = os_memory(); ck_assert(mem != nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -643,7 +643,7 @@ static void test_client_invalid(void) ck_assert(mem != nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -721,7 +721,7 @@ static void test_tcp_connection(void) ck_assert(mem != nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); tcp_data_callback_called = 0; uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -834,7 +834,7 @@ static void test_tcp_connection2(void) ck_assert(mem != nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); - Logger *logger = logger_new(); + Logger *logger = logger_new(mem); tcp_oobdata_callback_called = 0; tcp_data_callback_called = 0; diff --git a/auto_tests/announce_test.c b/auto_tests/announce_test.c index c4f080de4e..ef93095b1b 100644 --- a/auto_tests/announce_test.c +++ b/auto_tests/announce_test.c @@ -57,7 +57,7 @@ static void test_store_data(void) const Memory *mem = os_memory(); ck_assert(mem != nullptr); - Logger *log = logger_new(); + Logger *log = logger_new(mem); ck_assert(log != nullptr); logger_callback_log(log, print_debug_logger, nullptr, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); diff --git a/auto_tests/forwarding_test.c b/auto_tests/forwarding_test.c index 75e330a73e..3ba85ed151 100644 --- a/auto_tests/forwarding_test.c +++ b/auto_tests/forwarding_test.c @@ -112,7 +112,7 @@ static Forwarding_Subtox *new_forwarding_subtox(const Memory *mem, bool no_udp, Forwarding_Subtox *subtox = (Forwarding_Subtox *)calloc(1, sizeof(Forwarding_Subtox)); ck_assert(subtox != nullptr); - subtox->log = logger_new(); + subtox->log = logger_new(mem); ck_assert(subtox->log != nullptr); logger_callback_log(subtox->log, print_debug_logger, nullptr, index); subtox->mono_time = mono_time_new(mem, nullptr, nullptr); diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 3a81df41b8..7a323066ba 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c @@ -228,9 +228,9 @@ static void test_basic(void) const Random *rng = os_random(); ck_assert(rng != nullptr); - Logger *log1 = logger_new(); + Logger *log1 = logger_new(mem); logger_callback_log(log1, print_debug_logger, nullptr, &index[0]); - Logger *log2 = logger_new(); + Logger *log2 = logger_new(mem); logger_callback_log(log2, print_debug_logger, nullptr, &index[1]); Mono_Time *mono_time1 = mono_time_new(mem, nullptr, nullptr); @@ -329,7 +329,7 @@ static void test_basic(void) CRYPTO_PUBLIC_KEY_SIZE) != 0); c_sleep(1000); - Logger *log3 = logger_new(); + Logger *log3 = logger_new(mem); logger_callback_log(log3, print_debug_logger, nullptr, &index[2]); Mono_Time *mono_time3 = mono_time_new(mem, nullptr, nullptr); @@ -412,7 +412,7 @@ static Onions *new_onions(const Memory *mem, const Random *rng, uint16_t port, u return nullptr; } - on->log = logger_new(); + on->log = logger_new(mem); if (!on->log) { free(on); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 8a15fb5e00..3b22ad688c 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -144,16 +144,16 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, ipv6enabled); - Logger *logger = logger_new(); + const Random *rng = os_random(); + const Network *ns = os_network(); + const Memory *mem = os_memory(); + + Logger *logger = logger_new(mem); if (MIN_LOGGER_LEVEL <= LOGGER_LEVEL_DEBUG) { logger_callback_log(logger, print_log, nullptr, nullptr); } - const Random *rng = os_random(); - const Network *ns = os_network(); - const Memory *mem = os_memory(); - Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); const uint16_t start_port = PORT; const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 60e14c08f5..e29afe5c2b 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -283,16 +283,17 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, enable_ipv6); - Logger *logger = logger_new(); + const Memory *mem = os_memory(); + const Random *rng = os_random(); + const Network *ns = os_network(); + + Logger *logger = logger_new(mem); if (MIN_LOGGER_LEVEL <= LOGGER_LEVEL_DEBUG) { logger_callback_log(logger, toxcore_logger_callback, nullptr, nullptr); } const uint16_t end_port = start_port + (TOX_PORTRANGE_TO - TOX_PORTRANGE_FROM); - const Memory *mem = os_memory(); - const Random *rng = os_random(); - const Network *ns = os_network(); Networking_Core *net = new_networking_ex(logger, mem, ns, &ip, start_port, end_port, nullptr); if (net == nullptr) { diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 916c6221b2..1534d7a674 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -121,6 +121,7 @@ cc_library( deps = [ ":attributes", ":ccompat", + ":mem", ], ) diff --git a/toxcore/DHT_fuzz_test.cc b/toxcore/DHT_fuzz_test.cc index 868aedecf2..5b1b63503f 100644 --- a/toxcore/DHT_fuzz_test.cc +++ b/toxcore/DHT_fuzz_test.cc @@ -31,7 +31,8 @@ void TestUnpackNodes(Fuzz_Data &input) const int packed_count = unpack_nodes( nodes, node_count, &processed_data_len, input.data(), input.size(), tcp_enabled); if (packed_count > 0) { - Logger *logger = logger_new(); + const Memory *mem = os_memory(); + Logger *logger = logger_new(mem); std::vector packed(packed_count * PACKED_NODE_SIZE_IP6); const int packed_size = pack_nodes(logger, packed.data(), packed.size(), nodes, packed_count); diff --git a/toxcore/DHT_test.cc b/toxcore/DHT_test.cc index 1028fc7f18..45f886e3d8 100644 --- a/toxcore/DHT_test.cc +++ b/toxcore/DHT_test.cc @@ -334,7 +334,7 @@ TEST(AnnounceNodes, SetAndTest) Test_Memory mem; Test_Network ns; - Logger *log = logger_new(); + Logger *log = logger_new(mem); ASSERT_NE(log, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); ASSERT_NE(mono_time, nullptr); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 7c9730bb63..d2a84490b8 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -3482,7 +3482,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * return nullptr; } - m->log = logger_new(); + m->log = logger_new(mem); if (m->log == nullptr) { friendreq_kill(m->fr); diff --git a/toxcore/forwarding_fuzz_test.cc b/toxcore/forwarding_fuzz_test.cc index da772a197a..4b8521ab07 100644 --- a/toxcore/forwarding_fuzz_test.cc +++ b/toxcore/forwarding_fuzz_test.cc @@ -46,7 +46,10 @@ void TestSendForwardRequest(Fuzz_Data &input) // rest of the fuzz data is input for malloc and network Fuzz_System sys(input); - Ptr logger(logger_new(), logger_kill); + Ptr logger(logger_new(sys.mem.get()), logger_kill); + if (logger == nullptr) { + return; + } Ptr net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip, ipp.port, ipp.port + 100, nullptr), @@ -72,7 +75,10 @@ void TestForwardReply(Fuzz_Data &input) // rest of the fuzz data is input for malloc and network Fuzz_System sys(input); - Ptr logger(logger_new(), logger_kill); + Ptr logger(logger_new(sys.mem.get()), logger_kill); + if (logger == nullptr) { + return; + } Ptr net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip, ipp.port, ipp.port + 100, nullptr), diff --git a/toxcore/group_announce_fuzz_test.cc b/toxcore/group_announce_fuzz_test.cc index eb0dfb6290..9f5655ac15 100644 --- a/toxcore/group_announce_fuzz_test.cc +++ b/toxcore/group_announce_fuzz_test.cc @@ -19,7 +19,8 @@ void TestUnpackAnnouncesList(Fuzz_Data &input) // TODO(iphydf): How do we know the packed size? CONSUME1_OR_RETURN(const uint16_t, packed_size, input); - Logger *logger = logger_new(); + Test_Memory mem; + Logger *logger = logger_new(mem); if (gca_unpack_announces_list(logger, input.data(), input.size(), announces.data(), max_count) != -1) { // Always allocate at least something to avoid passing nullptr to functions below. @@ -38,7 +39,8 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input) // TODO(iphydf): How do we know the packed size? CONSUME1_OR_RETURN(const uint16_t, packed_size, input); - Logger *logger = logger_new(); + Test_Memory mem; + Logger *logger = logger_new(mem); if (gca_unpack_public_announce(logger, input.data(), input.size(), &public_announce) != -1) { // Always allocate at least something to avoid passing nullptr to functions below. std::vector packed(packed_size + 1); @@ -50,7 +52,7 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input) void TestDoGca(Fuzz_Data &input) { Test_Memory mem; - std::unique_ptr logger(logger_new(), logger_kill); + std::unique_ptr logger(logger_new(mem), logger_kill); uint64_t clock = 1; std::unique_ptr> mono_time( diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index 2918dc76c6..f1027f3437 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -118,11 +118,12 @@ TEST_F(Announces, AnnouncesGetAndCleanup) struct AnnouncesPack : ::testing::Test { protected: std::vector announces_; + Test_Memory mem_; Logger *logger_ = nullptr; void SetUp() override { - logger_ = logger_new(); + logger_ = logger_new(mem_); ASSERT_NE(logger_, nullptr); // Add an announce without TCP relay. diff --git a/toxcore/group_moderation_test.cc b/toxcore/group_moderation_test.cc index ea72023e93..89555b9a1e 100644 --- a/toxcore/group_moderation_test.cc +++ b/toxcore/group_moderation_test.cc @@ -191,9 +191,9 @@ struct SanctionsListMod : ::testing::Test { protected: Extended_Public_Key pk; Extended_Secret_Key sk; - Logger *log = logger_new(); Test_Random rng; Test_Memory mem; + Logger *log = logger_new(mem); Moderation mod{mem}; Mod_Sanction sanctions[2] = {}; diff --git a/toxcore/logger.c b/toxcore/logger.c index 67fa52330b..2088ca671e 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c @@ -15,67 +15,40 @@ #include "attributes.h" #include "ccompat.h" +#include "mem.h" struct Logger { + const Memory *mem; + logger_cb *callback; void *context; void *userdata; }; -#ifndef NDEBUG -static const char *logger_level_name(Logger_Level level) -{ - switch (level) { - case LOGGER_LEVEL_TRACE: - return "TRACE"; - - case LOGGER_LEVEL_DEBUG: - return "DEBUG"; - - case LOGGER_LEVEL_INFO: - return "INFO"; - - case LOGGER_LEVEL_WARNING: - return "WARNING"; - - case LOGGER_LEVEL_ERROR: - return "ERROR"; - } - - return ""; -} -#endif /* NDEBUG */ - -non_null(1, 3, 5, 6) nullable(7) -static void logger_stderr_handler(void *context, Logger_Level level, const char *file, int line, const char *func, - const char *message, void *userdata) -{ -#ifndef NDEBUG - // GL stands for "global logger". - fprintf(stderr, "[GL] %s %s:%d(%s): %s\n", logger_level_name(level), file, line, func, message); - fprintf(stderr, "Default stderr logger triggered; aborting program\n"); - abort(); -#endif /* NDEBUG */ -} - -static const Logger logger_stderr = { - logger_stderr_handler, - nullptr, - nullptr, -}; - /* * Public Functions */ -Logger *logger_new(void) +Logger *logger_new(const Memory *mem) { - return (Logger *)calloc(1, sizeof(Logger)); + Logger *log = (Logger *)mem_alloc(mem, sizeof(Logger)); + + if (log == nullptr) { + return nullptr; + } + + log->mem = mem; + + return log; } void logger_kill(Logger *log) { - free(log); + if (log == nullptr) { + return; + } + + mem_delete(log->mem, log); } void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata) @@ -89,7 +62,7 @@ void logger_write(const Logger *log, Logger_Level level, const char *file, int l const char *format, ...) { if (log == nullptr) { - log = &logger_stderr; + return; } if (log->callback == nullptr) { diff --git a/toxcore/logger.h b/toxcore/logger.h index 830db883aa..44d0389942 100644 --- a/toxcore/logger.h +++ b/toxcore/logger.h @@ -12,6 +12,7 @@ #include #include "attributes.h" +#include "mem.h" #ifdef __cplusplus extern "C" { @@ -38,7 +39,7 @@ typedef void logger_cb(void *context, Logger_Level level, const char *file, int /** * Creates a new logger with logging disabled (callback is NULL) by default. */ -Logger *logger_new(void); +Logger *logger_new(const Memory *mem); /** * Frees all resources associated with the logger.