???
This commit is contained in:
93
src/com/flaremicro/util/Util.java
Normal file
93
src/com/flaremicro/util/Util.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package com.flaremicro.util;
|
||||
|
||||
import java.applet.Applet;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Random;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.flaremicro.util.logging.LogFormatter;
|
||||
import com.flaremicro.util.logging.LogOutputStream;
|
||||
|
||||
|
||||
public class Util {
|
||||
public static final long INSTANCE_ID = Math.abs(new Random().nextLong());
|
||||
public static final String LOGGER_NAME = "VisualForecast 1000 Logger";
|
||||
|
||||
static
|
||||
{
|
||||
initLogger();
|
||||
}
|
||||
|
||||
public static Logger getDefaultLogger() {
|
||||
return Logger.getLogger(LOGGER_NAME);
|
||||
}
|
||||
|
||||
public static void initLogger() {
|
||||
Logger log = getDefaultLogger();
|
||||
log.setUseParentHandlers(false);
|
||||
ConsoleHandler handler = new ConsoleHandler();
|
||||
handler.setFormatter(new LogFormatter());
|
||||
log.addHandler(handler);
|
||||
System.setErr(new PrintStream(new LogOutputStream(log, java.util.logging.Level.SEVERE)));
|
||||
System.setOut(new PrintStream(new LogOutputStream(log, java.util.logging.Level.INFO)));
|
||||
log.info("Initialized logger successfully");
|
||||
log.info("Using native Java logger, ID 0J3QvtGCINGD0YHQtdC70LXRgdGBINCbNNCI");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse arguments, in arg=value format, or just arg for arguments with no
|
||||
* value. Values can contain = signs, but not arguments.
|
||||
*
|
||||
* @param args
|
||||
* @param keyToLower
|
||||
* @return
|
||||
*/
|
||||
public static HashMap<String, String> parseArgs(String args[], boolean keyToLower) {
|
||||
HashMap<String, String> map = new HashMap<String, String>();
|
||||
for (String s : args)
|
||||
{
|
||||
String[] res = s.split("=", 2);
|
||||
if (keyToLower)
|
||||
res[0].toLowerCase();
|
||||
if (res.length > 1)
|
||||
{
|
||||
map.put(res[0], res[1]);
|
||||
}
|
||||
else map.put(res[0], "");
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static boolean cleanClose(Closeable closeable) {
|
||||
try
|
||||
{
|
||||
if (closeable != null)
|
||||
closeable.close();
|
||||
return true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static HashMap<String, String> toArguments(Applet applet, String[] expectedParameters) {
|
||||
HashMap<String, String> arguments = new HashMap<String, String>();
|
||||
if (applet == null || expectedParameters == null)
|
||||
return arguments;
|
||||
for (String parameter : expectedParameters)
|
||||
{
|
||||
String value = applet.getParameter(parameter);
|
||||
if (value != null)
|
||||
{
|
||||
arguments.put(parameter, value);
|
||||
}
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
31
src/com/flaremicro/util/logging/LogFormatter.java
Normal file
31
src/com/flaremicro/util/logging/LogFormatter.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.flaremicro.util.logging;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
|
||||
public class LogFormatter extends Formatter {
|
||||
String sep = System.getProperty("line.separator");
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
@Override
|
||||
public String format(LogRecord record) {
|
||||
String fmessage = "";
|
||||
if(record.getLevel() == Level.SEVERE)
|
||||
fmessage += "["+format.format(System.currentTimeMillis())+"][SEVERE]";
|
||||
else if(record.getLevel() == Level.WARNING)
|
||||
fmessage += "["+format.format(System.currentTimeMillis())+"][WARNING]";
|
||||
else if(record.getLevel() == Level.INFO)
|
||||
fmessage += "["+format.format(System.currentTimeMillis())+"][INFO]";
|
||||
return fmessage += this.formatMessage(record) + sep;
|
||||
}
|
||||
public String getHead(Handler h) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String getTail(Handler h) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
38
src/com/flaremicro/util/logging/LogOutputStream.java
Normal file
38
src/com/flaremicro/util/logging/LogOutputStream.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.flaremicro.util.logging;
|
||||
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class LogOutputStream extends OutputStream {
|
||||
|
||||
String buffer = "";
|
||||
Logger logger;
|
||||
Level level;
|
||||
|
||||
public LogOutputStream(Logger logger, Level level)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
buffer += (char)((byte)b);
|
||||
if(buffer.endsWith("\n") || buffer.endsWith("\r\n"))
|
||||
{
|
||||
buffer = buffer.replace("\r", "").replace("\n", "");
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void flush()
|
||||
{
|
||||
logger.log(level, buffer);
|
||||
buffer = "";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user