summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2017-01-04 22:37:53 +0000
committeriphydf <iphydf@users.noreply.github.com>2017-01-11 19:43:08 +0000
commit8b4eae4038db87f2df1c40856c99c3ffca8887fe (patch)
treea3d712f2adf081f56a4295a34f5a8c75523e5a18
parent583d71680ec12349cd1bedea72ae136d55667ecb (diff)
Remove `TOX_DEBUG` and have asserts always enabled.
These are cheap asserts. I've also replaced the fprintf's with `LOGGER_ERROR` calls.
-rw-r--r--CMakeLists.txt1
-rw-r--r--configure.ac12
-rw-r--r--testing/misc_tools.c4
-rw-r--r--toxcore/DHT.c9
-rw-r--r--toxcore/Messenger.c23
-rw-r--r--toxcore/network.c8
-rw-r--r--toxcore/util.c14
-rw-r--r--toxcore/util.h4
8 files changed, 16 insertions, 59 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 60d63098..bd70ab38 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -127,7 +127,6 @@ endif()
127option(DEBUG "Enable assertions and other debugging facilities" OFF) 127option(DEBUG "Enable assertions and other debugging facilities" OFF)
128if(DEBUG) 128if(DEBUG)
129 set(MIN_LOGGER_LEVEL DEBUG) 129 set(MIN_LOGGER_LEVEL DEBUG)
130 add_definitions(-DTOX_DEBUG=1)
131 add_cflag("-g3") 130 add_cflag("-g3")
132endif() 131endif()
133 132
diff --git a/configure.ac b/configure.ac
index 8d59882c..eb16c0e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -34,7 +34,6 @@ BUILD_AV="yes"
34BUILD_TESTING="yes" 34BUILD_TESTING="yes"
35 35
36TOX_LOGGER="no" 36TOX_LOGGER="no"
37TOX_DEBUG="no"
38 37
39NCURSES_FOUND="no" 38NCURSES_FOUND="no"
40LIBCONFIG_FOUND="no" 39LIBCONFIG_FOUND="no"
@@ -93,17 +92,6 @@ AC_ARG_ENABLE([logging],
93 ] 92 ]
94) 93)
95 94
96AC_ARG_ENABLE([debug],
97 [AC_HELP_STRING([--enable-debug], [enable debugging (default: disabled)]) ],
98 [
99 if test "x$enableval" = "xyes"; then
100 TOX_DEBUG="yes"
101
102 AC_DEFINE([TOX_DEBUG], [], [If debugging enabled])
103 fi
104 ]
105)
106
107AC_ARG_WITH(log-level, 95AC_ARG_WITH(log-level,
108 AC_HELP_STRING([--with-log-level=LEVEL], 96 AC_HELP_STRING([--with-log-level=LEVEL],
109 [Logger levels: TRACE; DEBUG; INFO; WARNING; ERROR ]), 97 [Logger levels: TRACE; DEBUG; INFO; WARNING; ERROR ]),
diff --git a/testing/misc_tools.c b/testing/misc_tools.c
index 30c813ca..8798c6dd 100644
--- a/testing/misc_tools.c
+++ b/testing/misc_tools.c
@@ -31,10 +31,6 @@
31#include <string.h> 31#include <string.h>
32#include <strings.h> 32#include <strings.h>
33 33
34#ifdef TOX_DEBUG
35#include <assert.h>
36#endif // TOX_DEBUG
37
38// You are responsible for freeing the return value! 34// You are responsible for freeing the return value!
39uint8_t *hex_string_to_bin(const char *hex_string) 35uint8_t *hex_string_to_bin(const char *hex_string)
40{ 36{
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 94bbbe01..611cb0f8 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -2813,13 +2813,10 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
2813 2813
2814 break; 2814 break;
2815 2815
2816#ifdef TOX_DEBUG
2817
2818 default: 2816 default:
2819 fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n", 2817 LOGGER_ERROR(dht->log, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
2820 length, type); 2818 length, type);
2821 break; 2819 break;
2822#endif
2823 } 2820 }
2824 2821
2825 return 0; 2822 return 0;
@@ -2839,7 +2836,7 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length)
2839 lendian_to_host32(&data32, data); 2836 lendian_to_host32(&data32, data);
2840 2837
2841 if (data32 == DHT_STATE_COOKIE_GLOBAL) { 2838 if (data32 == DHT_STATE_COOKIE_GLOBAL) {
2842 return load_state(dht_load_state_callback, dht, data + cookie_len, 2839 return load_state(dht_load_state_callback, dht->log, dht, data + cookie_len,
2843 length - cookie_len, DHT_STATE_COOKIE_TYPE); 2840 length - cookie_len, DHT_STATE_COOKIE_TYPE);
2844 } 2841 }
2845 } 2842 }
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index cd4c8380..05a7c09e 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -31,9 +31,7 @@
31#include "network.h" 31#include "network.h"
32#include "util.h" 32#include "util.h"
33 33
34#ifdef TOX_DEBUG
35#include <assert.h> 34#include <assert.h>
36#endif
37 35
38 36
39static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata); 37static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status, void *userdata);
@@ -1339,9 +1337,7 @@ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
1339 return -3; 1337 return -3;
1340 } 1338 }
1341 1339
1342#ifdef TOX_DEBUG
1343 assert(temp_filenum <= UINT8_MAX); 1340 assert(temp_filenum <= UINT8_MAX);
1344#endif
1345 uint8_t file_number = temp_filenum; 1341 uint8_t file_number = temp_filenum;
1346 1342
1347 // We're always receiving at this point. 1343 // We're always receiving at this point.
@@ -2785,20 +2781,16 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
2785 } 2781 }
2786 2782
2787 uint8_t *next_data = friend_save(&temp, cur_data); 2783 uint8_t *next_data = friend_save(&temp, cur_data);
2788#ifdef TOX_DEBUG
2789 assert(next_data - cur_data == friend_size()); 2784 assert(next_data - cur_data == friend_size());
2790#ifdef __LP64__ 2785#ifdef __LP64__
2791 assert(memcmp(cur_data, &temp, friend_size()) == 0); 2786 assert(memcmp(cur_data, &temp, friend_size()) == 0);
2792#endif 2787#endif
2793#endif
2794 cur_data = next_data; 2788 cur_data = next_data;
2795 num++; 2789 num++;
2796 } 2790 }
2797 } 2791 }
2798 2792
2799#ifdef TOX_DEBUG
2800 assert(cur_data - data == num * friend_size()); 2793 assert(cur_data - data == num * friend_size());
2801#endif
2802 return cur_data - data; 2794 return cur_data - data;
2803} 2795}
2804 2796
@@ -2847,12 +2839,10 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
2847 for (i = 0; i < num; ++i) { 2839 for (i = 0; i < num; ++i) {
2848 struct SAVED_FRIEND temp = { 0 }; 2840 struct SAVED_FRIEND temp = { 0 };
2849 const uint8_t *next_data = friend_load(&temp, cur_data); 2841 const uint8_t *next_data = friend_load(&temp, cur_data);
2850#ifdef TOX_DEBUG
2851 assert(next_data - cur_data == friend_size()); 2842 assert(next_data - cur_data == friend_size());
2852#ifdef __LP64__ 2843#ifdef __LP64__
2853 assert(memcmp(&temp, cur_data, friend_size()) == 0); 2844 assert(memcmp(&temp, cur_data, friend_size()) == 0);
2854#endif 2845#endif
2855#endif
2856 cur_data = next_data; 2846 cur_data = next_data;
2857 2847
2858 if (temp.status >= 3) { 2848 if (temp.status >= 3) {
@@ -2922,9 +2912,7 @@ void messenger_save(const Messenger *m, uint8_t *data)
2922 host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL); 2912 host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL);
2923 data += size32; 2913 data += size32;
2924 2914
2925#ifdef TOX_DEBUG 2915 assert(sizeof(get_nospam(&m->fr)) == sizeof(uint32_t));
2926 assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t));
2927#endif
2928 len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE; 2916 len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
2929 type = MESSENGER_STATE_TYPE_NOSPAMKEYS; 2917 type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
2930 data = z_state_save_subheader(data, len, type); 2918 data = z_state_save_subheader(data, len, type);
@@ -3075,13 +3063,10 @@ static int messenger_load_state_callback(void *outer, const uint8_t *data, uint3
3075 return -2; 3063 return -2;
3076 } 3064 }
3077 3065
3078#ifdef TOX_DEBUG
3079
3080 default: 3066 default:
3081 fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n", 3067 LOGGER_ERROR(m->log, "Load state: contains unrecognized part (len %u, type %u)\n",
3082 length, type); 3068 length, type);
3083 break; 3069 break;
3084#endif
3085 } 3070 }
3086 3071
3087 return 0; 3072 return 0;
@@ -3101,7 +3086,7 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
3101 lendian_to_host32(data32 + 1, data + sizeof(uint32_t)); 3086 lendian_to_host32(data32 + 1, data + sizeof(uint32_t));
3102 3087
3103 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) { 3088 if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL)) {
3104 return load_state(messenger_load_state_callback, m, data + cookie_len, 3089 return load_state(messenger_load_state_callback, m->log, m, data + cookie_len,
3105 length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); 3090 length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
3106 } 3091 }
3107 3092
diff --git a/toxcore/network.c b/toxcore/network.c
index f58cde77..253c0611 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -550,9 +550,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
550 550
551 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ 551 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
552 if (ip.family != AF_INET && ip.family != AF_INET6) { 552 if (ip.family != AF_INET && ip.family != AF_INET6) {
553#ifdef TOX_DEBUG 553 LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family);
554 fprintf(stderr, "Invalid address family: %u\n", ip.family);
555#endif
556 return NULL; 554 return NULL;
557 } 555 }
558 556
@@ -576,9 +574,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
576 574
577 /* Check for socket error. */ 575 /* Check for socket error. */
578 if (!sock_valid(temp->sock)) { 576 if (!sock_valid(temp->sock)) {
579#ifdef TOX_DEBUG 577 LOGGER_ERROR(log, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
580 fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
581#endif
582 free(temp); 578 free(temp);
583 579
584 if (error) { 580 if (error) {
diff --git a/toxcore/util.c b/toxcore/util.c
index 1d2a85dc..b390b169 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -116,13 +116,11 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
116} 116}
117 117
118/* state load/save */ 118/* state load/save */
119int load_state(load_state_callback_func load_state_callback, void *outer, 119int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer,
120 const uint8_t *data, uint32_t length, uint16_t cookie_inner) 120 const uint8_t *data, uint32_t length, uint16_t cookie_inner)
121{ 121{
122 if (!load_state_callback || !data) { 122 if (!load_state_callback || !data) {
123#ifdef TOX_DEBUG 123 LOGGER_ERROR(log, "load_state() called with invalid args.\n");
124 fprintf(stderr, "load_state() called with invalid args.\n");
125#endif
126 return -1; 124 return -1;
127 } 125 }
128 126
@@ -139,17 +137,13 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
139 137
140 if (length < length_sub) { 138 if (length < length_sub) {
141 /* file truncated */ 139 /* file truncated */
142#ifdef TOX_DEBUG 140 LOGGER_ERROR(log, "state file too short: %u < %u\n", length, length_sub);
143 fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
144#endif
145 return -1; 141 return -1;
146 } 142 }
147 143
148 if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) { 144 if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
149 /* something is not matching up in a bad way, give up */ 145 /* something is not matching up in a bad way, give up */
150#ifdef DEBUG 146 LOGGER_ERROR(log, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
151 fprintf(stderr, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
152#endif
153 return -1; 147 return -1;
154 } 148 }
155 149
diff --git a/toxcore/util.h b/toxcore/util.h
index c435813b..daa5a26d 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -29,6 +29,8 @@
29#include <stdbool.h> 29#include <stdbool.h>
30#include <stdint.h> 30#include <stdint.h>
31 31
32#include "logger.h"
33
32#define MIN(a,b) (((a)<(b))?(a):(b)) 34#define MIN(a,b) (((a)<(b))?(a):(b))
33#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; } 35#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }
34 36
@@ -52,7 +54,7 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian);
52 54
53/* state load/save */ 55/* state load/save */
54typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type); 56typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type);
55int load_state(load_state_callback_func load_state_callback, void *outer, 57int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer,
56 const uint8_t *data, uint32_t length, uint16_t cookie_inner); 58 const uint8_t *data, uint32_t length, uint16_t cookie_inner);
57 59
58/* Returns -1 if failed or 0 if success */ 60/* Returns -1 if failed or 0 if success */