2014-08-08 04:59:47 +08:00
|
|
|
#ifndef TOXCORE_TEST_HELPERS_H
|
|
|
|
#define TOXCORE_TEST_HELPERS_H
|
|
|
|
|
2016-11-06 00:52:41 +08:00
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
#include "../toxcore/ccompat.h"
|
|
|
|
|
2016-12-22 18:44:33 +08:00
|
|
|
#include <assert.h>
|
2016-11-06 00:52:41 +08:00
|
|
|
#include <stdio.h>
|
2017-06-05 04:58:28 +08:00
|
|
|
#include <string.h>
|
2014-08-08 04:59:47 +08:00
|
|
|
|
2017-06-05 04:58:28 +08:00
|
|
|
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
|
2017-03-04 02:30:11 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#define c_sleep(x) Sleep(x)
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2017-06-05 04:58:28 +08:00
|
|
|
#define c_sleep(x) usleep(1000 * (x))
|
2017-03-04 02:30:11 +08:00
|
|
|
#endif
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
#define ITERATION_INTERVAL 200
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data);
|
|
|
|
|
2016-11-06 00:52:41 +08:00
|
|
|
static const char *tox_log_level_name(TOX_LOG_LEVEL level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case TOX_LOG_LEVEL_TRACE:
|
|
|
|
return "TRACE";
|
|
|
|
|
|
|
|
case TOX_LOG_LEVEL_DEBUG:
|
|
|
|
return "DEBUG";
|
|
|
|
|
|
|
|
case TOX_LOG_LEVEL_INFO:
|
|
|
|
return "INFO";
|
|
|
|
|
|
|
|
case TOX_LOG_LEVEL_WARNING:
|
|
|
|
return "WARNING";
|
|
|
|
|
|
|
|
case TOX_LOG_LEVEL_ERROR:
|
|
|
|
return "ERROR";
|
|
|
|
}
|
2018-01-18 23:14:50 +08:00
|
|
|
|
|
|
|
return "<unknown>";
|
2016-11-06 00:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *path, uint32_t line, const char *func,
|
|
|
|
const char *message, void *user_data)
|
|
|
|
{
|
2018-02-18 22:09:42 +08:00
|
|
|
if (level == TOX_LOG_LEVEL_TRACE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-06 00:52:41 +08:00
|
|
|
uint32_t index = user_data ? *(uint32_t *)user_data : 0;
|
|
|
|
const char *file = strrchr(path, '/');
|
|
|
|
file = file ? file + 1 : path;
|
2018-02-06 04:07:26 +08:00
|
|
|
fprintf(stderr, "[#%d] %s %s:%d\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message);
|
2016-11-06 00:52:41 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
static Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery)
|
2016-11-06 00:52:41 +08:00
|
|
|
{
|
2016-12-23 08:26:46 +08:00
|
|
|
struct Tox_Options *log_options = options;
|
2016-11-06 00:52:41 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (log_options == nullptr) {
|
|
|
|
log_options = tox_options_new(nullptr);
|
2016-11-06 00:52:41 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
assert(log_options != nullptr);
|
2016-12-23 08:26:46 +08:00
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
tox_options_set_local_discovery_enabled(log_options, lan_discovery);
|
2018-02-19 06:10:59 +08:00
|
|
|
tox_options_set_start_port(log_options, 33445);
|
|
|
|
tox_options_set_end_port(log_options, 33445 + 2000);
|
2016-12-22 18:44:33 +08:00
|
|
|
tox_options_set_log_callback(log_options, &print_debug_log);
|
|
|
|
tox_options_set_log_user_data(log_options, log_user_data);
|
2016-12-23 08:26:46 +08:00
|
|
|
Tox *tox = tox_new(log_options, err);
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (options == nullptr) {
|
2016-12-23 08:26:46 +08:00
|
|
|
tox_options_free(log_options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return tox;
|
2016-11-06 00:52:41 +08:00
|
|
|
}
|
|
|
|
|
2018-02-19 01:50:50 +08:00
|
|
|
Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data)
|
|
|
|
{
|
|
|
|
return tox_new_log_lan(options, err, log_user_data, false);
|
|
|
|
}
|
|
|
|
|
2014-08-10 05:35:23 +08:00
|
|
|
#endif // TOXCORE_TEST_HELPERS_H
|