Skip to content
Snippets Groups Projects
Commit 4893ea70 authored by Marek Vavruša's avatar Marek Vavruša
Browse files

stats: use json objects for output

parent e9f3ea51
Branches
Tags
No related merge requests found
......@@ -23,6 +23,7 @@
*/
#include <libknot/packet/pkt.h>
#include <ccan/json/json.h>
#include "lib/layer/iterate.h"
#include "lib/rplan.h"
......@@ -148,13 +149,9 @@ static char* stats_get(void *env, struct kr_module *module, const char *args)
static int list_entry(const char *key, void *val, void *baton)
{
char **strval = (char **)baton;
JsonNode *root = baton;
size_t number = (size_t) val;
char buf[512];
snprintf(buf, sizeof(buf), "'%s': %zu,\n", key, number);
char *ret = kr_strcatdup(2, *strval, buf);
free(*strval);
*strval = ret;
json_append_member(root, key, json_mknumber(number));
return 0;
}
......@@ -166,11 +163,10 @@ static int list_entry(const char *key, void *val, void *baton)
static char* stats_list(void *env, struct kr_module *module, const char *args)
{
map_t *map = module->data;
char *strval = NULL;
/* @todo This is very inefficient with memory */
map_walk_prefixed(map, args ? args : "", list_entry, &strval);
char *ret = kr_strcatdup(3, "{\n", strval, "}");
free(strval);
JsonNode *root = json_mkobject();
map_walk_prefixed(map, args ? args : "", list_entry, root);
char *ret = json_encode(root);
json_delete(root);
return ret;
}
......
stats_SOURCES := modules/stats/stats.c
stats_SOURCES := modules/stats/stats.c contrib/ccan/json/json.c
stats_DEPEND := $(libkresolve)
stats_LIBS := $(libkresolve_TARGET) $(libkresolve_LIBS)
$(call make_c_module,stats)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment