diff options
author | iphydf <iphydf@users.noreply.github.com> | 2016-11-05 16:52:41 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2016-11-06 20:35:09 +0000 |
commit | 82515f92eecde2dffc0659519317d00459a09239 (patch) | |
tree | f17b9afe68fdd475be368623d58c7f51e8cc0ba2 /toxcore | |
parent | fcc8ad943e93308b7048c4e0071ae00b54804934 (diff) |
Move log callback to options.
Previously, all log messages generated by tox_new (which is quite a lot)
were dropped, because client code had no chance to register a logging
callback, yet. This change allows setting the log callback from the
beginning and removes the ability to unset it.
Since the log callback is forever special, since it can't be stateless,
we don't necessarily need to treat it uniformly (with `event`).
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 15 | ||||
-rw-r--r-- | toxcore/Messenger.h | 5 | ||||
-rw-r--r-- | toxcore/tox.api.h | 109 | ||||
-rw-r--r-- | toxcore/tox.c | 23 | ||||
-rw-r--r-- | toxcore/tox.h | 134 |
5 files changed, 155 insertions, 131 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 0a3c52ee..b147e698 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1885,7 +1885,7 @@ static int friend_already_added(const uint8_t *real_pk, void *data) | |||
1885 | } | 1885 | } |
1886 | 1886 | ||
1887 | /* Run this at startup. */ | 1887 | /* Run this at startup. */ |
1888 | Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int *error) | 1888 | Messenger *new_messenger(Messenger_Options *options, unsigned int *error) |
1889 | { | 1889 | { |
1890 | Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); | 1890 | Messenger *m = (Messenger *)calloc(1, sizeof(Messenger)); |
1891 | 1891 | ||
@@ -1893,10 +1893,20 @@ Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int * | |||
1893 | *error = MESSENGER_ERROR_OTHER; | 1893 | *error = MESSENGER_ERROR_OTHER; |
1894 | } | 1894 | } |
1895 | 1895 | ||
1896 | if (! m) { | 1896 | if (!m) { |
1897 | return NULL; | 1897 | return NULL; |
1898 | } | 1898 | } |
1899 | 1899 | ||
1900 | Logger *log = NULL; | ||
1901 | |||
1902 | if (options && options->log_callback) { | ||
1903 | log = logger_new(); | ||
1904 | |||
1905 | if (log != NULL) { | ||
1906 | logger_callback_log(log, options->log_callback, m, options->log_user_data); | ||
1907 | } | ||
1908 | } | ||
1909 | |||
1900 | m->log = log; | 1910 | m->log = log; |
1901 | 1911 | ||
1902 | unsigned int net_err = 0; | 1912 | unsigned int net_err = 0; |
@@ -2012,6 +2022,7 @@ void kill_messenger(Messenger *m) | |||
2012 | clear_receipts(m, i); | 2022 | clear_receipts(m, i); |
2013 | } | 2023 | } |
2014 | 2024 | ||
2025 | logger_kill(m->log); | ||
2015 | free(m->friendlist); | 2026 | free(m->friendlist); |
2016 | free(m); | 2027 | free(m); |
2017 | } | 2028 | } |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 4bcd618f..23bd0009 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -76,6 +76,9 @@ typedef struct { | |||
76 | TCP_Proxy_Info proxy_info; | 76 | TCP_Proxy_Info proxy_info; |
77 | uint16_t port_range[2]; | 77 | uint16_t port_range[2]; |
78 | uint16_t tcp_server_port; | 78 | uint16_t tcp_server_port; |
79 | |||
80 | logger_cb *log_callback; | ||
81 | void *log_user_data; | ||
79 | } Messenger_Options; | 82 | } Messenger_Options; |
80 | 83 | ||
81 | 84 | ||
@@ -727,7 +730,7 @@ enum { | |||
727 | * | 730 | * |
728 | * if error is not NULL it will be set to one of the values in the enum above. | 731 | * if error is not NULL it will be set to one of the values in the enum above. |
729 | */ | 732 | */ |
730 | Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int *error); | 733 | Messenger *new_messenger(Messenger_Options *options, unsigned int *error); |
731 | 734 | ||
732 | /* Run this before closing shop | 735 | /* Run this before closing shop |
733 | * Free all datastructures. | 736 | * Free all datastructures. |
diff --git a/toxcore/tox.api.h b/toxcore/tox.api.h index 0a651c11..fd908a49 100644 --- a/toxcore/tox.api.h +++ b/toxcore/tox.api.h | |||
@@ -376,6 +376,52 @@ enum class SAVEDATA_TYPE { | |||
376 | } | 376 | } |
377 | 377 | ||
378 | 378 | ||
379 | /** | ||
380 | * Severity level of log messages. | ||
381 | */ | ||
382 | enum class LOG_LEVEL { | ||
383 | /** | ||
384 | * Very detailed traces including all network activity. | ||
385 | */ | ||
386 | TRACE, | ||
387 | /** | ||
388 | * Debug messages such as which port we bind to. | ||
389 | */ | ||
390 | DEBUG, | ||
391 | /** | ||
392 | * Informational log messages such as video call status changes. | ||
393 | */ | ||
394 | INFO, | ||
395 | /** | ||
396 | * Warnings about internal inconsistency or logic errors. | ||
397 | */ | ||
398 | WARNING, | ||
399 | /** | ||
400 | * Severe unexpected errors caused by external or internal inconsistency. | ||
401 | */ | ||
402 | ERROR, | ||
403 | } | ||
404 | |||
405 | /** | ||
406 | * This event is triggered when the toxcore library logs an internal message. | ||
407 | * This is mostly useful for debugging. This callback can be called from any | ||
408 | * function, not just $iterate. This means the user data lifetime must at | ||
409 | * least extend between registering and unregistering it or $kill. | ||
410 | * | ||
411 | * Other toxcore modules such as toxav may concurrently call this callback at | ||
412 | * any time. Thus, user code must make sure it is equipped to handle concurrent | ||
413 | * execution, e.g. by employing appropriate mutex locking. | ||
414 | * | ||
415 | * @param level The severity of the log message. | ||
416 | * @param file The source file from which the message originated. | ||
417 | * @param line The source line from which the message originated. | ||
418 | * @param func The function from which the message originated. | ||
419 | * @param message The log message. | ||
420 | * @param user_data The user data pointer passed to $new in options. | ||
421 | */ | ||
422 | typedef void log_cb(LOG_LEVEL level, string file, uint32_t line, string func, string message, any user_data); | ||
423 | |||
424 | |||
379 | static class options { | 425 | static class options { |
380 | /** | 426 | /** |
381 | * This struct contains all the startup options for Tox. You can either | 427 | * This struct contains all the startup options for Tox. You can either |
@@ -491,6 +537,18 @@ static class options { | |||
491 | */ | 537 | */ |
492 | size_t length; | 538 | size_t length; |
493 | } | 539 | } |
540 | |||
541 | namespace log { | ||
542 | /** | ||
543 | * Logging callback for the new tox instance. | ||
544 | */ | ||
545 | log_cb *callback; | ||
546 | |||
547 | /** | ||
548 | * User data pointer passed to the logging callback. | ||
549 | */ | ||
550 | any user_data; | ||
551 | } | ||
494 | } | 552 | } |
495 | 553 | ||
496 | 554 | ||
@@ -620,57 +678,6 @@ static this new(const options_t *options) { | |||
620 | void kill(); | 678 | void kill(); |
621 | 679 | ||
622 | 680 | ||
623 | /** | ||
624 | * Severity level of log messages. | ||
625 | */ | ||
626 | enum class LOG_LEVEL { | ||
627 | /** | ||
628 | * Very detailed traces including all network activity. | ||
629 | */ | ||
630 | TRACE, | ||
631 | /** | ||
632 | * Debug messages such as which port we bind to. | ||
633 | */ | ||
634 | DEBUG, | ||
635 | /** | ||
636 | * Informational log messages such as video call status changes. | ||
637 | */ | ||
638 | INFO, | ||
639 | /** | ||
640 | * Warnings about internal inconsistency or logic errors. | ||
641 | */ | ||
642 | WARNING, | ||
643 | /** | ||
644 | * Severe unexpected errors caused by external or internal inconsistency. | ||
645 | */ | ||
646 | ERROR, | ||
647 | } | ||
648 | |||
649 | /** | ||
650 | * This event is triggered when the toxcore library logs an internal message. | ||
651 | * This is mostly useful for debugging. This callback can be called from any | ||
652 | * function, not just $iterate. This means the user data lifetime must at | ||
653 | * least extend between registering and unregistering it or $kill. | ||
654 | * | ||
655 | * Other toxcore modules such as toxav may concurrently call this callback at | ||
656 | * any time. Thus, user code must make sure it is equipped to handle concurrent | ||
657 | * execution, e.g. by employing appropriate mutex locking. The callback | ||
658 | * registration function must not be called during execution of any other Tox | ||
659 | * library function (toxcore or toxav). | ||
660 | */ | ||
661 | event log { | ||
662 | /** | ||
663 | * @param level The severity of the log message. | ||
664 | * @param file The source file from which the message originated. | ||
665 | * @param line The source line from which the message originated. | ||
666 | * @param func The function from which the message originated. | ||
667 | * @param message The log message. | ||
668 | */ | ||
669 | typedef void(LOG_LEVEL level, string file, uint32_t line, string func, | ||
670 | string message); | ||
671 | } | ||
672 | |||
673 | |||
674 | uint8_t[size] savedata { | 681 | uint8_t[size] savedata { |
675 | /** | 682 | /** |
676 | * Calculates the number of bytes required to store the tox instance with | 683 | * Calculates the number of bytes required to store the tox instance with |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 29bbfba0..7898868b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -133,6 +133,8 @@ ACCESSORS(uint16_t, , end_port) | |||
133 | ACCESSORS(uint16_t, , tcp_port) | 133 | ACCESSORS(uint16_t, , tcp_port) |
134 | ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) | 134 | ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) |
135 | ACCESSORS(size_t, savedata_, length) | 135 | ACCESSORS(size_t, savedata_, length) |
136 | ACCESSORS(tox_log_cb *, log_, callback) | ||
137 | ACCESSORS(void *, log_, user_data) | ||
136 | 138 | ||
137 | const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) | 139 | const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) |
138 | { | 140 | { |
@@ -218,6 +220,9 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
218 | m_options.port_range[1] = options->end_port; | 220 | m_options.port_range[1] = options->end_port; |
219 | m_options.tcp_server_port = options->tcp_port; | 221 | m_options.tcp_server_port = options->tcp_port; |
220 | 222 | ||
223 | m_options.log_callback = (logger_cb *)options->log_callback; | ||
224 | m_options.log_user_data = options->log_user_data; | ||
225 | |||
221 | switch (options->proxy_type) { | 226 | switch (options->proxy_type) { |
222 | case TOX_PROXY_TYPE_HTTP: | 227 | case TOX_PROXY_TYPE_HTTP: |
223 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; | 228 | m_options.proxy_info.proxy_type = TCP_PROXY_HTTP; |
@@ -258,19 +263,11 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) | |||
258 | } | 263 | } |
259 | } | 264 | } |
260 | 265 | ||
261 | Logger *log = logger_new(); | ||
262 | |||
263 | if (log == NULL) { | ||
264 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); | ||
265 | return NULL; | ||
266 | } | ||
267 | |||
268 | unsigned int m_error; | 266 | unsigned int m_error; |
269 | Messenger *m = new_messenger(log, &m_options, &m_error); | 267 | Messenger *m = new_messenger(&m_options, &m_error); |
270 | 268 | ||
271 | if (!new_groupchats(m)) { | 269 | if (!new_groupchats(m)) { |
272 | kill_messenger(m); | 270 | kill_messenger(m); |
273 | logger_kill(log); | ||
274 | 271 | ||
275 | if (m_error == MESSENGER_ERROR_PORT) { | 272 | if (m_error == MESSENGER_ERROR_PORT) { |
276 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC); | 273 | SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PORT_ALLOC); |
@@ -302,16 +299,8 @@ void tox_kill(Tox *tox) | |||
302 | } | 299 | } |
303 | 300 | ||
304 | Messenger *m = tox; | 301 | Messenger *m = tox; |
305 | Logger *log = m->log; | ||
306 | kill_groupchats((Group_Chats *)m->conferences_object); | 302 | kill_groupchats((Group_Chats *)m->conferences_object); |
307 | kill_messenger(m); | 303 | kill_messenger(m); |
308 | logger_kill(log); | ||
309 | } | ||
310 | |||
311 | void tox_callback_log(Tox *tox, tox_log_cb *callback, void *user_data) | ||
312 | { | ||
313 | Messenger *m = tox; | ||
314 | m_callback_log(m, (logger_cb *)callback, tox, user_data); | ||
315 | } | 304 | } |
316 | 305 | ||
317 | size_t tox_get_savedata_size(const Tox *tox) | 306 | size_t tox_get_savedata_size(const Tox *tox) |
diff --git a/toxcore/tox.h b/toxcore/tox.h index d97d94c7..4c5266fa 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -419,6 +419,60 @@ typedef enum TOX_SAVEDATA_TYPE { | |||
419 | 419 | ||
420 | 420 | ||
421 | /** | 421 | /** |
422 | * Severity level of log messages. | ||
423 | */ | ||
424 | typedef enum TOX_LOG_LEVEL { | ||
425 | |||
426 | /** | ||
427 | * Very detailed traces including all network activity. | ||
428 | */ | ||
429 | TOX_LOG_LEVEL_TRACE, | ||
430 | |||
431 | /** | ||
432 | * Debug messages such as which port we bind to. | ||
433 | */ | ||
434 | TOX_LOG_LEVEL_DEBUG, | ||
435 | |||
436 | /** | ||
437 | * Informational log messages such as video call status changes. | ||
438 | */ | ||
439 | TOX_LOG_LEVEL_INFO, | ||
440 | |||
441 | /** | ||
442 | * Warnings about internal inconsistency or logic errors. | ||
443 | */ | ||
444 | TOX_LOG_LEVEL_WARNING, | ||
445 | |||
446 | /** | ||
447 | * Severe unexpected errors caused by external or internal inconsistency. | ||
448 | */ | ||
449 | TOX_LOG_LEVEL_ERROR, | ||
450 | |||
451 | } TOX_LOG_LEVEL; | ||
452 | |||
453 | |||
454 | /** | ||
455 | * This event is triggered when the toxcore library logs an internal message. | ||
456 | * This is mostly useful for debugging. This callback can be called from any | ||
457 | * function, not just tox_iterate. This means the user data lifetime must at | ||
458 | * least extend between registering and unregistering it or tox_kill. | ||
459 | * | ||
460 | * Other toxcore modules such as toxav may concurrently call this callback at | ||
461 | * any time. Thus, user code must make sure it is equipped to handle concurrent | ||
462 | * execution, e.g. by employing appropriate mutex locking. | ||
463 | * | ||
464 | * @param level The severity of the log message. | ||
465 | * @param file The source file from which the message originated. | ||
466 | * @param line The source line from which the message originated. | ||
467 | * @param func The function from which the message originated. | ||
468 | * @param message The log message. | ||
469 | * @param user_data The user data pointer passed to tox_new in options. | ||
470 | */ | ||
471 | typedef void tox_log_cb(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, | ||
472 | const char *message, void *user_data); | ||
473 | |||
474 | |||
475 | /** | ||
422 | * This struct contains all the startup options for Tox. You can either | 476 | * This struct contains all the startup options for Tox. You can either |
423 | * allocate this object yourself, and pass it to tox_options_default, or call tox_options_new to get | 477 | * allocate this object yourself, and pass it to tox_options_default, or call tox_options_new to get |
424 | * a new default options object. | 478 | * a new default options object. |
@@ -540,6 +594,18 @@ struct Tox_Options { | |||
540 | */ | 594 | */ |
541 | size_t savedata_length; | 595 | size_t savedata_length; |
542 | 596 | ||
597 | |||
598 | /** | ||
599 | * Logging callback for the new tox instance. | ||
600 | */ | ||
601 | tox_log_cb *log_callback; | ||
602 | |||
603 | |||
604 | /** | ||
605 | * User data pointer passed to the logging callback. | ||
606 | */ | ||
607 | void *log_user_data; | ||
608 | |||
543 | }; | 609 | }; |
544 | 610 | ||
545 | 611 | ||
@@ -587,6 +653,14 @@ size_t tox_options_get_savedata_length(const struct Tox_Options *options); | |||
587 | 653 | ||
588 | void tox_options_set_savedata_length(struct Tox_Options *options, size_t length); | 654 | void tox_options_set_savedata_length(struct Tox_Options *options, size_t length); |
589 | 655 | ||
656 | tox_log_cb *tox_options_get_log_callback(const struct Tox_Options *options); | ||
657 | |||
658 | void tox_options_set_log_callback(struct Tox_Options *options, tox_log_cb *callback); | ||
659 | |||
660 | void *tox_options_get_log_user_data(const struct Tox_Options *options); | ||
661 | |||
662 | void tox_options_set_log_user_data(struct Tox_Options *options, void *user_data); | ||
663 | |||
590 | /** | 664 | /** |
591 | * Initialises a Tox_Options object with the default options. | 665 | * Initialises a Tox_Options object with the default options. |
592 | * | 666 | * |
@@ -735,66 +809,6 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error); | |||
735 | void tox_kill(Tox *tox); | 809 | void tox_kill(Tox *tox); |
736 | 810 | ||
737 | /** | 811 | /** |
738 | * Severity level of log messages. | ||
739 | */ | ||
740 | typedef enum TOX_LOG_LEVEL { | ||
741 | |||
742 | /** | ||
743 | * Very detailed traces including all network activity. | ||
744 | */ | ||
745 | TOX_LOG_LEVEL_TRACE, | ||
746 | |||
747 | /** | ||
748 | * Debug messages such as which port we bind to. | ||
749 | */ | ||
750 | TOX_LOG_LEVEL_DEBUG, | ||
751 | |||
752 | /** | ||
753 | * Informational log messages such as video call status changes. | ||
754 | */ | ||
755 | TOX_LOG_LEVEL_INFO, | ||
756 | |||
757 | /** | ||
758 | * Warnings about internal inconsistency or logic errors. | ||
759 | */ | ||
760 | TOX_LOG_LEVEL_WARNING, | ||
761 | |||
762 | /** | ||
763 | * Severe unexpected errors caused by external or internal inconsistency. | ||
764 | */ | ||
765 | TOX_LOG_LEVEL_ERROR, | ||
766 | |||
767 | } TOX_LOG_LEVEL; | ||
768 | |||
769 | |||
770 | /** | ||
771 | * @param level The severity of the log message. | ||
772 | * @param file The source file from which the message originated. | ||
773 | * @param line The source line from which the message originated. | ||
774 | * @param func The function from which the message originated. | ||
775 | * @param message The log message. | ||
776 | */ | ||
777 | typedef void tox_log_cb(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, | ||
778 | const char *message, void *user_data); | ||
779 | |||
780 | |||
781 | /** | ||
782 | * Set the callback for the `log` event. Pass NULL to unset. | ||
783 | * | ||
784 | * This event is triggered when the toxcore library logs an internal message. | ||
785 | * This is mostly useful for debugging. This callback can be called from any | ||
786 | * function, not just tox_iterate. This means the user data lifetime must at | ||
787 | * least extend between registering and unregistering it or tox_kill. | ||
788 | * | ||
789 | * Other toxcore modules such as toxav may concurrently call this callback at | ||
790 | * any time. Thus, user code must make sure it is equipped to handle concurrent | ||
791 | * execution, e.g. by employing appropriate mutex locking. The callback | ||
792 | * registration function must not be called during execution of any other Tox | ||
793 | * library function (toxcore or toxav). | ||
794 | */ | ||
795 | void tox_callback_log(Tox *tox, tox_log_cb *callback, void *user_data); | ||
796 | |||
797 | /** | ||
798 | * Calculates the number of bytes required to store the tox instance with | 812 | * Calculates the number of bytes required to store the tox instance with |
799 | * tox_get_savedata. This function cannot fail. The result is always greater than 0. | 813 | * tox_get_savedata. This function cannot fail. The result is always greater than 0. |
800 | * | 814 | * |