📄 serverlogger.java
字号:
/*
* Created on May 28, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.GTADS.debug;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import org.GTADS.server.*;
import org.GTADS.helper.*;
/**
* @author Steven Day
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public final class ServerLogger extends Thread {
public static ServerLogger instance;
private Vector logLineQueue = new Vector();
public static ServerLogger getInstance(){
if (instance == null){
synchronized(ServerLogger.class){
instance = new ServerLogger();
}
}
return instance;
}
public static void clearInstance(){
if (instance != null){
instance = null;
}
}
public static boolean hasInstance(){
return instance != null;
}
public ServerLogger (){
ServerLogger.sendConsoleOutput("Starting up Logging to " + ServerConfig.getInstance().getLogFileName(), this.getClass());
this.setName("File Logging Thread");
this.setPriority(10);
start();
}
public void run(){
try {
while (true) {
Thread.sleep(2000);
synchronized(ServerLogger.class){
writeLinesToFile();
}
}
} catch (InterruptedException ie){
} catch (EOFException eof) {
instance = null;
ServerLogger.sendConsoleDebugMessage("Error writing to file " + ServerConfig.getInstance().getLogFileName(), ServerLogger.class, 5);
} catch (IOException ioe) {
ServerLogger.sendConsoleDebugMessage("Error writing to file " + ServerConfig.getInstance().getLogFileName(), ServerLogger.class, 5);
}
}
private void writeLinesToFile() throws EOFException, IOException{
File logFile = new File(ServerConfig.getInstance().getLogFileName());
BufferedWriter buffer = new BufferedWriter(new FileWriter(logFile, true));
for (int i = 0; i < logLineQueue.size(); i++){
buffer.newLine();
buffer.write((String)logLineQueue.get(i));
}
logLineQueue.clear();
buffer.close();
}
private void addToVectorQueue(String logLine){
//try {
//this.wait();
logLineQueue.add(logLine);
//} //catch (InterruptedException ie){
//}
}
public static void sendConsoleOutput(Object printable, Class printableClass){
if (ServerConfig.getInstance().isConsoleOutAllowed()) {
String logLine = Helper.getDate(true) + " -- " + printableClass.toString() + " -- " + printable;
System.out.println(logLine);
if (ServerLogger.hasInstance()){
ServerLogger.getInstance().addToVectorQueue(logLine);
}
}
/* TODO
* Give the option to write to a log file
* */
}
public static void sendConsoleDebugMessage(String printable, Class printableClass, int debugLevel) {
if (ServerConfig.getInstance().consoleDebugLevel() >= debugLevel){
String logLine = Helper.getDate(true) + " ++ " + printableClass.toString() + " ++ Debug Message ++ " + printable;
System.err.println(logLine);
if (ServerLogger.hasInstance()){
ServerLogger.getInstance().addToVectorQueue(logLine);
}
}
}
public static void sendConsoleDebugMessage(String printable, Class printableClass){
if (ServerConfig.getInstance().consoleDebugLevel() > 0)
sendConsoleDebugMessage(printable, printableClass, 0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -