From 9124be4600051c4d2ab2ef728a2800ea1f3cb10b Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 3 Jul 2010 11:46:58 +0200 Subject: [PATCH] src/utils_threshold.c: Improve the replacement macros. --- src/utils_threshold.c | 55 +++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/utils_threshold.c b/src/utils_threshold.c index 4da0893d..cc5d8e7b 100644 --- a/src/utils_threshold.c +++ b/src/utils_threshold.c @@ -1,6 +1,6 @@ /** * collectd - src/utils_threshold.c - * Copyright (C) 2007-2009 Florian octo Forster + * Copyright (C) 2007-2010 Florian octo Forster * Copyright (C) 2008-2009 Sebastian Harl * Copyright (C) 2009,2010 Andrés J. Díaz * @@ -618,27 +618,29 @@ int ut_build_message(char *out, size_t bufsize, const char *fmt, /* TODO: We could provide here a way to use meta information on thresholds * directly in the future. */ char msg[NOTIF_MAX_MSG_LEN]; - char temp[NOTIF_MAX_MSG_LEN]; gauge_t *rates; int rates_failed; - int i; sstrncpy (msg, fmt, sizeof (msg)); -#define REPLACE_FIELD(t,v) \ - if (subst_string (temp, sizeof (temp), msg, t, v) != NULL) \ - sstrncpy (msg, temp, sizeof (msg)); +#define REPLACE_FIELD(t,v) do { \ + char temp[sizeof (msg)]; \ + if (subst_string (temp, sizeof (temp), msg, (t), (v)) != NULL) \ + sstrncpy (msg, temp, sizeof (msg)); \ +} while (0) - char ftoa_temp[NOTIF_MAX_MSG_LEN]; -#define FTOA(string,f) \ - memset(string,0x00,sizeof(string)); \ - snprintf(string, sizeof(string), "%f", f); +#define REPLACE_FIELD_DBL(t,v) do { \ + char value_str[NOTIF_MAX_MSG_LEN]; \ + ssnprintf (value_str, sizeof (value_str), "%g", (v)); \ + REPLACE_FIELD(t, value_str); \ +} while (0) - char itoa_temp[NOTIF_MAX_MSG_LEN]; -#define ITOA(string,i) \ - memset(string,0x00,sizeof(string)); \ - snprintf(string, sizeof(string), "%i", i); +#define REPLACE_FIELD_INT(t,v) do { \ + char value_str[NOTIF_MAX_MSG_LEN]; \ + ssnprintf (value_str, sizeof (value_str), "%i", (v)); \ + REPLACE_FIELD(t, value_str); \ +} while (0) REPLACE_FIELD ("%{host}", n->host); REPLACE_FIELD ("%{plugin}", n->plugin); @@ -649,8 +651,7 @@ int ut_build_message(char *out, size_t bufsize, const char *fmt, /* This is the offending value, its equivalent to %{ds:value}, if * value is the data_source name. */ - FTOA(ftoa_temp,values[ds_index]) - REPLACE_FIELD ("%{value}", ftoa_temp); + REPLACE_FIELD_DBL ("%{value}", values[ds_index]); /* Now replace all %{ds: