00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.util;
00023
00024 import java.io.IOException;
00025 import java.io.Writer;
00026 import java.text.DateFormat;
00027 import java.text.SimpleDateFormat;
00028 import java.util.Date;
00029 import org.jetbrains.annotations.NotNull;
00030
00035 public class DebugWriter {
00036
00040 @NotNull
00041 private final Writer writer;
00042
00046 @NotNull
00047 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS ");
00048
00052 @NotNull
00053 private final Object sync = new Object();
00054
00059 public DebugWriter(@NotNull final Writer writer) {
00060 this.writer = writer;
00061 }
00062
00067 public void debugProtocolWrite(@NotNull final CharSequence str) {
00068 synchronized (sync) {
00069 try {
00070 writer.append(simpleDateFormat.format(new Date()));
00071 writer.append(str);
00072 writer.append("\n");
00073 writer.flush();
00074 } catch (final IOException ex) {
00075 System.err.println("Cannot write debug protocol: "+ex.getMessage());
00076 System.exit(1);
00077 throw new AssertionError();
00078 }
00079 }
00080 }
00081
00087 public void debugProtocolWrite(@NotNull final CharSequence str, @NotNull final Throwable throwable) {
00088 synchronized (sync) {
00089 try {
00090 writer.append(simpleDateFormat.format(new Date()));
00091 writer.append(str);
00092 writer.append("\n");
00093 for (Throwable t = throwable; t != null; t = t.getCause()) {
00094 writer.append(t.getClass().getName());
00095 writer.append("\n");
00096 for (final Object stack : t.getStackTrace()) {
00097 writer.append(stack.toString());
00098 writer.append("\n");
00099 }
00100 }
00101 writer.flush();
00102 } catch (final IOException ex) {
00103 System.err.println("Cannot write debug protocol: "+ex.getMessage());
00104 System.exit(1);
00105 throw new AssertionError();
00106 }
00107 }
00108 }
00109
00110 }