summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-11-05 16:52:41 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-11-06 20:35:09 +0000
commit82515f92eecde2dffc0659519317d00459a09239 (patch)
treef17b9afe68fdd475be368623d58c7f51e8cc0ba2
parentfcc8ad943e93308b7048c4e0071ae00b54804934 (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`).
-rw-r--r--auto_tests/conference_test.c6
-rw-r--r--auto_tests/encryptsave_test.c10
-rw-r--r--auto_tests/helpers.h51
-rw-r--r--auto_tests/messenger_test.c2
-rw-r--r--auto_tests/tox_many_tcp_test.c8
-rw-r--r--auto_tests/tox_many_test.c4
-rw-r--r--auto_tests/tox_one_test.c9
-rw-r--r--auto_tests/tox_test.c9
-rw-r--r--auto_tests/toxav_basic_test.c28
-rw-r--r--auto_tests/toxav_many_test.c32
-rw-r--r--testing/Messenger_test.c2
-rw-r--r--toxcore/Messenger.c15
-rw-r--r--toxcore/Messenger.h5
-rw-r--r--toxcore/tox.api.h109
-rw-r--r--toxcore/tox.c23
-rw-r--r--toxcore/tox.h134
16 files changed, 248 insertions, 199 deletions
diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c
index fba64bcc..aa2f8e13 100644
--- a/auto_tests/conference_test.c
+++ b/auto_tests/conference_test.c
@@ -7,7 +7,6 @@
7 7
8#include <check.h> 8#include <check.h>
9#include <inttypes.h> 9#include <inttypes.h>
10#include <stdio.h>
11#include <stdlib.h> 10#include <stdlib.h>
12#include <time.h> 11#include <time.h>
13 12
@@ -91,13 +90,16 @@ group_test_restart:
91 ; 90 ;
92 91
93 Tox *toxes[NUM_GROUP_TOX]; 92 Tox *toxes[NUM_GROUP_TOX];
93 uint32_t tox_index[NUM_GROUP_TOX];
94 unsigned int i, j, k; 94 unsigned int i, j, k;
95 uint32_t to_comp = 234212; 95 uint32_t to_comp = 234212;
96 int test_run = 0; 96 int test_run = 0;
97 long long unsigned int cur_time = time(NULL); 97 long long unsigned int cur_time = time(NULL);
98 98
99 for (i = 0; i < NUM_GROUP_TOX; ++i) { 99 for (i = 0; i < NUM_GROUP_TOX; ++i) {
100 toxes[i] = tox_new(0, 0); 100 tox_index[i] = i + 1;
101 toxes[i] = tox_new_log(0, 0, &tox_index[i]);
102
101 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); 103 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
102 tox_callback_friend_request(toxes[i], &g_accept_friend_request); 104 tox_callback_friend_request(toxes[i], &g_accept_friend_request);
103 tox_callback_conference_invite(toxes[i], &print_group_invite_callback); 105 tox_callback_conference_invite(toxes[i], &print_group_invite_callback);
diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c
index 2a2827da..fcdef3e6 100644
--- a/auto_tests/encryptsave_test.c
+++ b/auto_tests/encryptsave_test.c
@@ -56,8 +56,8 @@ END_TEST
56 56
57START_TEST(test_save_friend) 57START_TEST(test_save_friend)
58{ 58{
59 Tox *tox1 = tox_new(0, 0); 59 Tox *tox1 = tox_new_log(0, 0, 0);
60 Tox *tox2 = tox_new(0, 0); 60 Tox *tox2 = tox_new_log(0, 0, 0);
61 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); 61 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
62 tox_callback_friend_request(tox2, accept_friend_request); 62 tox_callback_friend_request(tox2, accept_friend_request);
63 uint8_t address[TOX_ADDRESS_SIZE]; 63 uint8_t address[TOX_ADDRESS_SIZE];
@@ -82,7 +82,7 @@ START_TEST(test_save_friend)
82 options.savedata_length = size2; 82 options.savedata_length = size2;
83 83
84 TOX_ERR_NEW err2; 84 TOX_ERR_NEW err2;
85 Tox *tox3 = tox_new(&options, &err2); 85 Tox *tox3 = tox_new_log(&options, &err2, 0);
86 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, 86 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
87 TOX_ERR_NEW_LOAD_ENCRYPTED); 87 TOX_ERR_NEW_LOAD_ENCRYPTED);
88 ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL"); 88 ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL");
@@ -92,7 +92,7 @@ START_TEST(test_save_friend)
92 ck_assert_msg(ret, "failed to decrypt save: %u", err3); 92 ck_assert_msg(ret, "failed to decrypt save: %u", err3);
93 options.savedata_data = dec_data; 93 options.savedata_data = dec_data;
94 options.savedata_length = size; 94 options.savedata_length = size;
95 tox3 = tox_new(&options, &err2); 95 tox3 = tox_new_log(&options, &err2, 0);
96 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2); 96 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2);
97 uint8_t address2[TOX_PUBLIC_KEY_SIZE]; 97 uint8_t address2[TOX_PUBLIC_KEY_SIZE];
98 ret = tox_friend_get_public_key(tox3, 0, address2, 0); 98 ret = tox_friend_get_public_key(tox3, 0, address2, 0);
@@ -122,7 +122,7 @@ START_TEST(test_save_friend)
122 // to remove the manual check now that it's there) 122 // to remove the manual check now that it's there)
123 options.savedata_data = out1; 123 options.savedata_data = out1;
124 options.savedata_length = size; 124 options.savedata_length = size;
125 Tox *tox4 = tox_new(&options, &err2); 125 Tox *tox4 = tox_new_log(&options, &err2, 0);
126 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); 126 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
127 uint8_t address5[TOX_PUBLIC_KEY_SIZE]; 127 uint8_t address5[TOX_PUBLIC_KEY_SIZE];
128 ret = tox_friend_get_public_key(tox4, 0, address5, 0); 128 ret = tox_friend_get_public_key(tox4, 0, address5, 0);
diff --git a/auto_tests/helpers.h b/auto_tests/helpers.h
index 9e8043db..99930e90 100644
--- a/auto_tests/helpers.h
+++ b/auto_tests/helpers.h
@@ -1,7 +1,10 @@
1#ifndef TOXCORE_TEST_HELPERS_H 1#ifndef TOXCORE_TEST_HELPERS_H
2#define TOXCORE_TEST_HELPERS_H 2#define TOXCORE_TEST_HELPERS_H
3 3
4#include "../toxcore/tox.h"
5
4#include <check.h> 6#include <check.h>
7#include <stdio.h>
5 8
6#define DEFTESTCASE(NAME) \ 9#define DEFTESTCASE(NAME) \
7 TCase *tc_##NAME = tcase_create(#NAME); \ 10 TCase *tc_##NAME = tcase_create(#NAME); \
@@ -12,4 +15,52 @@
12 DEFTESTCASE(NAME) \ 15 DEFTESTCASE(NAME) \
13 tcase_set_timeout(tc_##NAME, TIMEOUT); 16 tcase_set_timeout(tc_##NAME, TIMEOUT);
14 17
18static const char *tox_log_level_name(TOX_LOG_LEVEL level)
19{
20 switch (level) {
21 case TOX_LOG_LEVEL_TRACE:
22 return "TRACE";
23
24 case TOX_LOG_LEVEL_DEBUG:
25 return "DEBUG";
26
27 case TOX_LOG_LEVEL_INFO:
28 return "INFO";
29
30 case TOX_LOG_LEVEL_WARNING:
31 return "WARNING";
32
33 case TOX_LOG_LEVEL_ERROR:
34 return "ERROR";
35 }
36}
37
38static void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *path, uint32_t line, const char *func,
39 const char *message, void *user_data)
40{
41 if (level == TOX_LOG_LEVEL_TRACE) {
42 return;
43 }
44
45 uint32_t index = user_data ? *(uint32_t *)user_data : 0;
46 const char *file = strrchr(path, '/');
47 file = file ? file + 1 : path;
48 printf("[#%d] %s %s:%d\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
49}
50
51Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data)
52{
53 struct Tox_Options *my_options = tox_options_new(NULL);
54
55 if (options != NULL) {
56 *my_options = *options;
57 }
58
59 tox_options_set_log_callback(my_options, &print_debug_log);
60 tox_options_set_log_user_data(my_options, log_user_data);
61 Tox *tox = tox_new(my_options, err);
62 tox_options_free(my_options);
63 return tox;
64}
65
15#endif // TOXCORE_TEST_HELPERS_H 66#endif // TOXCORE_TEST_HELPERS_H
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c
index 5d0be284..eed52a31 100644
--- a/auto_tests/messenger_test.c
+++ b/auto_tests/messenger_test.c
@@ -342,7 +342,7 @@ int main(int argc, char *argv[])
342 /* IPv6 status from global define */ 342 /* IPv6 status from global define */
343 Messenger_Options options = {0}; 343 Messenger_Options options = {0};
344 options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; 344 options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT;
345 m = new_messenger(NULL, &options, 0); 345 m = new_messenger(&options, 0);
346 346
347 /* setup a default friend and friendnum */ 347 /* setup a default friend and friendnum */
348 if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) { 348 if (m_addfriend_norequest(m, (uint8_t *)friend_id) < 0) {
diff --git a/auto_tests/tox_many_tcp_test.c b/auto_tests/tox_many_tcp_test.c
index 59d69c49..f802bf57 100644
--- a/auto_tests/tox_many_tcp_test.c
+++ b/auto_tests/tox_many_tcp_test.c
@@ -50,6 +50,7 @@ START_TEST(test_many_clients_tcp)
50{ 50{
51 long long unsigned int cur_time = time(NULL); 51 long long unsigned int cur_time = time(NULL);
52 Tox *toxes[NUM_TOXES_TCP]; 52 Tox *toxes[NUM_TOXES_TCP];
53 uint32_t index[NUM_TOXES_TCP];
53 uint32_t i, j; 54 uint32_t i, j;
54 uint32_t to_comp = 974536; 55 uint32_t to_comp = 974536;
55 56
@@ -63,7 +64,8 @@ START_TEST(test_many_clients_tcp)
63 opts.udp_enabled = 0; 64 opts.udp_enabled = 0;
64 } 65 }
65 66
66 toxes[i] = tox_new(&opts, 0); 67 index[i] = i + 1;
68 toxes[i] = tox_new_log(&opts, 0, &index[i]);
67 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); 69 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
68 tox_callback_friend_request(toxes[i], accept_friend_request); 70 tox_callback_friend_request(toxes[i], accept_friend_request);
69 uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; 71 uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
@@ -149,6 +151,7 @@ START_TEST(test_many_clients_tcp_b)
149{ 151{
150 long long unsigned int cur_time = time(NULL); 152 long long unsigned int cur_time = time(NULL);
151 Tox *toxes[NUM_TOXES_TCP]; 153 Tox *toxes[NUM_TOXES_TCP];
154 uint32_t index[NUM_TOXES_TCP];
152 uint32_t i, j; 155 uint32_t i, j;
153 uint32_t to_comp = 974536; 156 uint32_t to_comp = 974536;
154 157
@@ -162,7 +165,8 @@ START_TEST(test_many_clients_tcp_b)
162 opts.udp_enabled = 0; 165 opts.udp_enabled = 0;
163 } 166 }
164 167
165 toxes[i] = tox_new(&opts, 0); 168 index[i] = i + 1;
169 toxes[i] = tox_new_log(&opts, 0, &index[i]);
166 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); 170 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
167 tox_callback_friend_request(toxes[i], accept_friend_request); 171 tox_callback_friend_request(toxes[i], accept_friend_request);
168 uint8_t dpk[TOX_PUBLIC_KEY_SIZE]; 172 uint8_t dpk[TOX_PUBLIC_KEY_SIZE];
diff --git a/auto_tests/tox_many_test.c b/auto_tests/tox_many_test.c
index fa657580..0c4c8c7d 100644
--- a/auto_tests/tox_many_test.c
+++ b/auto_tests/tox_many_test.c
@@ -42,11 +42,13 @@ START_TEST(test_many_clients)
42{ 42{
43 long long unsigned int cur_time = time(NULL); 43 long long unsigned int cur_time = time(NULL);
44 Tox *toxes[NUM_TOXES]; 44 Tox *toxes[NUM_TOXES];
45 uint32_t index[NUM_TOXES];
45 uint32_t i, j; 46 uint32_t i, j;
46 uint32_t to_comp = 974536; 47 uint32_t to_comp = 974536;
47 48
48 for (i = 0; i < NUM_TOXES; ++i) { 49 for (i = 0; i < NUM_TOXES; ++i) {
49 toxes[i] = tox_new(0, 0); 50 index[i] = i + 1;
51 toxes[i] = tox_new_log(0, 0, &index[i]);
50 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); 52 ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i);
51 tox_callback_friend_request(toxes[i], accept_friend_request); 53 tox_callback_friend_request(toxes[i], accept_friend_request);
52 } 54 }
diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c
index 1248de19..871aa443 100644
--- a/auto_tests/tox_one_test.c
+++ b/auto_tests/tox_one_test.c
@@ -26,8 +26,9 @@ START_TEST(test_one)
26 tox_options_free(o1); 26 tox_options_free(o1);
27 } 27 }
28 28
29 Tox *tox1 = tox_new(0, 0); 29 uint32_t index[] = { 1, 2 };
30 Tox *tox2 = tox_new(0, 0); 30 Tox *tox1 = tox_new_log(0, 0, &index[0]);
31 Tox *tox2 = tox_new_log(0, 0, &index[1]);
31 32
32 { 33 {
33 TOX_ERR_GET_PORT error; 34 TOX_ERR_GET_PORT error;
@@ -85,7 +86,7 @@ START_TEST(test_one)
85 options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; 86 options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
86 options.savedata_data = data; 87 options.savedata_data = data;
87 options.savedata_length = save_size; 88 options.savedata_length = save_size;
88 tox2 = tox_new(&options, &err_n); 89 tox2 = tox_new_log(&options, &err_n, &index[1]);
89 ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); 90 ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");
90 91
91 ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size."); 92 ck_assert_msg(tox_self_get_name_size(tox2) == sizeof name, "Wrong name size.");
@@ -105,7 +106,7 @@ START_TEST(test_one)
105 options.savedata_type = TOX_SAVEDATA_TYPE_SECRET_KEY; 106 options.savedata_type = TOX_SAVEDATA_TYPE_SECRET_KEY;
106 options.savedata_data = sk; 107 options.savedata_data = sk;
107 options.savedata_length = sizeof(sk); 108 options.savedata_length = sizeof(sk);
108 tox2 = tox_new(&options, &err_n); 109 tox2 = tox_new_log(&options, &err_n, &index[1]);
109 ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed"); 110 ck_assert_msg(err_n == TOX_ERR_NEW_OK, "Load failed");
110 uint8_t address3[TOX_ADDRESS_SIZE]; 111 uint8_t address3[TOX_ADDRESS_SIZE];
111 tox_self_get_address(tox2, address3); 112 tox_self_get_address(tox2, address3);
diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c
index 1bcb8d5e..2faa8cad 100644
--- a/auto_tests/tox_test.c
+++ b/auto_tests/tox_test.c
@@ -321,13 +321,14 @@ static void tox_connection_status(Tox *tox, TOX_CONNECTION connection_status, vo
321 321
322START_TEST(test_few_clients) 322START_TEST(test_few_clients)
323{ 323{
324 uint32_t index[] = { 1, 2, 3 };
324 long long unsigned int con_time = 0, cur_time = time(NULL); 325 long long unsigned int con_time = 0, cur_time = time(NULL);
325 TOX_ERR_NEW t_n_error; 326 TOX_ERR_NEW t_n_error;
326 Tox *tox1 = tox_new(0, &t_n_error); 327 Tox *tox1 = tox_new_log(0, &t_n_error, &index[0]);
327 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); 328 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
328 Tox *tox2 = tox_new(0, &t_n_error); 329 Tox *tox2 = tox_new_log(0, &t_n_error, &index[1]);
329 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); 330 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
330 Tox *tox3 = tox_new(0, &t_n_error); 331 Tox *tox3 = tox_new_log(0, &t_n_error, &index[2]);
331 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error"); 332 ck_assert_msg(t_n_error == TOX_ERR_NEW_OK, "wrong error");
332 333
333 ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances"); 334 ck_assert_msg(tox1 && tox2 && tox3, "Failed to create 3 tox instances");
@@ -422,7 +423,7 @@ START_TEST(test_few_clients)
422 options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE; 423 options.savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
423 options.savedata_data = save1; 424 options.savedata_data = save1;
424 options.savedata_length = save_size1; 425 options.savedata_length = save_size1;
425 tox2 = tox_new(&options, NULL); 426 tox2 = tox_new_log(&options, NULL, &index[1]);
426 cur_time = time(NULL); 427 cur_time = time(NULL);
427 off = 1; 428 off = 1;
428 429
diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c
index 860ad402..b5028da4 100644
--- a/auto_tests/toxav_basic_test.c
+++ b/auto_tests/toxav_basic_test.c
@@ -2,15 +2,7 @@
2#include "config.h" 2#include "config.h"
3#endif 3#endif
4 4
5#ifndef HAVE_LIBCHECK 5#include "helpers.h"
6# include <assert.h>
7
8# define ck_assert(X) assert(X);
9# define START_TEST(NAME) static void NAME (void)
10# define END_TEST
11#else
12# include "helpers.h"
13#endif
14 6
15#include <stdint.h> 7#include <stdint.h>
16#include <stdio.h> 8#include <stdio.h>
@@ -149,19 +141,20 @@ START_TEST(test_AV_flows)
149{ 141{
150 Tox *Alice, *Bob, *bootstrap; 142 Tox *Alice, *Bob, *bootstrap;
151 ToxAV *AliceAV, *BobAV; 143 ToxAV *AliceAV, *BobAV;
144 uint32_t index[] = { 1, 2, 3 };
152 145
153 CallControl AliceCC, BobCC; 146 CallControl AliceCC, BobCC;
154 147
155 { 148 {
156 TOX_ERR_NEW error; 149 TOX_ERR_NEW error;
157 150
158 bootstrap = tox_new(NULL, &error); 151 bootstrap = tox_new_log(NULL, &error, &index[0]);
159 ck_assert(error == TOX_ERR_NEW_OK); 152 ck_assert(error == TOX_ERR_NEW_OK);
160 153
161 Alice = tox_new(NULL, &error); 154 Alice = tox_new_log(NULL, &error, &index[1]);
162 ck_assert(error == TOX_ERR_NEW_OK); 155 ck_assert(error == TOX_ERR_NEW_OK);
163 156
164 Bob = tox_new(NULL, &error); 157 Bob = tox_new_log(NULL, &error, &index[2]);
165 ck_assert(error == TOX_ERR_NEW_OK); 158 ck_assert(error == TOX_ERR_NEW_OK);
166 } 159 }
167 160
@@ -591,16 +584,6 @@ START_TEST(test_AV_flows)
591} 584}
592END_TEST 585END_TEST
593 586
594#ifndef HAVE_LIBCHECK
595int main(int argc, char *argv[])
596{
597 (void) argc;
598 (void) argv;
599
600 test_AV_flows();
601 return 0;
602}
603#else
604static Suite *tox_suite(void) 587static Suite *tox_suite(void)
605{ 588{
606 Suite *s = suite_create("ToxAV"); 589 Suite *s = suite_create("ToxAV");
@@ -625,4 +608,3 @@ int main(int argc, char *argv[])
625 608
626 return number_failed; 609 return number_failed;
627} 610}
628#endif
diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c
index 6f864f07..a351129a 100644
--- a/auto_tests/toxav_many_test.c
+++ b/auto_tests/toxav_many_test.c
@@ -2,15 +2,7 @@
2#include "config.h" 2#include "config.h"
3#endif 3#endif
4 4
5#ifndef HAVE_LIBCHECK 5#include "helpers.h"
6# include <assert.h>
7
8# define ck_assert(X) assert(X);
9# define START_TEST(NAME) static void NAME (void)
10# define END_TEST
11#else
12# include "helpers.h"
13#endif
14 6
15#include <stdint.h> 7#include <stdint.h>
16#include <stdio.h> 8#include <stdio.h>
@@ -207,6 +199,7 @@ static void *call_thread(void *pd)
207 199
208START_TEST(test_AV_three_calls) 200START_TEST(test_AV_three_calls)
209{ 201{
202 uint32_t index[] = { 1, 2, 3, 4, 5 };
210 Tox *Alice, *bootstrap, *Bobs[3]; 203 Tox *Alice, *bootstrap, *Bobs[3];
211 ToxAV *AliceAV, *BobsAV[3]; 204 ToxAV *AliceAV, *BobsAV[3];
212 205
@@ -215,19 +208,19 @@ START_TEST(test_AV_three_calls)
215 { 208 {
216 TOX_ERR_NEW error; 209 TOX_ERR_NEW error;
217 210
218 bootstrap = tox_new(NULL, &error); 211 bootstrap = tox_new_log(NULL, &error, &index[0]);
219 ck_assert(error == TOX_ERR_NEW_OK); 212 ck_assert(error == TOX_ERR_NEW_OK);
220 213
221 Alice = tox_new(NULL, &error); 214 Alice = tox_new_log(NULL, &error, &index[1]);
222 ck_assert(error == TOX_ERR_NEW_OK); 215 ck_assert(error == TOX_ERR_NEW_OK);
223 216
224 Bobs[0] = tox_new(NULL, &error); 217 Bobs[0] = tox_new_log(NULL, &error, &index[2]);
225 ck_assert(error == TOX_ERR_NEW_OK); 218 ck_assert(error == TOX_ERR_NEW_OK);
226 219
227 Bobs[1] = tox_new(NULL, &error); 220 Bobs[1] = tox_new_log(NULL, &error, &index[3]);
228 ck_assert(error == TOX_ERR_NEW_OK); 221 ck_assert(error == TOX_ERR_NEW_OK);
229 222
230 Bobs[2] = tox_new(NULL, &error); 223 Bobs[2] = tox_new_log(NULL, &error, &index[4]);
231 ck_assert(error == TOX_ERR_NEW_OK); 224 ck_assert(error == TOX_ERR_NEW_OK);
232 } 225 }
233 226
@@ -341,16 +334,6 @@ START_TEST(test_AV_three_calls)
341END_TEST 334END_TEST
342 335
343 336
344#ifndef HAVE_LIBCHECK
345int main(int argc, char *argv[])
346{
347 (void) argc;
348 (void) argv;
349
350 test_AV_three_calls();
351 return 0;
352}
353#else
354static Suite *tox_suite(void) 337static Suite *tox_suite(void)
355{ 338{
356 Suite *s = suite_create("ToxAV"); 339 Suite *s = suite_create("ToxAV");
@@ -380,4 +363,3 @@ int main(int argc, char *argv[])
380 363
381 return number_failed; 364 return number_failed;
382} 365}
383#endif
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c
index 9f56c903..4fd81649 100644
--- a/testing/Messenger_test.c
+++ b/testing/Messenger_test.c
@@ -115,7 +115,7 @@ int main(int argc, char *argv[])
115 115
116 Messenger_Options options = {0}; 116 Messenger_Options options = {0};
117 options.ipv6enabled = ipv6enabled; 117 options.ipv6enabled = ipv6enabled;
118 m = new_messenger(NULL, &options, 0); 118 m = new_messenger(&options, 0);
119 119
120 if (!m) { 120 if (!m) {
121 fputs("Failed to allocate messenger datastructure\n", stderr); 121 fputs("Failed to allocate messenger datastructure\n", stderr);
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. */
1888Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int *error) 1888Messenger *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 */
730Messenger *new_messenger(Logger *log, Messenger_Options *options, unsigned int *error); 733Messenger *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 */
382enum 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 */
422typedef void log_cb(LOG_LEVEL level, string file, uint32_t line, string func, string message, any user_data);
423
424
379static class options { 425static 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) {
620void kill(); 678void kill();
621 679
622 680
623/**
624 * Severity level of log messages.
625 */
626enum 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 */
661event 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
674uint8_t[size] savedata { 681uint8_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)
133ACCESSORS(uint16_t, , tcp_port) 133ACCESSORS(uint16_t, , tcp_port)
134ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type) 134ACCESSORS(TOX_SAVEDATA_TYPE, savedata_, type)
135ACCESSORS(size_t, savedata_, length) 135ACCESSORS(size_t, savedata_, length)
136ACCESSORS(tox_log_cb *, log_, callback)
137ACCESSORS(void *, log_, user_data)
136 138
137const uint8_t *tox_options_get_savedata_data(const struct Tox_Options *options) 139const 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
311void 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
317size_t tox_get_savedata_size(const Tox *tox) 306size_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 */
424typedef 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 */
471typedef 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
588void tox_options_set_savedata_length(struct Tox_Options *options, size_t length); 654void tox_options_set_savedata_length(struct Tox_Options *options, size_t length);
589 655
656tox_log_cb *tox_options_get_log_callback(const struct Tox_Options *options);
657
658void tox_options_set_log_callback(struct Tox_Options *options, tox_log_cb *callback);
659
660void *tox_options_get_log_user_data(const struct Tox_Options *options);
661
662void 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);
735void tox_kill(Tox *tox); 809void tox_kill(Tox *tox);
736 810
737/** 811/**
738 * Severity level of log messages.
739 */
740typedef 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 */
777typedef 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 */
795void 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 *