From: Michael Tremer Date: Wed, 8 Oct 2025 16:33:10 +0000 (+0000) Subject: graphs: Write title/vlabel to the stack X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6da146f3be260c6784c81adad9cb9ef4fcdb52bc;p=telemetry.git graphs: Write title/vlabel to the stack Signed-off-by: Michael Tremer --- diff --git a/src/daemon/graph.c b/src/daemon/graph.c index 248aac4..1e84cca 100644 --- a/src/daemon/graph.c +++ b/src/daemon/graph.c @@ -31,6 +31,10 @@ #include "graph.h" #include "time.h" +// Maximum length of the title/vlabel +#define TITLE_MAX 128 +#define VLABEL_MAX 64 + struct collecty_graph { collecty_ctx* ctx; int nrefs; @@ -200,8 +204,8 @@ ERROR: int collecty_graph_render(collecty_graph* self, const char* object, const collecty_graph_render_options* options, char** buffer, size_t* length) { collecty_args* args = NULL; - char* vlabel = NULL; - char* title = NULL; + char vlabel[VLABEL_MAX] = ""; + char title[TITLE_MAX] = ""; char** data = NULL; FILE* f = NULL; int w = 0; @@ -283,14 +287,14 @@ int collecty_graph_render(collecty_graph* self, const char* object, // Set the title if (self->impl->title) { // Fetch the title - r = self->impl->title(self->ctx, self, object, &title); + r = self->impl->title(self->ctx, self, object, title, sizeof(title)); if (r < 0) { ERROR(self->ctx, "Failed to render the title: %s\n", strerror(-r)); goto ERROR; } // Add the title to the command line - if (title) { + if (*title) { r = collecty_args_push(args, "--title=%s", title); if (r < 0) goto ERROR; @@ -300,14 +304,14 @@ int collecty_graph_render(collecty_graph* self, const char* object, // Set the vertical label if (self->impl->vlabel) { // Fetch the vertical label - r = self->impl->vlabel(self->ctx, self, object, &vlabel); + r = self->impl->vlabel(self->ctx, self, object, vlabel, sizeof(vlabel)); if (r < 0) { ERROR(self->ctx, "Failed to render the vertical label: %s\n", strerror(-r)); goto ERROR; } // Add the vertical label to the command line - if (vlabel) { + if (*vlabel) { r = collecty_args_push(args, "--vertical-label=%s", vlabel); if (r < 0) goto ERROR; @@ -379,10 +383,6 @@ ERROR: free(data[i]); free(data); } - if (vlabel) - free(vlabel); - if (title) - free(title); if (args) collecty_args_unref(args); if (f) diff --git a/src/daemon/graph.h b/src/daemon/graph.h index 2f1fc62..5455bbd 100644 --- a/src/daemon/graph.h +++ b/src/daemon/graph.h @@ -50,11 +50,11 @@ typedef struct collecty_graph_impl { // Title int (*title)(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title); + const char* object, char* title, size_t length); // Vertical Label int (*vlabel)(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title); + const char* object, char* vlabel, size_t length); } collecty_graph_impl; int collecty_graph_create(collecty_graph** graph, diff --git a/src/daemon/graphs/conntrack.c b/src/daemon/graphs/conntrack.c index c19762b..ea97190 100644 --- a/src/daemon/graphs/conntrack.c +++ b/src/daemon/graphs/conntrack.c @@ -20,17 +20,18 @@ #include +#include "../string.h" #include "graph.h" #include "conntrack.h" static int conntrack_title(collecty_ctx* ctx, - collecty_graph* graph, const char* object, char** title) { - return collecty_format_title(title, "%s", _("Connection Tracking Table")); + collecty_graph* graph, const char* object, char* title, size_t length) { + return __collecty_string_set(title, length, _("Connection Tracking Table")); } static int conntrack_vlabel(collecty_ctx* ctx, - collecty_graph* graph, const char* object, char** title) { - return collecty_format_title(title, "%s", _("Entries")); + collecty_graph* graph, const char* object, char* vlabel, size_t length) { + return __collecty_string_set(vlabel, length, _("Entries")); } static int conntrack_render(collecty_ctx* ctx, diff --git a/src/daemon/graphs/contextswitches.c b/src/daemon/graphs/contextswitches.c index 45089ef..5351121 100644 --- a/src/daemon/graphs/contextswitches.c +++ b/src/daemon/graphs/contextswitches.c @@ -20,17 +20,18 @@ #include +#include "../string.h" #include "graph.h" #include "contextswitches.h" static int contextswitches_title(collecty_ctx* ctx, - collecty_graph* graph, const char* object, char** title) { - return collecty_format_title(title, "%s", _("Context Switches")); + collecty_graph* graph, const char* object, char* title, size_t length) { + return __collecty_string_set(title, length, _("Context Switches")); } static int contextswitches_vlabel(collecty_ctx* ctx, - collecty_graph* graph, const char* object, char** title) { - return collecty_format_title(title, "%s", _("Context Switches/s")); + collecty_graph* graph, const char* object, char* vlabel, size_t length) { + return __collecty_string_set(vlabel, length, _("Context Switches/s")); } static int contextswitches_render(collecty_ctx* ctx, diff --git a/src/daemon/graphs/loadavg.c b/src/daemon/graphs/loadavg.c index 7bf1534..cd5b537 100644 --- a/src/daemon/graphs/loadavg.c +++ b/src/daemon/graphs/loadavg.c @@ -20,6 +20,7 @@ #include +#include "../string.h" #include "graph.h" #include "loadavg.h" @@ -29,8 +30,8 @@ #define COLOR_LOAD1 YELLOW static int loadavg_title(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title) { - return collecty_format_title(title, "%s", _("Load Average")); + const char* object, char* title, size_t length) { + return __collecty_string_set(title, length, _("Load Average")); } static int loadavg_render(collecty_ctx* ctx, diff --git a/src/daemon/graphs/processor.c b/src/daemon/graphs/processor.c index b005808..093e656 100644 --- a/src/daemon/graphs/processor.c +++ b/src/daemon/graphs/processor.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include "../string.h" #include "graph.h" #include "processor.h" @@ -34,16 +35,15 @@ #define COLOR_IDLE LIGHT_GREY static int processor_title(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title) { - return collecty_format_title(title, "%s", _("Processor Usage")); + const char* object, char* title, size_t length) { + return __collecty_string_set(title, length, _("Processor Usage")); } static int processor_vlabel(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title) { - return collecty_format_title(title, "%s", _("Percent")); + const char* object, char* vlabel, size_t length) { + return __collecty_string_set(vlabel, length, _("Percent")); } - static int processor_render(collecty_ctx* ctx, collecty_graph* graph, collecty_args* args, const char* object) { int r; diff --git a/src/daemon/graphs/uptime.c b/src/daemon/graphs/uptime.c index 1b9e78d..9d29e47 100644 --- a/src/daemon/graphs/uptime.c +++ b/src/daemon/graphs/uptime.c @@ -20,17 +20,18 @@ #include +#include "../string.h" #include "graph.h" #include "uptime.h" static int uptime_title(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title) { - return collecty_format_title(title, "%s", _("Uptime")); + const char* object, char* title, size_t length) { + return __collecty_string_set(title, length, _("Uptime")); } static int uptime_vlabel(collecty_ctx* ctx, collecty_graph* graph, - const char* object, char** title) { - return collecty_format_title(title, "%s", _("Days")); + const char* object, char* vlabel, size_t length) { + return __collecty_string_set(vlabel, length, _("Days")); } static int uptime_render(collecty_ctx* ctx,