Crossfire JXClient, Trunk
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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.window;
24 
28 import java.io.BufferedWriter;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.StandardOpenOption;
33 import java.time.LocalDateTime;
34 import java.time.format.DateTimeFormatter;
35 import java.util.Locale;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
43 public class Logger {
44 
48  @NotNull
49  private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss ", Locale.ENGLISH);
50 
54  @Nullable
55  private String hostname;
56 
60  private final boolean enabled;
61 
66  @NotNull
67  @SuppressWarnings("FieldCanBeLocal")
69 
70  @Override
71  public void commandDrawextinfoReceived(final int color, final int type, final int subtype, @NotNull final String message) {
72  log(message);
73  }
74 
75  @Override
76  public void setDebugMode(final boolean printMessageTypes) {
77  // ignore
78  }
79 
80  };
81 
88  public Logger(@NotNull final CrossfireServerConnection crossfireServerConnection, @Nullable final String hostname, final boolean enabled) {
89  this.hostname = hostname;
90  this.enabled = enabled;
91  crossfireServerConnection.addCrossfireQueryListener((prompt, queryType) -> log(prompt));
92  crossfireServerConnection.addCrossfireDrawextinfoListener(crossfireDrawextinfoListener);
93  crossfireServerConnection.addCrossfireDrawinfoListener((text, type) -> log(text));
94  }
95 
100  public void setHostname(@Nullable final String hostname) {
101  this.hostname = hostname;
102  }
103 
108  private void log(@NotNull final String message) {
109  if (!enabled) {
110  return;
111  }
112 
113  final LocalDateTime now = LocalDateTime.now();
114  try {
115  final Path file = Filenames.getMessageLogFile(hostname);
116  try (BufferedWriter osw = Files.newBufferedWriter(file, StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) {
117  osw.write(FORMATTER.format(now)+message+"\n");
118  }
119  } catch (final IOException ex) {
120  System.err.println(ex.getMessage());
121  }
122  }
123 
124 }
com.realtime.crossfire.jxclient.window.Logger.hostname
String hostname
Definition: Logger.java:55
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.window.Logger.Logger
Logger(@NotNull final CrossfireServerConnection crossfireServerConnection, @Nullable final String hostname, final boolean enabled)
Definition: Logger.java:88
com.realtime.crossfire.jxclient.window.Logger
Definition: Logger.java:43
com.realtime.crossfire.jxclient.settings.Filenames.getMessageLogFile
static Path getMessageLogFile(@Nullable final String hostname)
Definition: Filenames.java:223
com.realtime.crossfire.jxclient.server.crossfire.CrossfireDrawextinfoListener
Definition: CrossfireDrawextinfoListener.java:33
com.realtime.crossfire.jxclient.settings
Definition: CommandHistory.java:23
com.realtime.crossfire.jxclient.window.Logger.crossfireDrawextinfoListener
final CrossfireDrawextinfoListener crossfireDrawextinfoListener
Definition: Logger.java:68
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.window.Logger.log
void log(@NotNull final String message)
Definition: Logger.java:108
com.realtime.crossfire.jxclient.window.Logger.FORMATTER
static final DateTimeFormatter FORMATTER
Definition: Logger.java:49
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.window.Logger.enabled
final boolean enabled
Definition: Logger.java:60
com
com.realtime.crossfire.jxclient.window.Logger.setHostname
void setHostname(@Nullable final String hostname)
Definition: Logger.java:100
com.realtime.crossfire.jxclient.settings.Filenames
Definition: Filenames.java:37