Crossfire JXClient, Trunk  R20561
Logger.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.window;
23 
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.OutputStreamWriter;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
42 public class Logger {
43 
47  @NotNull
48  private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
49 
53  @Nullable
54  private String hostname;
55 
59  private final boolean enabled;
60 
64  @NotNull
65  @SuppressWarnings("FieldCanBeLocal")
66  private final CrossfireQueryListener crossfireQueryListener = (prompt, queryType) -> log(prompt);
67 
72  @NotNull
73  @SuppressWarnings("FieldCanBeLocal")
75 
76  @Override
77  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
78  log(message);
79  }
80 
81  @Override
82  public void setDebugMode(final boolean printMessageTypes) {
83  // ignore
84  }
85 
86  };
87 
92  @NotNull
93  @SuppressWarnings("FieldCanBeLocal")
94  private final CrossfireDrawinfoListener crossfireDrawinfoListener = (text, type) -> log(text);
95 
102  public Logger(@NotNull final CrossfireServerConnection crossfireServerConnection, @Nullable final String hostname, final boolean enabled) {
103  this.hostname = hostname;
104  this.enabled = enabled;
105  crossfireServerConnection.addCrossfireQueryListener(crossfireQueryListener);
106  crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
107  crossfireServerConnection.addCrossfireDrawinfoListener(crossfireDrawinfoListener);
108  }
109 
114  public void setHostname(@Nullable final String hostname) {
115  this.hostname = hostname;
116  }
117 
122  private void log(@NotNull final String message) {
123  if (!enabled) {
124  return;
125  }
126 
127  final Date now = new Date();
128  try {
129  final File file = Filenames.getMessageLogFile(hostname);
130  try (final FileOutputStream fos = new FileOutputStream(file, true)) {
131  try (final OutputStreamWriter osw = new OutputStreamWriter(fos)) {
132  osw.write(format.format(now)+message+"\n");
133  }
134  }
135  } catch (final IOException ex) {
136  System.err.println(ex.getMessage());
137  }
138  }
139 
140 }
Utility class to return references to settings files.
Definition: Filenames.java:34
Interface for listeners interested in drawinfo messages received from the Crossfire server...
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
The CrossfireDrawextinfoListener registered to receive drawextinfo commands.
Definition: Logger.java:74
final CrossfireQueryListener crossfireQueryListener
The CrossfireQueryListener registered to receive query commands.
Definition: Logger.java:66
final CrossfireDrawinfoListener crossfireDrawinfoListener
The CrossfireDrawinfoListener registered to receive drawinfo commands.
Definition: Logger.java:94
void setHostname(@Nullable final String hostname)
Updates the hostname.
Definition: Logger.java:114
final boolean enabled
Whether the message logger is enabled.
Definition: Logger.java:59
static File getMessageLogFile(@Nullable final String hostname)
Returns the log file for text message logging.
Definition: Filenames.java:210
Interface for listeners interested in drawextinfo messages received from the Crossfire server...
final SimpleDateFormat format
The format for writing timestamps.
Definition: Logger.java:48
Logs received messages to a file.
Definition: Logger.java:42
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
Interface for listeners interested in query messages received from the Crossfire server.
void log(@NotNull final String message)
Adds a message to the log file.
Definition: Logger.java:122