Increase default stats json buffer size to 8k. Retry with a larger buffer if we run out of space, like we do with aircraft.json

With the addition of the per-DF stats, the stats output started to exceed 4k in some cases (notably, you need --net turned on).

Fixes #106
This commit is contained in:
Oliver Jowett 2021-02-07 22:22:01 +08:00
parent 0aedcffc5b
commit 1b1f9de119
1 changed files with 16 additions and 8 deletions

View File

@ -1828,15 +1828,20 @@ static char * appendStatsJson(char *p,
} }
char *generateStatsJson(const char *url_path, int *len) { char *generateStatsJson(const char *url_path, int *len) {
const size_t bufsize = 4096; MODES_NOTUSED(url_path);
char *buf = malloc(bufsize), *p = buf, *end = buf + bufsize;
if (!buf) { int buflen = 8192;
char *buf, *p, *end;
retry:
if (!(buf = malloc(buflen))) {
// allocation failed, give up // allocation failed, give up
*len = 0; *len = 0;
return NULL; return NULL;
} }
MODES_NOTUSED(url_path); p = buf;
end = buf + buflen;
p = safe_snprintf(p, end, "{\n"); p = safe_snprintf(p, end, "{\n");
p = appendStatsJson(p, end, &Modes.stats_latest, "latest"); p = appendStatsJson(p, end, &Modes.stats_latest, "latest");
@ -1854,12 +1859,15 @@ char *generateStatsJson(const char *url_path, int *len) {
p = appendStatsJson(p, end, &Modes.stats_alltime, "total"); p = appendStatsJson(p, end, &Modes.stats_alltime, "total");
p = safe_snprintf(p, end, "\n}\n"); p = safe_snprintf(p, end, "\n}\n");
if (p <= end) { int used = p - buf;
*len = p-buf; if (p >= end) {
} else { // overran the buffer
*len = 0; // ran out of buffer space, give up buflen = used + 50;
free(buf);
goto retry;
} }
*len = used;
return buf; return buf;
} }