LogFormatter.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.zookeeper.server;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.EOFException;
  19. import java.io.FileInputStream;
  20. import java.text.DateFormat;
  21. import java.util.Date;
  22. import org.apache.log4j.Logger;
  23. import org.apache.jute.BinaryInputArchive;
  24. import org.apache.jute.InputArchive;
  25. import org.apache.zookeeper.txn.TxnHeader;
  26. public class LogFormatter {
  27. private static final Logger LOG = Logger.getLogger(LogFormatter.class);
  28. /**
  29. * @param args
  30. */
  31. public static void main(String[] args) throws Exception {
  32. if (args.length != 1) {
  33. System.err.println("USAGE: LogFormatter log_file");
  34. System.exit(2);
  35. }
  36. FileInputStream fis = new FileInputStream(args[0]);
  37. BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis);
  38. while (true) {
  39. byte[] bytes = logStream.readBuffer("txnEntry");
  40. if (bytes.length == 0) {
  41. // Since we preallocate, we define EOF to be an
  42. // empty transaction
  43. throw new EOFException();
  44. }
  45. InputArchive ia = BinaryInputArchive
  46. .getArchive(new ByteArrayInputStream(bytes));
  47. TxnHeader hdr = new TxnHeader();
  48. hdr.deserialize(ia, "hdr");
  49. System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT,
  50. DateFormat.LONG).format(new Date(hdr.getTime()))
  51. + " "
  52. + Long.toHexString(hdr.getClientId())
  53. + ":"
  54. + hdr.getCxid()
  55. + " "
  56. + Long.toHexString(hdr.getZxid())
  57. + " " + TraceFormatter.op2String(hdr.getType()));
  58. if (logStream.readByte("EOR") != 'B') {
  59. LOG.error("Last transaction was partial.");
  60. throw new EOFException();
  61. }
  62. }
  63. }
  64. }