package zeta;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import zeta.util.StreamUtils;
public class ZetaInfo {
public static void init(ZetaProperties properties) {
String infoOutput = properties.get("info.output");
if (infoOutput != null && infoOutput.equals("false")) {
setStandardOutput(false);
}
String exceptionOutput = properties.get("exception.output");
if (exceptionOutput != null && exceptionOutput.equals("false")) {
setExceptionOutput(false);
}
String appendsTimestamp = properties.get("info.timestamp");
if (appendsTimestamp != null) {
setAppendsTimestamp(appendsTimestamp);
}
filename = properties.get("info.filename", "info.tmp"); eventLogFilename = properties.get("info.log.filename", "");
String eventLogTimestamp = properties.get("info.log.timestamp");
if (eventLogTimestamp != null) {
setEventLogTimestamp(eventLogTimestamp);
}
eventLogFileSize = toFileSize(properties.get("info.log.file_size"));
eventLogMaxBackupIndex = properties.get("info.log.max_backup_index", 0);
}
public static void write(String information) {
write(information, System.out);
}
public static void write(String information, PrintStream output) {
Date rightNow = new Date();
String informationTimestamp = information;
if (appendsTimestamp != null) {
StringBuffer buffer = new StringBuffer(information.length()+30);
buffer.append(information);
buffer.append(" (");
buffer.append(appendsTimestamp.format(rightNow));
buffer.append(')');
informationTimestamp = buffer.toString();
}
if (standardOutput) {
output.println(informationTimestamp);
}
if (filename != null && filename.length() > 0) {
FileOutputStream fout = null;
try {
fout = new FileOutputStream(filename);
fout.write(informationTimestamp.getBytes());
fout.flush();
} catch (IOException ioe) {
} finally {
StreamUtils.close(fout);
}
}
if (eventLogFileSize > 0 && eventLogFilename.length() > 0 && information.trim().length() > 0) {
synchronized (eventLogFilename) {
FileOutputStream fout = null;
try {
boolean append = (eventLogFileSize > new File(eventLogFilename).length());
if (!append) {
rollOver();
}
fout = new FileOutputStream(eventLogFilename, append);
StringBuffer buffer = null;
if (eventLogTimestamp != null) {
buffer = new StringBuffer(information.length()+newLine.length()+30);
buffer.append('[');
buffer.append(eventLogTimestamp.format(rightNow));
buffer.append("] ");
} else {
buffer = new StringBuffer(information.length()+newLine.length());
}
buffer.append(information);
buffer.append(newLine);
fout.write(buffer.toString().getBytes());
fout.flush();
} catch (IOException ioe) {
} finally {
StreamUtils.close(fout);
}
}
}
}
public static void handle(Throwable t) {
if (exceptionOutput) {
t.printStackTrace();
}
if (eventLogFileSize > 0 && eventLogFilename.length() > 0) {
synchronized (eventLogFilename) {
FileOutputStream fout = null;
try {
boolean append = (eventLogFileSize > new File(eventLogFilename).length());
if (!append) {
rollOver();
}
fout = new FileOutputStream(eventLogFilename, append);
if (eventLogTimestamp != null) {
StringBuffer buffer = new StringBuffer(30);
buffer.append('[');
buffer.append(eventLogTimestamp.format(new Date()));
buffer.append("] ");
fout.write(buffer.toString().getBytes());
}
t.printStackTrace(new PrintStream(fout));
fout.flush();
} catch (IOException ioe) {
} finally {
StreamUtils.close(fout);
}
}
}
}
public static void setAppendsTimestamp(String appendsTimestamp) {
if (appendsTimestamp == null || appendsTimestamp.length() == 0) {
ZetaInfo.appendsTimestamp = null;
} else {
ZetaInfo.appendsTimestamp = new SimpleDateFormat(appendsTimestamp);
}
}
public static void setEventLogTimestamp(String eventLogTimestamp) {
if (eventLogTimestamp == null || eventLogTimestamp.length() == 0) {
ZetaInfo.eventLogTimestamp = null;
} else {
ZetaInfo.eventLogTimestamp = new SimpleDateFormat(eventLogTimestamp);
}
}
public static void setStandardOutput(boolean standardOutput) {
ZetaInfo.standardOutput = standardOutput;
}
public static void setExceptionOutput(boolean exceptionOutput) {
ZetaInfo.exceptionOutput = exceptionOutput;
}
private static void rollOver() { if (eventLogMaxBackupIndex > 0) {
File file = new File(eventLogFilename + '.' + eventLogMaxBackupIndex);
if (file.exists()) {
file.delete();
}
for (int i = eventLogMaxBackupIndex-1; i >= 1; --i) {
File f = new File(eventLogFilename + '.' + i);
if (f.exists()) {
f.renameTo(file);
}
file = f;
}
new File(eventLogFilename).renameTo(file);
}
}
private static long toFileSize(String value) {
if (value != null && value.length() > 0) {
value = value.toLowerCase();
try {
int i = value.indexOf("kb");
if (i >= 0) {
return Long.valueOf(value.substring(0, i).trim()).longValue() * 1024;
}
i = value.indexOf("mb");
if (i >= 0) {
return Long.valueOf(value.substring(0, i).trim()).longValue() * 1024 * 1024;
}
i = value.indexOf("gb");
if (i >= 0) {
return Long.valueOf(value.substring(0, i).trim()).longValue() * 1024 * 1024 * 1024;
}
return Long.valueOf(value.trim()).longValue();
} catch (NumberFormatException e) {
handle(e);
}
}
return 0;
}
private static SimpleDateFormat appendsTimestamp = null;
private static boolean standardOutput = true;
private static boolean exceptionOutput = true;
private static String filename = null;
private static String eventLogFilename = "";
private static SimpleDateFormat eventLogTimestamp = null;
private static long eventLogFileSize = 0;
private static int eventLogMaxBackupIndex = 0;
private static String newLine = System.getProperty("line.separator");
}