summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-01-10 21:39:40 -0500
committerirungentoo <irungentoo@gmail.com>2015-01-10 21:39:40 -0500
commit90f68f578cafd8f81d0b2b2742de016ffe06851e (patch)
tree9cddefa5d894a650d360678e81ec7018be7dfa82 /toxcore
parentdd51e03857913bb1b73bc604a64adf5e2740cfe6 (diff)
parentad672595356f034d81990c307f4e19c619198076 (diff)
Merge branch 'mannol-master'
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/DHT.c12
-rw-r--r--toxcore/Messenger.c24
-rw-r--r--toxcore/assoc.c22
-rw-r--r--toxcore/logger.c239
-rw-r--r--toxcore/logger.h99
-rw-r--r--toxcore/network.c6
-rw-r--r--toxcore/tox.c5
7 files changed, 246 insertions, 161 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 653b401f..919655a2 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -330,9 +330,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
330 if (ip_port.ip.family == AF_INET) { 330 if (ip_port.ip.family == AF_INET) {
331 331
332 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 332 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
333 LOGGER_INFO("coipil[%u]: switching ipv4 from %s:%u to %s:%u", i, 333 LOGGER_TRACE("coipil[%u]: switching ipv4 from %s:%u to %s:%u", i,
334 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port), 334 ip_ntoa(&list[i].assoc4.ip_port.ip), ntohs(list[i].assoc4.ip_port.port),
335 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 335 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
336 } 336 }
337 ); 337 );
338 338
@@ -344,9 +344,9 @@ static int client_or_ip_port_in_list(Client_data *list, uint16_t length, const u
344 } else if (ip_port.ip.family == AF_INET6) { 344 } else if (ip_port.ip.family == AF_INET6) {
345 345
346 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) { 346 LOGGER_SCOPE( if (!ipport_equal(&list[i].assoc4.ip_port, &ip_port)) {
347 LOGGER_INFO("coipil[%u]: switching ipv6 from %s:%u to %s:%u", i, 347 LOGGER_TRACE("coipil[%u]: switching ipv6 from %s:%u to %s:%u", i,
348 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port), 348 ip_ntoa(&list[i].assoc6.ip_port.ip), ntohs(list[i].assoc6.ip_port.port),
349 ip_ntoa(&ip_port.ip), ntohs(ip_port.port)); 349 ip_ntoa(&ip_port.ip), ntohs(ip_port.port));
350 } 350 }
351 ); 351 );
352 352
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 9869f738..c310412d 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2424,9 +2424,9 @@ void do_messenger(Messenger *m)
2424 if (last_pinged > 999) 2424 if (last_pinged > 999)
2425 last_pinged = 999; 2425 last_pinged = 999;
2426 2426
2427 LOGGER_INFO("C[%2u] %s:%u [%3u] %s", 2427 LOGGER_TRACE("C[%2u] %s:%u [%3u] %s",
2428 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port), 2428 client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
2429 last_pinged, ID2String(cptr->client_id)); 2429 last_pinged, ID2String(cptr->client_id));
2430 } 2430 }
2431 } 2431 }
2432 2432
@@ -2457,7 +2457,7 @@ void do_messenger(Messenger *m)
2457 dht2m[m2dht[friend]] = friend; 2457 dht2m[m2dht[friend]] = friend;
2458 2458
2459 if (m->numfriends != m->dht->num_friends) { 2459 if (m->numfriends != m->dht->num_friends) {
2460 LOGGER_INFO("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); 2460 LOGGER_TRACE("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends);
2461 } 2461 }
2462 2462
2463 uint32_t ping_lastrecv; 2463 uint32_t ping_lastrecv;
@@ -2478,11 +2478,11 @@ void do_messenger(Messenger *m)
2478 if (ping_lastrecv > 999) 2478 if (ping_lastrecv > 999)
2479 ping_lastrecv = 999; 2479 ping_lastrecv = 999;
2480 2480
2481 LOGGER_INFO("F[%2u:%2u] <%s> [%03u] %s", 2481 LOGGER_TRACE("F[%2u:%2u] <%s> [%03u] %s",
2482 dht2m[friend], friend, msgfptr->name, 2482 dht2m[friend], friend, msgfptr->name,
2483 ping_lastrecv, ID2String(msgfptr->client_id)); 2483 ping_lastrecv, ID2String(msgfptr->client_id));
2484 } else { 2484 } else {
2485 LOGGER_INFO("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); 2485 LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id));
2486 } 2486 }
2487 2487
2488 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { 2488 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
@@ -2497,10 +2497,10 @@ void do_messenger(Messenger *m)
2497 if (last_pinged > 999) 2497 if (last_pinged > 999)
2498 last_pinged = 999; 2498 last_pinged = 999;
2499 2499
2500 LOGGER_INFO("F[%2u] => C[%2u] %s:%u [%3u] %s", 2500 LOGGER_TRACE("F[%2u] => C[%2u] %s:%u [%3u] %s",
2501 friend, client, ip_ntoa(&assoc->ip_port.ip), 2501 friend, client, ip_ntoa(&assoc->ip_port.ip),
2502 ntohs(assoc->ip_port.port), last_pinged, 2502 ntohs(assoc->ip_port.port), last_pinged,
2503 ID2String(cptr->client_id)); 2503 ID2String(cptr->client_id));
2504 } 2504 }
2505 } 2505 }
2506 } 2506 }
diff --git a/toxcore/assoc.c b/toxcore/assoc.c
index 8c78543f..3dbeba51 100644
--- a/toxcore/assoc.c
+++ b/toxcore/assoc.c
@@ -996,11 +996,11 @@ static char *idpart2str(uint8_t *id, size_t len)
996void Assoc_status(const Assoc *assoc) 996void Assoc_status(const Assoc *assoc)
997{ 997{
998 if (!assoc) { 998 if (!assoc) {
999 LOGGER_INFO("Assoc status: no assoc"); 999 LOGGER_TRACE("Assoc status: no assoc");
1000 return; 1000 return;
1001 } 1001 }
1002 1002
1003 LOGGER_INFO("[b:p] hash => [id...] used, seen, heard"); 1003 LOGGER_TRACE("[b:p] hash => [id...] used, seen, heard");
1004 1004
1005 size_t bid, cid, total = 0; 1005 size_t bid, cid, total = 0;
1006 1006
@@ -1013,20 +1013,20 @@ void Assoc_status(const Assoc *assoc)
1013 if (entry->hash) { 1013 if (entry->hash) {
1014 total++; 1014 total++;
1015 1015
1016 LOGGER_INFO("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n", 1016 LOGGER_TRACE("[%3i:%3i] %08x => [%s...] %i, %i(%c), %i(%c)\n",
1017 (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8), 1017 (int)bid, (int)cid, entry->hash, idpart2str(entry->client.client_id, 8),
1018 entry->used_at ? (int)(unix_time() - entry->used_at) : 0, 1018 entry->used_at ? (int)(unix_time() - entry->used_at) : 0,
1019 entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0, 1019 entry->seen_at ? (int)(unix_time() - entry->seen_at) : 0,
1020 entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?', 1020 entry->seen_at ? (entry->seen_family == AF_INET ? '4' : (entry->seen_family == AF_INET6 ? '6' : '?')) : '?',
1021 entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0, 1021 entry->heard_at ? (int)(unix_time() - entry->heard_at) : 0,
1022 entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?'); 1022 entry->heard_at ? (entry->heard_family == AF_INET ? '4' : (entry->heard_family == AF_INET6 ? '6' : '?')) : '?');
1023 } 1023 }
1024 } 1024 }
1025 } 1025 }
1026 1026
1027 if (total) { 1027 if (total) {
1028 LOGGER_INFO("Total: %i entries, table usage %i%%.\n", (int)total, 1028 LOGGER_TRACE("Total: %i entries, table usage %i%%.\n", (int)total,
1029 (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size))); 1029 (int)(total * 100 / (assoc->candidates_bucket_count * assoc->candidates_bucket_size)));
1030 } 1030 }
1031} 1031}
1032 1032
diff --git a/toxcore/logger.c b/toxcore/logger.c
index 674d72e9..fa9d1760 100644
--- a/toxcore/logger.c
+++ b/toxcore/logger.c
@@ -1,7 +1,5 @@
1/* logger.c 1/* logger.c
2 * 2 *
3 * Wrapping logger functions in nice macros
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 3 * Copyright (C) 2013 Tox project All Rights Reserved.
6 * 4 *
7 * This file is part of Tox. 5 * This file is part of Tox.
@@ -26,10 +24,7 @@
26#endif /* HAVE_CONFIG_H */ 24#endif /* HAVE_CONFIG_H */
27 25
28#include "logger.h" 26#include "logger.h"
29 27#include "crypto_core.h" /* for random_int() */
30#ifdef LOGGING
31
32#include "network.h" /* for time */
33 28
34#include <stdio.h> 29#include <stdio.h>
35#include <errno.h> 30#include <errno.h>
@@ -37,121 +32,203 @@
37#include <stdarg.h> 32#include <stdarg.h>
38#include <inttypes.h> 33#include <inttypes.h>
39#include <time.h> 34#include <time.h>
35#include <pthread.h>
40 36
41#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 37#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
42#define strerror_r(errno,buf,len) strerror_s(buf,len,errno) 38# define getpid() ((unsigned) GetCurrentProcessId())
39# define SFILE(FILE__M) (strrchr(FILE__M, '\\') ? strrchr(FILE__M, '\\') + 1 : FILE__M)
40#else
41# define SFILE(FILE__M) (strrchr(FILE__M, '/') ? strrchr(FILE__M, '/') + 1 : FILE__M)
43#endif 42#endif
44 43
45static struct logger_config { 44
45typedef struct logger {
46 FILE *log_file; 46 FILE *log_file;
47 LoggerLevel level; 47 LOG_LEVEL level;
48 uint64_t start_time; /* Time when lib loaded */ 48 uint64_t start_time; /* Time when lib loaded */
49} 49 char *id;
50logger = { 50
51 NULL, 51 /* Allocate these once */
52 DEBUG, 52 char *tstr;
53 0 53 char *posstr;
54 char *msg;
55
56 /* For thread synchronisation */
57 pthread_mutex_t mutex[1];
58} logger;
59
60logger *global = NULL;
61
62const char *LOG_LEVEL_STR [] = {
63 [LOG_TRACE] = "TRACE",
64 [LOG_DEBUG] = "DEBUG",
65 [LOG_INFO] = "INFO" ,
66 [LOG_WARNING] = "WARN" ,
67 [LOG_ERROR] = "ERROR",
54}; 68};
55 69
56void __attribute__((destructor)) terminate_logger() 70char *strtime(char *dest, size_t max_len)
57{ 71{
58 if ( !logger.log_file ) return; 72 time_t timer;
59 73 struct tm *tm_info;
60 time_t tim = time(NULL);
61 74
62 logger_write(ERROR, "\n============== Closing logger [%u] ==============\n" 75 time(&timer);
63 "Time: %s", logger_get_pid(), asctime(localtime(&tim))); 76 tm_info = localtime(&timer);
64 77
65 fclose(logger.log_file); 78 strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info);
79 return dest;
66} 80}
67 81
68unsigned logger_get_pid() 82
83/**
84 * Public Functions
85 */
86logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id)
69{ 87{
70 return 88#ifndef LOGGING /* Disabled */
71#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 89 return NULL;
72 GetCurrentProcessId();
73#else
74 getpid();
75#endif 90#endif
76}
77 91
78const char *logger_stringify_level(LoggerLevel level) 92 logger *retu = calloc(1, sizeof(logger));
79{
80 static const char *strings [] = {
81 "INFO",
82 "DEBUG",
83 "WARN",
84 "ERROR"
85 };
86
87 return strings[level];
88}
89 93
94 if (!retu)
95 return NULL;
90 96
91int logger_init(const char *file_name, LoggerLevel level) 97 if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) {
92{ 98 free(retu);
93 if (logger.log_file) { 99 return NULL;
94 fprintf(stderr, "Error opening logger name: %s with level %d: file already opened!\n",
95 file_name, level);
96 return -1;
97 } 100 }
98 101
99 logger.log_file = fopen(file_name, "ab"); 102 if (!(retu->log_file = fopen(file_name, "ab"))) {
100
101 if (logger.log_file == NULL) {
102 fprintf(stderr, "Error opening logger file: %s; info: %s\n", file_name, strerror(errno)); 103 fprintf(stderr, "Error opening logger file: %s; info: %s\n", file_name, strerror(errno));
103 return -1; 104 free(retu);
105 pthread_mutex_destroy(retu->mutex);
106 return NULL;
104 } 107 }
105 108
106 logger.level = level; 109 if (!(retu->tstr = calloc(16, sizeof (char))) ||
107 logger.start_time = current_time_monotonic(); 110 !(retu->posstr = calloc(300, sizeof (char))) ||
111 !(retu->msg = calloc(4096, sizeof (char))) )
112 goto ERROR;
108 113
109 time_t tim = time(NULL); 114 if (id) {
110 logger_write(ERROR, "\n============== Starting logger [%u] ==============\n" 115 if (!(retu->id = calloc(strlen(id) + 1, 1)))
111 "Time: %s", logger_get_pid(), asctime(localtime(&tim))); 116 goto ERROR;
112 return 0;
113}
114 117
118 strcpy(retu->id, id);
119 } else {
120 if (!(retu->id = malloc(8)))
121 goto ERROR;
115 122
116void logger_write (LoggerLevel level, const char *format, ...) 123 snprintf(retu->id, 8, "%u", random_int());
117{
118 if (logger.log_file == NULL) {
119 /*fprintf(stderr, "Logger file is NULL!\n");*/
120 return;
121 } 124 }
122 125
123 if (logger.level > level) return; /* Don't print some levels xuh */ 126 retu->level = level;
127 retu->start_time = current_time_monotonic();
124 128
125 va_list _arg; 129 fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n",
126 va_start (_arg, format); 130 retu->id, strtime(retu->tstr, 16));
127 vfprintf (logger.log_file, format, _arg);
128 va_end (_arg);
129 131
130 fflush(logger.log_file); 132 return retu;
133
134ERROR:
135 fprintf(stderr, "Failed to create logger!\n");
136 pthread_mutex_destroy(retu->mutex);
137 fclose(retu->log_file);
138 free(retu->tstr);
139 free(retu->posstr);
140 free(retu->msg);
141 free(retu->id);
142 free(retu);
143 return NULL;
131} 144}
132 145
133char *logger_timestr(char *dest, size_t max_size) 146void logger_kill(logger *log)
134{ 147{
135 time_t timer; 148#ifndef LOGGING /* Disabled */
136 struct tm *tm_info; 149 return;
150#endif
137 151
138 time(&timer); 152 if (!log)
139 tm_info = localtime(&timer); 153 return;
140 154
141 strftime(dest, max_size, "%m:%d %H:%M:%S", tm_info); 155 pthread_mutex_lock(log->mutex);
156 free(log->id);
157 free(log->tstr);
158 free(log->posstr);
159 free(log->msg);
142 160
143 return dest; 161 if (fclose(log->log_file) != 0 )
162 perror("Could not close log file");
144 163
145 /*uint64_t diff = (current_time_monotonic() - logger.start_time); /* ms * / 164 pthread_mutex_unlock(log->mutex);
146 snprintf(dest, max_size, "%"PRIu64"", diff); 165 pthread_mutex_destroy(log->mutex);
147 166
148 return dest; */ 167 free(log);
149} 168}
150 169
151char *logger_posstr (char *dest, size_t max_size, const char *file, int line) 170void logger_kill_global(void)
152{ 171{
153 snprintf(dest, max_size, "%s:%d", file, line); 172 logger_kill(global);
154 return dest; 173}
174
175void logger_set_global(logger *log)
176{
177#ifndef LOGGING /* Disabled */
178 return;
179#endif
180
181 global = log;
182}
183
184logger *logger_get_global(void)
185{
186#ifndef LOGGING /* Disabled */
187 return NULL;
188#endif
189
190 return global;
155} 191}
156 192
157#endif /* LOGGING */ 193void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...)
194{
195#ifndef LOGGING /* Disabled */
196 return;
197#endif
198
199 static const char *logger_format =
200 "%s " /* Logger id string */
201 "%-16s" /* Time string of format: %m:%d %H:%M:%S */
202 "%u " /* Thread id */
203 "%-5s " /* Logger lever string */
204 "%-20s " /* File:line string */
205 "- %s" /* Output message */
206 "\n"; /* Every new print new line */
207
208
209 logger *this_log = log ? log : global;
210
211 if (!this_log)
212 return;
213
214 /* Don't print levels lesser than set one */
215 if (this_log->level > level)
216 return;
217
218 pthread_mutex_lock(this_log->mutex);
219
220 /* Set position str */
221 snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line);
222
223 /* Set message */
224 va_list args;
225 va_start (args, format);
226 vsnprintf(this_log->msg, 4096, format, args);
227 va_end (args);
228
229 fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(),
230 LOG_LEVEL_STR[level], this_log->posstr, this_log->msg);
231 fflush(this_log->log_file);
232
233 pthread_mutex_unlock(this_log->mutex);
234}
diff --git a/toxcore/logger.h b/toxcore/logger.h
index 856c48d0..6f1f6075 100644
--- a/toxcore/logger.h
+++ b/toxcore/logger.h
@@ -1,7 +1,5 @@
1/* logger.h 1/* logger.h
2 * 2 *
3 * Wrapping logger functions in nice macros
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved. 3 * Copyright (C) 2013 Tox project All Rights Reserved.
6 * 4 *
7 * This file is part of Tox. 5 * This file is part of Tox.
@@ -22,67 +20,74 @@
22 */ 20 */
23 21
24 22
25#ifndef __TOXLOGGER 23#ifndef TOXLOGGER_H
26#define __TOXLOGGER 24#define TOXLOGGER_H
27 25
28#include <string.h> 26#include <string.h>
29 27
30#ifdef LOGGING 28/* In case these are undefined; define 'empty' */
31 29#ifndef LOGGER_OUTPUT_FILE
32typedef enum _LoggerLevel { 30# define LOGGER_OUTPUT_FILE ""
33 INFO, 31#endif
34 DEBUG,
35 WARNING,
36 ERROR
37} LoggerLevel;
38 32
39/* 33#ifndef LOGGER_LEVEL
40 * Set 'level' as the lowest printable level 34# define LOGGER_LEVEL LOG_ERROR
41 */
42int logger_init(const char *file_name, LoggerLevel level);
43const char *logger_stringify_level(LoggerLevel level);
44unsigned logger_get_pid();
45void logger_write (LoggerLevel level, const char *format, ...);
46char *logger_timestr (char *dest, size_t max_size);
47char *logger_posstr (char *dest, size_t max_size, const char *file, int line);
48
49#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
50#define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
51#else
52#define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
53#endif 35#endif
54 36
55#define LFORMAT "\n%-15s %-7u %-5s %-20s - %s"
56#define WRITE_FORMAT(__LEVEL__, __WHAT__) \
57 char __time__[15]; char posstr[200]; char the_str [4096]; \
58 snprintf(the_str, 4096, LFORMAT, logger_timestr(__time__, 15), logger_get_pid(), \
59 logger_stringify_level(__LEVEL__), logger_posstr(posstr, 200, _SFILE, __LINE__), __WHAT__)
60 37
61/* Use these macros */ 38typedef enum {
39 LOG_TRACE,
40 LOG_DEBUG,
41 LOG_INFO,
42 LOG_WARNING,
43 LOG_ERROR
44} LOG_LEVEL;
62 45
63#define LOGGER_INIT(name, level) logger_init(name, level); 46typedef struct logger logger;
64#define LOGGER_INFO(format, ...) do { WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); } while (0)
65#define LOGGER_DEBUG(format, ...) do { WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); } while (0)
66#define LOGGER_WARNING(format, ...) do { WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); } while (0)
67#define LOGGER_ERROR(format, ...) do { WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); } while (0)
68 47
69/* To do some checks or similar only when logging use this */ 48/**
70#define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) 49 * Set 'level' as the lowest printable level. If id == NULL, random number is used.
50 */
51logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id);
71 52
72#else 53void logger_kill (logger *log);
54void logger_kill_global (void);
73 55
56/**
57 * Global logger setter and getter.
58 */
59void logger_set_global (logger *log);
60logger *logger_get_global (void);
74 61
75#define LOGGER_INIT(name, level) do {} while(0) 62/**
76#define LOGGER_INFO(format, ...) do {} while(0) 63 * Main write function. If logging disabled does nothing. If log == NULL uses global logger.
77#define LOGGER_DEBUG(format, ...) do {} while(0) 64 */
78#define LOGGER_WARNING(format, ...) do {} while(0) 65void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...);
79#define LOGGER_ERROR(format, ...) do {} while(0)
80 66
81#define LOGGER_SCOPE(__SCOPE_DO__) do {} while(0)
82 67
68/* To do some checks or similar only when logging, use this */
69#ifdef LOGGING
70# define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0)
71# define LOGGER_WRITE(log, level, format, ...) \
72 logger_write(log, level, __FILE__, __LINE__, format, ##__VA_ARGS__ )
73#else
74# define LOGGER_SCOPE(__SCOPE_DO__) do {} while(0)
75# define LOGGER_WRITE(log, level, format, ...) do {} while(0)
83#endif /* LOGGING */ 76#endif /* LOGGING */
84 77
78/* To log with an logger */
79#define LOGGER_TRACE_(log, format, ...) LOGGER_WRITE(log, LOG_TRACE, format, ##__VA_ARGS__ )
80#define LOGGER_DEBUG_(log, format, ...) LOGGER_WRITE(log, LOG_DEBUG, format, ##__VA_ARGS__ )
81#define LOGGER_INFO_(log, format, ...) LOGGER_WRITE(log, LOG_INFO, format, ##__VA_ARGS__ )
82#define LOGGER_WARNING_(log, format, ...) LOGGER_WRITE(log, LOG_WARNING, format, ##__VA_ARGS__ )
83#define LOGGER_ERROR_(log, format, ...) LOGGER_WRITE(log, LOG_ERROR, format, ##__VA_ARGS__ )
85 84
85/* To log with the global logger */
86#define LOGGER_TRACE(format, ...) LOGGER_TRACE_(NULL, format, ##__VA_ARGS__)
87#define LOGGER_DEBUG(format, ...) LOGGER_DEBUG_(NULL, format, ##__VA_ARGS__)
88#define LOGGER_INFO(format, ...) LOGGER_INFO_(NULL, format, ##__VA_ARGS__)
89#define LOGGER_WARNING(format, ...) LOGGER_WARNING_(NULL, format, ##__VA_ARGS__)
90#define LOGGER_ERROR(format, ...) LOGGER_ERROR_(NULL, format, ##__VA_ARGS__)
86 91
87 92
88#endif /* __TOXLOGGER */ 93#endif /* TOXLOGGER_H */
diff --git a/toxcore/network.c b/toxcore/network.c
index 6e631cac..deccbb63 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -264,15 +264,15 @@ uint64_t current_time_monotonic(void)
264#define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) \ 264#define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) \
265 (__ip_port__) .ip; \ 265 (__ip_port__) .ip; \
266 if (__res__ < 0) /* Windows doesn't necessarily know %zu */ \ 266 if (__res__ < 0) /* Windows doesn't necessarily know %zu */ \
267 LOGGER_INFO("[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", \ 267 LOGGER_TRACE("[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", \
268 __buffer__[0], __message__, (__buflen__ < 999 ? (uint16_t)__buflen__ : 999), 'E', \ 268 __buffer__[0], __message__, (__buflen__ < 999 ? (uint16_t)__buflen__ : 999), 'E', \
269 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), errno, strerror(errno), data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ 269 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), errno, strerror(errno), data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \
270 else if ((__res__ > 0) && ((size_t)__res__ <= __buflen__)) \ 270 else if ((__res__ > 0) && ((size_t)__res__ <= __buflen__)) \
271 LOGGER_INFO("[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", \ 271 LOGGER_TRACE("[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", \
272 __buffer__[0], __message__, (__res__ < 999 ? (size_t)__res__ : 999), ((size_t)__res__ < __buflen__ ? '<' : '='), \ 272 __buffer__[0], __message__, (__res__ < 999 ? (size_t)__res__ : 999), ((size_t)__res__ < __buflen__ ? '<' : '='), \
273 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \ 273 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); \
274 else /* empty or overwrite */ \ 274 else /* empty or overwrite */ \
275 LOGGER_INFO("[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", \ 275 LOGGER_TRACE("[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", \
276 __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \ 276 __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \
277 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); 277 ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__));
278 278
diff --git a/toxcore/tox.c b/toxcore/tox.c
index a62bc884..a4a73838 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -1012,7 +1012,9 @@ uint32_t tox_do_interval(Tox *tox)
1012 */ 1012 */
1013Tox *tox_new(Tox_Options *options) 1013Tox *tox_new(Tox_Options *options)
1014{ 1014{
1015 LOGGER_INIT(LOGGER_OUTPUT_FILE, LOGGER_LEVEL); 1015 logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore"));
1016
1017
1016 Messenger_Options m_options = {0}; 1018 Messenger_Options m_options = {0};
1017 1019
1018 if (options == NULL) { 1020 if (options == NULL) {
@@ -1066,6 +1068,7 @@ void tox_kill(Tox *tox)
1066 Messenger *m = tox; 1068 Messenger *m = tox;
1067 kill_groupchats(m->group_chat_object); 1069 kill_groupchats(m->group_chat_object);
1068 kill_messenger(m); 1070 kill_messenger(m);
1071 logger_kill_global();
1069} 1072}
1070 1073
1071/* The main loop that needs to be run at least 20 times per second. */ 1074/* The main loop that needs to be run at least 20 times per second. */