Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.util;
24 
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.time.LocalDateTime;
28 import java.time.format.DateTimeFormatter;
29 import java.util.Locale;
30 import org.jetbrains.annotations.NotNull;
31 
36 public class DebugWriter {
37 
41  @NotNull
42  private final Writer writer;
43 
47  @NotNull
48  private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS ", Locale.ENGLISH);
49 
53  @NotNull
54  private final Object sync = new Object();
55 
60  public DebugWriter(@NotNull final Writer writer) {
61  this.writer = writer;
62  }
63 
68  public void debugProtocolWrite(@NotNull final CharSequence str) {
69  synchronized (sync) {
70  try {
71  //noinspection AccessToStaticFieldLockedOnInstance
72  writer.append(FORMATTER.format(LocalDateTime.now()));
73  writer.append(str);
74  writer.append("\n");
75  writer.flush();
76  } catch (final IOException ex) {
77  System.err.println("Cannot write debug protocol: "+ex.getMessage());
78  System.exit(1);
79  throw new AssertionError(ex);
80  }
81  }
82  }
83 
89  public void debugProtocolWrite(@NotNull final CharSequence str, @NotNull final Throwable throwable) {
90  synchronized (sync) {
91  try {
92  //noinspection AccessToStaticFieldLockedOnInstance
93  writer.append(FORMATTER.format(LocalDateTime.now()));
94  writer.append(str);
95  writer.append("\n");
96  for (Throwable t = throwable; t != null; t = t.getCause()) {
97  writer.append(t.getClass().getName());
98  writer.append("\n");
99  for (Object stack : t.getStackTrace()) {
100  writer.append(stack.toString());
101  writer.append("\n");
102  }
103  }
104  writer.flush();
105  } catch (final IOException ex) {
106  System.err.println("Cannot write debug protocol: "+ex.getMessage());
107  System.exit(1);
108  throw new AssertionError(ex);
109  }
110  }
111  }
112 
113 }
com.realtime.crossfire.jxclient.util.DebugWriter.sync
final Object sync
Definition: DebugWriter.java:54
com.realtime.crossfire.jxclient.util.DebugWriter.DebugWriter
DebugWriter(@NotNull final Writer writer)
Definition: DebugWriter.java:60
com.realtime.crossfire.jxclient.util.DebugWriter.writer
final Writer writer
Definition: DebugWriter.java:42
com.realtime.crossfire.jxclient.util.DebugWriter.debugProtocolWrite
void debugProtocolWrite(@NotNull final CharSequence str, @NotNull final Throwable throwable)
Definition: DebugWriter.java:89
com.realtime.crossfire.jxclient.util.DebugWriter.FORMATTER
static final DateTimeFormatter FORMATTER
Definition: DebugWriter.java:48
com.realtime.crossfire.jxclient.util.DebugWriter
Definition: DebugWriter.java:36
com.realtime.crossfire.jxclient.util.DebugWriter.debugProtocolWrite
void debugProtocolWrite(@NotNull final CharSequence str)
Definition: DebugWriter.java:68