summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/DHT.c20
-rw-r--r--toxcore/Messenger.c14
-rw-r--r--toxcore/assoc.c14
-rw-r--r--toxcore/logger.c106
-rw-r--r--toxcore/logger.h12
-rw-r--r--toxcore/tox.c4
6 files changed, 86 insertions, 84 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 42fdcc32..919655a2 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -330,11 +330,11 @@ 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_TRACE("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
339 if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) 339 if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
340 return 1; 340 return 1;
@@ -344,11 +344,11 @@ 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_TRACE("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
353 if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) 353 if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0)
354 return 1; 354 return 1;
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 5c487916..c310412d 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -2425,8 +2425,8 @@ void do_messenger(Messenger *m)
2425 last_pinged = 999; 2425 last_pinged = 999;
2426 2426
2427 LOGGER_TRACE("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
@@ -2479,8 +2479,8 @@ void do_messenger(Messenger *m)
2479 ping_lastrecv = 999; 2479 ping_lastrecv = 999;
2480 2480
2481 LOGGER_TRACE("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_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); 2485 LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id));
2486 } 2486 }
@@ -2498,9 +2498,9 @@ void do_messenger(Messenger *m)
2498 last_pinged = 999; 2498 last_pinged = 999;
2499 2499
2500 LOGGER_TRACE("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 658c0eb4..3dbeba51 100644
--- a/toxcore/assoc.c
+++ b/toxcore/assoc.c
@@ -1014,19 +1014,19 @@ void Assoc_status(const Assoc *assoc)
1014 total++; 1014 total++;
1015 1015
1016 LOGGER_TRACE("[%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_TRACE("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 21ff81c6..fa9d1760 100644
--- a/toxcore/logger.c
+++ b/toxcore/logger.c
@@ -46,20 +46,20 @@ typedef struct logger {
46 FILE *log_file; 46 FILE *log_file;
47 LOG_LEVEL level; 47 LOG_LEVEL level;
48 uint64_t start_time; /* Time when lib loaded */ 48 uint64_t start_time; /* Time when lib loaded */
49 char* id; 49 char *id;
50 50
51 /* Allocate these once */ 51 /* Allocate these once */
52 char* tstr; 52 char *tstr;
53 char* posstr; 53 char *posstr;
54 char* msg; 54 char *msg;
55 55
56 /* For thread synchronisation */ 56 /* For thread synchronisation */
57 pthread_mutex_t mutex[1]; 57 pthread_mutex_t mutex[1];
58} logger; 58} logger;
59 59
60logger* global = NULL; 60logger *global = NULL;
61 61
62const char* LOG_LEVEL_STR [] = { 62const char *LOG_LEVEL_STR [] = {
63 [LOG_TRACE] = "TRACE", 63 [LOG_TRACE] = "TRACE",
64 [LOG_DEBUG] = "DEBUG", 64 [LOG_DEBUG] = "DEBUG",
65 [LOG_INFO] = "INFO" , 65 [LOG_INFO] = "INFO" ,
@@ -67,14 +67,14 @@ const char* LOG_LEVEL_STR [] = {
67 [LOG_ERROR] = "ERROR", 67 [LOG_ERROR] = "ERROR",
68}; 68};
69 69
70char* strtime(char* dest, size_t max_len) 70char *strtime(char *dest, size_t max_len)
71{ 71{
72 time_t timer; 72 time_t timer;
73 struct tm *tm_info; 73 struct tm *tm_info;
74 74
75 time(&timer); 75 time(&timer);
76 tm_info = localtime(&timer); 76 tm_info = localtime(&timer);
77 77
78 strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info); 78 strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info);
79 return dest; 79 return dest;
80} 80}
@@ -83,22 +83,22 @@ char* strtime(char* dest, size_t max_len)
83/** 83/**
84 * Public Functions 84 * Public Functions
85 */ 85 */
86logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id) 86logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id)
87{ 87{
88#ifndef LOGGING /* Disabled */ 88#ifndef LOGGING /* Disabled */
89 return NULL; 89 return NULL;
90#endif 90#endif
91 91
92 logger* retu = calloc(1, sizeof(logger)); 92 logger *retu = calloc(1, sizeof(logger));
93 93
94 if (!retu) 94 if (!retu)
95 return NULL; 95 return NULL;
96 96
97 if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) { 97 if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) {
98 free(retu); 98 free(retu);
99 return NULL; 99 return NULL;
100 } 100 }
101 101
102 if (!(retu->log_file = fopen(file_name, "ab"))) { 102 if (!(retu->log_file = fopen(file_name, "ab"))) {
103 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));
104 free(retu); 104 free(retu);
@@ -107,30 +107,30 @@ logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id)
107 } 107 }
108 108
109 if (!(retu->tstr = calloc(16, sizeof (char))) || 109 if (!(retu->tstr = calloc(16, sizeof (char))) ||
110 !(retu->posstr = calloc(300, sizeof (char))) || 110 !(retu->posstr = calloc(300, sizeof (char))) ||
111 !(retu->msg = calloc(4096, sizeof (char))) ) 111 !(retu->msg = calloc(4096, sizeof (char))) )
112 goto ERROR; 112 goto ERROR;
113 113
114 if (id) { 114 if (id) {
115 if (!(retu->id = calloc(strlen(id) + 1, 1))) 115 if (!(retu->id = calloc(strlen(id) + 1, 1)))
116 goto ERROR; 116 goto ERROR;
117 117
118 strcpy(retu->id, id); 118 strcpy(retu->id, id);
119 } else { 119 } else {
120 if (!(retu->id = malloc(8))) 120 if (!(retu->id = malloc(8)))
121 goto ERROR; 121 goto ERROR;
122 122
123 snprintf(retu->id, 8, "%u", random_int()); 123 snprintf(retu->id, 8, "%u", random_int());
124 } 124 }
125 125
126 retu->level = level; 126 retu->level = level;
127 retu->start_time = current_time_monotonic(); 127 retu->start_time = current_time_monotonic();
128 128
129 fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n", 129 fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n",
130 retu->id, strtime(retu->tstr, 16)); 130 retu->id, strtime(retu->tstr, 16));
131 131
132 return retu; 132 return retu;
133 133
134ERROR: 134ERROR:
135 fprintf(stderr, "Failed to create logger!\n"); 135 fprintf(stderr, "Failed to create logger!\n");
136 pthread_mutex_destroy(retu->mutex); 136 pthread_mutex_destroy(retu->mutex);
@@ -143,25 +143,27 @@ ERROR:
143 return NULL; 143 return NULL;
144} 144}
145 145
146void logger_kill(logger* log) 146void logger_kill(logger *log)
147{ 147{
148#ifndef LOGGING /* Disabled */ 148#ifndef LOGGING /* Disabled */
149 return; 149 return;
150#endif 150#endif
151 151
152 if (!log) 152 if (!log)
153 return; 153 return;
154 154
155 pthread_mutex_lock(log->mutex); 155 pthread_mutex_lock(log->mutex);
156 free(log->id); 156 free(log->id);
157 free(log->tstr); 157 free(log->tstr);
158 free(log->posstr); 158 free(log->posstr);
159 free(log->msg); 159 free(log->msg);
160
160 if (fclose(log->log_file) != 0 ) 161 if (fclose(log->log_file) != 0 )
161 perror("Could not close log file"); 162 perror("Could not close log file");
163
162 pthread_mutex_unlock(log->mutex); 164 pthread_mutex_unlock(log->mutex);
163 pthread_mutex_destroy(log->mutex); 165 pthread_mutex_destroy(log->mutex);
164 166
165 free(log); 167 free(log);
166} 168}
167 169
@@ -170,31 +172,31 @@ void logger_kill_global(void)
170 logger_kill(global); 172 logger_kill(global);
171} 173}
172 174
173void logger_set_global(logger* log) 175void logger_set_global(logger *log)
174{ 176{
175 #ifndef LOGGING /* Disabled */ 177#ifndef LOGGING /* Disabled */
176 return; 178 return;
177 #endif 179#endif
178 180
179 global = log; 181 global = log;
180} 182}
181 183
182logger* logger_get_global(void) 184logger *logger_get_global(void)
183{ 185{
184 #ifndef LOGGING /* Disabled */ 186#ifndef LOGGING /* Disabled */
185 return NULL; 187 return NULL;
186 #endif 188#endif
187 189
188 return global; 190 return global;
189} 191}
190 192
191void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, const char *format, ...) 193void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...)
192{ 194{
193#ifndef LOGGING /* Disabled */ 195#ifndef LOGGING /* Disabled */
194 return; 196 return;
195#endif 197#endif
196 198
197 static const char* logger_format = 199 static const char *logger_format =
198 "%s " /* Logger id string */ 200 "%s " /* Logger id string */
199 "%-16s" /* Time string of format: %m:%d %H:%M:%S */ 201 "%-16s" /* Time string of format: %m:%d %H:%M:%S */
200 "%u " /* Thread id */ 202 "%u " /* Thread id */
@@ -202,23 +204,23 @@ void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, con
202 "%-20s " /* File:line string */ 204 "%-20s " /* File:line string */
203 "- %s" /* Output message */ 205 "- %s" /* Output message */
204 "\n"; /* Every new print new line */ 206 "\n"; /* Every new print new line */
205 207
206 208
207 logger* this_log = log ? log: global; 209 logger *this_log = log ? log : global;
208 210
209 if (!this_log) 211 if (!this_log)
210 return; 212 return;
211 213
212 /* Don't print levels lesser than set one */ 214 /* Don't print levels lesser than set one */
213 if (this_log->level > level) 215 if (this_log->level > level)
214 return; 216 return;
215 217
216 pthread_mutex_lock(this_log->mutex); 218 pthread_mutex_lock(this_log->mutex);
217 219
218 /* Set position str */ 220 /* Set position str */
219 snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line); 221 snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line);
220 222
221 /* Set message */ 223 /* Set message */
222 va_list args; 224 va_list args;
223 va_start (args, format); 225 va_start (args, format);
224 vsnprintf(this_log->msg, 4096, format, args); 226 vsnprintf(this_log->msg, 4096, format, args);
@@ -227,6 +229,6 @@ void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, con
227 fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(), 229 fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(),
228 LOG_LEVEL_STR[level], this_log->posstr, this_log->msg); 230 LOG_LEVEL_STR[level], this_log->posstr, this_log->msg);
229 fflush(this_log->log_file); 231 fflush(this_log->log_file);
230 232
231 pthread_mutex_unlock(this_log->mutex); 233 pthread_mutex_unlock(this_log->mutex);
232} \ No newline at end of file 234}
diff --git a/toxcore/logger.h b/toxcore/logger.h
index 312ed3c4..6f1f6075 100644
--- a/toxcore/logger.h
+++ b/toxcore/logger.h
@@ -48,21 +48,21 @@ typedef struct logger logger;
48/** 48/**
49 * Set 'level' as the lowest printable level. If id == NULL, random number is used. 49 * Set 'level' as the lowest printable level. If id == NULL, random number is used.
50 */ 50 */
51logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id); 51logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id);
52 52
53void logger_kill (logger* log); 53void logger_kill (logger *log);
54void logger_kill_global (void); 54void logger_kill_global (void);
55 55
56/** 56/**
57 * Global logger setter and getter. 57 * Global logger setter and getter.
58 */ 58 */
59void logger_set_global (logger* log); 59void logger_set_global (logger *log);
60logger* logger_get_global (void); 60logger *logger_get_global (void);
61 61
62/** 62/**
63 * Main write function. If logging disabled does nothing. If log == NULL uses global logger. 63 * Main write function. If logging disabled does nothing. If log == NULL uses global logger.
64 */ 64 */
65void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, const char* format, ...); 65void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...);
66 66
67 67
68/* To do some checks or similar only when logging, use this */ 68/* To do some checks or similar only when logging, use this */
@@ -90,4 +90,4 @@ void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, con
90#define LOGGER_ERROR(format, ...) LOGGER_ERROR_(NULL, format, ##__VA_ARGS__) 90#define LOGGER_ERROR(format, ...) LOGGER_ERROR_(NULL, format, ##__VA_ARGS__)
91 91
92 92
93#endif /* TOXLOGGER_H */ \ No newline at end of file 93#endif /* TOXLOGGER_H */
diff --git a/toxcore/tox.c b/toxcore/tox.c
index 89a8742a..a4a73838 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -1013,8 +1013,8 @@ uint32_t tox_do_interval(Tox *tox)
1013Tox *tox_new(Tox_Options *options) 1013Tox *tox_new(Tox_Options *options)
1014{ 1014{
1015 logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore")); 1015 logger_set_global(logger_new(LOGGER_OUTPUT_FILE, LOGGER_LEVEL, "toxcore"));
1016 1016
1017 1017
1018 Messenger_Options m_options = {0}; 1018 Messenger_Options m_options = {0};
1019 1019
1020 if (options == NULL) { 1020 if (options == NULL) {