diff options
author | mannol <eniz_vukovic@hotmail.com> | 2015-01-10 23:09:51 +0100 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2015-01-10 23:29:00 +0100 |
commit | ad672595356f034d81990c307f4e19c619198076 (patch) | |
tree | d1ad148a505841e6ec3eaaee58c1202f77c89f2e | |
parent | dd51e03857913bb1b73bc604a64adf5e2740cfe6 (diff) |
Logger fix
-rw-r--r-- | configure.ac | 33 | ||||
-rw-r--r-- | toxcore/DHT.c | 12 | ||||
-rw-r--r-- | toxcore/Messenger.c | 10 | ||||
-rw-r--r-- | toxcore/assoc.c | 8 | ||||
-rw-r--r-- | toxcore/logger.c | 263 | ||||
-rw-r--r-- | toxcore/logger.h | 99 | ||||
-rw-r--r-- | toxcore/network.c | 6 | ||||
-rw-r--r-- | toxcore/tox.c | 5 |
8 files changed, 261 insertions, 175 deletions
diff --git a/configure.ac b/configure.ac index ee516f80..3a45b35d 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -82,37 +82,40 @@ AC_ARG_ENABLE([randombytes-stir], | |||
82 | ] | 82 | ] |
83 | ) | 83 | ) |
84 | 84 | ||
85 | AC_ARG_ENABLE([logging], | 85 | AC_ARG_ENABLE([log], |
86 | [AC_HELP_STRING([--enable-logging], [enable logging (default: auto)]) ], | 86 | [AC_HELP_STRING([--enable-log], [enable logging (default: auto)]) ], |
87 | [ | 87 | [ |
88 | if test "x$enableval" = "xyes"; then | 88 | if test "x$enableval" = "xyes"; then |
89 | LOGGING="yes" | 89 | LOGGING="yes" |
90 | 90 | ||
91 | AC_DEFINE([LOGGING], [], [If logging enabled]) | 91 | AC_DEFINE([LOGGING], [], [If logging enabled]) |
92 | AC_DEFINE([LOGGER_LEVEL], [DEBUG], [LoggerLevel value]) | 92 | AC_DEFINE([LOGGER_LEVEL], [LOG_DEBUG], [LOG_LEVEL value]) |
93 | AC_DEFINE_UNQUOTED([LOGGER_OUTPUT_FILE], ["$LOGGING_OUTNAM"], [Output of logger]) | 93 | AC_DEFINE_UNQUOTED([LOGGER_OUTPUT_FILE], ["$LOGGING_OUTNAM"], [Output of logger]) |
94 | fi | 94 | fi |
95 | ] | 95 | ] |
96 | ) | 96 | ) |
97 | 97 | ||
98 | AC_ARG_WITH(logger-level, | 98 | AC_ARG_WITH(log-level, |
99 | AC_HELP_STRING([--with-logger-level=LEVEL], | 99 | AC_HELP_STRING([--with-log-level=LEVEL], |
100 | [Logger levels: INFO; DEBUG; WARNING; ERROR ]), | 100 | [Logger levels: TRACE; DEBUG; INFO; WARNING; ERROR ]), |
101 | [ | 101 | [ |
102 | if test "x$LOGGING" = "xno"; then | 102 | if test "x$LOGGING" = "xno"; then |
103 | AC_MSG_WARN([Logging disabled!]) | 103 | AC_MSG_WARN([Logging disabled!]) |
104 | else | 104 | else |
105 | if test "x$withval" = "xINFO"; then | 105 | if test "x$withval" = "xTRACE"; then |
106 | AC_DEFINE([LOGGER_LEVEL], [INFO], [LoggerLevel value]) | 106 | AC_DEFINE([LOGGER_LEVEL], [LOG_TRACE], [LOG_LEVEL value]) |
107 | 107 | ||
108 | elif test "x$withval" = "xDEBUG"; then | 108 | elif test "x$withval" = "xDEBUG"; then |
109 | AC_DEFINE([LOGGER_LEVEL], [DEBUG], [LoggerLevel value]) | 109 | AC_DEFINE([LOGGER_LEVEL], [LOG_DEBUG], [LOG_LEVEL value]) |
110 | 110 | ||
111 | elif test "x$withval" = "xINFO"; then | ||
112 | AC_DEFINE([LOGGER_LEVEL], [LOG_INFO], [LOG_LEVEL value]) | ||
113 | |||
111 | elif test "x$withval" = "xWARNING"; then | 114 | elif test "x$withval" = "xWARNING"; then |
112 | AC_DEFINE([LOGGER_LEVEL], [WARNING], [LoggerLevel value]) | 115 | AC_DEFINE([LOGGER_LEVEL], [LOG_WARNING], [LOG_LEVEL value]) |
113 | 116 | ||
114 | elif test "x$withval" = "xERROR"; then | 117 | elif test "x$withval" = "xERROR"; then |
115 | AC_DEFINE([LOGGER_LEVEL], [ERROR], [LoggerLevel value]) | 118 | AC_DEFINE([LOGGER_LEVEL], [LOG_ERROR], [LOG_LEVEL value]) |
116 | else | 119 | else |
117 | AC_MSG_WARN([Invalid logger level: $withval. Using default 'DEBUG']) | 120 | AC_MSG_WARN([Invalid logger level: $withval. Using default 'DEBUG']) |
118 | fi | 121 | fi |
@@ -120,8 +123,8 @@ AC_ARG_WITH(logger-level, | |||
120 | ] | 123 | ] |
121 | ) | 124 | ) |
122 | 125 | ||
123 | AC_ARG_WITH(logger-path, | 126 | AC_ARG_WITH(log-path, |
124 | AC_HELP_STRING([--with-logger-path=DIR], | 127 | AC_HELP_STRING([--with-log-path=DIR], |
125 | [Path of logger output]), | 128 | [Path of logger output]), |
126 | [ | 129 | [ |
127 | if test "x$LOGGING" = "xno"; then | 130 | if test "x$LOGGING" = "xno"; then |
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 653b401f..42fdcc32 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_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 | ||
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_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 | ||
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 9869f738..5c487916 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2424,7 +2424,7 @@ 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 | } |
@@ -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,7 +2497,7 @@ 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)); |
diff --git a/toxcore/assoc.c b/toxcore/assoc.c index 8c78543f..658c0eb4 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c | |||
@@ -996,11 +996,11 @@ static char *idpart2str(uint8_t *id, size_t len) | |||
996 | void Assoc_status(const Assoc *assoc) | 996 | void 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,7 +1013,7 @@ 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, |
@@ -1025,7 +1025,7 @@ void Assoc_status(const Assoc *assoc) | |||
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 | } |
diff --git a/toxcore/logger.c b/toxcore/logger.c index 674d72e9..21ff81c6 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,201 @@ | |||
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 | ||
45 | static struct logger_config { | 44 | |
45 | typedef 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; |
50 | logger = { | 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 | |||
60 | logger* global = NULL; | ||
61 | |||
62 | const 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 | ||
56 | void __attribute__((destructor)) terminate_logger() | 70 | char* strtime(char* dest, size_t max_len) |
57 | { | ||
58 | if ( !logger.log_file ) return; | ||
59 | |||
60 | time_t tim = time(NULL); | ||
61 | |||
62 | logger_write(ERROR, "\n============== Closing logger [%u] ==============\n" | ||
63 | "Time: %s", logger_get_pid(), asctime(localtime(&tim))); | ||
64 | |||
65 | fclose(logger.log_file); | ||
66 | } | ||
67 | |||
68 | unsigned logger_get_pid() | ||
69 | { | ||
70 | return | ||
71 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
72 | GetCurrentProcessId(); | ||
73 | #else | ||
74 | getpid(); | ||
75 | #endif | ||
76 | } | ||
77 | |||
78 | const char *logger_stringify_level(LoggerLevel level) | ||
79 | { | 71 | { |
80 | static const char *strings [] = { | 72 | time_t timer; |
81 | "INFO", | 73 | struct tm *tm_info; |
82 | "DEBUG", | 74 | |
83 | "WARN", | 75 | time(&timer); |
84 | "ERROR" | 76 | tm_info = localtime(&timer); |
85 | }; | 77 | |
86 | 78 | strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info); | |
87 | return strings[level]; | 79 | return dest; |
88 | } | 80 | } |
89 | 81 | ||
90 | 82 | ||
91 | int logger_init(const char *file_name, LoggerLevel level) | 83 | /** |
84 | * Public Functions | ||
85 | */ | ||
86 | logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id) | ||
92 | { | 87 | { |
93 | if (logger.log_file) { | 88 | #ifndef LOGGING /* Disabled */ |
94 | fprintf(stderr, "Error opening logger name: %s with level %d: file already opened!\n", | 89 | return NULL; |
95 | file_name, level); | 90 | #endif |
96 | return -1; | 91 | |
92 | logger* retu = calloc(1, sizeof(logger)); | ||
93 | |||
94 | if (!retu) | ||
95 | return NULL; | ||
96 | |||
97 | if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) { | ||
98 | free(retu); | ||
99 | return NULL; | ||
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))) || |
108 | 111 | !(retu->msg = calloc(4096, sizeof (char))) ) | |
109 | time_t tim = time(NULL); | 112 | goto ERROR; |
110 | logger_write(ERROR, "\n============== Starting logger [%u] ==============\n" | 113 | |
111 | "Time: %s", logger_get_pid(), asctime(localtime(&tim))); | 114 | if (id) { |
112 | return 0; | 115 | if (!(retu->id = calloc(strlen(id) + 1, 1))) |
116 | goto ERROR; | ||
117 | |||
118 | strcpy(retu->id, id); | ||
119 | } else { | ||
120 | if (!(retu->id = malloc(8))) | ||
121 | goto ERROR; | ||
122 | |||
123 | snprintf(retu->id, 8, "%u", random_int()); | ||
124 | } | ||
125 | |||
126 | retu->level = level; | ||
127 | retu->start_time = current_time_monotonic(); | ||
128 | |||
129 | fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n", | ||
130 | retu->id, strtime(retu->tstr, 16)); | ||
131 | |||
132 | return retu; | ||
133 | |||
134 | ERROR: | ||
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; | ||
113 | } | 144 | } |
114 | 145 | ||
115 | 146 | void logger_kill(logger* log) | |
116 | void logger_write (LoggerLevel level, const char *format, ...) | ||
117 | { | 147 | { |
118 | if (logger.log_file == NULL) { | 148 | #ifndef LOGGING /* Disabled */ |
119 | /*fprintf(stderr, "Logger file is NULL!\n");*/ | 149 | return; |
150 | #endif | ||
151 | |||
152 | if (!log) | ||
120 | return; | 153 | return; |
121 | } | 154 | |
122 | 155 | pthread_mutex_lock(log->mutex); | |
123 | if (logger.level > level) return; /* Don't print some levels xuh */ | 156 | free(log->id); |
124 | 157 | free(log->tstr); | |
125 | va_list _arg; | 158 | free(log->posstr); |
126 | va_start (_arg, format); | 159 | free(log->msg); |
127 | vfprintf (logger.log_file, format, _arg); | 160 | if (fclose(log->log_file) != 0 ) |
128 | va_end (_arg); | 161 | perror("Could not close log file"); |
129 | 162 | pthread_mutex_unlock(log->mutex); | |
130 | fflush(logger.log_file); | 163 | pthread_mutex_destroy(log->mutex); |
164 | |||
165 | free(log); | ||
131 | } | 166 | } |
132 | 167 | ||
133 | char *logger_timestr(char *dest, size_t max_size) | 168 | void logger_kill_global(void) |
134 | { | 169 | { |
135 | time_t timer; | 170 | logger_kill(global); |
136 | struct tm *tm_info; | 171 | } |
137 | |||
138 | time(&timer); | ||
139 | tm_info = localtime(&timer); | ||
140 | |||
141 | strftime(dest, max_size, "%m:%d %H:%M:%S", tm_info); | ||
142 | |||
143 | return dest; | ||
144 | |||
145 | /*uint64_t diff = (current_time_monotonic() - logger.start_time); /* ms * / | ||
146 | snprintf(dest, max_size, "%"PRIu64"", diff); | ||
147 | 172 | ||
148 | return dest; */ | 173 | void logger_set_global(logger* log) |
174 | { | ||
175 | #ifndef LOGGING /* Disabled */ | ||
176 | return; | ||
177 | #endif | ||
178 | |||
179 | global = log; | ||
149 | } | 180 | } |
150 | 181 | ||
151 | char *logger_posstr (char *dest, size_t max_size, const char *file, int line) | 182 | logger* logger_get_global(void) |
152 | { | 183 | { |
153 | snprintf(dest, max_size, "%s:%d", file, line); | 184 | #ifndef LOGGING /* Disabled */ |
154 | return dest; | 185 | return NULL; |
186 | #endif | ||
187 | |||
188 | return global; | ||
155 | } | 189 | } |
156 | 190 | ||
157 | #endif /* LOGGING */ | 191 | void logger_write (logger* log, LOG_LEVEL level, const char* file, int line, const char *format, ...) |
192 | { | ||
193 | #ifndef LOGGING /* Disabled */ | ||
194 | return; | ||
195 | #endif | ||
196 | |||
197 | static const char* logger_format = | ||
198 | "%s " /* Logger id string */ | ||
199 | "%-16s" /* Time string of format: %m:%d %H:%M:%S */ | ||
200 | "%u " /* Thread id */ | ||
201 | "%-5s " /* Logger lever string */ | ||
202 | "%-20s " /* File:line string */ | ||
203 | "- %s" /* Output message */ | ||
204 | "\n"; /* Every new print new line */ | ||
205 | |||
206 | |||
207 | logger* this_log = log ? log: global; | ||
208 | |||
209 | if (!this_log) | ||
210 | return; | ||
211 | |||
212 | /* Don't print levels lesser than set one */ | ||
213 | if (this_log->level > level) | ||
214 | return; | ||
215 | |||
216 | pthread_mutex_lock(this_log->mutex); | ||
217 | |||
218 | /* Set position str */ | ||
219 | snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line); | ||
220 | |||
221 | /* Set message */ | ||
222 | va_list args; | ||
223 | va_start (args, format); | ||
224 | vsnprintf(this_log->msg, 4096, format, args); | ||
225 | va_end (args); | ||
226 | |||
227 | 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); | ||
229 | fflush(this_log->log_file); | ||
230 | |||
231 | pthread_mutex_unlock(this_log->mutex); | ||
232 | } \ No newline at end of file | ||
diff --git a/toxcore/logger.h b/toxcore/logger.h index 856c48d0..312ed3c4 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 | |
32 | typedef 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 | */ | ||
42 | int logger_init(const char *file_name, LoggerLevel level); | ||
43 | const char *logger_stringify_level(LoggerLevel level); | ||
44 | unsigned logger_get_pid(); | ||
45 | void logger_write (LoggerLevel level, const char *format, ...); | ||
46 | char *logger_timestr (char *dest, size_t max_size); | ||
47 | char *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 */ | 38 | typedef 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); | 46 | typedef 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 | */ | ||
51 | logger* logger_new (const char *file_name, LOG_LEVEL level, const char* id); | ||
71 | 52 | ||
72 | #else | 53 | void logger_kill (logger* log); |
54 | void logger_kill_global (void); | ||
73 | 55 | ||
56 | /** | ||
57 | * Global logger setter and getter. | ||
58 | */ | ||
59 | void logger_set_global (logger* log); | ||
60 | logger* 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) | 65 | void 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 */ \ No newline at end of file |
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..89a8742a 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -1012,7 +1012,9 @@ uint32_t tox_do_interval(Tox *tox) | |||
1012 | */ | 1012 | */ |
1013 | Tox *tox_new(Tox_Options *options) | 1013 | Tox *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. */ |