diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-08-19 13:07:45 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-08-27 01:16:14 +0100 |
commit | 13ae9e9a93a1c02fad9475002c0391b86b7ad7bb (patch) | |
tree | a9575d3582c4f40e051c93ae18dded03fdddc432 /toxcore/Messenger.c | |
parent | 1f25fc0ae417bfc47dea4966cb5e43689aa88d5c (diff) |
Move logging to a callback.
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.
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b87e61c6..a1e4c110 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -762,6 +762,11 @@ static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t | |||
762 | m->friendlist[friendnumber].is_typing = is_typing; | 762 | m->friendlist[friendnumber].is_typing = is_typing; |
763 | } | 763 | } |
764 | 764 | ||
765 | void m_callback_log(Messenger *m, logger_cb *function, void *userdata) | ||
766 | { | ||
767 | logger_callback_log(m->log, function, userdata); | ||
768 | } | ||
769 | |||
765 | /* Set the function that will be executed when a friend request is received. */ | 770 | /* Set the function that will be executed when a friend request is received. */ |
766 | void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t, | 771 | void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const uint8_t *, const uint8_t *, size_t, |
767 | void *), void *userdata) | 772 | void *), void *userdata) |
@@ -1740,7 +1745,7 @@ static int friend_already_added(const uint8_t *real_pk, void *data) | |||
1740 | } | 1745 | } |
1741 | 1746 | ||
1742 | /* Run this at startup. */ | 1747 | /* Run this at startup. */ |
1743 | Messenger *new_messenger(Messenger_Options *options, unsigned int *error) | 1748 | Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int *error) |
1744 | { | 1749 | { |
1745 | Messenger *m = calloc(1, sizeof(Messenger)); | 1750 | Messenger *m = calloc(1, sizeof(Messenger)); |
1746 | 1751 | ||
@@ -1750,6 +1755,8 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) | |||
1750 | if ( ! m ) | 1755 | if ( ! m ) |
1751 | return NULL; | 1756 | return NULL; |
1752 | 1757 | ||
1758 | m->log = log; | ||
1759 | |||
1753 | unsigned int net_err = 0; | 1760 | unsigned int net_err = 0; |
1754 | 1761 | ||
1755 | if (options->udp_disabled) { | 1762 | if (options->udp_disabled) { |
@@ -1758,7 +1765,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) | |||
1758 | } else { | 1765 | } else { |
1759 | IP ip; | 1766 | IP ip; |
1760 | ip_init(&ip, options->ipv6enabled); | 1767 | ip_init(&ip, options->ipv6enabled); |
1761 | m->net = new_networking_ex(ip, options->port_range[0], options->port_range[1], &net_err); | 1768 | m->net = new_networking_ex(log, ip, options->port_range[0], options->port_range[1], &net_err); |
1762 | } | 1769 | } |
1763 | 1770 | ||
1764 | if (m->net == NULL) { | 1771 | if (m->net == NULL) { |
@@ -1771,7 +1778,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) | |||
1771 | return NULL; | 1778 | return NULL; |
1772 | } | 1779 | } |
1773 | 1780 | ||
1774 | m->dht = new_DHT(m->net); | 1781 | m->dht = new_DHT(m->log, m->net); |
1775 | 1782 | ||
1776 | if (m->dht == NULL) { | 1783 | if (m->dht == NULL) { |
1777 | kill_networking(m->net); | 1784 | kill_networking(m->net); |
@@ -1779,7 +1786,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) | |||
1779 | return NULL; | 1786 | return NULL; |
1780 | } | 1787 | } |
1781 | 1788 | ||
1782 | m->net_crypto = new_net_crypto(m->dht, &options->proxy_info); | 1789 | m->net_crypto = new_net_crypto(m->log, m->dht, &options->proxy_info); |
1783 | 1790 | ||
1784 | if (m->net_crypto == NULL) { | 1791 | if (m->net_crypto == NULL) { |
1785 | kill_networking(m->net); | 1792 | kill_networking(m->net); |
@@ -2238,7 +2245,6 @@ static void connection_status_cb(Messenger *m, void *userdata) | |||
2238 | } | 2245 | } |
2239 | 2246 | ||
2240 | 2247 | ||
2241 | #ifdef TOX_LOGGER | ||
2242 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL | 2248 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL |
2243 | static time_t lastdump = 0; | 2249 | static time_t lastdump = 0; |
2244 | static char IDString[crypto_box_PUBLICKEYBYTES * 2 + 1]; | 2250 | static char IDString[crypto_box_PUBLICKEYBYTES * 2 + 1]; |
@@ -2252,7 +2258,6 @@ static char *ID2String(const uint8_t *pk) | |||
2252 | IDString[crypto_box_PUBLICKEYBYTES * 2] = 0; | 2258 | IDString[crypto_box_PUBLICKEYBYTES * 2] = 0; |
2253 | return IDString; | 2259 | return IDString; |
2254 | } | 2260 | } |
2255 | #endif | ||
2256 | 2261 | ||
2257 | /* Minimum messenger run interval in ms | 2262 | /* Minimum messenger run interval in ms |
2258 | TODO: A/V */ | 2263 | TODO: A/V */ |
@@ -2314,8 +2319,6 @@ void do_messenger(Messenger *m, void *userdata) | |||
2314 | do_friends(m, userdata); | 2319 | do_friends(m, userdata); |
2315 | connection_status_cb(m, userdata); | 2320 | connection_status_cb(m, userdata); |
2316 | 2321 | ||
2317 | #ifdef TOX_LOGGER | ||
2318 | |||
2319 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { | 2322 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { |
2320 | 2323 | ||
2321 | #ifdef ENABLE_ASSOC_DHT | 2324 | #ifdef ENABLE_ASSOC_DHT |
@@ -2337,7 +2340,7 @@ void do_messenger(Messenger *m, void *userdata) | |||
2337 | if (last_pinged > 999) | 2340 | if (last_pinged > 999) |
2338 | last_pinged = 999; | 2341 | last_pinged = 999; |
2339 | 2342 | ||
2340 | LOGGER_TRACE("C[%2u] %s:%u [%3u] %s", | 2343 | LOGGER_TRACE(m->log, "C[%2u] %s:%u [%3u] %s", |
2341 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), | 2344 | client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), |
2342 | last_pinged, ID2String(cptr->public_key)); | 2345 | last_pinged, ID2String(cptr->public_key)); |
2343 | } | 2346 | } |
@@ -2370,7 +2373,7 @@ void do_messenger(Messenger *m, void *userdata) | |||
2370 | dht2m[m2dht[friend]] = friend; | 2373 | dht2m[m2dht[friend]] = friend; |
2371 | 2374 | ||
2372 | if (m->numfriends != m->dht->num_friends) { | 2375 | if (m->numfriends != m->dht->num_friends) { |
2373 | LOGGER_TRACE("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); | 2376 | LOGGER_TRACE(m->log, "Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); |
2374 | } | 2377 | } |
2375 | 2378 | ||
2376 | Friend *msgfptr; | 2379 | Friend *msgfptr; |
@@ -2385,11 +2388,11 @@ void do_messenger(Messenger *m, void *userdata) | |||
2385 | dhtfptr = &m->dht->friends_list[friend]; | 2388 | dhtfptr = &m->dht->friends_list[friend]; |
2386 | 2389 | ||
2387 | if (msgfptr) { | 2390 | if (msgfptr) { |
2388 | LOGGER_TRACE("F[%2u:%2u] <%s> %s", | 2391 | LOGGER_TRACE(m->log, "F[%2u:%2u] <%s> %s", |
2389 | dht2m[friend], friend, msgfptr->name, | 2392 | dht2m[friend], friend, msgfptr->name, |
2390 | ID2String(msgfptr->real_pk)); | 2393 | ID2String(msgfptr->real_pk)); |
2391 | } else { | 2394 | } else { |
2392 | LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->public_key)); | 2395 | LOGGER_TRACE(m->log, "F[--:%2u] %s", friend, ID2String(dhtfptr->public_key)); |
2393 | } | 2396 | } |
2394 | 2397 | ||
2395 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { | 2398 | for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { |
@@ -2404,7 +2407,7 @@ void do_messenger(Messenger *m, void *userdata) | |||
2404 | if (last_pinged > 999) | 2407 | if (last_pinged > 999) |
2405 | last_pinged = 999; | 2408 | last_pinged = 999; |
2406 | 2409 | ||
2407 | LOGGER_TRACE("F[%2u] => C[%2u] %s:%u [%3u] %s", | 2410 | LOGGER_TRACE(m->log, "F[%2u] => C[%2u] %s:%u [%3u] %s", |
2408 | friend, client, ip_ntoa(&assoc->ip_port.ip), | 2411 | friend, client, ip_ntoa(&assoc->ip_port.ip), |
2409 | ntohs(assoc->ip_port.port), last_pinged, | 2412 | ntohs(assoc->ip_port.port), last_pinged, |
2410 | ID2String(cptr->public_key)); | 2413 | ID2String(cptr->public_key)); |
@@ -2412,8 +2415,6 @@ void do_messenger(Messenger *m, void *userdata) | |||
2412 | } | 2415 | } |
2413 | } | 2416 | } |
2414 | } | 2417 | } |
2415 | |||
2416 | #endif /* TOX_LOGGER */ | ||
2417 | } | 2418 | } |
2418 | 2419 | ||
2419 | /* new messenger format for load/save, more robust and forward compatible */ | 2420 | /* new messenger format for load/save, more robust and forward compatible */ |