ticked_ttilog.c 5.59 KB
Newer Older
Cedric Roux's avatar
Cedric Roux committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#include "logger.h"
#include "logger_defs.h"
#include "event.h"
#include "database.h"
#include "handler.h"
#include "filter/filter.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct ticked_ttilog {
  struct logger common;
  void *database;
  int tick_frame_arg;
  int tick_subframe_arg;
  int data_frame_arg;
  int data_subframe_arg;
  int data_arg;
  int convert_to_dB;
  int last_tick_frame;
  int last_tick_subframe;
  float last_value;
  float empty_value;
  char *tick_event_name;
  unsigned long tick_handler_id;
  /* tick filter - NULL is no filter set */
  void *tick_filter;
};

static void _event(void *p, event e)
{
  struct ticked_ttilog *l = p;
  int frame;
  int subframe;
  float value;

  if (l->common.filter != NULL && filter_eval(l->common.filter, e) == 0)
    return;

  frame = e.e[l->data_frame_arg].i;
  subframe = e.e[l->data_subframe_arg].i;

  if (frame != l->last_tick_frame || subframe != l->last_tick_subframe) {
    printf("WARNING: %s:%d: data comes without previous tick!\n",
           __FILE__, __LINE__);
    return;
  }

  switch (e.e[l->data_arg].type) {
  case EVENT_INT: value = e.e[l->data_arg].i; break;
  case EVENT_ULONG: value = e.e[l->data_arg].ul; break;
  default: printf("%s:%d: unsupported type\n", __FILE__, __LINE__); abort();
  }

  if (l->convert_to_dB) value = 10 * log10(value);

  l->last_value = value;
}

static void _tick_event(void *p, event e)
{
  struct ticked_ttilog *l = p;
  int i;
  int frame;
  int subframe;

  if (l->tick_filter != NULL && filter_eval(l->tick_filter, e) == 0)
    return;

  frame = e.e[l->tick_frame_arg].i;
  subframe = e.e[l->tick_subframe_arg].i;

  for (i = 0; i < l->common.vsize; i++)
    l->common.v[i]->append(l->common.v[i], frame, subframe, l->last_value);

  l->last_value = l->empty_value;
  l->last_tick_frame = frame;
  l->last_tick_subframe = subframe;
}

logger *new_ticked_ttilog(event_handler *h, void *database,
    char *tick_event_name, char *frame_varname, char *subframe_varname,
    char *event_name, char *data_varname,
    int convert_to_dB, float empty_value)
{
  struct ticked_ttilog *ret;
  int event_id;
  database_event_format f;
  int i;

  ret = calloc(1, sizeof(struct ticked_ttilog)); if (ret == NULL) abort();

  ret->common.event_name = strdup(event_name);
  if (ret->common.event_name == NULL) abort();
  ret->database = database;
  ret->convert_to_dB = convert_to_dB;

  ret->tick_event_name = strdup(tick_event_name);
  if (ret->tick_event_name == NULL) abort();
  ret->empty_value = empty_value;
  ret->last_value = empty_value;

  /* tick event */
  event_id = event_id_from_name(database, tick_event_name);

  ret->tick_handler_id=register_handler_function(h,event_id,_tick_event,ret);

  f = get_format(database, event_id);

  /* look for frame, subframe and data args */
  ret->tick_frame_arg = -1;
  ret->tick_subframe_arg = -1;
  for (i = 0; i < f.count; i++) {
    if (!strcmp(f.name[i], frame_varname)) ret->tick_frame_arg = i;
    if (!strcmp(f.name[i], subframe_varname)) ret->tick_subframe_arg = i;
  }
  if (ret->tick_frame_arg == -1) {
    printf("%s:%d: frame argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, frame_varname, event_name);
    abort();
  }
  if (ret->tick_subframe_arg == -1) {
    printf("%s:%d: subframe argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, subframe_varname, event_name);
    abort();
  }
  if (strcmp(f.type[ret->tick_frame_arg], "int") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'int')\n",
        __FILE__, __LINE__, frame_varname);
    abort();
  }
  if (strcmp(f.type[ret->tick_subframe_arg], "int") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'int')\n",
        __FILE__, __LINE__, subframe_varname);
    abort();
  }

  /* data event */
  event_id = event_id_from_name(database, event_name);

  ret->common.handler_id = register_handler_function(h,event_id,_event,ret);

  f = get_format(database, event_id);

  /* look for frame and subframe */
  ret->data_frame_arg = -1;
  ret->data_subframe_arg = -1;
  ret->data_arg = -1;
  for (i = 0; i < f.count; i++) {
    if (!strcmp(f.name[i], frame_varname)) ret->data_frame_arg = i;
    if (!strcmp(f.name[i], subframe_varname)) ret->data_subframe_arg = i;
    if (!strcmp(f.name[i], data_varname)) ret->data_arg = i;
  }
  if (ret->data_frame_arg == -1) {
    printf("%s:%d: frame argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, frame_varname, event_name);
    abort();
  }
  if (ret->data_subframe_arg == -1) {
    printf("%s:%d: subframe argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, subframe_varname, event_name);
    abort();
  }
  if (ret->data_arg == -1) {
    printf("%s:%d: data argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, data_varname, event_name);
    abort();
  }
  if (strcmp(f.type[ret->data_frame_arg], "int") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'int')\n",
        __FILE__, __LINE__, frame_varname);
    abort();
  }
  if (strcmp(f.type[ret->data_subframe_arg], "int") != 0) {
    printf("%s:%d: argument '%s' has wrong type (should be 'int')\n",
        __FILE__, __LINE__, subframe_varname);
    abort();
  }
  if (strcmp(f.type[ret->data_arg], "int") != 0 &&
      strcmp(f.type[ret->data_arg], "float") != 0) {
    printf("%s:%d: argument '%s' has wrong type"
           " (should be 'int' or 'float')\n",
        __FILE__, __LINE__, data_varname);
    abort();
  }

  return ret;
}

void ticked_ttilog_set_tick_filter(logger *_l, void *filter)
{
  struct ticked_ttilog *l = (struct ticked_ttilog *)_l;
  free(l->tick_filter);
  l->tick_filter = filter;
}