📄 logwriter.java
字号:
/**
* Copyright (C) 2003 Manfred Andres
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Created on 05.10.2003
*/
package freecs.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import freecs.Server;
/**
* @author Manfred Andres
*
* freecs.util
*/
public class LogWriter extends Thread {
public static LogWriter instance;
private FileChannel logFile[];
private ObjectBuffer logQueue;
private boolean stopped = false;
private LogWriter () throws FileNotFoundException, IOException {
logFile = new FileChannel[5];
init();
}
public void init () {
if (logQueue != null && logQueue.capacity() != Server.srv.LOG_QUEUE_SIZE) {
ObjectBuffer tlq = new ObjectBuffer (Server.srv.LOG_QUEUE_SIZE);
synchronized (logQueue) {
while (logQueue.size() > Server.srv.LOG_QUEUE_SIZE) {
LogEntry le = (LogEntry) logQueue.get();
writeToFile (le);
}
while (logQueue.size() > 0)
tlq.put(logQueue.pop());
logQueue = tlq;
}
} else {
logQueue = new ObjectBuffer (Server.srv.LOG_QUEUE_SIZE);
}
if (!Server.LOGFILE_CFG.equals ("console")) try {
File f = checkFile (Server.LOGFILE_CFG);
FileOutputStream fo = new FileOutputStream (f, true);
logFile[Server.MSG_CONFIG] = fo.getChannel();
} catch (Exception e) {
Server.LOGFILE_CFG = "console";
e.printStackTrace ();
System.out.println ("Switching to console-logging for CONFIG-Logmessages!");
}
if (!Server.LOGFILE_AUTH.equals ("console")) try {
File f = checkFile (Server.LOGFILE_AUTH);
FileOutputStream fo = new FileOutputStream (f, true);
logFile[Server.MSG_AUTH] = fo.getChannel();
} catch (Exception e) {
Server.LOGFILE_AUTH = "console";
e.printStackTrace ();
System.out.println ("Switching to console-logging for AUTH-Logmessages!");
}
if (!Server.LOGFILE_STATE.equals ("console")) try {
File f = checkFile (Server.LOGFILE_STATE);
FileOutputStream fo = new FileOutputStream (f, true);
logFile[Server.MSG_STATE] = fo.getChannel();
} catch (Exception e) {
Server.LOGFILE_STATE = "console";
e.printStackTrace ();
System.out.println ("Switching to console-logging for STATE-Logmessages!");
}
if (!Server.LOGFILE_TRAFFIC.equals ("console")) try {
File f = checkFile (Server.LOGFILE_TRAFFIC);
FileOutputStream fo = new FileOutputStream (f, true);
logFile[Server.MSG_TRAFFIC] = fo.getChannel();
} catch (Exception e) {
Server.LOGFILE_TRAFFIC = "console";
e.printStackTrace ();
System.out.println ("Switching to console-logging for TRAFFIC-Logmessages!");
}
if (!Server.LOGFILE_ERROR.equals ("console")) try {
File f = checkFile (Server.LOGFILE_ERROR);
FileOutputStream fo = new FileOutputStream (f, true);
logFile[Server.MSG_ERROR] = fo.getChannel();
} catch (Exception e) {
Server.LOGFILE_ERROR = "console";
e.printStackTrace ();
System.out.println ("Switching to console-logging for ERROR-Logmessages!");
}
}
private File checkFile (String path) throws IOException, FileNotFoundException {
File f = new File (path);
File parent = f.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
StringBuffer sb = new StringBuffer("Unable to create directory for Logfile '");
sb.append (f.getCanonicalPath());
sb.append ("'!");
throw new FileNotFoundException (sb.toString());
}
return f;
}
public static LogWriter getLogWriter() throws FileNotFoundException, IOException {
if (instance==null) {
instance = new LogWriter();
instance.setPriority (Thread.NORM_PRIORITY-2);
}
if (!instance.isAlive())
instance.start();
return instance;
}
public void addLogMessage (int type, String message) {
synchronized (logQueue) {
logQueue.put(new LogEntry (type, message));
logQueue.notify();
}
}
public void stopLogging () {
stopped = true;
}
public void run() {
LogEntry le;
while (!stopped) try {
synchronized (logQueue) {
le = (LogEntry) logQueue.pop();
if (le == null) {
try {
logQueue.wait (1000);
} catch (InterruptedException ie) {
// is ok
}
continue;
}
logQueue.notifyAll();
}
writeToFile(le);
} catch (Exception e) {
Server.debug ("LogWriter.run: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
}
}
private void writeToFile (LogEntry le) {
FileChannel fc = logFile[le.type];
if (le.buf == null) {
CharBuffer cb = CharBuffer.wrap (le.msg);
try {
le.buf = Charset.forName (Server.srv.DEFAULT_CHARSET).newEncoder ().encode (cb);
} catch (CharacterCodingException cce) {
cce.printStackTrace();
}
}
try {
int written = fc.write (le.buf);
if (!le.buf.hasRemaining())
logQueue.pop();
} catch (IOException ioe) {
Server.debug ("LogWriter.writeToFile: ", ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
} catch (Exception e) {
Server.debug ("LogWriter.writeToFile: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
}
}
private class LogEntry {
int type;
String msg;
ByteBuffer buf;
LogEntry (int type, String msg) {
this.type=type;
this.msg=msg;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -