text plugin: add color support
[babeltrace.git] / plugins / trimmer / trimmer.c
index a68828931aa40988022e6aa7ecd3a9bbdf383387..06814ef82be408e6459bcbf6e86576ef7923b397 100644 (file)
  * SOFTWARE.
  */
 
-#include <babeltrace/plugin/plugin-macros.h>
-#include <babeltrace/plugin/component.h>
-#include <babeltrace/plugin/filter.h>
-#include <babeltrace/plugin/notification/notification.h>
-#include <babeltrace/plugin/notification/iterator.h>
-#include <babeltrace/plugin/notification/event.h>
+#include <babeltrace/plugin/plugin-dev.h>
+#include <babeltrace/component/component.h>
+#include <babeltrace/component/component-filter.h>
+#include <babeltrace/component/notification/notification.h>
+#include <babeltrace/component/notification/iterator.h>
+#include <babeltrace/component/notification/event.h>
+#include <plugins-common.h>
 #include "trimmer.h"
 #include "iterator.h"
 #include <assert.h>
@@ -80,7 +81,7 @@ void destroy_trimmer(struct bt_component *component)
  *   ss
  */
 static
-int timestamp_from_arg(const char *arg,
+int timestamp_from_arg(const char *arg, struct trimmer *trimmer,
                struct trimmer_bound *result_bound, bool gmt)
 {
        int ret;
@@ -116,19 +117,54 @@ int timestamp_from_arg(const char *arg,
                value = (int64_t) result;
                value *= NSEC_PER_SEC;
                value += ns;
+               if (!trimmer->date) {
+                       trimmer->year = year;
+                       trimmer->month = month;
+                       trimmer->day = day;
+                       trimmer->date = true;
+               }
                goto set;
        }
        /* hh:mm:ss.ns */
        ret = sscanf(arg, "%u:%u:%u.%u",
                &hh, &mm, &ss, &ns);
        if (ret == 4) {
-               /* We don't know which day until we get an event. */
-               result_bound->lazy_values.hh = hh;
-               result_bound->lazy_values.mm = mm;
-               result_bound->lazy_values.ss = ss;
-               result_bound->lazy_values.ns = ns;
-               result_bound->lazy_values.gmt = gmt;
-               goto lazy;
+               if (!trimmer->date) {
+                       /* We don't know which day until we get an event. */
+                       result_bound->lazy_values.hh = hh;
+                       result_bound->lazy_values.mm = mm;
+                       result_bound->lazy_values.ss = ss;
+                       result_bound->lazy_values.ns = ns;
+                       result_bound->lazy_values.gmt = gmt;
+                       goto lazy;
+               } else {
+                       struct tm tm = {
+                               .tm_sec = ss,
+                               .tm_min = mm,
+                               .tm_hour = hh,
+                               .tm_mday = trimmer->day,
+                               .tm_mon = trimmer->month - 1,
+                               .tm_year = trimmer->year - 1900,
+                               .tm_isdst = -1,
+                       };
+                       time_t result;
+
+                       if (gmt) {
+                               result = timegm(&tm);
+                               if (result < 0) {
+                                       return -1;
+                               }
+                       } else {
+                               result = mktime(&tm);
+                               if (result < 0) {
+                                       return -1;
+                               }
+                       }
+                       value = (int64_t) result;
+                       value *= NSEC_PER_SEC;
+                       value += ns;
+                       goto set;
+               }
        }
        /* -ss.ns */
        ret = sscanf(arg, "-%u.%u",
@@ -173,19 +209,53 @@ int timestamp_from_arg(const char *arg,
                        }
                }
                value *= NSEC_PER_SEC;
+               if (!trimmer->date) {
+                       trimmer->year = year;
+                       trimmer->month = month;
+                       trimmer->day = day;
+                       trimmer->date = true;
+               }
                goto set;
        }
        /* hh:mm:ss */
        ret = sscanf(arg, "%u:%u:%u",
                &hh, &mm, &ss);
        if (ret == 3) {
-               /* We don't know which day until we get an event. */
-               result_bound->lazy_values.hh = hh;
-               result_bound->lazy_values.mm = mm;
-               result_bound->lazy_values.ss = ss;
-               result_bound->lazy_values.ns = 0;
-               result_bound->lazy_values.gmt = gmt;
-               goto lazy;
+               if (!trimmer->date) {
+                       /* We don't know which day until we get an event. */
+                       result_bound->lazy_values.hh = hh;
+                       result_bound->lazy_values.mm = mm;
+                       result_bound->lazy_values.ss = ss;
+                       result_bound->lazy_values.ns = 0;
+                       result_bound->lazy_values.gmt = gmt;
+                       goto lazy;
+               } else {
+                       struct tm tm = {
+                               .tm_sec = ss,
+                               .tm_min = mm,
+                               .tm_hour = hh,
+                               .tm_mday = trimmer->day,
+                               .tm_mon = trimmer->month - 1,
+                               .tm_year = trimmer->year - 1900,
+                               .tm_isdst = -1,
+                       };
+                       time_t result;
+
+                       if (gmt) {
+                               result = timegm(&tm);
+                               if (result < 0) {
+                                       return -1;
+                               }
+                       } else {
+                               result = mktime(&tm);
+                               if (result < 0) {
+                                       return -1;
+                               }
+                       }
+                       value = (int64_t) result;
+                       value *= NSEC_PER_SEC;
+                       goto set;
+               }
        }
        /* -ss */
        ret = sscanf(arg, "-%u",
@@ -231,7 +301,7 @@ enum bt_component_status init_from_params(struct trimmer *trimmer, struct bt_val
                value_ret = bt_value_bool_get(value, &gmt);
                if (value_ret) {
                        ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve clock-gmt value. Expecting a boolean.\n");
+                       printf_error("Failed to retrieve clock-gmt value. Expecting a boolean");
                }
        }
        bt_put(value);
@@ -246,9 +316,9 @@ enum bt_component_status init_from_params(struct trimmer *trimmer, struct bt_val
 
                value_ret = bt_value_string_get(value, &str);
                if (value_ret || timestamp_from_arg(str,
-                               &trimmer->begin, gmt)) {
+                               trimmer, &trimmer->begin, gmt)) {
                        ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve begin value. Expecting a timestamp string.\n");
+                       printf_error("Failed to retrieve begin value. Expecting a timestamp string");
                }
        }
        bt_put(value);
@@ -263,18 +333,25 @@ enum bt_component_status init_from_params(struct trimmer *trimmer, struct bt_val
 
                value_ret = bt_value_string_get(value, &str);
                if (value_ret || timestamp_from_arg(str,
-                               &trimmer->end, gmt)) {
+                               trimmer, &trimmer->end, gmt)) {
                        ret = BT_COMPONENT_STATUS_INVALID;
-                       printf_error("Failed to retrieve end value. Expecting a timestamp string.\n");
+                       printf_error("Failed to retrieve end value. Expecting a timestamp string");
                }
        }
        bt_put(value);
 end:
+       if (trimmer->begin.set && trimmer->end.set) {
+               if (trimmer->begin.value > trimmer->end.value) {
+                       printf_error("Unexpected: time range begin value is above end value");
+                       ret = BT_COMPONENT_STATUS_INVALID;
+               }
+       }
        return ret;
 }
 
 enum bt_component_status trimmer_component_init(
-       struct bt_component *component, struct bt_value *params)
+       struct bt_component *component, struct bt_value *params,
+       UNUSED_VAR void *init_method_data)
 {
        enum bt_component_status ret;
        struct trimmer *trimmer = create_trimmer_data();
@@ -284,23 +361,11 @@ enum bt_component_status trimmer_component_init(
                goto end;
        }
 
-       ret = bt_component_set_destroy_cb(component,
-                       destroy_trimmer);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto error;
-       }
-
        ret = bt_component_set_private_data(component, trimmer);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto error;
        }
 
-       ret = bt_component_filter_set_iterator_init_cb(component,
-                       trimmer_iterator_init);
-       if (ret != BT_COMPONENT_STATUS_OK) {
-               goto error;
-       }
-
        ret = init_from_params(trimmer, params);
 end:
        return ret;
@@ -310,13 +375,19 @@ error:
 }
 
 /* Initialize plug-in entry points. */
-BT_PLUGIN_NAME("utils");
+BT_PLUGIN(utils);
 BT_PLUGIN_DESCRIPTION("Babeltrace Trace Trimmer Plug-In.");
 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
 BT_PLUGIN_LICENSE("MIT");
-
-BT_PLUGIN_COMPONENT_CLASSES_BEGIN
-BT_PLUGIN_FILTER_COMPONENT_CLASS_ENTRY("trimmer",
-               "Ensure that trace notifications outside of a given range are filtered-out.",
-               trimmer_component_init)
-BT_PLUGIN_COMPONENT_CLASSES_END
+BT_PLUGIN_FILTER_COMPONENT_CLASS(trimmer, trimmer_iterator_get,
+       trimmer_iterator_next);
+BT_PLUGIN_FILTER_COMPONENT_CLASS_DESCRIPTION(trimmer,
+       "Ensure that trace notifications outside of a given range are filtered-out.");
+BT_PLUGIN_FILTER_COMPONENT_CLASS_INIT_METHOD(trimmer, trimmer_component_init);
+BT_PLUGIN_FILTER_COMPONENT_CLASS_DESTROY_METHOD(trimmer, destroy_trimmer);
+BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_INIT_METHOD(trimmer,
+       trimmer_iterator_init);
+BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_DESTROY_METHOD(trimmer,
+       trimmer_iterator_destroy);
+BT_PLUGIN_FILTER_COMPONENT_CLASS_NOTIFICATION_ITERATOR_SEEK_TIME_METHOD(trimmer,
+       trimmer_iterator_seek_time);
This page took 0.03035 seconds and 4 git commands to generate.