diff options
Diffstat (limited to 'toxcore/util.c')
-rw-r--r-- | toxcore/util.c | 52 |
1 files changed, 49 insertions, 3 deletions
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) { |