Crossfire JXClient, Trunk  R20561
DebugWriter.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.util;
23 
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import org.jetbrains.annotations.NotNull;
30 
35 public class DebugWriter {
36 
40  @NotNull
41  private final Writer writer;
42 
46  @NotNull
47  private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS ");
48 
52  @NotNull
53  private final Object sync = new Object();
54 
59  public DebugWriter(@NotNull final Writer writer) {
60  this.writer = writer;
61  }
62 
67  public void debugProtocolWrite(@NotNull final CharSequence str) {
68  synchronized (sync) {
69  try {
70  writer.append(simpleDateFormat.format(new Date()));
71  writer.append(str);
72  writer.append("\n");
73  writer.flush();
74  } catch (final IOException ex) {
75  System.err.println("Cannot write debug protocol: "+ex.getMessage());
76  System.exit(1);
77  throw new AssertionError(ex);
78  }
79  }
80  }
81 
87  public void debugProtocolWrite(@NotNull final CharSequence str, @NotNull final Throwable throwable) {
88  synchronized (sync) {
89  try {
90  writer.append(simpleDateFormat.format(new Date()));
91  writer.append(str);
92  writer.append("\n");
93  for (Throwable t = throwable; t != null; t = t.getCause()) {
94  writer.append(t.getClass().getName());
95  writer.append("\n");
96  for (final Object stack : t.getStackTrace()) {
97  writer.append(stack.toString());
98  writer.append("\n");
99  }
100  }
101  writer.flush();
102  } catch (final IOException ex) {
103  System.err.println("Cannot write debug protocol: "+ex.getMessage());
104  System.exit(1);
105  throw new AssertionError(ex);
106  }
107  }
108  }
109 
110 }
void debugProtocolWrite(@NotNull final CharSequence str)
Writes a message to the debug protocol.
Writer debug information to a log file.
final DateFormat simpleDateFormat
A formatter for timestamps.
final Object sync
The object for synchronizing messages.
DebugWriter(@NotNull final Writer writer)
Creates a new instance.
final Writer writer
The Writer to write to.
void debugProtocolWrite(@NotNull final CharSequence str, @NotNull final Throwable throwable)
Writes a message to the debug protocol including a throwable.