Skip to content

Commit

Permalink
lib: Add some event handling functions.
Browse files Browse the repository at this point in the history
event_has_all_categories() - checks if given event contains all the
categories of an another event.
event_has_all_fields() - checks if given event contains all the fields
of an another event.
event_dup() - duplicates an event.
event_copy_categories_fields() - copies all categories and fields from
source to dest.
  • Loading branch information
Sergey-Kitov authored and villesavolainen committed Sep 7, 2018
1 parent 97ef926 commit 43f4580
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/lib/lib-event.c
Expand Up @@ -46,6 +46,75 @@ static void event_copy_parent_defaults(struct event *event,
event->forced_debug = parent->forced_debug;
}

static bool
event_find_category(struct event *event, const struct event_category *category);

static struct event_field *
event_find_field_int(struct event *event, const char *key);

void event_copy_categories_fields(struct event *to, struct event *from)
{
unsigned int cat_count;
struct event_category *const *categories = event_get_categories(from, &cat_count);
while (cat_count-- > 0)
event_add_category(to, categories[cat_count]);
const struct event_field *fld;
if (!array_is_created(&from->fields))
return;
array_foreach(&from->fields, fld) {
switch (fld->value_type) {
case EVENT_FIELD_VALUE_TYPE_STR:
event_add_str(to, fld->key, fld->value.str);
break;
case EVENT_FIELD_VALUE_TYPE_INTMAX:
event_add_int(to, fld->key, fld->value.intmax);
break;
case EVENT_FIELD_VALUE_TYPE_TIMEVAL:
event_add_timeval(to, fld->key, &fld->value.timeval);
break;
default:
break;
}
}
}

bool event_has_all_categories(struct event *event, const struct event *other)
{
struct event_category **cat;
if (!array_is_created(&other->categories))
return TRUE;
if (!array_is_created(&event->categories))
return FALSE;
array_foreach_modifiable(&other->categories, cat) {
if (!event_find_category(event, *cat))
return FALSE;
}
return TRUE;
}

bool event_has_all_fields(struct event *event, const struct event *other)
{
struct event_field *fld;
if (!array_is_created(&other->fields))
return TRUE;
array_foreach_modifiable(&other->fields, fld) {
if (event_find_field_int(event, fld->key) == NULL)
return FALSE;
}
return TRUE;
}

struct event *event_dup(const struct event *source)
{
struct event *ret = event_create(source->parent);
string_t *str = t_str_new(256);
const char *err;
event_export(source, str);
if (!event_import(ret, str_c(str), &err))
i_panic("event_import(%s) failed: %s", str_c(str), err);
return ret;
}

#undef event_create
struct event *event_create(struct event *parent, const char *source_filename,
unsigned int source_linenum)
Expand Down
13 changes: 13 additions & 0 deletions src/lib/lib-event.h
Expand Up @@ -73,6 +73,19 @@ struct event_passthrough {
struct event *(*event)(void);
};

/* Returns TRUE if the event has all the categories that the "other" event has (and maybe more). */
bool event_has_all_categories(struct event *event, const struct event *other);
/* Returns TRUE if the event has all the fields that the "other" event has (and maybe more).
Only the fields in the events themselves are checked. Parent events' fields are not checked. */
bool event_has_all_fields(struct event *event, const struct event *other);

/* Returns the source event duplicated into a new event. */
struct event *event_dup(const struct event *source);
/* Copy all categories and fields from source to dest.
Only the fields and categories in source event itself are copied.
Parent events' fields and categories aren't copied. */
void event_copy_categories_fields(struct event *dest, struct event *source);

/* Create a new empty event under the parent event, or NULL for root event. */
struct event *event_create(struct event *parent, const char *source_filename,
unsigned int source_linenum);
Expand Down

0 comments on commit 43f4580

Please sign in to comment.