Skip to content

Commit

Permalink
Disallow stderr logger by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Mar 17, 2018
1 parent a2496af commit 9706d9a
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ if(NOT USE_IPV6)
add_definitions(-DUSE_IPV6=0)
endif()

option(USE_STDERR_LOGGER "Enable logging to stderr when the logger is NULL" OFF)
if(USE_STDERR_LOGGER)
add_definitions(-DUSE_STDERR_LOGGER=1)
endif()

option(BUILD_TOXAV "Whether to build the tox AV library" ON)
option(MUST_BUILD_TOXAV "Fail the build if toxav cannot be built" OFF)

Expand Down
37 changes: 26 additions & 11 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,14 @@ static void test_addto_lists_good(DHT *dht,

static void test_addto_lists(IP ip)
{
Networking_Core *net = new_networking(nullptr, ip, TOX_PORT_DEFAULT);
Logger *log = logger_new();
uint32_t index = 1;
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index);

Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != nullptr, "Failed to create Networking_Core");

DHT *dht = new_DHT(nullptr, net, true);
DHT *dht = new_DHT(log, net, true);
ck_assert_msg(dht != nullptr, "Failed to create DHT");

IP_Port ip_port;
Expand Down Expand Up @@ -453,6 +457,7 @@ static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1],
static void test_list_main(void)
{
DHT *dhts[NUM_DHT];
uint32_t index[NUM_DHT];

uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1];
memset(cmp_list1, 0, sizeof(cmp_list1));
Expand All @@ -463,7 +468,11 @@ static void test_list_main(void)
IP ip;
ip_init(&ip, 1);

dhts[i] = new_DHT(nullptr, new_networking(nullptr, ip, DHT_DEFAULT_PORT + i), true);
Logger *log = logger_new();
index[i] = i + 1;
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index[i]);

dhts[i] = new_DHT(log, new_networking(log, ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand All @@ -489,15 +498,16 @@ static void test_list_main(void)
}
}

/*
print_pk(dhts[0]->self_public_key);
#if 0
print_pk(dhts[0]->self_public_key);

for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
printf("----Entry %u----\n", i);
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
printf("----Entry %u----\n", i);

print_pk(cmp_list1[i]);
}
*/
print_pk(cmp_list1[i]);
}

#endif
unsigned int m_count = 0;

for (l = 0; l < NUM_DHT; ++l) {
Expand Down Expand Up @@ -592,14 +602,19 @@ START_TEST(test_DHT_test)
{
uint32_t to_comp = 8394782;
DHT *dhts[NUM_DHT];
uint32_t index[NUM_DHT];

unsigned int i, j;

for (i = 0; i < NUM_DHT; ++i) {
IP ip;
ip_init(&ip, 1);

dhts[i] = new_DHT(nullptr, new_networking(nullptr, ip, DHT_DEFAULT_PORT + i), true);
Logger *log = logger_new();
index[i] = i + 1;
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index[i]);

dhts[i] = new_DHT(log, new_networking(log, ip, DHT_DEFAULT_PORT + i), true);
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand Down
18 changes: 8 additions & 10 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1977,26 +1977,24 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
return nullptr;
}

Logger *log = nullptr;
m->log = logger_new();

if (options->log_callback) {
log = logger_new();

if (log != nullptr) {
logger_callback_log(log, options->log_callback, m, options->log_user_data);
}
if (m->log == nullptr) {
friendreq_kill(m->fr);
free(m);
return nullptr;
}

m->log = log;
logger_callback_log(m->log, options->log_callback, m, options->log_user_data);

unsigned int net_err = 0;

if (options->udp_disabled) {
m->net = new_networking_no_udp(log);
m->net = new_networking_no_udp(m->log);
} else {
IP ip;
ip_init(&ip, options->ipv6enabled);
m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err);
m->net = new_networking_ex(m->log, ip, options->port_range[0], options->port_range[1], &net_err);
}

if (m->net == nullptr) {
Expand Down
9 changes: 7 additions & 2 deletions toxcore/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "logger.h"

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -38,7 +39,7 @@ struct Logger {
void *userdata;
};


#ifdef USE_STDERR_LOGGER
static const char *logger_level_name(LOGGER_LEVEL level)
{
switch (level) {
Expand Down Expand Up @@ -73,7 +74,7 @@ static const Logger logger_stderr = {
nullptr,
nullptr,
};

#endif

/**
* Public Functions
Expand All @@ -99,7 +100,11 @@ void logger_write(const Logger *log, LOGGER_LEVEL level, const char *file, int l
const char *format, ...)
{
if (!log) {
#ifdef USE_STDERR_LOGGER
log = &logger_stderr;
#else
assert(!"NULL logger not permitted");
#endif
}

if (!log->callback) {
Expand Down
4 changes: 3 additions & 1 deletion toxcore/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void *
* If the logger is NULL, this writes to stderr. This behaviour should not be
* used in production code, but can be useful for temporarily debugging a
* function that does not have a logger available. It's essentially
* fprintf(stderr, ...), but with timestamps and source location.
* fprintf(stderr, ...), but with timestamps and source location. Toxcore must
* be built with -DUSE_STDERR_LOGGER for this to work. It will cause an
* assertion failure otherwise.
*/
void logger_write(
const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func,
Expand Down

1 comment on commit 9706d9a

@sum01
Copy link

@sum01 sum01 commented on 9706d9a Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cmake auto-defines NDEBUG on release-type builds, so you can use #ifndef NDEBUG instead of a custom option. Then the logger would only trigger on Cmake debug-type builds.

Please sign in to comment.