struct ybp_event* ybp_get_event(void) { struct ybp_event* event = malloc(sizeof(struct ybp_event)); Dprintf("Creating event at 0x%p\n", (void*)event); ybp_init_event(event); return event; }
void ybp_reset_event(struct ybp_event* evbuf) { Dprintf("Resetting event\n"); if (evbuf->data != 0) { Dprintf("Freeing data at 0x%p\n", (void*)evbuf->data); free(evbuf->data); evbuf->data = 0; } ybp_init_event(evbuf); }
int main(int argc, char** argv) { int opt; int fd; struct ybp_binlog_parser* bp; struct ybp_event* evbuf; long starting_offset = -1; long starting_time = -1; int num_to_show = 2; int show_all = false; bool q_mode = false; bool esi = true; char* database_limit = NULL; while ((opt = getopt(argc, argv, "ho:t:a:D:qE")) != -1) { switch (opt) { case 'h': usage(); return 0; case 'o': /* Offset mode */ starting_offset = atoll(optarg); break; case 'E': esi = false; break; case 't': /* Time mode */ starting_time = atoll(optarg); break; case 'a': if (strncmp(optarg, "all", 3) == 0) { num_to_show = 2; show_all = 1; break; } num_to_show = atoi(optarg); if (num_to_show < 1) num_to_show = 1; break; case 'D': database_limit = strdup(optarg); break; case 'q': q_mode = true; break; case '?': fprintf(stderr, "Unknown argument %c\n", optopt); usage(); return 1; break; } } if (optind >= argc) { usage(); return 2; } if ((fd = open(argv[optind], O_RDONLY)) <= 0) { perror("Error opening file"); return 1; } if ((bp = ybp_get_binlog_parser(fd)) == NULL) { perror("init_binlog_parser"); return 1; } bp->enforce_server_id = esi; if ((evbuf = malloc(sizeof(struct ybp_event))) == NULL) { perror("malloc event"); return 1; } ybp_init_event(evbuf); if (starting_offset >= 0) { off_t offset = ybp_nearest_offset(bp, starting_offset); if (offset == -2) { fprintf(stderr, "Unable to find anything after offset %ld\n", starting_offset); return 1; } else if (offset == -1) { perror("nearest_offset"); return 1; } else { ybp_rewind_bp(bp, offset); } } if (starting_time >= 0) { off_t offset = ybp_nearest_time(bp, starting_time); if (offset == -2) { fprintf(stderr, "Unable to find anything after time %ld\n", starting_time); return 1; } else if (offset == -1) { perror("nearest_time"); return 1; } else { ybp_rewind_bp(bp, offset); } } int i = 0; while ((ybp_next_event(bp, evbuf) >= 0) && (show_all || i < num_to_show)) { if (q_mode) { if (evbuf->type_code == QUERY_EVENT) { struct ybp_query_event_safe* s = ybp_event_to_safe_qe(evbuf); if ((database_limit == NULL) || (strcmp(s->db_name, database_limit) == 0)) { printf("%d %s\n", evbuf->timestamp, s->statement); } ybp_dispose_safe_qe(s); } else if (evbuf->type_code == XID_EVENT) { struct ybp_xid_event* s = ybp_event_to_safe_xe(evbuf); printf("%d XID %llu\n", evbuf->timestamp, (long long unsigned)s->id); ybp_dispose_safe_xe(s); } } else { ybp_print_event(evbuf, bp, stdout, q_mode, false, database_limit); fprintf(stdout, "\n"); } ybp_reset_event(evbuf); i+=1; } ybp_dispose_event(evbuf); ybp_dispose_binlog_parser(bp); }