Skip to content

Commit 8b4eae4

Browse files
committed
Remove TOX_DEBUG and have asserts always enabled.
These are cheap asserts. I've also replaced the fprintf's with `LOGGER_ERROR` calls.
1 parent 583d716 commit 8b4eae4

File tree

8 files changed

+16
-59
lines changed

8 files changed

+16
-59
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ endif()
127127
option(DEBUG "Enable assertions and other debugging facilities" OFF)
128128
if(DEBUG)
129129
set(MIN_LOGGER_LEVEL DEBUG)
130-
add_definitions(-DTOX_DEBUG=1)
131130
add_cflag("-g3")
132131
endif()
133132

configure.ac

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ BUILD_AV="yes"
3434
BUILD_TESTING="yes"
3535

3636
TOX_LOGGER="no"
37-
TOX_DEBUG="no"
3837

3938
NCURSES_FOUND="no"
4039
LIBCONFIG_FOUND="no"
@@ -93,17 +92,6 @@ AC_ARG_ENABLE([logging],
9392
]
9493
)
9594

96-
AC_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-
10795
AC_ARG_WITH(log-level,
10896
AC_HELP_STRING([--with-log-level=LEVEL],
10997
[Logger levels: TRACE; DEBUG; INFO; WARNING; ERROR ]),

testing/misc_tools.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131
#include <string.h>
3232
#include <strings.h>
3333

34-
#ifdef TOX_DEBUG
35-
#include <assert.h>
36-
#endif // TOX_DEBUG
37-
3834
// You are responsible for freeing the return value!
3935
uint8_t *hex_string_to_bin(const char *hex_string)
4036
{

toxcore/DHT.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,13 +2813,10 @@ static int dht_load_state_callback(void *outer, const uint8_t *data, uint32_t le
28132813

28142814
break;
28152815

2816-
#ifdef TOX_DEBUG
2817-
28182816
default:
2819-
fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
2820-
length, type);
2817+
LOGGER_ERROR(dht->log, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
2818+
length, type);
28212819
break;
2822-
#endif
28232820
}
28242821

28252822
return 0;
@@ -2839,7 +2836,7 @@ int DHT_load(DHT *dht, const uint8_t *data, uint32_t length)
28392836
lendian_to_host32(&data32, data);
28402837

28412838
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,
28432840
length - cookie_len, DHT_STATE_COOKIE_TYPE);
28442841
}
28452842
}

toxcore/Messenger.c

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
#include "network.h"
3232
#include "util.h"
3333

34-
#ifdef TOX_DEBUG
3534
#include <assert.h>
36-
#endif
3735

3836

3937
static 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
13391337
return -3;
13401338
}
13411339

1342-
#ifdef TOX_DEBUG
13431340
assert(temp_filenum <= UINT8_MAX);
1344-
#endif
13451341
uint8_t file_number = temp_filenum;
13461342

13471343
// We're always receiving at this point.
@@ -2785,20 +2781,16 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data)
27852781
}
27862782

27872783
uint8_t *next_data = friend_save(&temp, cur_data);
2788-
#ifdef TOX_DEBUG
27892784
assert(next_data - cur_data == friend_size());
27902785
#ifdef __LP64__
27912786
assert(memcmp(cur_data, &temp, friend_size()) == 0);
2792-
#endif
27932787
#endif
27942788
cur_data = next_data;
27952789
num++;
27962790
}
27972791
}
27982792

2799-
#ifdef TOX_DEBUG
28002793
assert(cur_data - data == num * friend_size());
2801-
#endif
28022794
return cur_data - data;
28032795
}
28042796

@@ -2847,11 +2839,9 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length)
28472839
for (i = 0; i < num; ++i) {
28482840
struct SAVED_FRIEND temp = { 0 };
28492841
const uint8_t *next_data = friend_load(&temp, cur_data);
2850-
#ifdef TOX_DEBUG
28512842
assert(next_data - cur_data == friend_size());
28522843
#ifdef __LP64__
28532844
assert(memcmp(&temp, cur_data, friend_size()) == 0);
2854-
#endif
28552845
#endif
28562846
cur_data = next_data;
28572847

@@ -2922,9 +2912,7 @@ void messenger_save(const Messenger *m, uint8_t *data)
29222912
host_to_lendian32(data, MESSENGER_STATE_COOKIE_GLOBAL);
29232913
data += size32;
29242914

2925-
#ifdef TOX_DEBUG
2926-
assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t));
2927-
#endif
2915+
assert(sizeof(get_nospam(&m->fr)) == sizeof(uint32_t));
29282916
len = size32 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_SECRET_KEY_SIZE;
29292917
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
29302918
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
30753063
return -2;
30763064
}
30773065

3078-
#ifdef TOX_DEBUG
3079-
30803066
default:
3081-
fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
3082-
length, type);
3067+
LOGGER_ERROR(m->log, "Load state: contains unrecognized part (len %u, type %u)\n",
3068+
length, type);
30833069
break;
3084-
#endif
30853070
}
30863071

30873072
return 0;
@@ -3101,7 +3086,7 @@ int messenger_load(Messenger *m, const uint8_t *data, uint32_t length)
31013086
lendian_to_host32(data32 + 1, data + sizeof(uint32_t));
31023087

31033088
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,
31053090
length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
31063091
}
31073092

toxcore/network.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
550550

551551
/* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */
552552
if (ip.family != AF_INET && ip.family != AF_INET6) {
553-
#ifdef TOX_DEBUG
554-
fprintf(stderr, "Invalid address family: %u\n", ip.family);
555-
#endif
553+
LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family);
556554
return NULL;
557555
}
558556

@@ -576,9 +574,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
576574

577575
/* Check for socket error. */
578576
if (!sock_valid(temp->sock)) {
579-
#ifdef TOX_DEBUG
580-
fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
581-
#endif
577+
LOGGER_ERROR(log, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
582578
free(temp);
583579

584580
if (error) {

toxcore/util.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,11 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian)
116116
}
117117

118118
/* state load/save */
119-
int load_state(load_state_callback_func load_state_callback, void *outer,
119+
int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer,
120120
const uint8_t *data, uint32_t length, uint16_t cookie_inner)
121121
{
122122
if (!load_state_callback || !data) {
123-
#ifdef TOX_DEBUG
124-
fprintf(stderr, "load_state() called with invalid args.\n");
125-
#endif
123+
LOGGER_ERROR(log, "load_state() called with invalid args.\n");
126124
return -1;
127125
}
128126

@@ -139,17 +137,13 @@ int load_state(load_state_callback_func load_state_callback, void *outer,
139137

140138
if (length < length_sub) {
141139
/* file truncated */
142-
#ifdef TOX_DEBUG
143-
fprintf(stderr, "state file too short: %u < %u\n", length, length_sub);
144-
#endif
140+
LOGGER_ERROR(log, "state file too short: %u < %u\n", length, length_sub);
145141
return -1;
146142
}
147143

148144
if (lendian_to_host16((cookie_type >> 16)) != cookie_inner) {
149145
/* something is not matching up in a bad way, give up */
150-
#ifdef DEBUG
151-
fprintf(stderr, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
152-
#endif
146+
LOGGER_ERROR(log, "state file garbled: %04x != %04x\n", (cookie_type >> 16), cookie_inner);
153147
return -1;
154148
}
155149

toxcore/util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <stdbool.h>
3030
#include <stdint.h>
3131

32+
#include "logger.h"
33+
3234
#define MIN(a,b) (((a)<(b))?(a):(b))
3335
#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }
3436

@@ -52,7 +54,7 @@ void lendian_to_host32(uint32_t *dest, const uint8_t *lendian);
5254

5355
/* state load/save */
5456
typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32_t len, uint16_t type);
55-
int load_state(load_state_callback_func load_state_callback, void *outer,
57+
int load_state(load_state_callback_func load_state_callback, Logger *log, void *outer,
5658
const uint8_t *data, uint32_t length, uint16_t cookie_inner);
5759

5860
/* Returns -1 if failed or 0 if success */

0 commit comments

Comments
 (0)