static void destroy_output_plugin(hs_output_plugin *p) { if (!p) return; hs_free_input(&p->analysis); hs_free_input(&p->input); char *msg = lsb_heka_destroy_sandbox(p->hsb); if (msg) { hs_log(NULL, p->name, 3, "lsb_heka_destroy_sandbox failed: %s", msg); free(msg); } lsb_destroy_message_matcher(p->mm); free(p->name); free(p->async_cp); pthread_mutex_destroy(&p->cp_lock); free(p); }
int main(int argc, char **argv) { bool argerr = false; bool follow = false; bool use_stdin = false; long num = -1; char *matcher = "TRUE"; char *filename = NULL; output_function ofn = output_text; int c; while ((c = getopt(argc, argv, "tchfn:m:")) != -1) { switch (c) { case 't': ofn = output_text; break; case 'c': ofn = NULL; break; case 'h': ofn = output_heka; break; case 'f': follow = true; break; case 'n': num = strtol(optarg, NULL, 10); if (num < 0) argerr = true; break; case 'm': matcher = optarg; break; default: argerr = true; break; } } if (argc - optind == 1) { filename = argv[optind]; use_stdin = strcmp("-", filename) == 0; } else { argerr = true; } if (argerr) { log_cb(NULL, NULL, 0, "usage: %s [-t|-c|-h] [-m message_matcher] [-f] [-n #] <FILE>\n" "description:\n" " -t output the messages in text format (default)\n" " -c only output the message count\n" " -h output the messages as a Heka protobuf stream\n" " -f output appended data as the file grows\n" " -n output the last # of messages (simple header check so not " "100%% accurate)\n" " -m message_matcher expression (default \"TRUE\")\n" " FILE name of the file to cat or '-' for stdin\n" "notes:\n" " All output is written to stdout and all log/error messages are " "written to stderr.\n", argv[0]); return EXIT_FAILURE; } lsb_message_matcher *mm = lsb_create_message_matcher(matcher); if (!mm) { log_cb(NULL, NULL, 0, "invalid message matcher: %s", matcher); return EXIT_FAILURE; } FILE *fh = stdin; if (!use_stdin) { fh = fopen(filename, "r"); if (!fh) { log_cb(NULL, NULL, 0, "error opening: %s", filename); return EXIT_FAILURE; } if (num >= 0) { move_to_offset(fh, num); } } size_t discarded_bytes; size_t bytes_read = 0; size_t pcnt = 0; size_t mcnt = 0; lsb_input_buffer ib; lsb_init_input_buffer(&ib, 1024 * 1024 * 1024); lsb_heka_message msg; lsb_init_heka_message(&msg, 8); do { if (lsb_find_heka_message(&msg, &ib, true, &discarded_bytes, &logger)) { if (lsb_eval_message_matcher(mm, &msg)) { if (ofn) { ofn(&msg); } ++mcnt; } ++pcnt; } else { bytes_read = read_file(fh, &ib); } if (bytes_read == 0 && follow && !use_stdin) { sleep(1); } } while (bytes_read > 0 || follow); lsb_free_heka_message(&msg); lsb_free_input_buffer(&ib); lsb_destroy_message_matcher(mm); if (!use_stdin) { fclose(fh); } if (ofn) { log_cb(NULL, NULL, 0, "Processed: %zu, matched: %zu messages\n", pcnt, mcnt); } else { printf("Processed: %zu, matched: %zu messages\n", pcnt, mcnt); } }