Skip to content

Commit

Permalink
macros: extract type information from MESSAGE, PROGRAM and HOST
Browse files Browse the repository at this point in the history
MESSAGE in particular can contain not just string
data, but for example JSON as well.

This is necessary for the $(format-json) function
to work properly with a json type MESSAGE.

Kudos to b1oodborne from Discord for finding this
issue.

Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed Jun 11, 2024
1 parent 45caab9 commit 309091d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/template/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ static struct timespec app_uptime;
static GHashTable *macro_hash;

static void
_result_append_value(GString *result, const LogMessage *lm, NVHandle handle)
_result_append_value(GString *result, const LogMessage *lm, NVHandle handle, LogMessageValueType *type)
{
const gchar *str;
gssize len = 0;

str = log_msg_get_value(lm, handle, &len);
str = log_msg_get_value_with_type(lm, handle, &len, type);
g_string_append_len(result, str, len);
}

Expand Down Expand Up @@ -530,7 +530,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg
const gchar *p1, *p2;
int remaining, length;
gssize host_len;
const gchar *host = log_msg_get_value(msg, LM_V_HOST, &host_len);
const gchar *host = log_msg_get_value_with_type(msg, LM_V_HOST, &host_len, &t);

p1 = memchr(host, '@', host_len);

Expand All @@ -547,7 +547,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg
}
else
{
_result_append_value(result, msg, LM_V_HOST);
_result_append_value(result, msg, LM_V_HOST, &t);
}
break;
}
Expand All @@ -561,14 +561,14 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg
gssize len;
const gchar *p;

p = log_msg_get_value(msg, LM_V_LEGACY_MSGHDR, &len);
p = log_msg_get_value_with_type(msg, LM_V_LEGACY_MSGHDR, &len, &t);
if (len > 0)
g_string_append_len(result, p, len);
else
{
/* message, complete with program name and pid */
len = result->len;
_result_append_value(result, msg, LM_V_PROGRAM);
_result_append_value(result, msg, LM_V_PROGRAM, &t);
if (len != result->len)
{
const gchar *pid = log_msg_get_value(msg, LM_V_PID, &len);
Expand All @@ -585,7 +585,7 @@ log_macro_expand(gint id, LogTemplateEvalOptions *options, const LogMessage *msg
}
case M_MESSAGE:
{
_result_append_value(result, msg, LM_V_MESSAGE);
_result_append_value(result, msg, LM_V_MESSAGE, &t);
break;
}
case M_SOURCE_IP:
Expand Down

0 comments on commit 309091d

Please sign in to comment.