Skip to content

Commit

Permalink
cleanup: Allocate logger using tox memory allocator.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Nov 12, 2024
1 parent 41fb183 commit 9463003
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 78 deletions.
12 changes: 6 additions & 6 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/announce_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/forwarding_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ cc_library(
deps = [
":attributes",
":ccompat",
":mem",
],
)

Expand Down
3 changes: 2 additions & 1 deletion toxcore/DHT_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> packed(packed_count * PACKED_NODE_SIZE_IP6);
const int packed_size
= pack_nodes(logger, packed.data(), packed.size(), nodes, packed_count);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/DHT_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions toxcore/forwarding_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(logger_new(), logger_kill);
Ptr<Logger> logger(logger_new(sys.mem.get()), logger_kill);
if (logger == nullptr) {
return;
}

Ptr<Networking_Core> net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip,
ipp.port, ipp.port + 100, nullptr),
Expand All @@ -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(logger_new(), logger_kill);
Ptr<Logger> logger(logger_new(sys.mem.get()), logger_kill);
if (logger == nullptr) {
return;
}

Ptr<Networking_Core> net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip,
ipp.port, ipp.port + 100, nullptr),
Expand Down
8 changes: 5 additions & 3 deletions toxcore/group_announce_fuzz_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<uint8_t> packed(packed_size + 1);
Expand All @@ -50,7 +52,7 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input)
void TestDoGca(Fuzz_Data &input)
{
Test_Memory mem;
std::unique_ptr<Logger, void (*)(Logger *)> logger(logger_new(), logger_kill);
std::unique_ptr<Logger, void (*)(Logger *)> logger(logger_new(mem), logger_kill);

uint64_t clock = 1;
std::unique_ptr<Mono_Time, std::function<void(Mono_Time *)>> mono_time(
Expand Down
3 changes: 2 additions & 1 deletion toxcore/group_announce_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ TEST_F(Announces, AnnouncesGetAndCleanup)
struct AnnouncesPack : ::testing::Test {
protected:
std::vector<GC_Announce> 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.
Expand Down
2 changes: 1 addition & 1 deletion toxcore/group_moderation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand Down
65 changes: 19 additions & 46 deletions toxcore/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<unknown>";
}
#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)
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion toxcore/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <stdint.h>

#include "attributes.h"
#include "mem.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -38,7 +39,8 @@ 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);
non_null()
Logger *logger_new(const Memory *mem);

/**
* Frees all resources associated with the logger.
Expand Down

0 comments on commit 9463003

Please sign in to comment.