diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-07-16 22:46:02 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-07-21 20:44:26 +0000 |
commit | 7245ac11ef9be2420c8356c12acc79f93ea211bb (patch) | |
tree | e971c5c6e10c2310afe4b2cd80212feac9839f2a /testing/misc_tools.c | |
parent | 7c2b95ef5e4ccdae01bd104aa7400294c9ea391b (diff) |
Avoid implementations in .h files or #including .c files.
Also, avoid the need for putting `_XOPEN_SOURCE` in every test file.
Diffstat (limited to 'testing/misc_tools.c')
-rw-r--r-- | testing/misc_tools.c | 97 |
1 files changed, 81 insertions, 16 deletions
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 9b0d9956..8683830d 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -21,35 +21,40 @@ | |||
21 | * You should have received a copy of the GNU General Public License | 21 | * You should have received a copy of the GNU General Public License |
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
23 | */ | 23 | */ |
24 | #ifndef MISC_TOOLS_C | 24 | #ifndef _POSIX_C_SOURCE |
25 | #define MISC_TOOLS_C | 25 | // For nanosleep(). |
26 | 26 | #define _POSIX_C_SOURCE 199309L | |
27 | #ifdef HAVE_CONFIG_H | ||
28 | #include "config.h" | ||
29 | #endif | 27 | #endif |
30 | 28 | ||
29 | #include "misc_tools.h" | ||
30 | |||
31 | #include <assert.h> | ||
31 | #include <ctype.h> | 32 | #include <ctype.h> |
32 | #include <stdint.h> | 33 | #include <stdint.h> |
33 | #include <stdio.h> | 34 | #include <stdio.h> |
34 | #include <stdlib.h> | 35 | #include <stdlib.h> |
35 | #include <string.h> | 36 | #include <string.h> |
36 | 37 | ||
37 | //Sleep function (x = milliseconds) | 38 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
38 | #ifndef c_sleep | ||
39 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
40 | #include <windows.h> | 39 | #include <windows.h> |
41 | #define c_sleep(x) Sleep(x) | ||
42 | #else | 40 | #else |
43 | #include <unistd.h> | 41 | #include <time.h> |
44 | #define c_sleep(x) usleep(1000*(x)) | ||
45 | #endif | ||
46 | #endif | 42 | #endif |
47 | 43 | ||
48 | #include "../toxcore/ccompat.h" | 44 | #include "../toxcore/ccompat.h" |
45 | #include "../toxcore/tox.h" | ||
49 | 46 | ||
50 | uint8_t *hex_string_to_bin(const char *hex_string); | 47 | void c_sleep(uint32_t x) |
51 | int tox_strncasecmp(const char *s1, const char *s2, size_t n); | 48 | { |
52 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled); | 49 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
50 | Sleep(x); | ||
51 | #else | ||
52 | struct timespec req; | ||
53 | req.tv_sec = x / 1000; | ||
54 | req.tv_nsec = (long)x % 1000 * 1000 * 1000; | ||
55 | nanosleep(&req, nullptr); | ||
56 | #endif | ||
57 | } | ||
53 | 58 | ||
54 | // You are responsible for freeing the return value! | 59 | // You are responsible for freeing the return value! |
55 | uint8_t *hex_string_to_bin(const char *hex_string) | 60 | uint8_t *hex_string_to_bin(const char *hex_string) |
@@ -123,4 +128,64 @@ int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) | |||
123 | return argvoffset; | 128 | return argvoffset; |
124 | } | 129 | } |
125 | 130 | ||
126 | #endif // MISC_TOOLS_C | 131 | static const char *tox_log_level_name(TOX_LOG_LEVEL level) |
132 | { | ||
133 | switch (level) { | ||
134 | case TOX_LOG_LEVEL_TRACE: | ||
135 | return "TRACE"; | ||
136 | |||
137 | case TOX_LOG_LEVEL_DEBUG: | ||
138 | return "DEBUG"; | ||
139 | |||
140 | case TOX_LOG_LEVEL_INFO: | ||
141 | return "INFO"; | ||
142 | |||
143 | case TOX_LOG_LEVEL_WARNING: | ||
144 | return "WARNING"; | ||
145 | |||
146 | case TOX_LOG_LEVEL_ERROR: | ||
147 | return "ERROR"; | ||
148 | } | ||
149 | |||
150 | return "<unknown>"; | ||
151 | } | ||
152 | |||
153 | void print_debug_log(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, | ||
154 | const char *message, void *user_data) | ||
155 | { | ||
156 | if (level == TOX_LOG_LEVEL_TRACE) { | ||
157 | return; | ||
158 | } | ||
159 | |||
160 | uint32_t index = user_data ? *(uint32_t *)user_data : 0; | ||
161 | fprintf(stderr, "[#%u] %s %s:%u\t%s:\t%s\n", index, tox_log_level_name(level), file, line, func, message); | ||
162 | } | ||
163 | |||
164 | Tox *tox_new_log_lan(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data, bool lan_discovery) | ||
165 | { | ||
166 | struct Tox_Options *log_options = options; | ||
167 | |||
168 | if (log_options == nullptr) { | ||
169 | log_options = tox_options_new(nullptr); | ||
170 | } | ||
171 | |||
172 | assert(log_options != nullptr); | ||
173 | |||
174 | tox_options_set_local_discovery_enabled(log_options, lan_discovery); | ||
175 | tox_options_set_start_port(log_options, 33445); | ||
176 | tox_options_set_end_port(log_options, 33445 + 2000); | ||
177 | tox_options_set_log_callback(log_options, &print_debug_log); | ||
178 | tox_options_set_log_user_data(log_options, log_user_data); | ||
179 | Tox *tox = tox_new(log_options, err); | ||
180 | |||
181 | if (options == nullptr) { | ||
182 | tox_options_free(log_options); | ||
183 | } | ||
184 | |||
185 | return tox; | ||
186 | } | ||
187 | |||
188 | Tox *tox_new_log(struct Tox_Options *options, TOX_ERR_NEW *err, void *log_user_data) | ||
189 | { | ||
190 | return tox_new_log_lan(options, err, log_user_data, false); | ||
191 | } | ||