diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-04-27 19:21:26 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-04-27 19:21:26 +0200 |
commit | 42b25a4d3e2fe66f03cbd8c866d8af7bd4f6e5a7 (patch) | |
tree | 161a21847a79f7fe052a9e9ad1b9b802d04defc6 /toxcore | |
parent | 736f5f80347a39f6b82cda8a4ddc1f7d88fcc2f5 (diff) |
Yeah many calls
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 67 | ||||
-rw-r--r-- | toxcore/Makefile.inc | 2 | ||||
-rw-r--r-- | toxcore/Messenger.c | 46 | ||||
-rw-r--r-- | toxcore/assoc.c | 89 | ||||
-rw-r--r-- | toxcore/assoc.h | 2 | ||||
-rw-r--r-- | toxcore/logger.c | 159 | ||||
-rw-r--r-- | toxcore/logger.h | 86 | ||||
-rw-r--r-- | toxcore/network.c | 153 | ||||
-rw-r--r-- | toxcore/tox.c | 2 | ||||
-rw-r--r-- | toxcore/util.c | 84 | ||||
-rw-r--r-- | toxcore/util.h | 7 |
11 files changed, 379 insertions, 318 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 1089e2ff..870e6ca2 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #include "config.h" | 27 | #include "config.h" |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #include "logger.h" | ||
31 | |||
30 | #include "DHT.h" | 32 | #include "DHT.h" |
31 | 33 | ||
32 | #ifdef ENABLE_ASSOC_DHT | 34 | #ifdef ENABLE_ASSOC_DHT |
@@ -198,18 +200,12 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
198 | /* Refresh the client timestamp. */ | 200 | /* Refresh the client timestamp. */ |
199 | if (ip_port.ip.family == AF_INET) { | 201 | if (ip_port.ip.family == AF_INET) { |
200 | 202 | ||
201 | #ifdef LOGGING | 203 | LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
202 | 204 | LOGGER_INFO("coipil[%u]: switching ipv4 from %s:%u to %s:%u", i, | |
203 | if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { | 205 | ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port), |
204 | size_t x; | 206 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
205 | x = sprintf(logbuffer, "coipil[%u]: switching ipv4 from %s:%u ", i, | 207 | } |
206 | ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port)); | 208 | ); |
207 | sprintf(logbuffer + x, "to %s:%u\n", | ||
208 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | ||
209 | loglog(logbuffer); | ||
210 | } | ||
211 | |||
212 | #endif | ||
213 | 209 | ||
214 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 210 | if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) |
215 | return 1; | 211 | return 1; |
@@ -217,19 +213,13 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
217 | list[i].assoc4.ip_port = ip_port; | 213 | list[i].assoc4.ip_port = ip_port; |
218 | list[i].assoc4.timestamp = temp_time; | 214 | list[i].assoc4.timestamp = temp_time; |
219 | } else if (ip_port.ip.family == AF_INET6) { | 215 | } else if (ip_port.ip.family == AF_INET6) { |
220 | 216 | ||
221 | #ifdef LOGGING | 217 | LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { |
222 | 218 | LOGGER_INFO("coipil[%u]: switching ipv6 from %s:%u to %s:%u", i, | |
223 | if (!ipport_equal(&list[i].assoc6.ip_port, &ip_port)) { | 219 | ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port), |
224 | size_t x; | 220 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); |
225 | x = sprintf(logbuffer, "coipil[%u]: switching ipv6 from %s:%u ", i, | 221 | } |
226 | ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port)); | 222 | ); |
227 | sprintf(logbuffer + x, "to %s:%u\n", | ||
228 | ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); | ||
229 | loglog(logbuffer); | ||
230 | } | ||
231 | |||
232 | #endif | ||
233 | 223 | ||
234 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) | 224 | if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) |
235 | return 1; | 225 | return 1; |
@@ -251,10 +241,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
251 | /* Initialize client timestamp. */ | 241 | /* Initialize client timestamp. */ |
252 | list[i].assoc4.timestamp = temp_time; | 242 | list[i].assoc4.timestamp = temp_time; |
253 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 243 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
254 | #ifdef LOGGING | 244 | |
255 | sprintf(logbuffer, "coipil[%u]: switching client_id (ipv4) \n", i); | 245 | LOGGER_DEBUG("coipil[%u]: switching client_id (ipv4)", i); |
256 | loglog(logbuffer); | 246 | |
257 | #endif | ||
258 | /* kill the other address, if it was set */ | 247 | /* kill the other address, if it was set */ |
259 | memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); | 248 | memset(&list[i].assoc6, 0, sizeof(list[i].assoc6)); |
260 | return 1; | 249 | return 1; |
@@ -262,10 +251,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint32_t length, uint8_t | |||
262 | /* Initialize client timestamp. */ | 251 | /* Initialize client timestamp. */ |
263 | list[i].assoc6.timestamp = temp_time; | 252 | list[i].assoc6.timestamp = temp_time; |
264 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 253 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
265 | #ifdef LOGGING | 254 | |
266 | sprintf(logbuffer, "coipil[%u]: switching client_id (ipv6) \n", i); | 255 | LOGGER_DEBUG("coipil[%u]: switching client_id (ipv6)", i); |
267 | loglog(logbuffer); | 256 | |
268 | #endif | ||
269 | /* kill the other address, if it was set */ | 257 | /* kill the other address, if it was set */ |
270 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); | 258 | memset(&list[i].assoc4, 0, sizeof(list[i].assoc4)); |
271 | return 1; | 259 | return 1; |
@@ -469,18 +457,11 @@ int get_close_nodes(DHT *dht, uint8_t *client_id, Node_format *nodes_list, sa_fa | |||
469 | uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); | 457 | uint8_t num_found = Assoc_get_close_entries(dht->assoc, &request); |
470 | 458 | ||
471 | if (!num_found) { | 459 | if (!num_found) { |
472 | #ifdef LOGGING | 460 | LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned zero nodes"); |
473 | loglog("get_close_nodes(): Assoc_get_close_entries() returned zero nodes.\n"); | ||
474 | #endif | ||
475 | |||
476 | return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); | 461 | return get_somewhat_close_nodes(dht, client_id, nodes_list, sa_family, is_LAN, want_good); |
477 | } | 462 | } |
478 | 463 | ||
479 | #ifdef LOGGING | 464 | LOGGER_DEBUG("get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes", request.count_good, num_found - request.count_good); |
480 | sprintf(logbuffer, "get_close_nodes(): Assoc_get_close_entries() returned %i 'direct' and %i 'indirect' nodes.\n", | ||
481 | request.count_good, num_found - request.count_good); | ||
482 | loglog(logbuffer); | ||
483 | #endif | ||
484 | 465 | ||
485 | uint8_t i, num_returned = 0; | 466 | uint8_t i, num_returned = 0; |
486 | 467 | ||
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 5e529267..5f9ab0d7 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc | |||
@@ -31,6 +31,8 @@ libtoxcore_la_SOURCES = ../toxcore/DHT.h \ | |||
31 | ../toxcore/assoc.c \ | 31 | ../toxcore/assoc.c \ |
32 | ../toxcore/onion.h \ | 32 | ../toxcore/onion.h \ |
33 | ../toxcore/onion.c \ | 33 | ../toxcore/onion.c \ |
34 | ../toxcore/logger.h \ | ||
35 | ../toxcore/logger.c \ | ||
34 | ../toxcore/onion_announce.h \ | 36 | ../toxcore/onion_announce.h \ |
35 | ../toxcore/onion_announce.c \ | 37 | ../toxcore/onion_announce.c \ |
36 | ../toxcore/onion_client.h \ | 38 | ../toxcore/onion_client.h \ |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index db5390c0..2033e6a9 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "config.h" | 25 | #include "config.h" |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "logger.h" | ||
28 | #include "Messenger.h" | 29 | #include "Messenger.h" |
29 | #include "assoc.h" | 30 | #include "assoc.h" |
30 | #include "network.h" | 31 | #include "network.h" |
@@ -2245,7 +2246,7 @@ void do_messenger(Messenger *m) | |||
2245 | #ifdef LOGGING | 2246 | #ifdef LOGGING |
2246 | 2247 | ||
2247 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { | 2248 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { |
2248 | loglog(" = = = = = = = = \n"); | 2249 | |
2249 | #ifdef ENABLE_ASSOC_DHT | 2250 | #ifdef ENABLE_ASSOC_DHT |
2250 | Assoc_status(m->dht->assoc); | 2251 | Assoc_status(m->dht->assoc); |
2251 | #endif | 2252 | #endif |
@@ -2254,12 +2255,10 @@ void do_messenger(Messenger *m) | |||
2254 | size_t c; | 2255 | size_t c; |
2255 | 2256 | ||
2256 | for (c = 0; c < m->numchats; c++) { | 2257 | for (c = 0; c < m->numchats; c++) { |
2257 | loglog("---------------- \n"); | ||
2258 | Assoc_status(m->chats[c]->assoc); | 2258 | Assoc_status(m->chats[c]->assoc); |
2259 | } | 2259 | } |
2260 | } | 2260 | } |
2261 | 2261 | ||
2262 | loglog(" = = = = = = = = \n"); | ||
2263 | 2262 | ||
2264 | lastdump = unix_time(); | 2263 | lastdump = unix_time(); |
2265 | uint32_t client, last_pinged; | 2264 | uint32_t client, last_pinged; |
@@ -2276,14 +2275,12 @@ void do_messenger(Messenger *m) | |||
2276 | if (last_pinged > 999) | 2275 | if (last_pinged > 999) |
2277 | last_pinged = 999; | 2276 | last_pinged = 999; |
2278 | 2277 | ||
2279 | snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", | 2278 | LOGGER_DEBUG("C[%2u] %s:%u [%3u] %s", |
2280 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), | 2279 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), |
2281 | last_pinged, ID2String(cptr->client_id)); | 2280 | last_pinged, ID2String(cptr->client_id)); |
2282 | loglog(logbuffer); | ||
2283 | } | 2281 | } |
2284 | } | 2282 | } |
2285 | 2283 | ||
2286 | loglog(" = = = = = = = = \n"); | ||
2287 | 2284 | ||
2288 | uint32_t friend, dhtfriend; | 2285 | uint32_t friend, dhtfriend; |
2289 | 2286 | ||
@@ -2311,9 +2308,7 @@ void do_messenger(Messenger *m) | |||
2311 | dht2m[m2dht[friend]] = friend; | 2308 | dht2m[m2dht[friend]] = friend; |
2312 | 2309 | ||
2313 | if (m->numfriends != m->dht->num_friends) { | 2310 | if (m->numfriends != m->dht->num_friends) { |
2314 | sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", | 2311 | LOGGER_DEBUG("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); |
2315 | m->dht->num_friends, m->numfriends); | ||
2316 | loglog(logbuffer); | ||
2317 | } | 2312 | } |
2318 | 2313 | ||
2319 | uint32_t ping_lastrecv; | 2314 | uint32_t ping_lastrecv; |
@@ -2334,14 +2329,11 @@ void do_messenger(Messenger *m) | |||
2334 | if (ping_lastrecv > 999) | 2329 | if (ping_lastrecv > 999) |
2335 | ping_lastrecv = 999; | 2330 | ping_lastrecv = 999; |
2336 | 2331 | ||
2337 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u:%2u] <%s> %02i [%03u] %s\n", | 2332 | LOGGER_DEBUG("F[%2u:%2u] <%s> %02i [%03u] %s", |
2338 | dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id, | 2333 | dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id, |
2339 | ping_lastrecv, ID2String(msgfptr->client_id)); | 2334 | ping_lastrecv, ID2String(msgfptr->client_id)); |
2340 | loglog(logbuffer); | ||
2341 | } else { | 2335 | } else { |
2342 | snprintf(logbuffer, sizeof(logbuffer), "F[--:%2u] %s\n", | 2336 | LOGGER_DEBUG("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); |
2343 | friend, ID2String(dhtfptr->client_id)); | ||
2344 | loglog(logbuffer); | ||
2345 | } | 2337 | } |
2346 | 2338 | ||
2347 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { | 2339 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { |
@@ -2355,20 +2347,16 @@ void do_messenger(Messenger *m) | |||
2355 | 2347 | ||
2356 | if (last_pinged > 999) | 2348 | if (last_pinged > 999) |
2357 | last_pinged = 999; | 2349 | last_pinged = 999; |
2358 | 2350 | ||
2359 | snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", | 2351 | LOGGER_DEBUG("F[%2u] => C[%2u] %s:%u [%3u] %s", |
2360 | friend, client, ip_ntoa(&assoc->ip_port.ip), | 2352 | friend, client, ip_ntoa(&assoc->ip_port.ip), |
2361 | ntohs(assoc->ip_port.port), last_pinged, | 2353 | ntohs(assoc->ip_port.port), last_pinged, |
2362 | ID2String(cptr->client_id)); | 2354 | ID2String(cptr->client_id)); |
2363 | loglog(logbuffer); | ||
2364 | } | 2355 | } |
2365 | } | 2356 | } |
2366 | } | 2357 | } |
2367 | |||
2368 | loglog(" = = = = = = = = \n"); | ||
2369 | } | 2358 | } |
2370 | 2359 | #endif /* LOGGING */ | |
2371 | #endif | ||
2372 | } | 2360 | } |
2373 | 2361 | ||
2374 | /* | 2362 | /* |
diff --git a/toxcore/assoc.c b/toxcore/assoc.c index 2c1f0bad..c8f58c9c 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c | |||
@@ -3,6 +3,7 @@ | |||
3 | #include "config.h" | 3 | #include "config.h" |
4 | #endif | 4 | #endif |
5 | 5 | ||
6 | #include "logger.h" | ||
6 | #include "DHT.h" | 7 | #include "DHT.h" |
7 | #include "assoc.h" | 8 | #include "assoc.h" |
8 | #include "ping.h" | 9 | #include "ping.h" |
@@ -524,9 +525,7 @@ static void client_id_self_update(Assoc *assoc) | |||
524 | assoc->self_hash = id_hash(assoc, assoc->self_client_id); | 525 | assoc->self_hash = id_hash(assoc, assoc->self_client_id); |
525 | } | 526 | } |
526 | 527 | ||
527 | #ifdef LOGGING | 528 | LOGGER_DEBUG("id is now set, purging cache of self-references"); |
528 | loglog("assoc: id is now set, purging cache of self-references...\n"); | ||
529 | #endif | ||
530 | 529 | ||
531 | /* if we already added some (or loaded some) entries, | 530 | /* if we already added some (or loaded some) entries, |
532 | * look and remove if we find a match | 531 | * look and remove if we find a match |
@@ -821,10 +820,8 @@ Assoc *new_Assoc(size_t bits, size_t entries, uint8_t *public_id) | |||
821 | entries_test = prime_upto_min9(entries_test - 1); | 820 | entries_test = prime_upto_min9(entries_test - 1); |
822 | 821 | ||
823 | if (entries_test != entries) { | 822 | if (entries_test != entries) { |
824 | #ifdef LOGGING | 823 | |
825 | sprintf(logbuffer, "new_Assoc(): trimmed %i to %i.\n", (int)entries, (int)entries_test); | 824 | LOGGER_DEBUG("trimmed %i to %i.\n", (int)entries, (int)entries_test); |
826 | loglog(logbuffer); | ||
827 | #endif | ||
828 | entries = (size_t)entries_test; | 825 | entries = (size_t)entries_test; |
829 | } | 826 | } |
830 | } | 827 | } |
@@ -873,7 +870,7 @@ void Assoc_self_client_id_changed(Assoc *assoc, uint8_t *id) | |||
873 | 870 | ||
874 | #ifdef LOGGING | 871 | #ifdef LOGGING |
875 | static char *idpart2str(uint8_t *id, size_t len); | 872 | static char *idpart2str(uint8_t *id, size_t len); |
876 | #endif | 873 | #endif /* LOGGING */ |
877 | 874 | ||
878 | /* refresh buckets */ | 875 | /* refresh buckets */ |
879 | void do_Assoc(Assoc *assoc, DHT *dht) | 876 | void do_Assoc(Assoc *assoc, DHT *dht) |
@@ -929,53 +926,30 @@ void do_Assoc(Assoc *assoc, DHT *dht) | |||
929 | break; | 926 | break; |
930 | } | 927 | } |
931 | 928 | ||
932 | #ifdef LOGGING | ||
933 | size_t total = 0, written = sprintf(logbuffer, "assoc: [%u] => ", | ||
934 | (uint32_t)(candidate % assoc->candidates_bucket_count)); | ||
935 | |||
936 | if (written > 0) | ||
937 | total += written; | ||
938 | |||
939 | #endif | ||
940 | |||
941 | if (seen) { | 929 | if (seen) { |
942 | IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6; | 930 | IPPTsPng *ippts = seen->seen_family == AF_INET ? &seen->client.assoc4 : &seen->client.assoc6; |
943 | #ifdef LOGGING | 931 | |
944 | written = sprintf(logbuffer + total, " S[%s...] %s:%u", idpart2str(seen->client.client_id, 8), | 932 | LOGGER_DEBUG("[%u] => S[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), |
945 | ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port)); | 933 | idpart2str(seen->client.client_id, 8), ip_ntoa(&ippts->ip_port.ip), htons(ippts->ip_port.port)); |
946 | 934 | ||
947 | if (written > 0) | ||
948 | total += written; | ||
949 | |||
950 | #endif | ||
951 | DHT_getnodes(dht, &ippts->ip_port, seen->client.client_id, target_id); | 935 | DHT_getnodes(dht, &ippts->ip_port, seen->client.client_id, target_id); |
952 | seen->getnodes = unix_time(); | 936 | seen->getnodes = unix_time(); |
953 | } | 937 | } |
954 | 938 | ||
955 | if (heard && (heard != seen)) { | 939 | if (heard && (heard != seen)) { |
956 | IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6; | 940 | IP_Port *ipp = heard->heard_family == AF_INET ? &heard->assoc_heard4 : &heard->assoc_heard6; |
957 | #ifdef LOGGING | 941 | |
958 | written = sprintf(logbuffer + total, " H[%s...] %s:%u", idpart2str(heard->client.client_id, 8), ip_ntoa(&ipp->ip), | 942 | LOGGER_DEBUG("[%u] => H[%s...] %s:%u", (uint32_t)(candidate % assoc->candidates_bucket_count), |
959 | htons(ipp->port)); | 943 | idpart2str(heard->client.client_id, 8), ip_ntoa(&ipp->ip), htons(ipp->port)); |
960 | 944 | ||
961 | if (written > 0) | ||
962 | total += written; | ||
963 | |||
964 | #endif | ||
965 | DHT_getnodes(dht, ipp, heard->client.client_id, target_id); | 945 | DHT_getnodes(dht, ipp, heard->client.client_id, target_id); |
966 | heard->getnodes = unix_time(); | 946 | heard->getnodes = unix_time(); |
967 | } | 947 | } |
968 | 948 | ||
969 | #ifdef LOGGING | 949 | LOGGER_SCOPE ( |
970 | 950 | if ( !heard && !seen ) | |
971 | if (!heard && !seen) | 951 | LOGGER_DEBUG("[%u] => no nodes to talk to??", (uint32_t)(candidate % assoc->candidates_bucket_count)); |
972 | sprintf(logbuffer + total, "no nodes to talk to??\n"); | 952 | ); |
973 | else | ||
974 | /* for arcane reasons, sprintf(str, "\n") doesn't function */ | ||
975 | sprintf(logbuffer + total, "%s", "\n"); | ||
976 | |||
977 | loglog(logbuffer); | ||
978 | #endif | ||
979 | } | 953 | } |
980 | } | 954 | } |
981 | 955 | ||
@@ -1009,11 +983,11 @@ static char *idpart2str(uint8_t *id, size_t len) | |||
1009 | void Assoc_status(Assoc *assoc) | 983 | void Assoc_status(Assoc *assoc) |
1010 | { | 984 | { |
1011 | if (!assoc) { | 985 | if (!assoc) { |
1012 | loglog("Assoc status: no assoc\n"); | 986 | LOGGER_INFO("Assoc status: no assoc"); |
1013 | return; | 987 | return; |
1014 | } | 988 | } |
1015 | 989 | ||
1016 | loglog("[b:p] hash => [id...] used, seen, heard\n"); | 990 | LOGGER_INFO("[b:p] hash => [id...] used, seen, heard"); |
1017 | 991 | ||
1018 | size_t bid, cid, total = 0; | 992 | size_t bid, cid, total = 0; |
1019 | 993 | ||
@@ -1024,24 +998,23 @@ void Assoc_status(Assoc *assoc) | |||
1024 | Client_entry *entry = &bucket->list[cid]; | 998 | Client_entry *entry = &bucket->list[cid]; |
1025 | 999 | ||
1026 | if (entry->hash) { | 1000 | if (entry->hash) { |
1027 | sprintf(logbuffer, "[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", | 1001 | total++; |
1028 | (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), | 1002 | |
1029 | entry->used_at ? (int)(unix_time() - entry->used_at) : 0, | 1003 | LOGGER_INFO("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", |
1030 | entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, | 1004 | (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), |
1031 | entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', | 1005 | entry->used_at ? (int)(unix_time() - entry->used_at) : 0, |
1006 | entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, | ||
1007 | entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', | ||
1032 | entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, | 1008 | entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, |
1033 | entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); | 1009 | entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); |
1034 | loglog(logbuffer); | ||
1035 | total++; | ||
1036 | } | 1010 | } |
1037 | } | 1011 | } |
1038 | } | 1012 | } |
1039 | 1013 | ||
1040 | if (total) { | 1014 | if (total) { |
1041 | sprintf(logbuffer, "Total: %i entries, table usage %i%%.\n", (int)total, | 1015 | LOGGER_INFO("Total: %i entries, table usage %i%%.\n", (int)total, |
1042 | (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size))); | 1016 | (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size))); |
1043 | loglog(logbuffer); | ||
1044 | } | 1017 | } |
1045 | } | 1018 | } |
1046 | 1019 | ||
1047 | #endif | 1020 | #endif /* LOGGING */ |
diff --git a/toxcore/assoc.h b/toxcore/assoc.h index 9dbc75f2..0fdff4fe 100644 --- a/toxcore/assoc.h +++ b/toxcore/assoc.h | |||
@@ -98,6 +98,6 @@ void kill_Assoc(Assoc *assoc); | |||
98 | 98 | ||
99 | #ifdef LOGGING | 99 | #ifdef LOGGING |
100 | void Assoc_status(Assoc *assoc); | 100 | void Assoc_status(Assoc *assoc); |
101 | #endif | 101 | #endif /* LOGGING */ |
102 | 102 | ||
103 | #endif /* !__ASSOC_H__ */ | 103 | #endif /* !__ASSOC_H__ */ |
diff --git a/toxcore/logger.c b/toxcore/logger.c new file mode 100644 index 00000000..e700fe71 --- /dev/null +++ b/toxcore/logger.c | |||
@@ -0,0 +1,159 @@ | |||
1 | /* logger.c | ||
2 | * | ||
3 | * Wrapping logger functions in nice macros | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif /* HAVE_CONFIG_H */ | ||
27 | |||
28 | |||
29 | #include "logger.h" | ||
30 | |||
31 | #ifdef LOGGING | ||
32 | |||
33 | #include "network.h" /* for time */ | ||
34 | |||
35 | #include <stdio.h> | ||
36 | #include <errno.h> | ||
37 | #include <stdlib.h> | ||
38 | #include <stdarg.h> | ||
39 | #include <inttypes.h> | ||
40 | #include <time.h> | ||
41 | |||
42 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
43 | #define strerror_r(errno,buf,len) strerror_s(buf,len,errno) | ||
44 | #endif | ||
45 | |||
46 | static struct logger_config { | ||
47 | FILE* log_file; | ||
48 | LoggerLevel level; | ||
49 | uint64_t start_time; /* Time when lib loaded */ | ||
50 | } | ||
51 | logger = { | ||
52 | NULL, | ||
53 | DEBUG, | ||
54 | 0 | ||
55 | }; | ||
56 | |||
57 | void __attribute__((destructor)) terminate_logger() | ||
58 | { | ||
59 | if ( !logger.log_file ) return; | ||
60 | |||
61 | time_t tim = time(NULL); | ||
62 | |||
63 | logger_write(ERROR, "============== Closing logger ==============\n" | ||
64 | "Time: %s", asctime(localtime(&tim))); | ||
65 | |||
66 | fclose(logger.log_file); | ||
67 | } | ||
68 | |||
69 | unsigned logger_get_pid() | ||
70 | { | ||
71 | return | ||
72 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
73 | GetCurrentProcessId(); | ||
74 | #else | ||
75 | getpid(); | ||
76 | #endif | ||
77 | } | ||
78 | |||
79 | const char* logger_stringify_level(LoggerLevel level) | ||
80 | { | ||
81 | static const char* strings [] = | ||
82 | { | ||
83 | "INFO", | ||
84 | "DEBUG", | ||
85 | "WARNING", | ||
86 | "ERROR" | ||
87 | }; | ||
88 | |||
89 | return strings[level]; | ||
90 | } | ||
91 | |||
92 | |||
93 | int logger_init(const char* file_name, LoggerLevel level) | ||
94 | { | ||
95 | char* final_l = calloc(sizeof(char), strlen(file_name) + 32); | ||
96 | sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid()); | ||
97 | |||
98 | if ( logger.log_file ) { | ||
99 | fprintf(stderr, "Error opening logger name: %s with level %d: already opened!\n", final_l, level); | ||
100 | free (final_l); | ||
101 | return -1; | ||
102 | } | ||
103 | |||
104 | logger.log_file = fopen(final_l, "wb"); | ||
105 | |||
106 | if ( logger.log_file == NULL ) { | ||
107 | char error[1000]; | ||
108 | if ( strerror_r(errno, error, 1000) == 0 ) | ||
109 | fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, error); | ||
110 | else | ||
111 | fprintf(stderr, "Error opening logger file: %s\n", final_l); | ||
112 | |||
113 | free (final_l); | ||
114 | return -1; | ||
115 | } | ||
116 | |||
117 | |||
118 | logger.level = level; | ||
119 | logger.start_time = current_time(); | ||
120 | |||
121 | |||
122 | time_t tim = time(NULL); | ||
123 | logger_write(ERROR, "============== Starting logger ==============\n" | ||
124 | "Time: %s", asctime(localtime(&tim))); | ||
125 | |||
126 | |||
127 | |||
128 | free (final_l); | ||
129 | return 0; | ||
130 | } | ||
131 | |||
132 | |||
133 | void logger_write (LoggerLevel level, const char* format, ...) | ||
134 | { | ||
135 | if (logger.log_file == NULL) { | ||
136 | /*fprintf(stderr, "Logger file is NULL!\n");*/ | ||
137 | return; | ||
138 | } | ||
139 | |||
140 | if (logger.level > level) return; /* Don't print some levels xuh */ | ||
141 | |||
142 | va_list _arg; | ||
143 | va_start (_arg, format); | ||
144 | vfprintf (logger.log_file, format, _arg); | ||
145 | va_end (_arg); | ||
146 | |||
147 | fflush(logger.log_file); | ||
148 | } | ||
149 | |||
150 | char* logger_timestr(char* dest) | ||
151 | { | ||
152 | uint64_t diff = (current_time() - logger.start_time) / 1000; /* ms */ | ||
153 | sprintf(dest, "%"PRIu64"", diff); | ||
154 | |||
155 | return dest; | ||
156 | } | ||
157 | |||
158 | |||
159 | #endif /* LOGGING */ \ No newline at end of file | ||
diff --git a/toxcore/logger.h b/toxcore/logger.h new file mode 100644 index 00000000..6c65850e --- /dev/null +++ b/toxcore/logger.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /* logger.h | ||
2 | * | ||
3 | * Wrapping logger functions in nice macros | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef __TOXLOGGER | ||
26 | #define __TOXLOGGER | ||
27 | |||
28 | // #define LOGGING | ||
29 | |||
30 | #ifdef LOGGING | ||
31 | #include <string.h> | ||
32 | |||
33 | typedef enum _LoggerLevel | ||
34 | { | ||
35 | INFO, | ||
36 | DEBUG, | ||
37 | WARNING, | ||
38 | ERROR | ||
39 | } LoggerLevel; | ||
40 | |||
41 | /* | ||
42 | * Set 'level' as the lowest printable level | ||
43 | */ | ||
44 | int logger_init(const char* file_name, LoggerLevel level); | ||
45 | const char* logger_stringify_level(LoggerLevel level); | ||
46 | unsigned logger_get_pid(); | ||
47 | void logger_write (LoggerLevel level, const char* format, ...); | ||
48 | char* logger_timestr (char* dest); | ||
49 | |||
50 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
51 | #define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) | ||
52 | #else | ||
53 | #define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) | ||
54 | #endif | ||
55 | |||
56 | #define WRITE_FORMAT(__LEVEL__, format) char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "[%u] [%s] [%s] [%s:%d %s()] %s\n", \ | ||
57 | logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__), _SFILE, __LINE__, __func__, format) | ||
58 | |||
59 | /* Use these macros */ | ||
60 | |||
61 | #define LOGGER_INIT(name, level) logger_init(name, level); | ||
62 | #define LOGGER_INFO(format, ...) do { char __time__[20]; WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
63 | #define LOGGER_DEBUG(format, ...) do { char __time__[20]; WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
64 | #define LOGGER_WARNING(format, ...) do { char __time__[20]; WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
65 | #define LOGGER_ERROR(format, ...) do { char __time__[20]; WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0) | ||
66 | |||
67 | /* To do some checks or similar only when logging use this */ | ||
68 | #define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) | ||
69 | |||
70 | #else | ||
71 | |||
72 | |||
73 | #define LOGGER_INIT(name, level) | ||
74 | #define LOGGER_INFO(format, ...) | ||
75 | #define LOGGER_DEBUG(format, ...) | ||
76 | #define LOGGER_WARNING(format, ...) | ||
77 | #define LOGGER_ERROR(format, ...) | ||
78 | |||
79 | #define LOGGER_SCOPE(__SCOPE_DO__) | ||
80 | |||
81 | #endif /* LOGGING */ | ||
82 | |||
83 | |||
84 | |||
85 | |||
86 | #endif /* __TOXLOGGER */ \ No newline at end of file | ||
diff --git a/toxcore/network.c b/toxcore/network.c index 47afab8e..7262f352 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -29,6 +29,9 @@ | |||
29 | #include "config.h" | 29 | #include "config.h" |
30 | #endif | 30 | #endif |
31 | 31 | ||
32 | #define LOGGING | ||
33 | #include "logger.h" | ||
34 | |||
32 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) | 35 | #if !defined(_WIN32) && !defined(__WIN32__) && !defined (WIN32) |
33 | #include <errno.h> | 36 | #include <errno.h> |
34 | #endif | 37 | #endif |
@@ -205,9 +208,32 @@ uint64_t random_64b(void) | |||
205 | return randnum; | 208 | return randnum; |
206 | } | 209 | } |
207 | 210 | ||
208 | #ifdef LOGGING | 211 | /* In case no logging */ |
209 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); | 212 | #ifndef LOGGING |
210 | #endif | 213 | |
214 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) | ||
215 | |||
216 | #else | ||
217 | |||
218 | #define data_0(__buflen__, __buffer__) __buflen__ > 4 ? ntohl(*(uint32_t *)&__buffer__[1]) : 0 | ||
219 | #define data_1(__buflen__, __buffer__) __buflen__ > 7 ? ntohl(*(uint32_t *)&__buffer__[5]) : 0 | ||
220 | |||
221 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) \ | ||
222 | (__ip_port__) .ip; \ | ||
223 | if (__res__ < 0) /* Windows doesn't necessarily know %zu */ \ | ||
224 | LOGGER_INFO("[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", \ | ||
225 | __buffer__[0], __message__, (__buflen__ < 999 ? (uint16_t)__buflen__ : 999), 'E', \ | ||
226 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), errno, strerror(errno), data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ | ||
227 | else if ((__res__ > 0) && ((size_t)__res__ <= __buflen__)) \ | ||
228 | LOGGER_INFO("[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", \ | ||
229 | __buffer__[0], __message__, (__res__ < 999 ? (size_t)__res__ : 999), ((size_t)__res__ < __buflen__ ? '<' : '='), \ | ||
230 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ | ||
231 | else /* empty or overwrite */ \ | ||
232 | LOGGER_INFO("[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", \ | ||
233 | __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \ | ||
234 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); | ||
235 | |||
236 | #endif /* LOGGING */ | ||
211 | 237 | ||
212 | /* Basic network functions: | 238 | /* Basic network functions: |
213 | * Function to send packet(data) of length length to ip_port. | 239 | * Function to send packet(data) of length length to ip_port. |
@@ -266,9 +292,9 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
266 | } | 292 | } |
267 | 293 | ||
268 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); | 294 | int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); |
269 | #ifdef LOGGING | 295 | |
270 | loglogdata("O=>", data, length, &ip_port, res); | 296 | loglogdata("O=>", data, length, ip_port, res); |
271 | #endif | 297 | |
272 | 298 | ||
273 | if ((res >= 0) && ((uint32_t)res == length)) | 299 | if ((res >= 0) && ((uint32_t)res == length)) |
274 | net->send_fail_eagain = 0; | 300 | net->send_fail_eagain = 0; |
@@ -297,14 +323,10 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
297 | int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 323 | int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); |
298 | 324 | ||
299 | if (fail_or_len <= 0) { | 325 | if (fail_or_len <= 0) { |
300 | #ifdef LOGGING | 326 | |
301 | 327 | LOGGER_SCOPE( if ((fail_or_len < 0) && (errno != EWOULDBLOCK)) | |
302 | if ((fail_or_len < 0) && (errno != EWOULDBLOCK)) { | 328 | LOGGER_ERROR("Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); ); |
303 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); | 329 | |
304 | loglog(logbuffer); | ||
305 | } | ||
306 | |||
307 | #endif | ||
308 | return -1; /* Nothing received or empty packet. */ | 330 | return -1; /* Nothing received or empty packet. */ |
309 | } | 331 | } |
310 | 332 | ||
@@ -329,9 +351,7 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
329 | } else | 351 | } else |
330 | return -1; | 352 | return -1; |
331 | 353 | ||
332 | #ifdef LOGGING | 354 | loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, *ip_port, *length); |
333 | loglogdata("=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); | ||
334 | #endif | ||
335 | 355 | ||
336 | return 0; | 356 | return 0; |
337 | } | 357 | } |
@@ -354,10 +374,7 @@ void networking_poll(Networking_Core *net) | |||
354 | if (length < 1) continue; | 374 | if (length < 1) continue; |
355 | 375 | ||
356 | if (!(net->packethandlers[data[0]].function)) { | 376 | if (!(net->packethandlers[data[0]].function)) { |
357 | #ifdef LOGGING | 377 | LOGGER_WARNING("[%02u] -- Packet has no handler.\n", data[0]); |
358 | sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]); | ||
359 | loglog(logbuffer); | ||
360 | #endif | ||
361 | continue; | 378 | continue; |
362 | } | 379 | } |
363 | 380 | ||
@@ -460,22 +477,14 @@ int networking_wait_execute(uint8_t *data, long seconds, long microseconds) | |||
460 | timeout.tv_usec = microseconds; | 477 | timeout.tv_usec = microseconds; |
461 | } | 478 | } |
462 | 479 | ||
463 | #ifdef LOGGING | ||
464 | errno = 0; | ||
465 | #endif | ||
466 | /* returns -1 on error, 0 on timeout, the socket on activity */ | 480 | /* returns -1 on error, 0 on timeout, the socket on activity */ |
467 | int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); | 481 | int res = select(nfds, &readfds, &writefds, &exceptfds, timeout_ptr); |
468 | #ifdef LOGGING | 482 | |
469 | 483 | LOGGER_SCOPE( | |
470 | /* only dump if not timeout */ | 484 | if (res) LOGGER_INFO("select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, |
471 | if (res) { | 485 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), |
472 | sprintf(logbuffer, "select(%d, %d): %d (%d, %s) - %d %d %d\n", microseconds, seconds, res, errno, | 486 | FD_ISSET(s->sock, &exceptfds)); |
473 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), | 487 | ); |
474 | FD_ISSET(s->sock, &exceptfds)); | ||
475 | loglog(logbuffer); | ||
476 | } | ||
477 | |||
478 | #endif | ||
479 | 488 | ||
480 | if (FD_ISSET(s->sock, &writefds)) { | 489 | if (FD_ISSET(s->sock, &writefds)) { |
481 | s->send_fail_reset = 1; | 490 | s->send_fail_reset = 1; |
@@ -628,20 +637,12 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
628 | } | 637 | } |
629 | 638 | ||
630 | if (ip.family == AF_INET6) { | 639 | if (ip.family == AF_INET6) { |
640 | |||
631 | #ifdef LOGGING | 641 | #ifdef LOGGING |
632 | int is_dualstack = | 642 | int is_dualstack = |
633 | #endif | 643 | #endif /* LOGGING */ |
634 | set_socket_dualstack(temp->sock); | 644 | set_socket_dualstack(temp->sock); |
635 | #ifdef LOGGING | 645 | LOGGER_DEBUG( "Dual-stack socket: %s", is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses" ); |
636 | |||
637 | if (is_dualstack) { | ||
638 | loglog("Dual-stack socket: enabled.\n"); | ||
639 | } else { | ||
640 | loglog("Dual-stack socket: Failed to enable, won't be able to receive from/send to IPv4 addresses.\n"); | ||
641 | } | ||
642 | |||
643 | #endif | ||
644 | |||
645 | /* multicast local nodes */ | 646 | /* multicast local nodes */ |
646 | struct ipv6_mreq mreq; | 647 | struct ipv6_mreq mreq; |
647 | memset(&mreq, 0, sizeof(mreq)); | 648 | memset(&mreq, 0, sizeof(mreq)); |
@@ -649,21 +650,13 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
649 | mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; | 650 | mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; |
650 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; | 651 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; |
651 | mreq.ipv6mr_interface = 0; | 652 | mreq.ipv6mr_interface = 0; |
653 | |||
652 | #ifdef LOGGING | 654 | #ifdef LOGGING |
653 | errno = 0; | ||
654 | int res = | 655 | int res = |
655 | #endif | 656 | #endif /* LOGGING */ |
656 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); | 657 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); |
657 | #ifdef LOGGING | 658 | |
658 | 659 | LOGGER_DEBUG(res < 0 ? "Failed to activate local multicast membership. (%u, %s)" : "Local multicast group FF02::1 joined successfully", errno, strerror(errno) ); | |
659 | if (res < 0) { | ||
660 | sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n", | ||
661 | errno, strerror(errno)); | ||
662 | loglog(logbuffer); | ||
663 | } else | ||
664 | loglog("Local multicast group FF02::1 joined successfully.\n"); | ||
665 | |||
666 | #endif | ||
667 | } | 660 | } |
668 | 661 | ||
669 | /* a hanging program or a different user might block the standard port; | 662 | /* a hanging program or a different user might block the standard port; |
@@ -691,13 +684,9 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
691 | 684 | ||
692 | if (!res) { | 685 | if (!res) { |
693 | temp->port = *portptr; | 686 | temp->port = *portptr; |
694 | #ifdef LOGGING | 687 | |
695 | loginit(temp->port); | 688 | LOGGER_DEBUG("Bound successfully to %s:%u", ip_ntoa(&ip), ntohs(temp->port)); |
696 | 689 | ||
697 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); | ||
698 | loglog(logbuffer); | ||
699 | #endif | ||
700 | |||
701 | /* errno isn't reset on success, only set on failure, the failed | 690 | /* errno isn't reset on success, only set on failure, the failed |
702 | * binds with parallel clients yield a -EPERM to the outside if | 691 | * binds with parallel clients yield a -EPERM to the outside if |
703 | * errno isn't cleared here */ | 692 | * errno isn't cleared here */ |
@@ -1038,31 +1027,3 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) | |||
1038 | 1027 | ||
1039 | return 1; | 1028 | return 1; |
1040 | }; | 1029 | }; |
1041 | |||
1042 | #ifdef LOGGING | ||
1043 | static char errmsg_ok[3] = "OK"; | ||
1044 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) | ||
1045 | { | ||
1046 | uint16_t port = ntohs(ip_port->port); | ||
1047 | uint32_t data[2]; | ||
1048 | data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; | ||
1049 | data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; | ||
1050 | |||
1051 | /* Windows doesn't necessarily know %zu */ | ||
1052 | if (res < 0) { | ||
1053 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", | ||
1054 | buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', | ||
1055 | ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); | ||
1056 | } else if ((res > 0) && ((size_t)res <= buflen)) | ||
1057 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", | ||
1058 | buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), | ||
1059 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); | ||
1060 | else /* empty or overwrite */ | ||
1061 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x\n", | ||
1062 | buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, | ||
1063 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); | ||
1064 | |||
1065 | logbuffer[sizeof(logbuffer) - 1] = 0; | ||
1066 | loglog(logbuffer); | ||
1067 | } | ||
1068 | #endif | ||
diff --git a/toxcore/tox.c b/toxcore/tox.c index c07473dd..9b99174c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #include "Messenger.h" | 28 | #include "Messenger.h" |
29 | #include "logger.h" | ||
29 | 30 | ||
30 | #define __TOX_DEFINED__ | 31 | #define __TOX_DEFINED__ |
31 | typedef struct Messenger Tox; | 32 | typedef struct Messenger Tox; |
@@ -773,6 +774,7 @@ int tox_isconnected(Tox *tox) | |||
773 | */ | 774 | */ |
774 | Tox *tox_new(uint8_t ipv6enabled) | 775 | Tox *tox_new(uint8_t ipv6enabled) |
775 | { | 776 | { |
777 | LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL); | ||
776 | return new_messenger(ipv6enabled); | 778 | return new_messenger(ipv6enabled); |
777 | } | 779 | } |
778 | 780 | ||
diff --git a/toxcore/util.c b/toxcore/util.c index d56c446e..edc611ec 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -134,87 +134,3 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
134 | 134 | ||
135 | return length == 0 ? 0 : -1; | 135 | return length == 0 ? 0 : -1; |
136 | }; | 136 | }; |
137 | |||
138 | #ifdef LOGGING | ||
139 | time_t starttime = 0; | ||
140 | size_t logbufferprelen = 0; | ||
141 | char *logbufferpredata = NULL; | ||
142 | char *logbufferprehead = NULL; | ||
143 | char logbuffer[512]; | ||
144 | static FILE *logfile = NULL; | ||
145 | void loginit(uint16_t port) | ||
146 | { | ||
147 | if (logfile) | ||
148 | fclose(logfile); | ||
149 | |||
150 | if (!starttime) { | ||
151 | unix_time_update(); | ||
152 | starttime = unix_time(); | ||
153 | } | ||
154 | |||
155 | struct tm *tm = localtime(&starttime); | ||
156 | |||
157 | /* "%F %T" might not be Windows compatible */ | ||
158 | if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) | ||
159 | sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); | ||
160 | else | ||
161 | sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); | ||
162 | |||
163 | logfile = fopen(logbuffer, "w"); | ||
164 | |||
165 | if (logbufferpredata) { | ||
166 | if (logfile) | ||
167 | fprintf(logfile, "%s", logbufferpredata); | ||
168 | |||
169 | free(logbufferpredata); | ||
170 | logbufferpredata = NULL; | ||
171 | } | ||
172 | |||
173 | }; | ||
174 | void loglog(char *text) | ||
175 | { | ||
176 | if (logfile) { | ||
177 | fprintf(logfile, "%4u %s", (uint32_t)(unix_time() - starttime), text); | ||
178 | fflush(logfile); | ||
179 | |||
180 | return; | ||
181 | } | ||
182 | |||
183 | /* log messages before file was opened: store */ | ||
184 | |||
185 | size_t len = strlen(text); | ||
186 | |||
187 | if (!starttime) { | ||
188 | unix_time_update(); | ||
189 | starttime = unix_time(); | ||
190 | |||
191 | logbufferprelen = 1024 + len - (len % 1024); | ||
192 | logbufferpredata = malloc(logbufferprelen); | ||
193 | logbufferprehead = logbufferpredata; | ||
194 | } | ||
195 | |||
196 | /* loginit() called meanwhile? (but failed to open) */ | ||
197 | if (!logbufferpredata) | ||
198 | return; | ||
199 | |||
200 | if (len + (logbufferprehead - logbufferpredata) + 16U < logbufferprelen) { | ||
201 | size_t logpos = logbufferprehead - logbufferpredata; | ||
202 | size_t lennew = logbufferprelen * 1.4; | ||
203 | logbufferpredata = realloc(logbufferpredata, lennew); | ||
204 | logbufferprehead = logbufferpredata + logpos; | ||
205 | logbufferprelen = lennew; | ||
206 | } | ||
207 | |||
208 | int written = sprintf(logbufferprehead, "%4u %s", (uint32_t)(unix_time() - starttime), text); | ||
209 | logbufferprehead += written; | ||
210 | } | ||
211 | |||
212 | void logexit() | ||
213 | { | ||
214 | if (logfile) { | ||
215 | fclose(logfile); | ||
216 | logfile = NULL; | ||
217 | } | ||
218 | }; | ||
219 | #endif | ||
220 | |||
diff --git a/toxcore/util.h b/toxcore/util.h index ae364d52..e40b6968 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -45,11 +45,4 @@ typedef int (*load_state_callback_func)(void *outer, uint8_t *data, uint32_t len | |||
45 | int load_state(load_state_callback_func load_state_callback, void *outer, | 45 | int load_state(load_state_callback_func load_state_callback, void *outer, |
46 | uint8_t *data, uint32_t length, uint16_t cookie_inner); | 46 | uint8_t *data, uint32_t length, uint16_t cookie_inner); |
47 | 47 | ||
48 | #ifdef LOGGING | ||
49 | extern char logbuffer[512]; | ||
50 | void loginit(uint16_t port); | ||
51 | void loglog(char *text); | ||
52 | void logexit(); | ||
53 | #endif | ||
54 | |||
55 | #endif /* __UTIL_H__ */ | 48 | #endif /* __UTIL_H__ */ |