diff --git a/lib/filterx/expr-variable.c b/lib/filterx/expr-variable.c index ee76cfaea..2eede2f4e 100644 --- a/lib/filterx/expr-variable.c +++ b/lib/filterx/expr-variable.c @@ -34,9 +34,10 @@ typedef struct _FilterXVariableExpr { FilterXExpr super; - FilterXObject *variable_name; + FilterXVariableType variable_type; NVHandle handle; - guint32 declared:1, handle_is_macro:1; + FilterXObject *variable_name; + guint32 handle_is_macro:1; } FilterXVariableExpr; static FilterXObject * @@ -61,7 +62,7 @@ _pull_variable_from_message(FilterXVariableExpr *self, FilterXEvalContext *conte static void _whiteout_variable(FilterXVariableExpr *self, FilterXEvalContext *context) { - filterx_scope_register_variable(context->scope, self->handle, NULL); + filterx_scope_register_variable(context->scope, FX_VAR_MESSAGE_TIED, self->handle, NULL); } static FilterXObject * @@ -87,7 +88,7 @@ _eval(FilterXExpr *s) FilterXObject *msg_ref = _pull_variable_from_message(self, context, context->msg); if(!msg_ref) return NULL; - filterx_scope_register_variable(context->scope, self->handle, msg_ref); + filterx_scope_register_variable(context->scope, FX_VAR_MESSAGE_TIED, self->handle, msg_ref); return msg_ref; } @@ -124,10 +125,7 @@ _assign(FilterXExpr *s, FilterXObject *new_value) /* NOTE: we pass NULL as initial_value to make sure the new variable * is considered changed due to the assignment */ - if (self->declared) - variable = filterx_scope_register_declared_variable(scope, self->handle, NULL); - else - variable = filterx_scope_register_variable(scope, self->handle, NULL); + variable = filterx_scope_register_variable(scope, self->variable_type, self->handle, NULL); } /* this only clones mutable objects */ @@ -217,7 +215,7 @@ _deinit(FilterXExpr *s, GlobalConfig *cfg) } static FilterXExpr * -filterx_variable_expr_new(FilterXString *name, FilterXVariableType type) +filterx_variable_expr_new(FilterXString *name, FilterXVariableType variable_type) { FilterXVariableExpr *self = g_new0(FilterXVariableExpr, 1); @@ -231,9 +229,10 @@ filterx_variable_expr_new(FilterXString *name, FilterXVariableType type) self->super.is_set = _isset; self->super.unset = _unset; + self->variable_type = variable_type; self->variable_name = (FilterXObject *) name; - self->handle = filterx_map_varname_to_handle(filterx_string_get_value_ref(self->variable_name, NULL), type); - if (type == FX_VAR_MESSAGE) + self->handle = filterx_map_varname_to_handle(filterx_string_get_value_ref(self->variable_name, NULL), variable_type); + if (variable_type == FX_VAR_MESSAGE_TIED) self->handle_is_macro = log_msg_is_handle_macro(filterx_variable_handle_to_nv_handle(self->handle)); return &self->super; @@ -242,7 +241,7 @@ filterx_variable_expr_new(FilterXString *name, FilterXVariableType type) FilterXExpr * filterx_msg_variable_expr_new(FilterXString *name) { - return filterx_variable_expr_new(name, FX_VAR_MESSAGE); + return filterx_variable_expr_new(name, FX_VAR_MESSAGE_TIED); } FilterXExpr * @@ -257,5 +256,7 @@ filterx_variable_expr_declare(FilterXExpr *s) FilterXVariableExpr *self = (FilterXVariableExpr *) s; g_assert(s->eval == _eval); - self->declared = TRUE; + /* we can only declare a floating variable */ + g_assert(self->variable_type == FX_VAR_FLOATING); + self->variable_type = FX_VAR_DECLARED_FLOATING; } diff --git a/lib/filterx/filterx-scope.c b/lib/filterx/filterx-scope.c index f85d839b7..d742ae199 100644 --- a/lib/filterx/filterx-scope.c +++ b/lib/filterx/filterx-scope.c @@ -115,6 +115,7 @@ filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle) static FilterXVariable * _register_variable(FilterXScope *self, + FilterXVariableType variable_type, FilterXVariableHandle handle, FilterXObject *initial_value) { @@ -142,7 +143,7 @@ _register_variable(FilterXScope *self, FilterXVariable v; - filterx_variable_init_instance(&v, handle, initial_value, self->generation); + filterx_variable_init_instance(&v, variable_type, handle, initial_value, self->generation); g_array_insert_val(self->variables, v_index, v); return &g_array_index(self->variables, FilterXVariable, v_index); @@ -150,11 +151,11 @@ _register_variable(FilterXScope *self, FilterXVariable * filterx_scope_register_variable(FilterXScope *self, + FilterXVariableType variable_type, FilterXVariableHandle handle, FilterXObject *initial_value) { - FilterXVariable *v = _register_variable(self, handle, initial_value); - filterx_variable_set_declared(v, FALSE); + FilterXVariable *v = _register_variable(self, variable_type, handle, initial_value); /* the scope needs to be synced with the message if it holds a * message-tied variable (e.g. $MSG) */ @@ -163,20 +164,6 @@ filterx_scope_register_variable(FilterXScope *self, return v; } -FilterXVariable * -filterx_scope_register_declared_variable(FilterXScope *self, - FilterXVariableHandle handle, - FilterXObject *initial_value) - -{ - g_assert(filterx_variable_handle_is_floating(handle)); - - FilterXVariable *v = _register_variable(self, handle, initial_value); - filterx_variable_set_declared(v, TRUE); - - return v; -} - gboolean filterx_scope_foreach_variable(FilterXScope *self, FilterXScopeForeachFunc func, gpointer user_data) { diff --git a/lib/filterx/filterx-scope.h b/lib/filterx/filterx-scope.h index 89f12c366..2e996d202 100644 --- a/lib/filterx/filterx-scope.h +++ b/lib/filterx/filterx-scope.h @@ -52,11 +52,9 @@ void filterx_scope_sync(FilterXScope *self, LogMessage *msg); FilterXVariable *filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle); FilterXVariable *filterx_scope_register_variable(FilterXScope *self, + FilterXVariableType variable_type, FilterXVariableHandle handle, FilterXObject *initial_value); -FilterXVariable *filterx_scope_register_declared_variable(FilterXScope *self, - FilterXVariableHandle handle, - FilterXObject *initial_value); gboolean filterx_scope_foreach_variable(FilterXScope *self, FilterXScopeForeachFunc func, gpointer user_data); void filterx_scope_invalidate_log_msg_cache(FilterXScope *self); diff --git a/lib/filterx/filterx-variable.c b/lib/filterx/filterx-variable.c index 95a10e08d..3b5b726e2 100644 --- a/lib/filterx/filterx-variable.c +++ b/lib/filterx/filterx-variable.c @@ -28,12 +28,12 @@ FilterXVariableHandle filterx_map_varname_to_handle(const gchar *name, FilterXVariableType type) { - if (type == FX_VAR_MESSAGE) + if (type == FX_VAR_MESSAGE_TIED) name++; NVHandle nv_handle = log_msg_get_value_handle(name); - if (type == FX_VAR_MESSAGE) + if (type == FX_VAR_MESSAGE_TIED) return (FilterXVariableHandle) nv_handle; return (FilterXVariableHandle) nv_handle | FILTERX_HANDLE_FLOATING_BIT; } @@ -45,12 +45,15 @@ filterx_variable_clear(FilterXVariable *v) } void -filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle, - FilterXObject *initial_value, guint32 generation) +filterx_variable_init_instance(FilterXVariable *v, + FilterXVariableType variable_type, + FilterXVariableHandle handle, + FilterXObject *initial_value, + guint32 generation) { v->handle = handle; + v->variable_type = variable_type; v->assigned = FALSE; - v->declared = FALSE; v->generation = generation; v->value = filterx_object_ref(initial_value); } diff --git a/lib/filterx/filterx-variable.h b/lib/filterx/filterx-variable.h index 2cd498b52..447774c1c 100644 --- a/lib/filterx/filterx-variable.h +++ b/lib/filterx/filterx-variable.h @@ -31,9 +31,9 @@ typedef enum { - FX_VAR_MESSAGE, + FX_VAR_MESSAGE_TIED, FX_VAR_FLOATING, - FX_VAR_DECLARED, + FX_VAR_DECLARED_FLOATING, } FilterXVariableType; #define FILTERX_SCOPE_MAX_GENERATION ((1UL << 20) - 1) @@ -72,13 +72,16 @@ typedef struct _FilterXVariable * declared -- this variable is declared (e.g. retained for the entire input pipeline) */ guint32 assigned:1, - declared:1, + variable_type:2, generation:20; FilterXObject *value; } FilterXVariable; -void filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle, - FilterXObject *initial_value, guint32 generation); +void filterx_variable_init_instance(FilterXVariable *v, + FilterXVariableType variable_type, + FilterXVariableHandle handle, + FilterXObject *initial_value, + guint32 generation); void filterx_variable_clear(FilterXVariable *v); static inline gboolean @@ -134,7 +137,7 @@ filterx_variable_is_set(FilterXVariable *v) static inline gboolean filterx_variable_is_declared(FilterXVariable *v) { - return v->declared; + return v->variable_type == FX_VAR_DECLARED_FLOATING; } static inline gboolean @@ -161,10 +164,4 @@ filterx_variable_unassign(FilterXVariable *v) v->assigned = FALSE; } -static inline void -filterx_variable_set_declared(FilterXVariable *v, gboolean declared) -{ - v->declared = declared; -} - #endif diff --git a/lib/filterx/func-vars.c b/lib/filterx/func-vars.c index f760e31b2..6c24ceed5 100644 --- a/lib/filterx/func-vars.c +++ b/lib/filterx/func-vars.c @@ -111,14 +111,11 @@ _load_from_dict(FilterXObject *key, FilterXObject *value, gpointer user_data) return FALSE; } - gboolean is_floating = key_str[0] != '$'; - FilterXVariableHandle handle = filterx_map_varname_to_handle(key_str, is_floating ? FX_VAR_FLOATING : FX_VAR_MESSAGE); + FilterXVariableType variable_type = (key_str[0] == '$') ? FX_VAR_MESSAGE_TIED : FX_VAR_DECLARED_FLOATING; + FilterXVariableHandle handle = filterx_map_varname_to_handle(key_str, variable_type); FilterXVariable *variable = NULL; - if (is_floating) - variable = filterx_scope_register_declared_variable(scope, handle, NULL); - else - variable = filterx_scope_register_variable(scope, handle, NULL); + variable = filterx_scope_register_variable(scope, variable_type, handle, NULL); FilterXObject *cloned_value = filterx_object_clone(value); filterx_variable_set_value(variable, cloned_value); @@ -143,7 +140,7 @@ _load_from_dict(FilterXObject *key, FilterXObject *value, gpointer user_data) evt_tag_str("key", key_str), evt_tag_str("value", repr->str), evt_tag_str("type", value->type->name), - evt_tag_str("variable_type", is_floating ? "declared" : "message")); + evt_tag_int("variable_type", variable_type)); } return TRUE;