David Reiss | bbdb06e | 2008-04-08 05:07:14 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #include <cstdlib> |
| 8 | #include <iostream> |
| 9 | |
| 10 | #include <transport/TTransportUtils.h> |
| 11 | #include <transport/TFDTransport.h> |
| 12 | #include <protocol/TBinaryProtocol.h> |
| 13 | #include <protocol/TDebugProtocol.h> |
| 14 | #include <protocol/TProtocolTap.h> |
| 15 | |
| 16 | using namespace std; |
| 17 | using boost::shared_ptr; |
| 18 | using namespace facebook::thrift::transport; |
| 19 | using namespace facebook::thrift::protocol; |
| 20 | |
| 21 | void usage() { |
| 22 | fprintf(stderr, |
| 23 | "usage: thrift_dump {-b|-f|-s} < input > ouput\n" |
| 24 | " -b TBufferedTransport messages\n" |
| 25 | " -f TFramedTransport messages\n" |
| 26 | " -s Raw structures\n"); |
| 27 | exit(EXIT_FAILURE); |
| 28 | } |
| 29 | |
| 30 | int main(int argc, char *argv[]) { |
| 31 | if (argc != 2) { |
| 32 | usage(); |
| 33 | } |
| 34 | |
| 35 | shared_ptr<TTransport> stdin_trans(new TFDTransport(STDIN_FILENO)); |
| 36 | shared_ptr<TTransport> itrans; |
| 37 | |
| 38 | if (argv[1] == std::string("-b") || argv[1] == std::string("-s")) { |
| 39 | itrans.reset(new TBufferedTransport(stdin_trans)); |
| 40 | } else if (argv[1] == std::string("-f")) { |
| 41 | itrans.reset(new TFramedTransport(stdin_trans)); |
| 42 | } else { |
| 43 | usage(); |
| 44 | } |
| 45 | |
| 46 | shared_ptr<TProtocol> iprot(new TBinaryProtocol(itrans)); |
| 47 | shared_ptr<TProtocol> oprot( |
| 48 | new TDebugProtocol( |
| 49 | shared_ptr<TTransport>(new TBufferedTransport( |
| 50 | shared_ptr<TTransport>(new TFDTransport(STDOUT_FILENO)))))); |
| 51 | |
| 52 | TProtocolTap tap(iprot, oprot); |
| 53 | |
| 54 | try { |
| 55 | if (argv[1] == std::string("-s")) { |
| 56 | for (;;) { |
| 57 | tap.skip(T_STRUCT); |
| 58 | } |
| 59 | } else { |
| 60 | std::string name; |
| 61 | TMessageType messageType; |
| 62 | int32_t seqid; |
| 63 | for (;;) { |
| 64 | tap.readMessageBegin(name, messageType, seqid); |
| 65 | tap.skip(T_STRUCT); |
| 66 | tap.readMessageEnd(); |
| 67 | } |
| 68 | } |
| 69 | } catch (TProtocolException exn) { |
| 70 | cout << "Protocol Exception: " << exn.what() << endl; |
| 71 | } catch (...) { |
| 72 | } |
| 73 | |
| 74 | cout << endl; |
| 75 | |
| 76 | return 0; |
| 77 | } |