textlog.c 5.73 KB
Newer Older
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
4
#include <pthread.h>
5
#include <signal.h>
6
#include "database.h"
7 8
#include "event.h"
#include "handler.h"
9
#include "logger/logger.h"
10
#include "view/view.h"
11
#include "gui/gui.h"
12
#include "utils.h"
13
#include "event_selector.h"
14
#include "config.h"
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
typedef struct {
  int socket;
  int *is_on;
  int nevents;
  pthread_mutex_t lock;
} textlog_data;

void is_on_changed(void *_d)
{
  textlog_data *d = _d;
  char t;

  if (pthread_mutex_lock(&d->lock)) abort();

  if (d->socket == -1) goto no_connection;

  t = 1;
33 34 35 36
  if (socket_send(d->socket, &t, 1) == -1 ||
      socket_send(d->socket, &d->nevents, sizeof(int)) == -1 ||
      socket_send(d->socket, d->is_on, d->nevents * sizeof(int)) == -1)
    abort();
37 38 39 40 41

no_connection:
  if (pthread_mutex_unlock(&d->lock)) abort();
}

42
void usage(void)
43
{
44 45 46 47 48 49 50 51 52 53
  printf(
"options:\n"
"    -d <database file>        this option is mandatory\n"
"    -on <GROUP or ID>         turn log ON for given GROUP or ID\n"
"    -off <GROUP or ID>        turn log OFF for given GROUP or ID\n"
"    -ON                       turn all logs ON\n"
"    -OFF                      turn all logs OFF\n"
"                              note: you may pass several -on/-off/-ON/-OFF,\n"
"                                    they will be processed in order\n"
"                                    by default, all is off\n"
54
"    -full                     also dump buffers' content\n"
55
"    -raw-time                 also prints 'raw time'\n"
56 57
"    -ip <host>                connect to given IP address (default %s)\n"
"    -p <port>                 connect to given port (default %d)\n"
58 59 60
"    -x                        GUI output\n"
"    -debug-gui                activate GUI debug logs\n"
"    -no-gui                   disable GUI entirely\n",
61
  DEFAULT_REMOTE_IP,
62 63 64
  DEFAULT_REMOTE_PORT
  );
  exit(1);
65 66
}

67
static void *gui_thread(void *_g)
68
{
69 70 71 72 73 74 75 76 77 78
  gui *g = _g;
  gui_loop(g);
  return NULL;
}

int main(int n, char **v)
{
  extern int volatile gui_logd;
  char *database_filename = NULL;
  void *database;
79
  char *ip = DEFAULT_REMOTE_IP;
80 81 82 83 84 85 86 87
  int port = DEFAULT_REMOTE_PORT;
  char **on_off_name;
  int *on_off_action;
  int on_off_n = 0;
  int *is_on;
  int number_of_events;
  int i;
  event_handler *h;
88
  logger *textlog;
89 90 91 92
  gui *g;
  int gui_mode = 0;
  view *out;
  int gui_active = 1;
93
  textlog_data textlog_data;
94
  int full = 0;
95
  int raw_time = 0;
96

97 98 99
  /* write on a socket fails if the other end is closed and we get SIGPIPE */
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) abort();

100 101 102 103 104 105 106
  on_off_name = malloc(n * sizeof(char *)); if (on_off_name == NULL) abort();
  on_off_action = malloc(n * sizeof(int)); if (on_off_action == NULL) abort();

  for (i = 1; i < n; i++) {
    if (!strcmp(v[i], "-h") || !strcmp(v[i], "--help")) usage();
    if (!strcmp(v[i], "-d"))
      { if (i > n-2) usage(); database_filename = v[++i]; continue; }
107
    if (!strcmp(v[i], "-ip")) { if (i > n-2) usage(); ip = v[++i]; continue; }
108 109 110 111 112 113 114 115 116 117 118 119 120
    if (!strcmp(v[i], "-p"))
      { if (i > n-2) usage(); port = atoi(v[++i]); continue; }
    if (!strcmp(v[i], "-on")) { if (i > n-2) usage();
      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=1; continue; }
    if (!strcmp(v[i], "-off")) { if (i > n-2) usage();
      on_off_name[on_off_n]=v[++i]; on_off_action[on_off_n++]=0; continue; }
    if (!strcmp(v[i], "-ON"))
      { on_off_name[on_off_n]=NULL; on_off_action[on_off_n++]=1; continue; }
    if (!strcmp(v[i], "-OFF"))
      { on_off_name[on_off_n]=NULL; on_off_action[on_off_n++]=0; continue; }
    if (!strcmp(v[i], "-x")) { gui_mode = 1; continue; }
    if (!strcmp(v[i], "-debug-gui")) { gui_logd = 1; continue; }
    if (!strcmp(v[i], "-no-gui")) { gui_active = 0; continue; }
121
    if (!strcmp(v[i], "-full")) { full = 1; continue; }
122
    if (!strcmp(v[i], "-raw-time")) { raw_time = 1; continue; }
123
    usage();
124 125
  }

126
  if (gui_active == 0) gui_mode = 0;
127

128 129 130 131
  if (database_filename == NULL) {
    printf("ERROR: provide a database file (-d)\n");
    exit(1);
  }
132

133 134
  database = parse_database(database_filename);

135
  load_config_file(database_filename);
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
  number_of_events = number_of_ids(database);
  is_on = calloc(number_of_events, sizeof(int));
  if (is_on == NULL) abort();

  h = new_handler(database);

  if (gui_active) {
    g = gui_init();
    new_thread(gui_thread, g);
  }

  if (gui_mode) {
    widget *w, *win;
//    w = new_textlist(g, 600, 20, 0);
    w = new_textlist(g, 800, 50, BACKGROUND_COLOR);
    win = new_toplevel_window(g, 800, 50*12, "textlog");
    widget_add_child(g, win, w, -1);
    out = new_view_textlist(1000, 10, g, w);
    //tout = new_view_textlist(7, 4, g, w);
  } else {
    out = new_view_stdout();
  }

  for (i = 0; i < number_of_events; i++) {
    char *name, *desc;
    database_get_generic_description(database, i, &name, &desc);
    textlog = new_textlog(h, database, name, desc);
164
//        "ENB_PHY_UL_CHANNEL_ESTIMATE",
165
//        "ev: {} eNB_id [eNB_ID] frame [frame] subframe [subframe]");
166
    logger_add_view(textlog, out);
167
    if (full) textlog_dump_buffer(textlog, 1);
168
    if (raw_time) textlog_raw_time(textlog, 1);
169 170 171 172 173 174 175
    free(name);
    free(desc);
  }

  for (i = 0; i < on_off_n; i++)
    on_off(database, on_off_name[i], is_on, on_off_action[i]);

176 177 178 179 180 181
  textlog_data.socket = -1;
  textlog_data.is_on = is_on;
  textlog_data.nevents = number_of_events;
  if (pthread_mutex_init(&textlog_data.lock, NULL)) abort();
  if (gui_active)
    setup_event_selector(g, database, is_on, is_on_changed, &textlog_data);
182

183
  textlog_data.socket = connect_to(ip, port);
184

185 186
  /* send the first message - activate selected traces */
  is_on_changed(&textlog_data);
187

188 189
  OBUF ebuf = { osize: 0, omaxsize: 0, obuf: NULL };

190 191 192
  /* read messages */
  while (1) {
    event e;
193
    e = get_event(textlog_data.socket, &ebuf, database);
194
    if (e.type == -1) break;
195 196 197 198
    handle_event(h, e);
  }

  return 0;
199
}