Migrate some users of FilePrinter to slf4j.Logger

At least we got rid of one printError method
This commit is contained in:
P0nk
2022-01-19 18:36:19 +01:00
parent 0b66766e8a
commit 53465bba25
8 changed files with 47 additions and 80 deletions

View File

@@ -57,6 +57,8 @@ import net.server.services.task.world.CharacterSaveService;
import net.server.services.type.ChannelServices;
import net.server.services.type.WorldServices;
import net.server.world.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.AbstractPlayerInteraction;
import scripting.event.EventInstanceManager;
import scripting.item.ItemScriptManager;
@@ -99,6 +101,7 @@ import java.util.regex.Pattern;
import static java.util.concurrent.TimeUnit.*;
public class Character extends AbstractCharacterObject {
private static final Logger log = LoggerFactory.getLogger(Character.class);
private static final ItemInformationProvider ii = ItemInformationProvider.getInstance();
private static final String LEVEL_200 = "[Congrats] %s has reached Level %d! Congratulate %s on such an amazing achievement!";
private static final String[] BLOCKED_NAMES = {"admin", "owner", "moderator", "intern", "donor", "administrator", "FREDRICK", "help", "helper", "alert", "notice", "maplestory", "fuck", "wizet", "fucking", "negro", "fuk", "fuc", "penis", "pussy", "asshole", "gay",
@@ -10968,8 +10971,7 @@ public class Character extends AbstractCharacterObject {
return "Character is the leader of a guild.";
}
} catch (SQLException e) {
e.printStackTrace();
FilePrinter.printError(FilePrinter.CHANGE_CHARACTER_NAME, e);
log.error("Change character name", e);
return "SQL Error";
}
try (PreparedStatement ps = con.prepareStatement("SELECT tempban FROM accounts WHERE id = ?")) {
@@ -10983,8 +10985,7 @@ public class Character extends AbstractCharacterObject {
return "Account has been banned.";
}
} catch (SQLException e) {
e.printStackTrace();
FilePrinter.printError(FilePrinter.CHANGE_CHARACTER_NAME, e);
log.error("Change character name", e);
return "SQL Error";
}
try (PreparedStatement ps = con.prepareStatement("SELECT COUNT(*) AS rowcount FROM characters WHERE accountid = ? AND world = ?")) {
@@ -10998,8 +10999,7 @@ public class Character extends AbstractCharacterObject {
return "Too many characters on destination world.";
}
} catch (SQLException e) {
e.printStackTrace();
FilePrinter.printError(FilePrinter.CHANGE_CHARACTER_NAME, e);
log.error("Change character name", e);
return "SQL Error";
}
return null;

View File

@@ -381,7 +381,7 @@ public class Client extends ChannelInboundHandlerAdapter {
voteTime = rs.getInt("date");
}
} catch (SQLException e) {
FilePrinter.printError("hasVotedAlready.txt", e);
log.error("Error getting voting time");
return -1;
}
return voteTime;
@@ -938,7 +938,7 @@ public class Client extends ChannelInboundHandlerAdapter {
}
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.ACCOUNT_STUCK, t);
log.error("Account stuck", t);
}
}
@@ -1011,7 +1011,7 @@ public class Client extends ChannelInboundHandlerAdapter {
}
}
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.ACCOUNT_STUCK, e);
log.error("Account stuck", e);
} finally {
if (!this.serverTransition) {
if (chrg != null) {

View File

@@ -23,8 +23,9 @@ package scripting.map;
import client.Character;
import client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.AbstractScriptManager;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptException;
@@ -32,6 +33,7 @@ import java.util.HashMap;
import java.util.Map;
public class MapScriptManager extends AbstractScriptManager {
private static final Logger log = LoggerFactory.getLogger(MapScriptManager.class);
private static final MapScriptManager instance = new MapScriptManager();
private final Map<String, Invocable> scripts = new HashMap<>();
@@ -75,7 +77,7 @@ public class MapScriptManager extends AbstractScriptManager {
iv.invokeFunction("start", new MapScriptMethods(c));
return true;
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.MAP_SCRIPT + mapScriptPath + ".txt", e);
log.error("Error running map script {}", mapScriptPath, e);
}
return false;

View File

@@ -24,9 +24,10 @@ package scripting.npc;
import client.Character;
import client.Client;
import net.server.world.PartyCharacter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.AbstractScriptManager;
import server.ItemInformationProvider.ScriptedItem;
import tools.FilePrinter;
import tools.PacketCreator;
import javax.script.Invocable;
@@ -40,6 +41,7 @@ import java.util.Map;
* @author Matze
*/
public class NPCScriptManager extends AbstractScriptManager {
private static final Logger log = LoggerFactory.getLogger(NPCScriptManager.class);
private static final NPCScriptManager instance = new NPCScriptManager();
private final Map<Client, NPCConversationManager> cms = new HashMap<>();
@@ -104,7 +106,7 @@ public class NPCScriptManager extends AbstractScriptManager {
}
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", e);
log.error("Error starting NPC script: {}", npc, e);
dispose(c);
}
}
@@ -154,7 +156,7 @@ public class NPCScriptManager extends AbstractScriptManager {
}
return true;
} catch (final Exception ute) {
FilePrinter.printError(FilePrinter.NPC + npc + ".txt", ute);
log.error("Error starting NPC script: {}", npc);
dispose(c);
return false;
@@ -169,7 +171,7 @@ public class NPCScriptManager extends AbstractScriptManager {
iv.invokeFunction("action", mode, type, selection);
} catch (ScriptException | NoSuchMethodException t) {
if (getCM(c) != null) {
FilePrinter.printError(FilePrinter.NPC + getCM(c).getNpc() + ".txt", t);
log.error("Error performing NPC script action for npc: {}", getCM(c).getNpc(), t);
}
dispose(c);
}

View File

@@ -24,13 +24,14 @@ package scripting.quest;
import client.Client;
import client.QuestStatus;
import constants.game.GameConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.AbstractScriptManager;
import server.quest.Quest;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.HashMap;
import java.util.Map;
@@ -38,6 +39,7 @@ import java.util.Map;
* @author RMZero213
*/
public class QuestScriptManager extends AbstractScriptManager {
private static final Logger log = LoggerFactory.getLogger(QuestScriptManager.class);
private static final QuestScriptManager instance = new QuestScriptManager();
private final Map<Client, QuestActionManager> qms = new HashMap<>();
@@ -85,11 +87,8 @@ public class QuestScriptManager extends AbstractScriptManager {
c.setClickedNPC();
iv.invokeFunction("start", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
log.error("Error starting quest script: {}", questid, t);
dispose(c);
}
}
@@ -101,7 +100,7 @@ public class QuestScriptManager extends AbstractScriptManager {
c.setClickedNPC();
iv.invokeFunction("start", mode, type, selection);
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", e);
log.error("Error starting quest script: {}", getQM(c).getQuest(), e);
dispose(c);
}
}
@@ -140,11 +139,8 @@ public class QuestScriptManager extends AbstractScriptManager {
c.setClickedNPC();
iv.invokeFunction("end", (byte) 1, (byte) 0, 0);
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
log.error("Error starting quest script: {}", questid, t);
dispose(c);
}
}
@@ -156,7 +152,7 @@ public class QuestScriptManager extends AbstractScriptManager {
c.setClickedNPC();
iv.invokeFunction("end", mode, type, selection);
} catch (final Exception e) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", e);
log.error("Error ending quest script: {}", getQM(c).getQuest(), e);
dispose(c);
}
}
@@ -185,11 +181,8 @@ public class QuestScriptManager extends AbstractScriptManager {
c.setClickedNPC();
iv.invokeFunction("raiseOpen");
}
} catch (final UndeclaredThrowableException ute) {
FilePrinter.printError(FilePrinter.QUEST + questid + ".txt", ute);
dispose(c);
} catch (final Throwable t) {
FilePrinter.printError(FilePrinter.QUEST + getQM(c).getQuest() + ".txt", t);
log.error("Error during quest script raiseOpen for quest: {}", questid, t);
dispose(c);
}
}

View File

@@ -22,11 +22,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package scripting.reactor;
import client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scripting.AbstractScriptManager;
import server.maps.Reactor;
import server.maps.ReactorDropEntry;
import tools.DatabaseConnection;
import tools.FilePrinter;
import javax.script.Invocable;
import javax.script.ScriptEngine;
@@ -43,6 +44,7 @@ import java.util.Map;
* @author Lerk
*/
public class ReactorScriptManager extends AbstractScriptManager {
private static final Logger log = LoggerFactory.getLogger(ReactorScriptManager.class);
private static final ReactorScriptManager instance = new ReactorScriptManager();
private final Map<Integer, List<ReactorDropEntry>> drops = new HashMap<>();
@@ -60,10 +62,9 @@ public class ReactorScriptManager extends AbstractScriptManager {
iv.invokeFunction("hit");
} catch (final NoSuchMethodException e) {
} //do nothing, hit is OPTIONAL
catch (final ScriptException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
//do nothing, hit is OPTIONAL
} catch (final ScriptException | NullPointerException e) {
log.error("Error during onHit script for reactor: {}", reactor.getId(), e);
}
}
@@ -76,17 +77,17 @@ public class ReactorScriptManager extends AbstractScriptManager {
iv.invokeFunction("act");
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", e);
log.error("Error during act script for reactor: {}", reactor.getId(), e);
}
}
public List<ReactorDropEntry> getDrops(int rid) {
List<ReactorDropEntry> ret = drops.get(rid);
public List<ReactorDropEntry> getDrops(int reactorId) {
List<ReactorDropEntry> ret = drops.get(reactorId);
if (ret == null) {
ret = new LinkedList<>();
try (Connection con = DatabaseConnection.getConnection()) {
try (PreparedStatement ps = con.prepareStatement("SELECT itemid, chance, questid FROM reactordrops WHERE reactorid = ? AND chance >= 0")) {
ps.setInt(1, rid);
ps.setInt(1, reactorId);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
ret.add(new ReactorDropEntry(rs.getInt("itemid"), rs.getInt("chance"), rs.getInt("questid")));
@@ -94,9 +95,9 @@ public class ReactorScriptManager extends AbstractScriptManager {
}
}
} catch (Throwable e) {
FilePrinter.printError(FilePrinter.REACTOR + rid + ".txt", e);
log.error("Error getting drops for reactor: {}", reactorId);
}
drops.put(rid, ret);
drops.put(reactorId, ret);
}
return ret;
}
@@ -114,19 +115,16 @@ public class ReactorScriptManager extends AbstractScriptManager {
}
private void touching(Client c, Reactor reactor, boolean touching) {
final String functionName = touching ? "touch" : "untouch";
try {
Invocable iv = initializeInvocable(c, reactor);
if (iv == null) {
return;
}
if (touching) {
iv.invokeFunction("touch");
} else {
iv.invokeFunction("untouch");
}
} catch (final ScriptException | NoSuchMethodException | NullPointerException ute) {
FilePrinter.printError(FilePrinter.REACTOR + reactor.getId() + ".txt", ute);
iv.invokeFunction(functionName);
} catch (final ScriptException | NoSuchMethodException | NullPointerException e) {
log.error("Error during {} script for reactor: {}", functionName, reactor.getId(), e);
}
}

View File

@@ -22,7 +22,8 @@
package server;
import net.server.Server;
import tools.FilePrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
@@ -36,6 +37,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
public class TimerManager implements TimerManagerMBean {
private static final Logger log = LoggerFactory.getLogger(TimerManager.class);
private static final TimerManager instance = new TimerManager();
public static TimerManager getInstance() {
@@ -147,7 +149,7 @@ public class TimerManager implements TimerManagerMBean {
try {
r.run();
} catch (Throwable t) {
FilePrinter.printError(FilePrinter.EXCEPTION_CAUGHT, t);
log.error("Error in scheduled task", t);
}
}
}

View File

@@ -71,36 +71,6 @@ public class FilePrinter {
private static final String FILE_PATH = "logs/" + sdf.format(Calendar.getInstance().getTime()) + "/"; // + sdf.format(Calendar.getInstance().getTime()) + "/"
private static final String ERROR = "error/";
public static void printError(final String name, final Throwable t) {
String stringT = getString(t);
System.out.println("Error thrown: " + name);
System.out.println(stringT);
System.out.println();
FileOutputStream out = null;
final String file = FILE_PATH + ERROR + name;
try {
File outputFile = new File(file);
if (outputFile.getParentFile() != null) {
outputFile.getParentFile().mkdirs();
}
out = new FileOutputStream(file, true);
out.write(stringT.getBytes());
out.write("\r\n---------------------------------\r\n".getBytes());
out.write("\r\n".getBytes()); // thanks Vcoc for suggesting review body log structure
} catch (IOException ess) {
ess.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ignore) {
ignore.printStackTrace();
}
}
}
public static void printError(final String name, final Throwable t, final String info) {
String stringT = getString(t);