Skip to content

Commit

Permalink
Move logging to a callback.
Browse files Browse the repository at this point in the history
This removes the global logger (which by the way was deleted when the first tox
was killed, so other toxes would then stop logging). Various bits of the code
now carry a logger or pass it around. It's a bit less transparent now, but now
there is no need to have a global logger, and clients can decide what to log and
where.
  • Loading branch information
iphydf committed Aug 20, 2016
1 parent f9c83c1 commit e038d9f
Show file tree
Hide file tree
Showing 37 changed files with 510 additions and 550 deletions.
9 changes: 1 addition & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,10 @@ set(PROJECT_VERSION "0.0.0")

set(CMAKE_MACOSX_RPATH ON)

option(LOGGING "Enable internal library logging" OFF)
if(LOGGING)
add_definitions(
-DTOX_LOGGER=1
-DLOGGER_LEVEL=LOG_DEBUG
-DLOGGER_OUTPUT_FILE="libtoxcore.log")
endif()

option(DEBUG "Enable assertions and other debugging facilities" OFF)
if(DEBUG)
add_definitions(-DTOX_DEBUG=1)
add_definitions(-DMIN_LOGGER_LEVEL=LOG_TRACE)
endif()

option(ASSOC_DHT "Enable module to store currently unused ID <=> IP associations" OFF)
Expand Down
6 changes: 3 additions & 3 deletions auto_tests/assoc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ START_TEST(test_basics)
{
/* TODO: real test */
uint8_t id[crypto_box_PUBLICKEYBYTES];
Assoc *assoc = new_Assoc_default(id);
Assoc *assoc = new_Assoc_default(NULL, id);
ck_assert_msg(assoc != NULL, "failed to create default assoc");

kill_Assoc(assoc);
assoc = new_Assoc(17, 4, id); /* results in an assoc of 16/3 */
assoc = new_Assoc(NULL, 17, 4, id); /* results in an assoc of 16/3 */
ck_assert_msg(assoc != NULL, "failed to create customized assoc");

IP_Port ipp;
Expand Down Expand Up @@ -72,7 +72,7 @@ START_TEST(test_fillup)
id[i] = rand();
}

Assoc *assoc = new_Assoc(6, 15, id);
Assoc *assoc = new_Assoc(NULL, 6, 15, id);
ck_assert_msg(assoc != NULL, "failed to create default assoc");
struct entry {
uint8_t id[crypto_box_PUBLICKEYBYTES];
Expand Down
8 changes: 4 additions & 4 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ void test_addto_lists_good(DHT *dht,
#if 0
static void test_addto_lists(IP ip)
{
Networking_Core *net = new_networking(ip, TOX_PORT_DEFAULT);
Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT);
ck_assert_msg(net != 0, "Failed to create Networking_Core");

DHT *dht = new_DHT(net);
DHT *dht = new_DHT(NULL, net);
ck_assert_msg(dht != 0, "Failed to create DHT");

IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT };
Expand Down Expand Up @@ -428,7 +428,7 @@ void test_list_main()
IP ip;
ip_init(&ip, 1);

dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i));
dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i));
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand Down Expand Up @@ -566,7 +566,7 @@ START_TEST(test_DHT_test)
IP ip;
ip_init(&ip, 1);

dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i));
dhts[i] = new_DHT(NULL, new_networking(NULL, ip, DHT_DEFAULT_PORT + i));
ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i);
ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port");
}
Expand Down
2 changes: 1 addition & 1 deletion auto_tests/messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ int main(int argc, char *argv[])
/* IPv6 status from global define */
Messenger_Options options = {0};
options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
m = new_messenger(&options, 0);
m = new_messenger(NULL, &options, 0);

/* setup a default friend and friendnum */
if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/onion_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ START_TEST(test_basic)
IP ip;
ip_init(&ip, 1);
ip.ip6.uint8[15] = 1;
Onion *onion1 = new_onion(new_DHT(new_networking(ip, 34567)));
Onion *onion2 = new_onion(new_DHT(new_networking(ip, 34568)));
Onion *onion1 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34567)));
Onion *onion2 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34568)));
ck_assert_msg((onion1 != NULL) && (onion2 != NULL), "Onion failed initializing.");
networking_registerhandler(onion2->net, 'I', &handle_test_1, onion2);

Expand Down Expand Up @@ -209,7 +209,7 @@ START_TEST(test_basic)
}

c_sleep(1000);
Onion *onion3 = new_onion(new_DHT(new_networking(ip, 34569)));
Onion *onion3 = new_onion(new_DHT(NULL, new_networking(NULL, ip, 34569)));
ck_assert_msg((onion3 != NULL), "Onion failed initializing.");

new_nonce(nonce);
Expand Down Expand Up @@ -272,11 +272,11 @@ Onions *new_onions(uint16_t port)
ip_init(&ip, 1);
ip.ip6.uint8[15] = 1;
Onions *on = malloc(sizeof(Onions));
DHT *dht = new_DHT(new_networking(ip, port));
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, port));
on->onion = new_onion(dht);
on->onion_a = new_onion_announce(dht);
TCP_Proxy_Info inf = {{{0}}};
on->onion_c = new_onion_client(new_net_crypto(dht, &inf));
on->onion_c = new_onion_client(new_net_crypto(NULL, dht, &inf));

if (on->onion && on->onion_a && on->onion_c)
return on;
Expand Down
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(int argc, char *argv[])
IP ip;
ip_init(&ip, ipv6enabled);

DHT *dht = new_DHT(new_networking(ip, PORT));
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT));
Onion *onion = new_onion(dht);
Onion_Announce *onion_a = new_onion_announce(dht);

Expand Down
45 changes: 45 additions & 0 deletions other/apidsl/tox.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,51 @@ static this new(const options_t *options) {
void kill();


/**
* Severity level of log messages.
*/
enum class LOG_LEVEL {
/**
* Very detailed traces including all network activity.
*/
LOG_TRACE,
/**
* Debug messages such as which port we bind to.
*/
LOG_DEBUG,
/**
* Informational log messages such as video call status changes.
*/
LOG_INFO,
/**
* Warnings about internal inconsistency or logic errors.
*/
LOG_WARNING,
/**
* Severe unexpected errors caused by external or internal inconsistency.
*/
LOG_ERROR,
}

/**
* This event is triggered when the toxcore library logs an internal message.
* This is mostly useful for debugging. Currently, userdata will always be NULL
* in the log callback.
*/
event log const {
/**
* @param id The Tox instance number. This is a random 32 bit number.
* @param level The severity of the log message.
* @param file The source file from which the message originated.
* @param line The source line from which the message originated.
* @param func The function from which the message originated.
* @param message The log message.
*/
typedef void(uint32_t id, LOG_LEVEL level, string file, uint32_t line,
string func, string message);
}


uint8_t[size] savedata {
/**
* Calculates the number of bytes required to store the tox instance with
Expand Down
6 changes: 3 additions & 3 deletions other/bootstrap_daemon/src/tox-bootstrapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ int main(int argc, char *argv[])
IP ip;
ip_init(&ip, enable_ipv6);

Networking_Core *net = new_networking(ip, port);
Networking_Core *net = new_networking(NULL, ip, port);

if (net == NULL) {
if (enable_ipv6 && enable_ipv4_fallback) {
write_log(LOG_LEVEL_WARNING, "Couldn't initialize IPv6 networking. Falling back to using IPv4.\n");
enable_ipv6 = 0;
ip_init(&ip, enable_ipv6);
net = new_networking(ip, port);
net = new_networking(NULL, ip, port);

if (net == NULL) {
write_log(LOG_LEVEL_ERROR, "Couldn't fallback to IPv4. Exiting.\n");
Expand All @@ -244,7 +244,7 @@ int main(int argc, char *argv[])
}
}

DHT *dht = new_DHT(net);
DHT *dht = new_DHT(NULL, net);

if (dht == NULL) {
write_log(LOG_LEVEL_ERROR, "Couldn't initialize Tox DHT instance. Exiting.\n");
Expand Down
2 changes: 1 addition & 1 deletion other/travis/toxcore-script
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ git diff --exit-code

# Build toxcore and run tests.
export CFLAGS="-O3 -fprofile-arcs -ftest-coverage -DTRAVIS_ENV=1"
RUN $CMAKE -B$BUILD_DIR -H. -DCMAKE_INSTALL_PREFIX:PATH=$PREFIX -DLOGGING=ON -DDEBUG=ON -DASSOC_DHT=ON
RUN $CMAKE -B$BUILD_DIR -H. -DCMAKE_INSTALL_PREFIX:PATH=$PREFIX -DDEBUG=ON -DASSOC_DHT=ON

export CTEST_OUTPUT_ON_FAILURE=1

Expand Down
2 changes: 1 addition & 1 deletion testing/DHT_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ int main(int argc, char *argv[])
IP ip;
ip_init(&ip, ipv6enabled);

DHT *dht = new_DHT(new_networking(ip, PORT));
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, PORT));
printf("OUR ID: ");
uint32_t i;

Expand Down
2 changes: 1 addition & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ int main(int argc, char *argv[])

Messenger_Options options = {0};
options.ipv6enabled = ipv6enabled;
m = new_messenger(&options, 0);
m = new_messenger(NULL, &options, 0);

if ( !m ) {
fputs("Failed to allocate messenger datastructure\n", stderr);
Expand Down
Loading

0 comments on commit e038d9f

Please sign in to comment.