Ejemplo n.º 1
0
static int
pylog_initobj(PyObject *s, PyObject *args, PyObject *kwds)
{
    PyLogObject *self = (PyLogObject *)s;
    static char *keywords[] = { "filename", "mode", 0 };
    char *filename = NULL;
    char *mode = "r";

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|s", keywords, &filename,
                &mode))
        return -1;

    if (!strcmp (mode, "r")) {
        self->mode = 'r';
    } else if (!strcmp (mode, "w")) {
        self->mode = 'w';
    } else {
        PyErr_SetString (PyExc_ValueError, "invalid mode");
        return -1;
    }

    if (self->eventlog) { lcm_eventlog_destroy (self->eventlog); }

    self->eventlog = lcm_eventlog_create (filename, mode);
    if (!self->eventlog) {
        PyErr_SetFromErrno (PyExc_IOError);
        return -1;
    }

    return 0;
}
int main(int argc, char **argv)
{
    if(argc < 2) {
        fprintf(stderr, "usage: example1-poses <logfile>\n");
        return 1;
    }

    lcm_eventlog_t *log = lcm_eventlog_create(argv[1], "r");
    if(!log) {
        fprintf(stderr, "error opening log file\n");
        return 1;
    }

    printf("# utime x y z x' y' z'\n");
    lcm_eventlog_event_t *event = lcm_eventlog_read_next_event(log);
    while(event) {
        if(!strcmp(event->channel, "POSE")) {
            lcmtypes_pose_t pose;
            lcmtypes_pose_t_decode(event->data, 0, event->datalen, &pose);

            printf("%lld %f %f %f %f %f %f\n", 
                    (long long)pose.utime, 
                    pose.pos[0], 
                    pose.pos[1], 
                    pose.pos[2],
                    pose.vel[0],
                    pose.vel[1],
                    pose.vel[2]);

            lcmtypes_pose_t_decode_cleanup(&pose);
        }

        lcm_eventlog_free_event(event);
        event = lcm_eventlog_read_next_event(log);
    }

    lcm_eventlog_destroy(log);

    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    if(argc < 2) {
        fprintf(stderr, "usage: example3-gps <logfile>\n");
        return 1;
    }

    lcm_eventlog_t *log = lcm_eventlog_create(argv[1], "r");
    if(!log) {
        fprintf(stderr, "error opening log file\n");
        return 1;
    }

    lcm_eventlog_event_t *event = lcm_eventlog_read_next_event(log);
    while(event) {
        if(!strcmp(event->channel, "GPS_TO_LOCAL")) {
            lcmtypes_gps_to_local_t gps;
            lcmtypes_gps_to_local_t_decode(event->data, 0, event->datalen, 
                    &gps);

            printf("%lld %.10f %.10f %f %f\n", 
                    (long long)gps.utime, 
                    gps.lat_lon_el_theta[0], 
                    gps.lat_lon_el_theta[1], 
                    gps.lat_lon_el_theta[2],
                    gps.lat_lon_el_theta[3]); 

            lcmtypes_gps_to_local_t_decode_cleanup(&gps);
        }

        lcm_eventlog_free_event(event);
        event = lcm_eventlog_read_next_event(log);
    }

    lcm_eventlog_destroy(log);

    return 0;
}
Ejemplo n.º 4
0
LogFile::LogFile(const std::string & path, const std::string & mode) :
  eventlog(lcm_eventlog_create(path.c_str(), mode.c_str())),
  last_event(NULL)
{ 
}
Ejemplo n.º 5
0
int
main(int argc, char **argv)
{
    int verbose = 0;
    int filterChannels =0;
    char *pattern = strdup(".*");
    char *dest_fname = NULL;
    int64_t start_utime = 0;
    int64_t end_utime = -1;
    int have_end_utime = 0;
    int invert_regex = 0;

    char *optstring = "hc:vs:e:i";
    int c;

    while ((c = getopt_long(argc, argv, optstring, NULL, 0)) >= 0) {
        switch (c)
            {
        case 'h':
            usage();
            break;
        case 's':
            {
                char *eptr = NULL;
                double start_time = strtod(optarg, &eptr);
                if (*eptr != 0)
                    usage();
                start_utime = (int64_t) (start_time * 1000000);
            }
            break;
        case 'e':
            {
                char *eptr = NULL;
                double end_time = strtod(optarg, &eptr);
                if (*eptr != 0)
                    usage();
                end_utime = (int64_t) (end_time * 1000000);
                have_end_utime = 1;
            }
            break;
        case 'i':
            invert_regex = 1;
            filterChannels = 1;
            break;
        case 'c':
            free(pattern);
            pattern = strdup(optarg);
            filterChannels = 1;
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage();
            break;
            };
    }

    if (start_utime < 0 || (have_end_utime && end_utime < start_utime))
        usage();

    if (optind > argc - 3)
        usage();

    regex_t preg;
    if (0 != regcomp(&preg, pattern, REG_NOSUB | REG_EXTENDED)) {
        fprintf(stderr, "bad regex\n");
        exit(1);
    }

    int num_src_logs = argc - optind - 1;
    fprintf(stderr, "Splicing together %d logs\n", num_src_logs);
    lcm_eventlog_t *src_logs[num_src_logs];
    for (int i = 0; i < argc - optind - 1; i++) {
        char * src_fname = argv[optind + i];
        src_logs[i] = lcm_eventlog_create(src_fname, "r");
        if (!src_logs[i]) {
            perror("Unable to open source logfile");
            for (int j = 0; j < i; j++)
                lcm_eventlog_destroy(src_logs[j]);
            regfree(&preg);
            return 1;
        }
    }

    dest_fname = argv[argc - 1];

    lcm_eventlog_t *dst_log = lcm_eventlog_create(dest_fname, "w");
    if (!dst_log) {
        perror("Unable to open destination logfile");
        for (int i = 0; i < num_src_logs; i++)
            lcm_eventlog_destroy(src_logs[i]);
        regfree(&preg);
        return 1;
    }

    GHashTable *counts = g_hash_table_new_full(g_str_hash, g_str_equal, free,
            free);
    int nwritten = 0;
    int have_first_event_timestamp = 0;
    int64_t first_event_timestamp = -1;

    lcm_eventlog_event_t *events[num_src_logs];
    for (int i = 0; i < num_src_logs; i++)
        events[i] = lcm_eventlog_read_next_event(src_logs[i]);
    while (1) {
        lcm_eventlog_event_t *event;
        int mind = -1;
        int64_t mtime = INT64_MAX;
        for (int i = 0; i < num_src_logs; i++) {
            if (events[i] == NULL)
                continue;
            else if (events[i]->timestamp < mtime) {
                mtime = events[i]->timestamp;
                mind = i;
            }
        }
        if (mind < 0) //all are null
            break;

        event = events[mind];
        events[mind] = lcm_eventlog_read_next_event(src_logs[mind]);

        if (!have_first_event_timestamp) {
            first_event_timestamp = event->timestamp;
            have_first_event_timestamp = 1;
        }

        int64_t elapsed = event->timestamp - first_event_timestamp;
        if (elapsed < start_utime) {
            lcm_eventlog_free_event(event);
            continue;
        }
        if (have_end_utime && elapsed > end_utime) {
            lcm_eventlog_free_event(event);
            break;
        }

        int copy_to_dest = 1;
        if (filterChannels) {
            int regmatch = regexec(&preg, event->channel, 0, NULL, 0);
            copy_to_dest = (regmatch == 0 && !invert_regex) || (regmatch != 0
                    && invert_regex);
        }
        if (copy_to_dest) {
            lcm_eventlog_write_event(dst_log, event);
            nwritten++;

            if (verbose) {
                int *count = g_hash_table_lookup(counts, event->channel);
                if (!count) {
                    count = (int*) malloc(sizeof(int));
                    *count = 1;
                    g_hash_table_insert(counts, strdup(event->channel), count);
                    printf("matched channel %s\n", event->channel);
                } else {
                    *count += 1;
                }
            }
        }
        lcm_eventlog_free_event(event);
    }

    if (verbose) {
        g_hash_table_foreach(counts, _verbose_entry_summary, NULL);
        printf("=====\n");
        printf("Events written: %d\n", nwritten);
    }

    regfree(&preg);
    for (int i = 0; i < num_src_logs; i++)
        lcm_eventlog_destroy(src_logs[i]);
    lcm_eventlog_destroy(dst_log);
    g_hash_table_destroy(counts);
    return 0;
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
    int verbose = 0;
    char *pattern = g_strdup(".*");
    char *source_fname = NULL;
    char *dest_fname = NULL;
    int64_t start_utime = 0;
    int64_t end_utime = -1;
    int have_end_utime = 0;
    int invert_regex = 0;

    char *optstring = "hc:vs:e:i";
    int c;

    while ((c = getopt_long(argc, argv, optstring, NULL, 0)) >= 0) {
        switch (c) {
        case 'h':
            usage();
            break;
        case 's': {
            char *eptr = NULL;
            double start_time = strtod(optarg, &eptr);
            if (*eptr != 0)
                usage();
            start_utime = (int64_t)(start_time * 1000000);
        } break;
        case 'e': {
            char *eptr = NULL;
            double end_time = strtod(optarg, &eptr);
            if (*eptr != 0)
                usage();
            end_utime = (int64_t)(end_time * 1000000);
            have_end_utime = 1;
        } break;
        case 'i':
            invert_regex = 1;
            break;
        case 'c':
            g_free(pattern);
            pattern = g_strdup(optarg);
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage();
            break;
        };
    }

    if (start_utime < 0 || (have_end_utime && end_utime < start_utime))
        usage();

    if (optind != argc - 2)
        usage();

    GRegex *regex;
    GError *rerr = NULL;
    regex = g_regex_new(pattern, (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, &rerr);
    if (rerr) {
        fprintf(stderr, "bad regex\n");
        exit(1);
    }

    source_fname = argv[argc - 2];
    dest_fname = argv[argc - 1];

    lcm_eventlog_t *src_log = lcm_eventlog_create(source_fname, "r");
    if (!src_log) {
        perror("Unable to open source logfile");
        g_regex_unref(regex);
        return 1;
    }
    lcm_eventlog_t *dst_log = lcm_eventlog_create(dest_fname, "w");
    if (!dst_log) {
        perror("Unable to open destination logfile");
        lcm_eventlog_destroy(src_log);
        g_regex_unref(regex);
        return 1;
    }

    GHashTable *counts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, free);
    int nwritten = 0;
    int have_first_event_timestamp = 0;
    int64_t first_event_timestamp = 0;

    for (lcm_eventlog_event_t *event = lcm_eventlog_read_next_event(src_log); event != NULL;
         event = lcm_eventlog_read_next_event(src_log)) {
        if (!have_first_event_timestamp) {
            first_event_timestamp = event->timestamp;
            have_first_event_timestamp = 1;
        }

        int64_t elapsed = event->timestamp - first_event_timestamp;
        if (elapsed < start_utime) {
            lcm_eventlog_free_event(event);
            continue;
        }
        if (have_end_utime && elapsed > end_utime) {
            lcm_eventlog_free_event(event);
            break;
        }

        int regmatch = g_regex_match(regex, event->channel, (GRegexMatchFlags) 0, NULL);
        if ((!regmatch && invert_regex) || (regmatch && !invert_regex)) {
            lcm_eventlog_write_event(dst_log, event);
            nwritten++;

            if (verbose) {
                int *count = (int *) g_hash_table_lookup(counts, event->channel);
                if (!count) {
                    count = (int *) malloc(sizeof(int));
                    *count = 1;
                    g_hash_table_insert(counts, g_strdup(event->channel), count);
                    printf("matched channel %s\n", event->channel);
                } else {
                    *count += 1;
                }
            }
        }
        lcm_eventlog_free_event(event);
    }

    if (verbose) {
        g_hash_table_foreach(counts, _verbose_entry_summary, NULL);
        printf("=====\n");
        printf("Events written: %d\n", nwritten);
    }

    g_regex_unref(regex);
    lcm_eventlog_destroy(src_log);
    lcm_eventlog_destroy(dst_log);
    g_hash_table_destroy(counts);
    return 0;
}
Ejemplo n.º 7
0
Archivo: lcm_file.c Proyecto: ahans/lcm
static lcm_provider_t *
lcm_logprov_create (lcm_t * parent, const char *target, const GHashTable *args)
{
    if (!target || !strlen (target)) {
        fprintf (stderr, "Error: Missing filename\n");
        return NULL;
    }

    lcm_logprov_t * lr = (lcm_logprov_t *) calloc (1, sizeof (lcm_logprov_t));
    lr->lcm = parent;
    lr->filename = strdup(target);
    lr->speed = 1;
    lr->next_clock_time = -1;
    lr->start_timestamp = -1;

    g_hash_table_foreach ((GHashTable*) args, new_argument, lr);

    dbg (DBG_LCM, "Initializing LCM log provider context...\n");
    dbg (DBG_LCM, "Filename %s\n", lr->filename);

    if(lcm_internal_pipe_create(lr->notify_pipe) != 0) {
        perror(__FILE__ " - pipe (notify)");
        lcm_logprov_destroy (lr);
        return NULL;
    }
    if(lcm_internal_pipe_create(lr->timer_pipe) != 0) {
        perror(__FILE__ " - pipe (timer)");
        lcm_logprov_destroy (lr);
        return NULL;
    }
    //fcntl (lcm->notify_pipe[1], F_SETFL, O_NONBLOCK);

    if (!lr->writer) {
        lr->log = lcm_eventlog_create (lr->filename, "r");
    } else {
        lr->log = lcm_eventlog_create (lr->filename, "w");
    }

    if (!lr->log) {
        fprintf (stderr, "Error: Failed to open %s: %s\n", lr->filename,
                strerror (errno));
        lcm_logprov_destroy (lr);
        return NULL;
    }

    // only start the reader thread if not in write mode
    if (!lr->writer){
        if (load_next_event (lr) < 0) {
            fprintf (stderr, "Error: Failed to read first event from log\n");
            lcm_logprov_destroy (lr);
            return NULL;
        }

        /* Start the reader thread */
        lr->timer_thread = g_thread_create (timer_thread, lr, TRUE, NULL);
        if (!lr->timer_thread) {
            fprintf (stderr, "Error: LCM failed to start timer thread\n");
            lcm_logprov_destroy (lr);
            return NULL;
        }
        lr->thread_created = 1;

        if(lcm_internal_pipe_write(lr->notify_pipe[1], "+", 1) < 0) {
            perror(__FILE__ " - write (reader create)");
        }

        if(lr->start_timestamp > 0){
            dbg (DBG_LCM, "Seeking to timestamp: %lld\n", (long long)lr->start_timestamp);
            lcm_eventlog_seek_to_timestamp(lr->log, lr->start_timestamp);
        }
    }

    return lr;
}
Ejemplo n.º 8
0
static int
open_logfile(logger_t* logger)
{
    // maybe run the filename through strftime
    if (logger->use_strftime) {
        char new_prefix[PATH_MAX];
        time_t now = time (NULL);
        strftime(new_prefix, sizeof(new_prefix),
                logger->input_fname, localtime(&now));

        // If auto-increment is enabled and the strftime-formatted filename
        // prefix has changed, then reset the auto-increment counter.
        if(logger->auto_increment && strcmp(new_prefix, logger->fname_prefix))
            logger->next_increment_num = 0;
        strcpy(logger->fname_prefix, new_prefix);
    } else {
        strcpy(logger->fname_prefix, logger->input_fname);
    }

    if (logger->auto_increment) {
        /* Loop through possible file names until we find one that doesn't
         * already exist.  This way, we never overwrite an existing file. */
        do {
            snprintf(logger->fname, sizeof(logger->fname), "%s.%02d",
                    logger->fname_prefix, logger->next_increment_num);
            logger->next_increment_num++;
        } while(g_file_test(logger->fname, G_FILE_TEST_EXISTS));

        if (errno != ENOENT) {
            perror ("Error: checking for previous logs");
            return 1;
        }
    } else {
        strcpy(logger->fname, logger->fname_prefix);
        if (! logger->force_overwrite) {
            if (g_file_test(logger->fname, G_FILE_TEST_EXISTS))
            {
                fprintf (stderr, "Refusing to overwrite existing file \"%s\"\n",
                        logger->fname);
                return 1;
            }
        }
    }

    // create directories if needed
    char *dirpart = g_path_get_dirname (logger->fname);
    if (! g_file_test (dirpart, G_FILE_TEST_IS_DIR)) {
        mkdir_with_parents (dirpart, 0755);
    }
    g_free (dirpart);

    fprintf (stderr, "Opening log file \"%s\"\n", logger->fname);

    // open output file
    logger->log = lcm_eventlog_create(logger->fname, "w");
    if (logger->log == NULL) {
        perror ("Error: fopen failed");
        return 1;
    }
    return 0;
}