summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-03-17 14:48:34 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-03-17 15:04:03 +0000
commit9706d9a0f015a58937255abd95101045ab865796 (patch)
treef80d6558912e09b22b4f0c44440e54f92fd40cae
parenta2496af71aeed3cd7324fb59d9f32c2d52759d56 (diff)
Disallow stderr logger by default.
-rw-r--r--CMakeLists.txt5
-rw-r--r--auto_tests/dht_test.c37
-rw-r--r--toxcore/Messenger.c18
-rw-r--r--toxcore/logger.c9
-rw-r--r--toxcore/logger.h4
5 files changed, 49 insertions, 24 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1f18988c..21182d33 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -192,6 +192,11 @@ if(NOT USE_IPV6)
192 add_definitions(-DUSE_IPV6=0) 192 add_definitions(-DUSE_IPV6=0)
193endif() 193endif()
194 194
195option(USE_STDERR_LOGGER "Enable logging to stderr when the logger is NULL" OFF)
196if(USE_STDERR_LOGGER)
197 add_definitions(-DUSE_STDERR_LOGGER=1)
198endif()
199
195option(BUILD_TOXAV "Whether to build the tox AV library" ON) 200option(BUILD_TOXAV "Whether to build the tox AV library" ON)
196option(MUST_BUILD_TOXAV "Fail the build if toxav cannot be built" OFF) 201option(MUST_BUILD_TOXAV "Fail the build if toxav cannot be built" OFF)
197 202
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c
index 2814263f..034147b9 100644
--- a/auto_tests/dht_test.c
+++ b/auto_tests/dht_test.c
@@ -322,10 +322,14 @@ static void test_addto_lists_good(DHT *dht,
322 322
323static void test_addto_lists(IP ip) 323static void test_addto_lists(IP ip)
324{ 324{
325 Networking_Core *net = new_networking(nullptr, ip, TOX_PORT_DEFAULT); 325 Logger *log = logger_new();
326 uint32_t index = 1;
327 logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index);
328
329 Networking_Core *net = new_networking(log, ip, TOX_PORT_DEFAULT);
326 ck_assert_msg(net != nullptr, "Failed to create Networking_Core"); 330 ck_assert_msg(net != nullptr, "Failed to create Networking_Core");
327 331
328 DHT *dht = new_DHT(nullptr, net, true); 332 DHT *dht = new_DHT(log, net, true);
329 ck_assert_msg(dht != nullptr, "Failed to create DHT"); 333 ck_assert_msg(dht != nullptr, "Failed to create DHT");
330 334
331 IP_Port ip_port; 335 IP_Port ip_port;
@@ -453,6 +457,7 @@ static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1],
453static void test_list_main(void) 457static void test_list_main(void)
454{ 458{
455 DHT *dhts[NUM_DHT]; 459 DHT *dhts[NUM_DHT];
460 uint32_t index[NUM_DHT];
456 461
457 uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1]; 462 uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1];
458 memset(cmp_list1, 0, sizeof(cmp_list1)); 463 memset(cmp_list1, 0, sizeof(cmp_list1));
@@ -463,7 +468,11 @@ static void test_list_main(void)
463 IP ip; 468 IP ip;
464 ip_init(&ip, 1); 469 ip_init(&ip, 1);
465 470
466 dhts[i] = new_DHT(nullptr, new_networking(nullptr, ip, DHT_DEFAULT_PORT + i), true); 471 Logger *log = logger_new();
472 index[i] = i + 1;
473 logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index[i]);
474
475 dhts[i] = new_DHT(log, new_networking(log, ip, DHT_DEFAULT_PORT + i), true);
467 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); 476 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
468 ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port"); 477 ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
469 } 478 }
@@ -489,15 +498,16 @@ static void test_list_main(void)
489 } 498 }
490 } 499 }
491 500
492 /* 501#if 0
493 print_pk(dhts[0]->self_public_key); 502 print_pk(dhts[0]->self_public_key);
494 503
495 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { 504 for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
496 printf("----Entry %u----\n", i); 505 printf("----Entry %u----\n", i);
497 506
498 print_pk(cmp_list1[i]); 507 print_pk(cmp_list1[i]);
499 } 508 }
500 */ 509
510#endif
501 unsigned int m_count = 0; 511 unsigned int m_count = 0;
502 512
503 for (l = 0; l < NUM_DHT; ++l) { 513 for (l = 0; l < NUM_DHT; ++l) {
@@ -592,6 +602,7 @@ START_TEST(test_DHT_test)
592{ 602{
593 uint32_t to_comp = 8394782; 603 uint32_t to_comp = 8394782;
594 DHT *dhts[NUM_DHT]; 604 DHT *dhts[NUM_DHT];
605 uint32_t index[NUM_DHT];
595 606
596 unsigned int i, j; 607 unsigned int i, j;
597 608
@@ -599,7 +610,11 @@ START_TEST(test_DHT_test)
599 IP ip; 610 IP ip;
600 ip_init(&ip, 1); 611 ip_init(&ip, 1);
601 612
602 dhts[i] = new_DHT(nullptr, new_networking(nullptr, ip, DHT_DEFAULT_PORT + i), true); 613 Logger *log = logger_new();
614 index[i] = i + 1;
615 logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index[i]);
616
617 dhts[i] = new_DHT(log, new_networking(log, ip, DHT_DEFAULT_PORT + i), true);
603 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i); 618 ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
604 ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port"); 619 ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
605 } 620 }
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 4a24ae5f..2408c844 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1977,26 +1977,24 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error)
1977 return nullptr; 1977 return nullptr;
1978 } 1978 }
1979 1979
1980 Logger *log = nullptr; 1980 m->log = logger_new();
1981 1981
1982 if (options->log_callback) { 1982 if (m->log == nullptr) {
1983 log = logger_new(); 1983 friendreq_kill(m->fr);
1984 1984 free(m);
1985 if (log != nullptr) { 1985 return nullptr;
1986 logger_callback_log(log, options->log_callback, m, options->log_user_data);
1987 }
1988 } 1986 }
1989 1987
1990 m->log = log; 1988 logger_callback_log(m->log, options->log_callback, m, options->log_user_data);
1991 1989
1992 unsigned int net_err = 0; 1990 unsigned int net_err = 0;
1993 1991
1994 if (options->udp_disabled) { 1992 if (options->udp_disabled) {
1995 m->net = new_networking_no_udp(log); 1993 m->net = new_networking_no_udp(m->log);
1996 } else { 1994 } else {
1997 IP ip; 1995 IP ip;
1998 ip_init(&ip, options->ipv6enabled); 1996 ip_init(&ip, options->ipv6enabled);
1999 m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err); 1997 m->net = new_networking_ex(m->log, ip, options->port_range[0], options->port_range[1], &net_err);
2000 } 1998 }
2001 1999
2002 if (m->net == nullptr) { 2000 if (m->net == nullptr) {
diff --git a/toxcore/logger.c b/toxcore/logger.c
index 80195e93..ff34f994 100644
--- a/toxcore/logger.c
+++ b/toxcore/logger.c
@@ -27,6 +27,7 @@
27 27
28#include "logger.h" 28#include "logger.h"
29 29
30#include <assert.h>
30#include <stdarg.h> 31#include <stdarg.h>
31#include <stdio.h> 32#include <stdio.h>
32#include <stdlib.h> 33#include <stdlib.h>
@@ -38,7 +39,7 @@ struct Logger {
38 void *userdata; 39 void *userdata;
39}; 40};
40 41
41 42#ifdef USE_STDERR_LOGGER
42static const char *logger_level_name(LOGGER_LEVEL level) 43static const char *logger_level_name(LOGGER_LEVEL level)
43{ 44{
44 switch (level) { 45 switch (level) {
@@ -73,7 +74,7 @@ static const Logger logger_stderr = {
73 nullptr, 74 nullptr,
74 nullptr, 75 nullptr,
75}; 76};
76 77#endif
77 78
78/** 79/**
79 * Public Functions 80 * Public Functions
@@ -99,7 +100,11 @@ void logger_write(const Logger *log, LOGGER_LEVEL level, const char *file, int l
99 const char *format, ...) 100 const char *format, ...)
100{ 101{
101 if (!log) { 102 if (!log) {
103#ifdef USE_STDERR_LOGGER
102 log = &logger_stderr; 104 log = &logger_stderr;
105#else
106 assert(!"NULL logger not permitted");
107#endif
103 } 108 }
104 109
105 if (!log->callback) { 110 if (!log->callback) {
diff --git a/toxcore/logger.h b/toxcore/logger.h
index 2a6ee2ed..acd21fb8 100644
--- a/toxcore/logger.h
+++ b/toxcore/logger.h
@@ -67,7 +67,9 @@ void logger_callback_log(Logger *log, logger_cb *function, void *context, void *
67 * If the logger is NULL, this writes to stderr. This behaviour should not be 67 * If the logger is NULL, this writes to stderr. This behaviour should not be
68 * used in production code, but can be useful for temporarily debugging a 68 * used in production code, but can be useful for temporarily debugging a
69 * function that does not have a logger available. It's essentially 69 * function that does not have a logger available. It's essentially
70 * fprintf(stderr, ...), but with timestamps and source location. 70 * fprintf(stderr, ...), but with timestamps and source location. Toxcore must
71 * be built with -DUSE_STDERR_LOGGER for this to work. It will cause an
72 * assertion failure otherwise.
71 */ 73 */
72void logger_write( 74void logger_write(
73 const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func, 75 const Logger *log, LOGGER_LEVEL level, const char *file, int line, const char *func,