diff options
-rw-r--r-- | toxcore/network.c | 6 | ||||
-rw-r--r-- | toxcore/util.c | 52 |
2 files changed, 51 insertions, 7 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index a44ef4c4..e04d2608 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -352,10 +352,6 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
352 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); | 352 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); |
353 | #endif | 353 | #endif |
354 | 354 | ||
355 | #ifdef LOGGING | ||
356 | loginit(ntohs(port)); | ||
357 | #endif | ||
358 | |||
359 | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ | 355 | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ |
360 | uint16_t *portptr = NULL; | 356 | uint16_t *portptr = NULL; |
361 | struct sockaddr_storage addr; | 357 | struct sockaddr_storage addr; |
@@ -461,6 +457,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) | |||
461 | if (!res) { | 457 | if (!res) { |
462 | temp->port = *portptr; | 458 | temp->port = *portptr; |
463 | #ifdef LOGGING | 459 | #ifdef LOGGING |
460 | loginit(temp->port); | ||
461 | |||
464 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); | 462 | sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); |
465 | loglog(logbuffer); | 463 | loglog(logbuffer); |
466 | #endif | 464 | #endif |
diff --git a/toxcore/util.c b/toxcore/util.c index b3263d05..7b2735a0 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -94,6 +94,9 @@ int load_state(load_state_callback_func load_state_callback, void *outer, | |||
94 | 94 | ||
95 | #ifdef LOGGING | 95 | #ifdef LOGGING |
96 | time_t starttime = 0; | 96 | time_t starttime = 0; |
97 | size_t logbufferprelen = 0; | ||
98 | char *logbufferpredata = NULL; | ||
99 | char *logbufferprehead = NULL; | ||
97 | char logbuffer[512]; | 100 | char logbuffer[512]; |
98 | static FILE *logfile = NULL; | 101 | static FILE *logfile = NULL; |
99 | void loginit(uint16_t port) | 102 | void loginit(uint16_t port) |
@@ -101,9 +104,23 @@ void loginit(uint16_t port) | |||
101 | if (logfile) | 104 | if (logfile) |
102 | fclose(logfile); | 105 | fclose(logfile); |
103 | 106 | ||
104 | sprintf(logbuffer, "%u-%u.log", ntohs(port), (uint32_t)now()); | 107 | if (!starttime) |
108 | starttime = now(); | ||
109 | |||
110 | struct tm *tm = localtime(&starttime); | ||
111 | if (strftime(logbuffer + 32, sizeof(logbuffer) - 32, "%F %T", tm)) | ||
112 | sprintf(logbuffer, "%u-%s.log", ntohs(port), logbuffer + 32); | ||
113 | else | ||
114 | sprintf(logbuffer, "%u-%lu.log", ntohs(port), starttime); | ||
105 | logfile = fopen(logbuffer, "w"); | 115 | logfile = fopen(logbuffer, "w"); |
106 | starttime = now(); | 116 | if (logbufferpredata) { |
117 | if (logfile) | ||
118 | fprintf(logfile, logbufferpredata); | ||
119 | |||
120 | free(logbufferpredata); | ||
121 | logbufferpredata = NULL; | ||
122 | } | ||
123 | |||
107 | }; | 124 | }; |
108 | void loglog(char *text) | 125 | void loglog(char *text) |
109 | { | 126 | { |
@@ -111,8 +128,37 @@ void loglog(char *text) | |||
111 | fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); | 128 | fprintf(logfile, "%4u ", (uint32_t)(now() - starttime)); |
112 | fprintf(logfile, text); | 129 | fprintf(logfile, text); |
113 | fflush(logfile); | 130 | fflush(logfile); |
131 | |||
132 | return; | ||
114 | } | 133 | } |
115 | }; | 134 | |
135 | /* log messages before file was opened: store */ | ||
136 | |||
137 | size_t len = strlen(text); | ||
138 | if (!starttime) { | ||
139 | starttime = now(); | ||
140 | logbufferprelen = 1024 + len - (len % 1024); | ||
141 | logbufferpredata = malloc(logbufferprelen); | ||
142 | logbufferprehead = logbufferpredata; | ||
143 | } | ||
144 | |||
145 | /* loginit() called meanwhile? (but failed to open) */ | ||
146 | if (!logbufferpredata) | ||
147 | return; | ||
148 | |||
149 | if (len + logbufferprehead - logbufferpredata + 16U < logbufferprelen) { | ||
150 | size_t logpos = logbufferprehead - logbufferpredata; | ||
151 | size_t lennew = logbufferprelen * 1.4; | ||
152 | logbufferpredata = realloc(logbufferpredata, lennew); | ||
153 | logbufferprehead = logbufferpredata + logpos; | ||
154 | logbufferprelen = lennew; | ||
155 | } | ||
156 | |||
157 | size_t written; | ||
158 | sprintf(logbufferprehead, "%4u %s%n", (uint32_t)(now() - starttime), text, &written); | ||
159 | logbufferprehead += written; | ||
160 | } | ||
161 | |||
116 | void logexit() | 162 | void logexit() |
117 | { | 163 | { |
118 | if (logfile) { | 164 | if (logfile) { |