ttilog.c 3.09 KB
Newer Older
1
2
#include "logger.h"
#include "logger_defs.h"
Cedric Roux's avatar
Cedric Roux committed
3
4
5
#include "event.h"
#include "database.h"
#include "handler.h"
Cedric Roux's avatar
Cedric Roux committed
6
#include "filter/filter.h"
Cedric Roux's avatar
Cedric Roux committed
7
8
9
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
10
#include <math.h>
Cedric Roux's avatar
Cedric Roux committed
11
12

struct ttilog {
13
  struct logger common;
Cedric Roux's avatar
Cedric Roux committed
14
15
16
17
  void *database;
  int frame_arg;
  int subframe_arg;
  int data_arg;
18
  int convert_to_dB;
Cedric Roux's avatar
Cedric Roux committed
19
20
21
22
23
24
25
26
27
28
};

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

Cedric Roux's avatar
Cedric Roux committed
29
30
31
  if (l->common.filter != NULL && filter_eval(l->common.filter, e) == 0)
    return;

Cedric Roux's avatar
Cedric Roux committed
32
33
34
  frame = e.e[l->frame_arg].i;
  subframe = e.e[l->subframe_arg].i;
  switch (e.e[l->data_arg].type) {
35
  case EVENT_INT: value = e.e[l->data_arg].i; break;
Cedric Roux's avatar
Cedric Roux committed
36
  case EVENT_ULONG: value = e.e[l->data_arg].ul; break;
Cedric Roux's avatar
Cedric Roux committed
37
38
39
  default: printf("%s:%d: unsupported type\n", __FILE__, __LINE__); abort();
  }

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

42
43
  for (i = 0; i < l->common.vsize; i++)
    l->common.v[i]->append(l->common.v[i], frame, subframe, value);
Cedric Roux's avatar
Cedric Roux committed
44
45
}

46
logger *new_ttilog(event_handler *h, void *database,
Cedric Roux's avatar
Cedric Roux committed
47
    char *event_name, char *frame_varname, char *subframe_varname,
48
    char *data_varname, int convert_to_dB)
Cedric Roux's avatar
Cedric Roux committed
49
50
51
52
53
54
55
56
{
  struct ttilog *ret;
  int event_id;
  database_event_format f;
  int i;

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

57
58
  ret->common.event_name = strdup(event_name);
  if (ret->common.event_name == NULL) abort();
Cedric Roux's avatar
Cedric Roux committed
59
  ret->database = database;
60
  ret->convert_to_dB = convert_to_dB;
Cedric Roux's avatar
Cedric Roux committed
61
62
63

  event_id = event_id_from_name(database, event_name);

64
  ret->common.handler_id = register_handler_function(h,event_id,_event,ret);
Cedric Roux's avatar
Cedric Roux committed
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

  f = get_format(database, event_id);

  /* look for frame, subframe and data args */
  ret->frame_arg = -1;
  ret->subframe_arg = -1;
  ret->data_arg = -1;
  for (i = 0; i < f.count; i++) {
    if (!strcmp(f.name[i], frame_varname)) ret->frame_arg = i;
    if (!strcmp(f.name[i], subframe_varname)) ret->subframe_arg = i;
    if (!strcmp(f.name[i], data_varname)) ret->data_arg = i;
  }
  if (ret->frame_arg == -1) {
    printf("%s:%d: frame argument '%s' not found in event '%s'\n",
        __FILE__, __LINE__, frame_varname, event_name);
    abort();
  }
  if (ret->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->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->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;
}