summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2014-05-24 16:02:01 +0200
committermannol <eniz_vukovic@hotmail.com>2014-05-24 16:02:01 +0200
commit565e95301f19884b3597404f41c06566cf5f9dc0 (patch)
tree162864fe2d4dabe70c10ad7a624f52d76abad56c
parent9db41e7582baf15d650376e8afc38dd6146492a8 (diff)
Tests works and some fixes
-rw-r--r--auto_tests/Makefile.inc28
-rw-r--r--auto_tests/toxav_basic_test.c86
-rw-r--r--auto_tests/toxav_many_test.c21
-rwxr-xr-xtoxav/msi.c84
-rwxr-xr-xtoxav/msi.h6
-rwxr-xr-xtoxav/rtp.c6
-rwxr-xr-xtoxav/toxav.c5
-rwxr-xr-xtoxav/toxav.h2
-rw-r--r--toxcore/logger.c19
-rw-r--r--toxcore/logger.h14
10 files changed, 164 insertions, 107 deletions
diff --git a/auto_tests/Makefile.inc b/auto_tests/Makefile.inc
index 5e101f60..55a52541 100644
--- a/auto_tests/Makefile.inc
+++ b/auto_tests/Makefile.inc
@@ -1,9 +1,7 @@
1if BUILD_TESTS 1if BUILD_TESTS
2 2
3TESTS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test 3TESTS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test
4#toxav_basic_test toxav_many_test 4check_PROGRAMS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test
5check_PROGRAMS = messenger_autotest crypto_test network_test assoc_test onion_test TCP_test tox_test
6#toxav_basic_test toxav_many_test
7 5
8AUTOTEST_CFLAGS = \ 6AUTOTEST_CFLAGS = \
9 $(LIBSODIUM_CFLAGS) \ 7 $(LIBSODIUM_CFLAGS) \
@@ -20,11 +18,11 @@ AUTOTEST_LDADD = \
20 $(CHECK_LIBS) 18 $(CHECK_LIBS)
21 19
22 20
23#Tests don't work atm 21
24if BUILD_AV 22if BUILD_AV
25#TESTS += toxav_basic_test 23TESTS += toxav_basic_test toxav_many_test
26#check_PROGRAMS += toxav_basic_test 24check_PROGRAMS += toxav_basic_test toxav_many_test
27#AUTOTEST_LDADD += libtoxav.la 25AUTOTEST_LDADD += libtoxav.la
28endif 26endif
29 27
30messenger_autotest_SOURCES = ../auto_tests/messenger_test.c 28messenger_autotest_SOURCES = ../auto_tests/messenger_test.c
@@ -76,20 +74,20 @@ tox_test_CFLAGS = $(AUTOTEST_CFLAGS)
76tox_test_LDADD = $(AUTOTEST_LDADD) 74tox_test_LDADD = $(AUTOTEST_LDADD)
77 75
78 76
79#Tests don't work atm 77
80if BUILD_AV 78if BUILD_AV
81#toxav_basic_test_SOURCES = ../auto_tests/toxav_basic_test.c 79toxav_basic_test_SOURCES = ../auto_tests/toxav_basic_test.c
82 80
83#toxav_basic_test_CFLAGS = $(AUTOTEST_CFLAGS) 81toxav_basic_test_CFLAGS = $(AUTOTEST_CFLAGS)
84 82
85#toxav_basic_test_LDADD = $(AUTOTEST_LDADD) 83toxav_basic_test_LDADD = $(AUTOTEST_LDADD)
86 84
87 85
88#toxav_many_test_SOURCES = ../auto_tests/toxav_many_test.c 86toxav_many_test_SOURCES = ../auto_tests/toxav_many_test.c
89 87
90#toxav_many_test_CFLAGS = $(AUTOTEST_CFLAGS) 88toxav_many_test_CFLAGS = $(AUTOTEST_CFLAGS)
91 89
92#toxav_many_test_LDADD = $(AUTOTEST_LDADD) 90toxav_many_test_LDADD = $(AUTOTEST_LDADD)
93endif 91endif
94 92
95endif 93endif
diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c
index c0162582..116a519a 100644
--- a/auto_tests/toxav_basic_test.c
+++ b/auto_tests/toxav_basic_test.c
@@ -13,6 +13,7 @@
13 13
14#include "../toxcore/tox.h" 14#include "../toxcore/tox.h"
15#include "../toxcore/logger.h" 15#include "../toxcore/logger.h"
16#include "../toxcore/crypto_core.h"
16#include "../toxav/toxav.h" 17#include "../toxav/toxav.h"
17 18
18#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 19#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@@ -159,6 +160,7 @@ case 3: /* Wait for Both to have status ended */\
159if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n"); 160if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n");
160 161
161START_TEST(test_AV_flows) 162START_TEST(test_AV_flows)
163// int test_AV_flows()
162{ 164{
163 long long unsigned int cur_time = time(NULL); 165 long long unsigned int cur_time = time(NULL);
164 Tox *bootstrap_node = tox_new(0); 166 Tox *bootstrap_node = tox_new(0);
@@ -200,11 +202,10 @@ START_TEST(test_AV_flows)
200 muhcaps.video_height = muhcaps.video_width = 128; 202 muhcaps.video_height = muhcaps.video_width = 128;
201 203
202 Status status_control = { 204 Status status_control = {
203 {none, toxav_new(Alice, 1), NULL}, 205 {none, toxav_new(Alice, 1), NULL, -1},
204 {none, toxav_new(Bob, 1), NULL}, 206 {none, toxav_new(Bob, 1), NULL, -1},
205 }; 207 };
206 208
207
208 ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances"); 209 ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances");
209 210
210 211
@@ -222,9 +223,11 @@ START_TEST(test_AV_flows)
222 toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control); 223 toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control);
223 224
224 225
225 226 const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
226 int16_t sample_payload[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 227 int16_t sample_payload[frame_size];
227 uint8_t prepared_payload[1000]; 228 randombytes_salsa20_random_buf(sample_payload, sizeof(int16_t) * frame_size);
229
230 uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
228 int payload_size; 231 int payload_size;
229 232
230 vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1); 233 vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1);
@@ -241,30 +244,34 @@ START_TEST(test_AV_flows)
241 /* 244 /*
242 * Call with audio only on both sides. Alice calls Bob. 245 * Call with audio only on both sides. Alice calls Bob.
243 */ 246 */
247
248
244 CALL_AND_START_LOOP(TypeAudio, TypeAudio) { 249 CALL_AND_START_LOOP(TypeAudio, TypeAudio) {
245 /* Both send */ 250 /* Both send */
246 int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120); 251 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
247 if (!( payload_size > 0 )) { /* FIXME: this will always fail */ 252 if ( payload_size < 0 ) {
248 ck_assert_msg ( 0, "Failed to encode payload" ); 253 ck_assert_msg ( 0, "Failed to encode payload" );
249 } 254 }
250
251 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 255 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
256
257 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
258 if ( payload_size < 0 ) {
259 ck_assert_msg ( 0, "Failed to encode payload" );
260 }
252 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 261 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
253 262
254 /* Both receive */ 263 /* Both receive */
255 int16_t storage[10]; 264 int16_t storage[frame_size];
256 int recved; 265 int recved;
257 266
258 /* Payload from Bob */ 267 /* Payload from Bob */
259 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); 268 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
260 269
261 if ( recved ) { 270 if ( recved ) {
262 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ 271 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
263 memset(storage, 0, 10);
264 } 272 }
265 273
266 /* Payload from Alice */ 274 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
267 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage);
268 275
269 if ( recved ) { 276 if ( recved ) {
270 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ 277 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@@ -286,28 +293,30 @@ START_TEST(test_AV_flows)
286 * Call with audio on both sides and video on one side. Alice calls Bob. 293 * Call with audio on both sides and video on one side. Alice calls Bob.
287 */ 294 */
288 CALL_AND_START_LOOP(TypeAudio, TypeVideo) { 295 CALL_AND_START_LOOP(TypeAudio, TypeVideo) {
289 /* Both send */ 296 /* Both send */
290 int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120); 297 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
291 if (!( payload_size > 0 )) { /* FIXME: this will always fail */ 298 if ( payload_size < 0 ) {
292 ck_assert_msg ( 0, "Failed to encode payload" ); 299 ck_assert_msg ( 0, "Failed to encode payload" );
293 } 300 }
294
295 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 301 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
296 302
303 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
304 if ( payload_size < 0 ) {
305 ck_assert_msg ( 0, "Failed to encode payload" );
306 }
297 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 307 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
298// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image); 308// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
299 309
300 /* Both receive */ 310 /* Both receive */
301 int16_t storage[10]; 311 int16_t storage[frame_size];
302 vpx_image_t *video_storage; 312 vpx_image_t *video_storage;
303 int recved; 313 int recved;
304 314
305 /* Payload from Bob */ 315 /* Payload from Bob */
306 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); 316 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
307 317
308 if ( recved ) { 318 if ( recved ) {
309 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ 319 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
310 memset(storage, 0, 10);
311 } 320 }
312 321
313 /* Video payload */ 322 /* Video payload */
@@ -324,7 +333,7 @@ START_TEST(test_AV_flows)
324 333
325 334
326 /* Payload from Alice */ 335 /* Payload from Alice */
327 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage); 336 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
328 337
329 if ( recved ) { 338 if ( recved ) {
330 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ 339 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@@ -347,28 +356,32 @@ START_TEST(test_AV_flows)
347 */ 356 */
348 CALL_AND_START_LOOP(TypeVideo, TypeVideo) { 357 CALL_AND_START_LOOP(TypeVideo, TypeVideo) {
349 /* Both send */ 358 /* Both send */
350 int payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, 120); 359
351 if (!( payload_size > 0 )) { /* FIXME: this will always fail */ 360 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 1000, sample_payload, frame_size);
361 if ( payload_size < 0 ) {
352 ck_assert_msg ( 0, "Failed to encode payload" ); 362 ck_assert_msg ( 0, "Failed to encode payload" );
353 } 363 }
364 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
354 365
355 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 366 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, sample_payload, frame_size);
356// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image); 367 if ( payload_size < 0 ) {
357 368 ck_assert_msg ( 0, "Failed to encode payload" );
369 }
358 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 370 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
371
372// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image);
359// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image); 373// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
360 374
361 /* Both receive */ 375 /* Both receive */
362 int16_t storage[10]; 376 int16_t storage[frame_size];
363 vpx_image_t *video_storage; 377 vpx_image_t *video_storage;
364 int recved; 378 int recved;
365 379
366 /* Payload from Bob */ 380 /* Payload from Bob */
367 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, 120, storage); 381 recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
368 382
369 if ( recved ) { 383 if ( recved ) {
370 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/ 384 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
371 memset(storage, 0, 10);
372 } 385 }
373 386
374 /* Video payload */ 387 /* Video payload */
@@ -385,7 +398,7 @@ START_TEST(test_AV_flows)
385 398
386 399
387 /* Payload from Alice */ 400 /* Payload from Alice */
388 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, 120, storage); 401 recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
389 402
390 if ( recved ) { 403 if ( recved ) {
391 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/ 404 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
@@ -437,7 +450,6 @@ START_TEST(test_AV_flows)
437 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10); 450 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10);
438 step++; 451 step++;
439 break; 452 break;
440 \
441 453
442 case 1: /* Bob */ 454 case 1: /* Bob */
443 if (status_control.Bob.status == Ringing) { 455 if (status_control.Bob.status == Ringing) {
@@ -491,7 +503,7 @@ START_TEST(test_AV_flows)
491 break; 503 break;
492 504
493 case 2: /* Wait for Both to have status ended */ 505 case 2: /* Wait for Both to have status ended */
494 if (status_control.Alice.status == Ended && status_control.Bob.status == Cancel) running = 0; 506 if (status_control.Bob.status == Cancel) running = 0;
495 507
496 break; 508 break;
497 } 509 }
@@ -521,7 +533,7 @@ Suite *tox_suite(void)
521 533
522 TCase *tc_av_flows = tcase_create("AV_flows"); 534 TCase *tc_av_flows = tcase_create("AV_flows");
523 tcase_add_test(tc_av_flows, test_AV_flows); 535 tcase_add_test(tc_av_flows, test_AV_flows);
524 tcase_set_timeout(tc_av_flows, 100); /* Timeout on 100 too much? */ 536 tcase_set_timeout(tc_av_flows, 200);
525 suite_add_tcase(s, tc_av_flows); 537 suite_add_tcase(s, tc_av_flows);
526 538
527 return s; 539 return s;
@@ -539,4 +551,6 @@ int main(int argc, char *argv[])
539 srunner_free(test_runner); 551 srunner_free(test_runner);
540 552
541 return number_failed; 553 return number_failed;
554
555// return test_AV_flows();
542} 556}
diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c
index b499b439..f9a24012 100644
--- a/auto_tests/toxav_many_test.c
+++ b/auto_tests/toxav_many_test.c
@@ -13,6 +13,7 @@
13 13
14#include "../toxcore/tox.h" 14#include "../toxcore/tox.h"
15#include "../toxcore/logger.h" 15#include "../toxcore/logger.h"
16#include "../toxcore/crypto_core.h"
16#include "../toxav/toxav.h" 17#include "../toxav/toxav.h"
17 18
18#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 19#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@@ -131,9 +132,11 @@ void* in_thread_call (void* arg)
131 int step = 0,running = 1; 132 int step = 0,running = 1;
132 int call_idx; 133 int call_idx;
133 134
135 const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
136 int16_t sample_payload[frame_size];
137 randombytes_salsa20_random_buf(sample_payload, sizeof(int16_t) * frame_size);
134 138
135 int16_t sample_payload[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 139 uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
136 uint8_t prepared_payload[1000];
137 140
138 141
139 /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */ 142 /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */
@@ -162,37 +165,37 @@ void* in_thread_call (void* arg)
162 toxav_prepare_transmission(this_call->Callee.av, 0, &cast, 1); 165 toxav_prepare_transmission(this_call->Callee.av, 0, &cast, 1);
163 toxav_prepare_transmission(this_call->Caller.av, call_idx, &cast, 1); 166 toxav_prepare_transmission(this_call->Caller.av, call_idx, &cast, 1);
164 167
165 int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, 1000, sample_payload, 120); 168 int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, RTP_PAYLOAD_SIZE, sample_payload, frame_size);
166 if (!( payload_size > 0 )) { 169 if ( payload_size < 0 ) {
167 ck_assert_msg ( 0, "Failed to encode payload" ); 170 ck_assert_msg ( 0, "Failed to encode payload" );
168 } 171 }
169 172
173
170 while (time(NULL) - start < 10) { /* 10 seconds */ 174 while (time(NULL) - start < 10) { /* 10 seconds */
171 /* Both send */ 175 /* Both send */
172 toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size); 176 toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size);
173 177
174 toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size); 178 toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size);
175 179
176 call_print(time(NULL) - start, "Blaaah");
177 /* Both receive */ 180 /* Both receive */
178 int16_t storage[1000]; 181 int16_t storage[RTP_PAYLOAD_SIZE];
179 int recved; 182 int recved;
180 183
181 /* Payload from CALLER */ 184 /* Payload from CALLER */
182 recved = toxav_recv_audio(this_call->Callee.av, 0, 120, storage); 185 recved = toxav_recv_audio(this_call->Callee.av, 0, frame_size, storage);
183 186
184 if ( recved ) { 187 if ( recved ) {
185 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLER is invalid");*/ 188 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLER is invalid");*/
186 } 189 }
187 190
188 /* Payload from CALLEE */ 191 /* Payload from CALLEE */
189 recved = toxav_recv_audio(this_call->Caller.av, call_idx, 120, storage); 192 recved = toxav_recv_audio(this_call->Caller.av, call_idx, frame_size, storage);
190 193
191 if ( recved ) { 194 if ( recved ) {
192 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLEE is invalid");*/ 195 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLEE is invalid");*/
193 } 196 }
194 197
195 //c_sleep(20); 198 c_sleep(20);
196 } 199 }
197 200
198 step++; /* This terminates the loop */ 201 step++; /* This terminates the loop */
diff --git a/toxav/msi.c b/toxav/msi.c
index fd92e784..048b13eb 100755
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -792,8 +792,10 @@ MSICall* find_call ( MSISession* session, uint8_t* call_id )
792 792
793 uint32_t i = 0; 793 uint32_t i = 0;
794 for (; i < session->max_calls; i ++ ) 794 for (; i < session->max_calls; i ++ )
795 if ( session->calls[i] && memcmp(session->calls[i]->id, call_id, CALL_ID_LEN) == 0 ) 795 if ( session->calls[i] && memcmp(session->calls[i]->id, call_id, CALL_ID_LEN) == 0 ) {
796 LOGGER_DEBUG("Found call id: %s", session->calls[i]->id);
796 return session->calls[i]; 797 return session->calls[i];
798 }
797 799
798 return NULL; 800 return NULL;
799} 801}
@@ -843,8 +845,9 @@ int has_call_error ( MSISession *session, MSICall* call, MSIMessage *msg )
843 return handle_error ( session, call, error_no_callid, msg->friend_id ); 845 return handle_error ( session, call, error_no_callid, msg->friend_id );
844 846
845 } else if ( !call ) { 847 } else if ( !call ) {
846 return handle_error ( session, call, error_no_call, msg->friend_id ); 848 LOGGER_WARNING("Handling message while no call!");
847 849 return 0;
850
848 } else if ( memcmp ( call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) { 851 } else if ( memcmp ( call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) {
849 return handle_error ( session, call, error_id_mismatch, msg->friend_id ); 852 return handle_error ( session, call, error_id_mismatch, msg->friend_id );
850 853
@@ -957,7 +960,10 @@ int terminate_call ( MSISession *session, MSICall *call )
957 LOGGER_WARNING("Tried to terminate non-existing call!"); 960 LOGGER_WARNING("Tried to terminate non-existing call!");
958 return -1; 961 return -1;
959 } 962 }
960 963
964 int rc = pthread_mutex_trylock(&session->mutex); /* Lock if not locked */
965
966 LOGGER_DEBUG("Terminated call id: %d", call->call_idx);
961 /* Check event loop and cancel timed events if there are any 967 /* Check event loop and cancel timed events if there are any
962 * NOTE: This has to be done before possibly 968 * NOTE: This has to be done before possibly
963 * locking the mutex the second time 969 * locking the mutex the second time
@@ -982,6 +988,9 @@ int terminate_call ( MSISession *session, MSICall *call )
982 988
983 free ( call ); 989 free ( call );
984 990
991 if ( rc != EBUSY ) /* Unlock if locked by this call */
992 pthread_mutex_unlock(&session->mutex);
993
985 return 0; 994 return 0;
986} 995}
987 996
@@ -1159,10 +1168,10 @@ int handle_recv_end ( MSISession *session, MSICall* call, MSIMessage *msg )
1159/********** Response handlers **********/ 1168/********** Response handlers **********/
1160int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg ) 1169int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg )
1161{ 1170{
1162 LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %s", session, call->id );
1163
1164 if ( has_call_error ( session, call, msg ) == 0 ) 1171 if ( has_call_error ( session, call, msg ) == 0 )
1165 return 0; 1172 return 0;
1173
1174 LOGGER_DEBUG("Session: %p Handling 'ringing' on call: %s", session, call->id );
1166 1175
1167 call->ringing_timer_id = event.timer_alloc ( handle_timeout, call, call->ringing_tout_ms ); 1176 call->ringing_timer_id = event.timer_alloc ( handle_timeout, call, call->ringing_tout_ms );
1168 1177
@@ -1172,10 +1181,11 @@ int handle_recv_ringing ( MSISession *session, MSICall* call, MSIMessage *msg )
1172} 1181}
1173int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg ) 1182int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg )
1174{ 1183{
1175 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
1176
1177 if ( has_call_error ( session, call, msg ) == 0 ) 1184 if ( has_call_error ( session, call, msg ) == 0 )
1178 return 0; 1185 return 0;
1186
1187 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
1188
1179 1189
1180 if ( !msg->cryptokey.header_value ) { 1190 if ( !msg->cryptokey.header_value ) {
1181 return handle_error ( session, call, error_no_crypto_key, msg->friend_id ); 1191 return handle_error ( session, call, error_no_crypto_key, msg->friend_id );
@@ -1213,11 +1223,11 @@ int handle_recv_starting ( MSISession *session, MSICall* call, MSIMessage *msg )
1213} 1223}
1214int handle_recv_ending ( MSISession *session, MSICall* call, MSIMessage *msg ) 1224int handle_recv_ending ( MSISession *session, MSICall* call, MSIMessage *msg )
1215{ 1225{
1216 LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
1217
1218 if ( has_call_error ( session, call, msg ) == 0 ) 1226 if ( has_call_error ( session, call, msg ) == 0 )
1219 return 0; 1227 return 0;
1220 1228
1229 LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
1230
1221 /* Stop timer */ 1231 /* Stop timer */
1222 event.timer_release ( call->request_timer_id ); 1232 event.timer_release ( call->request_timer_id );
1223 1233
@@ -1308,9 +1318,13 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
1308 1318
1309 _msg->friend_id = source; 1319 _msg->friend_id = source;
1310 1320
1321 pthread_mutex_lock(&_session->mutex);
1322
1311 /* Find what call */ 1323 /* Find what call */
1312 MSICall* _call = _msg->callid.header_value ? find_call(_session, _msg->callid.header_value ) : NULL; 1324 MSICall* _call = _msg->callid.header_value ? find_call(_session, _msg->callid.header_value ) : NULL;
1313 1325
1326
1327
1314 /* Now handle message */ 1328 /* Now handle message */
1315 1329
1316 if ( _msg->request.header_value ) { /* Handle request */ 1330 if ( _msg->request.header_value ) { /* Handle request */
@@ -1381,7 +1395,9 @@ void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16
1381 LOGGER_WARNING("Invalid message: no resp nor requ headers"); 1395 LOGGER_WARNING("Invalid message: no resp nor requ headers");
1382 } 1396 }
1383 1397
1384 free_end:free_message ( _msg ); 1398free_end:
1399 free_message ( _msg );
1400 pthread_mutex_unlock(&_session->mutex);
1385} 1401}
1386 1402
1387 1403
@@ -1438,6 +1454,8 @@ MSISession *msi_init_session ( Messenger* messenger, int32_t max_calls )
1438 /* This is called when remote terminates session */ 1454 /* This is called when remote terminates session */
1439 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu); 1455 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu);
1440 1456
1457 pthread_mutex_init(&_retu->mutex, NULL);
1458
1441 LOGGER_DEBUG("New msi session: %p max calls: %u", _retu, max_calls); 1459 LOGGER_DEBUG("New msi session: %p max calls: %u", _retu, max_calls);
1442 return _retu; 1460 return _retu;
1443} 1461}
@@ -1456,6 +1474,10 @@ int msi_terminate_session ( MSISession *session )
1456 return -1; 1474 return -1;
1457 } 1475 }
1458 1476
1477 pthread_mutex_lock(&session->mutex);
1478 m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
1479 pthread_mutex_unlock(&session->mutex);
1480
1459 int _status = 0; 1481 int _status = 0;
1460 1482
1461 /* If have calls, cancel them */ 1483 /* If have calls, cancel them */
@@ -1467,7 +1489,8 @@ int msi_terminate_session ( MSISession *session )
1467 msi_cancel ( session, idx, session->calls[idx]->peers [_it], "MSI session terminated!" ); 1489 msi_cancel ( session, idx, session->calls[idx]->peers [_it], "MSI session terminated!" );
1468 } 1490 }
1469 1491
1470 m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL); 1492
1493 pthread_mutex_destroy(&session->mutex);
1471 1494
1472 LOGGER_DEBUG("Terminated session: %p", session); 1495 LOGGER_DEBUG("Terminated session: %p", session);
1473 free ( session ); 1496 free ( session );
@@ -1486,12 +1509,17 @@ int msi_terminate_session ( MSISession *session )
1486 */ 1509 */
1487int msi_invite ( MSISession* session, int32_t* call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) 1510int msi_invite ( MSISession* session, int32_t* call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
1488{ 1511{
1512 pthread_mutex_lock(&session->mutex);
1513
1489 LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); 1514 LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
1490 1515
1491 MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); 1516 MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
1492 1517
1493 MSICall* _call = init_call ( session, 1, rngsec ); /* Just one peer for now */ 1518 MSICall* _call = init_call ( session, 1, rngsec ); /* Just one peer for now */
1494 if ( !_call ) return -1; /* Cannot handle more calls */ 1519 if ( !_call ) {
1520 pthread_mutex_unlock(&session->mutex);
1521 return -1; /* Cannot handle more calls */
1522 }
1495 1523
1496 *call_index = _call->call_idx; 1524 *call_index = _call->call_idx;
1497 1525
@@ -1517,6 +1545,8 @@ int msi_invite ( MSISession* session, int32_t* call_index, MSICallType call_type
1517 1545
1518 LOGGER_DEBUG("Invite sent"); 1546 LOGGER_DEBUG("Invite sent");
1519 1547
1548 pthread_mutex_unlock(&session->mutex);
1549
1520 return 0; 1550 return 0;
1521} 1551}
1522 1552
@@ -1532,15 +1562,18 @@ int msi_invite ( MSISession* session, int32_t* call_index, MSICallType call_type
1532 */ 1562 */
1533int msi_hangup ( MSISession* session, int32_t call_index ) 1563int msi_hangup ( MSISession* session, int32_t call_index )
1534{ 1564{
1565 pthread_mutex_lock(&session->mutex);
1535 LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index); 1566 LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
1536 1567
1537 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1568 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1538 LOGGER_ERROR("Invalid call index!"); 1569 LOGGER_ERROR("Invalid call index!");
1570 pthread_mutex_unlock(&session->mutex);
1539 return -1; 1571 return -1;
1540 } 1572 }
1541 1573
1542 if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) { 1574 if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) {
1543 LOGGER_ERROR("No call with such index or call is not active!"); 1575 LOGGER_ERROR("No call with such index or call is not active!");
1576 pthread_mutex_unlock(&session->mutex);
1544 return -1; 1577 return -1;
1545 } 1578 }
1546 1579
@@ -1557,6 +1590,7 @@ int msi_hangup ( MSISession* session, int32_t call_index )
1557 1590
1558 session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); 1591 session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );
1559 1592
1593 pthread_mutex_unlock(&session->mutex);
1560 return 0; 1594 return 0;
1561} 1595}
1562 1596
@@ -1571,10 +1605,12 @@ int msi_hangup ( MSISession* session, int32_t call_index )
1571 */ 1605 */
1572int msi_answer ( MSISession* session, int32_t call_index, MSICallType call_type ) 1606int msi_answer ( MSISession* session, int32_t call_index, MSICallType call_type )
1573{ 1607{
1608 pthread_mutex_lock(&session->mutex);
1574 LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index); 1609 LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
1575 1610
1576 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ 1611 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){
1577 LOGGER_ERROR("Invalid call index!"); 1612 LOGGER_ERROR("Invalid call index!");
1613 pthread_mutex_unlock(&session->mutex);
1578 return -1; 1614 return -1;
1579 } 1615 }
1580 1616
@@ -1605,7 +1641,8 @@ int msi_answer ( MSISession* session, int32_t call_index, MSICallType call_type
1605 free_message ( _msg_starting ); 1641 free_message ( _msg_starting );
1606 1642
1607 session->calls[call_index]->state = call_active; 1643 session->calls[call_index]->state = call_active;
1608 1644
1645 pthread_mutex_unlock(&session->mutex);
1609 return 0; 1646 return 0;
1610} 1647}
1611 1648
@@ -1620,10 +1657,12 @@ int msi_answer ( MSISession* session, int32_t call_index, MSICallType call_type
1620 */ 1657 */
1621int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const char* reason ) 1658int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const char* reason )
1622{ 1659{
1660 pthread_mutex_lock(&session->mutex);
1623 LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason? reason : "Unknown"); 1661 LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason? reason : "Unknown");
1624 1662
1625 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ 1663 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){
1626 LOGGER_ERROR("Invalid call index!"); 1664 LOGGER_ERROR("Invalid call index!");
1665 pthread_mutex_unlock(&session->mutex);
1627 return -1; 1666 return -1;
1628 } 1667 }
1629 1668
@@ -1634,7 +1673,9 @@ int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const c
1634 send_message ( session, session->calls[call_index], _msg_cancel, peer ); 1673 send_message ( session, session->calls[call_index], _msg_cancel, peer );
1635 free_message ( _msg_cancel ); 1674 free_message ( _msg_cancel );
1636 1675
1637 session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); 1676 /*session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
1677 terminate_call ( session, session->calls[call_index] );
1678 pthread_mutex_unlock(&session->mutex);
1638 1679
1639 return 0; 1680 return 0;
1640} 1681}
@@ -1649,10 +1690,12 @@ int msi_cancel ( MSISession* session, int32_t call_index, uint32_t peer, const c
1649 */ 1690 */
1650int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason ) 1691int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason )
1651{ 1692{
1693 pthread_mutex_lock(&session->mutex);
1652 LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason? (char*)reason : "Unknown"); 1694 LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason? (char*)reason : "Unknown");
1653 1695
1654 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){ 1696 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ){
1655 LOGGER_ERROR("Invalid call index!"); 1697 LOGGER_ERROR("Invalid call index!");
1698 pthread_mutex_unlock(&session->mutex);
1656 return -1; 1699 return -1;
1657 } 1700 }
1658 1701
@@ -1664,7 +1707,8 @@ int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason
1664 free_message ( _msg_reject ); 1707 free_message ( _msg_reject );
1665 1708
1666 session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout ); 1709 session->calls[call_index]->request_timer_id = event.timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );
1667 1710
1711 pthread_mutex_unlock(&session->mutex);
1668 return 0; 1712 return 0;
1669} 1713}
1670 1714
@@ -1678,14 +1722,18 @@ int msi_reject ( MSISession* session, int32_t call_index, const uint8_t* reason
1678 */ 1722 */
1679int msi_stopcall ( MSISession* session, int32_t call_index ) 1723int msi_stopcall ( MSISession* session, int32_t call_index )
1680{ 1724{
1725 pthread_mutex_lock(&session->mutex);
1681 LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index); 1726 LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index);
1682 1727
1683 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) 1728 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1729 pthread_mutex_unlock(&session->mutex);
1684 return -1; 1730 return -1;
1731 }
1685 1732
1686 /* just terminate it */ 1733 /* just terminate it */
1687 1734
1688 terminate_call ( session, session->calls[call_index] ); 1735 terminate_call ( session, session->calls[call_index] );
1689 1736
1737 pthread_mutex_unlock(&session->mutex);
1690 return 0; 1738 return 0;
1691} 1739}
diff --git a/toxav/msi.h b/toxav/msi.h
index 54ae8faf..9c748303 100755
--- a/toxav/msi.h
+++ b/toxav/msi.h
@@ -84,9 +84,7 @@ typedef struct _MSICall { /* Call info structure */
84 int ringing_timer_id; /* Timer id for ringing timeout */ 84 int ringing_timer_id; /* Timer id for ringing timeout */
85 85
86 86
87 pthread_mutex_t mutex; /* It's to be assumed that call will have 87 pthread_mutex_t mutex; /* */
88 * separate thread so add mutex
89 */
90 uint32_t *peers; 88 uint32_t *peers;
91 uint16_t peer_count; 89 uint16_t peer_count;
92 90
@@ -113,7 +111,7 @@ typedef struct _MSISession {
113 uint32_t frequ; 111 uint32_t frequ;
114 uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */ 112 uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
115 113
116 114 pthread_mutex_t mutex;
117} MSISession; 115} MSISession;
118 116
119 117
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 1a42f99e..f8f5b63b 100755
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -588,7 +588,7 @@ int rtp_handle_packet ( void *object, uint8_t *data, uint32_t length )
588 _session->queue_size++; 588 _session->queue_size++;
589 589
590 pthread_mutex_unlock(&_session->mutex); 590 pthread_mutex_unlock(&_session->mutex);
591 591
592 return 0; 592 return 0;
593} 593}
594 594
@@ -778,13 +778,12 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
778 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/ 778 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
779 (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 ); 779 (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
780 780
781 int full_length = encrypted_length + 3;
782 781
783 _send_data[1] = msg->data[0]; 782 _send_data[1] = msg->data[0];
784 _send_data[2] = msg->data[1]; 783 _send_data[2] = msg->data[1];
785 784
786 785
787 if ( full_length != send_custom_user_packet(messenger, session->dest, _send_data, full_length) ) { 786 if ( -1 == send_custom_user_packet(messenger, session->dest, _send_data, encrypted_length + 3) ) {
788 LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno)); 787 LOGGER_WARNING("Failed to send full packet! std error: %s", strerror(errno));
789 rtp_free_msg ( session, msg ); 788 rtp_free_msg ( session, msg );
790 return -1; 789 return -1;
@@ -800,6 +799,7 @@ int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *dat
800 } 799 }
801 800
802 rtp_free_msg ( session, msg ); 801 rtp_free_msg ( session, msg );
802
803 return 0; 803 return 0;
804} 804}
805 805
diff --git a/toxav/toxav.c b/toxav/toxav.c
index f92964aa..8289c4e9 100755
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -96,9 +96,10 @@ ToxAv *toxav_new( Tox* messenger, int32_t max_calls)
96{ 96{
97 ToxAv *av = calloc ( sizeof(ToxAv), 1); 97 ToxAv *av = calloc ( sizeof(ToxAv), 1);
98 98
99 if (av == NULL) 99 if (av == NULL) {
100 LOGGER_WARNING("Allocation failed!");
100 return NULL; 101 return NULL;
101 102 }
102 av->messenger = (Messenger *)messenger; 103 av->messenger = (Messenger *)messenger;
103 104
104 av->msi_session = msi_init_session(av->messenger, max_calls); 105 av->msi_session = msi_init_session(av->messenger, max_calls);
diff --git a/toxav/toxav.h b/toxav/toxav.h
index 74f2333b..341b38b4 100755
--- a/toxav/toxav.h
+++ b/toxav/toxav.h
@@ -67,7 +67,7 @@ typedef enum {
67 * @brief Call type identifier. 67 * @brief Call type identifier.
68 */ 68 */
69typedef enum { 69typedef enum {
70 TypeAudio = 70, 70 TypeAudio = 192,
71 TypeVideo 71 TypeVideo
72} ToxAvCallType; 72} ToxAvCallType;
73 73
diff --git a/toxcore/logger.c b/toxcore/logger.c
index ff9146d4..933e8d4b 100644
--- a/toxcore/logger.c
+++ b/toxcore/logger.c
@@ -25,7 +25,6 @@
25#include "config.h" 25#include "config.h"
26#endif /* HAVE_CONFIG_H */ 26#endif /* HAVE_CONFIG_H */
27 27
28
29#include "logger.h" 28#include "logger.h"
30 29
31#ifdef LOGGING 30#ifdef LOGGING
@@ -96,7 +95,7 @@ int logger_init(const char* file_name, LoggerLevel level)
96 sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid()); 95 sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid());
97 96
98 if ( logger.log_file ) { 97 if ( logger.log_file ) {
99 fprintf(stderr, "Error opening logger name: %s with level %d: %s!\n", final_l, level, strerror(errno)); 98 fprintf(stderr, "Error opening logger name: %s with level %d: file already opened!\n", final_l, level);
100 free (final_l); 99 free (final_l);
101 return -1; 100 return -1;
102 } 101 }
@@ -104,19 +103,15 @@ int logger_init(const char* file_name, LoggerLevel level)
104 logger.log_file = fopen(final_l, "ab"); 103 logger.log_file = fopen(final_l, "ab");
105 104
106 if ( logger.log_file == NULL ) { 105 if ( logger.log_file == NULL ) {
107 char error[1000]; 106 fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, strerror(errno));
108 if ( strerror_r(errno, error, 1000) == 0 ) 107
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); 108 free (final_l);
114 return -1; 109 return -1;
115 } 110 }
116 111
117 112
118 logger.level = level; 113 logger.level = level;
119 logger.start_time = current_time(); 114 logger.start_time = current_time_monotonic();
120 115
121 116
122 time_t tim = time(NULL); 117 time_t tim = time(NULL);
@@ -147,10 +142,10 @@ void logger_write (LoggerLevel level, const char* format, ...)
147 fflush(logger.log_file); 142 fflush(logger.log_file);
148} 143}
149 144
150char* logger_timestr(char* dest) 145char* logger_timestr(char* dest, size_t max_size)
151{ 146{
152 uint64_t diff = (current_time() - logger.start_time) / 1000; /* ms */ 147 uint64_t diff = (current_time_monotonic() - logger.start_time); /* ms */
153 sprintf(dest, "%"PRIu64"", diff); 148 snprintf(dest, max_size, "%"PRIu64"", diff);
154 149
155 return dest; 150 return dest;
156} 151}
diff --git a/toxcore/logger.h b/toxcore/logger.h
index d8292f31..0b6367c5 100644
--- a/toxcore/logger.h
+++ b/toxcore/logger.h
@@ -45,7 +45,7 @@ int logger_init(const char* file_name, LoggerLevel level);
45const char* logger_stringify_level(LoggerLevel level); 45const char* logger_stringify_level(LoggerLevel level);
46unsigned logger_get_pid(); 46unsigned logger_get_pid();
47void logger_write (LoggerLevel level, const char* format, ...); 47void logger_write (LoggerLevel level, const char* format, ...);
48char* logger_timestr (char* dest); 48char* logger_timestr (char* dest, size_t max_size);
49 49
50#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 50#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
51#define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) 51#define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
@@ -53,16 +53,16 @@ char* logger_timestr (char* dest);
53#define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) 53#define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
54#endif 54#endif
55 55
56#define WRITE_FORMAT(__LEVEL__, format) char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "\n[%u] [%s] [%s] [%s:%d %s()] %s", \ 56#define WRITE_FORMAT(__LEVEL__, format) char __time__[20]; char* the_str = calloc(sizeof(char), strlen(format)+ 500); sprintf(the_str, "\n[%u] [%s] [%s] [%s:%d %s()] %s", \
57 logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__), _SFILE, __LINE__, __func__, format) 57 logger_get_pid(), logger_stringify_level(__LEVEL__), logger_timestr(__time__, 20), _SFILE, __LINE__, __func__, format)
58 58
59/* Use these macros */ 59/* Use these macros */
60 60
61#define LOGGER_INIT(name, level) logger_init(name, level); 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) 62#define LOGGER_INFO(format, ...) do { 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) 63#define LOGGER_DEBUG(format, ...) do { 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) 64#define LOGGER_WARNING(format, ...) do { 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) 65#define LOGGER_ERROR(format, ...) do { WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); free(the_str); } while (0)
66 66
67/* To do some checks or similar only when logging use this */ 67/* To do some checks or similar only when logging use this */
68#define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) 68#define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0)