|
From: <ian...@us...> - 2007-11-13 00:39:05
|
Revision: 573
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=573&view=rev
Author: iansmith
Date: 2007-11-12 16:39:09 -0800 (Mon, 12 Nov 2007)
Log Message:
-----------
Added support for logging using log4j if you have it. Otherwise, no effect.
A
Modified Paths:
--------------
maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/MigratedResourceServlet.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/proto/AsyncProtoFactory.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/util/Log.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/loadtest/MultiuserTests.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/SpaceSimulator.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/MessageProxy.java
Modified: maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/MigratedResourceServlet.java
===================================================================
--- maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/MigratedResourceServlet.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/appdev/servlet/MigratedResourceServlet.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -50,14 +50,14 @@
private SessionFactory getOrCreateHibernateSessionFactory(ServletConfig config, Context context) {
SessionFactory sessionFactory = (SessionFactory) config.getServletContext().getAttribute(HIBERNATE_SESSION_FACTORY_KEY);
if (sessionFactory == null) {
- Log.test(config.getServletName() + ": checking DB version");
+ Log.info(config.getServletName() + ": checking DB version");
MigrationSupport ms = getMigration();
if (!ms.verifyVersion(config, context)) {
throw new IllegalStateException("Cannot find a DB configuration for hibernate!");
}
sessionFactory = ms.getCurrentConfiguration().configure().buildSessionFactory();
config.getServletContext().setAttribute(HIBERNATE_SESSION_FACTORY_KEY, sessionFactory);
- Log.test(config.getServletName() + ": DB version OK");
+ //Log.info(config.getServletName() + ": DB version OK");
}
return sessionFactory;
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/proto/AsyncProtoFactory.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/proto/AsyncProtoFactory.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/proto/AsyncProtoFactory.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -41,6 +41,15 @@
}
}
+ public static AsyncProto getDefaultClient(URI uri) throws IOException {
+ if (USE_SIMPLE_SOCKET) {
+ return simpleSocketProto(uri.getHost(), uri.getPort());
+ } else {
+ return cometProto(uri);
+ }
+ }
+
+
private static AsyncProto cometProto(URI cometURI) throws IOException {
//return new CometClient(host,selector).getProto();
return CometClient.getProto(cometURI);
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/util/Log.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/util/Log.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/util/Log.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -2,10 +2,14 @@
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import com.ogoglio.xml.SpaceEvent;
+
public class Log {
- private static class Logger {
+ private static class SimpleLogger {
public void debug(Object msg) {
System.out.println(msg);
@@ -42,41 +46,163 @@
}
}
- static Logger log = new Logger();
+ static SimpleLogger simple = new SimpleLogger();
+ static boolean haveLog4J = false;
+ static Object reallyALogger=null;
+ static Object reallyTheSpaceLogger=null;
+ static boolean faultedInAlready=false;
+ static Method m_debug=null;
+ static Method m_info=null;
+ static Method m_info_throw=null;
+ static Method m_warn=null;
+ static Method m_error=null;
+ static Method m_error_throw=null;
+ static Method m_trace=null;
+ static Method m_test=null;
+ static Method m_logMemory=null;
+ static String[] ignoredSpaceEventNames = {
+ SpaceEvent.USER_START_MOTION_EVENT,
+ SpaceEvent.THING_START_MOTION_EVENT,
+ SpaceEvent.SHAPE_START_MOTION_EVENT
+ };
+ public static void faultInLogger() {
+ try {
+ if (faultedInAlready) {
+ return;
+ }
+ faultedInAlready=true;
+ //do we have linkage to log4j?
+ Class clazz=Class.forName("org.apache.log4j.Logger");
+ Object[] arrayOfOneInstWithString = new Object[] {"com.ogoglio"};
+ Object[] arrayOfOneInstWithSpaceString = new Object[] {"com.ogoglio.space"};
+
+ Class[] arrayOfOneClass = new Class[] {new Object().getClass()};
+ Class[] arrayOfTwoClasses = new Class[] {new Object().getClass(), new Throwable().getClass()};
+
+ haveLog4J=true;
+ Method m=clazz.getMethod("getLogger", new Class[] { "".getClass()});
+ //setup the methods
+ reallyALogger=m.invoke(null, arrayOfOneInstWithString);
+ reallyTheSpaceLogger=m.invoke(null, arrayOfOneInstWithSpaceString);
+ m_debug=clazz.getMethod("debug",arrayOfOneClass);
+ m_info=clazz.getMethod("info",arrayOfOneClass);
+ m_info_throw=clazz.getMethod("info",arrayOfTwoClasses);
+ m_warn=clazz.getMethod("warn",arrayOfOneClass);
+ m_error=clazz.getMethod("error",arrayOfOneClass);
+ m_error_throw=clazz.getMethod("error",arrayOfTwoClasses);
+ m_trace=clazz.getMethod("info",arrayOfOneClass);
+ m_test=clazz.getMethod("debug",arrayOfOneClass);
+ m_logMemory=clazz.getMethod("info",arrayOfOneClass);
+
+
+
+ } catch (IllegalAccessException e) {
+ haveLog4J=false;
+ simple.warn("Something went wrong in log4j init [illegalaccess]:"+e.getMessage());
+ } catch (InvocationTargetException e) {
+ haveLog4J=false;
+ simple.warn("Something went wrong in log4j init [invocationtarget]:"+e.getMessage());
+ } catch (NoSuchMethodException e) {
+ haveLog4J=false;
+ simple.warn("Something went wrong in log4j init [nosuchmethod]:"+e.getMessage());
+ } catch (ClassNotFoundException exception) {
+ haveLog4J=false;
+ simple.warn("Didn't find log4j, so using a simple logger.");
+ }
+ }
+
public static void debug(Object msg) {
- log.debug(msg);
+ faultInLogger();
+ useLog4jOrSimple(msg, m_debug);
}
+ private static void useLog4jOrSimple(Object msg,Method method) {
+ useLog4jOrSimple(msg, null,method);
+ }
+ private static void useLog4jOrSimple(Object msg, Object throwable, Method method) {
+ useLog4jOrSimple(msg, reallyALogger, throwable,method);
+ }
+
+ private static void useLog4jOrSimple(Object msg, Object logger, Object throwable, Method method) {
+ try {
+ if (haveLog4J) {
+ if (throwable==null) {
+ method.invoke(logger, new Object[] {msg});
+ } else {
+ method.invoke(logger, new Object[] {msg,throwable});
+ }
+ } else {
+ simple.debug(msg);
+ }
+ } catch (InvocationTargetException ex) {
+ haveLog4J=false;
+ simple.debug("Probablem using log4j [invocationtarget]:"+ex.getMessage());
+ } catch (IllegalAccessException ex) {
+ haveLog4J=false;
+ simple.debug("Probably using log4j [illegalaccess]:"+ex.getMessage());
+ }
+ }
+
public static void info(Object msg) {
- log.info(msg);
+ faultInLogger();
+ useLog4jOrSimple(msg, m_info);
}
public static void info(Object msg, Throwable t) {
- log.info(msg, t);
+ useLog4jOrSimple(msg, t, m_info_throw);
}
public static void warn(Object msg) {
- log.warn(msg);
+ faultInLogger();
+ useLog4jOrSimple(msg, m_warn);
}
public static void error(Object msg) {
- log.error(msg);
+ faultInLogger();
+ useLog4jOrSimple(msg, m_error);
}
public static void error(Object msg, Throwable t) {
- log.error(msg, t);
+ useLog4jOrSimple(msg, t, m_error_throw);
}
public static void trace(Object msg) {
- log.trace(msg);
+ faultInLogger();
+ useLog4jOrSimple(msg, m_trace);
}
public static void test(Object msg) {
- log.debug("TEST:" + msg);
+ faultInLogger();
+ useLog4jOrSimple("TEST:"+msg, m_debug);
}
public static void logMemoryUsage(){
- log.logMemoryUsage();
+ faultInLogger();
+ MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
+ useLog4jOrSimple("\tHeap: " + (memoryBean.getHeapMemoryUsage().getUsed() / 1024 / 1024f) + "MB / " + (memoryBean.getHeapMemoryUsage().getCommitted() / 1024 / 1024f) + "MB / " + (memoryBean.getHeapMemoryUsage().getMax() / 1024 / 1024f) + "MB",m_info);
+ useLog4jOrSimple("\tNon: " + (memoryBean.getNonHeapMemoryUsage().getUsed() / 1024 / 1024f) + "MB / " + (memoryBean.getNonHeapMemoryUsage().getCommitted() / 1024 / 1024f) + "MB / " + (memoryBean.getNonHeapMemoryUsage().getMax() / 1024 / 1024f) + "MB", m_info);
}
+
+ public static void logSpaceEvent(SpaceEvent event) {
+ faultInLogger();
+ if (!haveLog4J) {
+ return;//don't bother unless we have fancy logging capabilities
+ }
+ String n=event.getName();
+ for (int i=0; i<ignoredSpaceEventNames.length;++i) {
+ //reduce size of logs a bit
+ if (n.equals(ignoredSpaceEventNames[i])) {
+ return;
+ }
+ }
+ useLog4jOrSimple(event.toString(), reallyTheSpaceLogger, null, m_info);
+ }
+ public static void logSpaceEvent(String msg) {
+ faultInLogger();
+ if (!haveLog4J) {
+ return;
+ }
+ useLog4jOrSimple(msg, reallyTheSpaceLogger, null, m_info);
+ }
}
Modified: maven/trunk/ogoglio-common/src/test/java/com/ogoglio/loadtest/MultiuserTests.java
===================================================================
--- maven/trunk/ogoglio-common/src/test/java/com/ogoglio/loadtest/MultiuserTests.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-common/src/test/java/com/ogoglio/loadtest/MultiuserTests.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -65,7 +65,7 @@
for (int i = 0; i < numRobots; i++) {
startPosition.setTranslation(new Vector3d(0, 0, -50));
tests.addRobot(startPosition, true);
- Log.test("Added robot " + (i + 1) + " of " + numRobots);
+ Log.info("Added robot " + (i + 1) + " of " + numRobots);
Thread.sleep(1000);
}
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/SpaceSimulator.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/SpaceSimulator.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/SpaceSimulator.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -188,7 +188,7 @@
}
public void startSim() {
- simThread.setName("space-sim-thread-"+Thread.currentThread().getId());
+ simThread.setName("space-"+space.getSpaceID());
//Log.info("Starting up thread: "+simThread.getName());
simThread.start();
}
@@ -201,14 +201,16 @@
BlockingQueue queue = new BlockingQueue();
public SimThread() {
- super("SimThread");
+ super();
setDaemon(true);
}
public void run() {
+ Log.logSpaceEvent("SPACE START:"+space.getDisplayName()+","+space.getSpaceID());
while (!cleaned) {
try {
SpaceEvent event = (SpaceEvent) queue.dequeue();
+ Log.logSpaceEvent(event);
if (cleaned) {
return;
}
@@ -428,6 +430,8 @@
Log.error("Error handling space event", e);
}
}
+ Log.logSpaceEvent("SPACE STOP:"+space.getDisplayName()+","+space.getSpaceID());
+
}
private String markdownChatMessage(String chatMessage) {
Modified: maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/MessageProxy.java
===================================================================
--- maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/MessageProxy.java 2007-11-12 17:37:16 UTC (rev 572)
+++ maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/MessageProxy.java 2007-11-13 00:39:09 UTC (rev 573)
@@ -16,6 +16,7 @@
import java.io.IOException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Timer;
import java.util.TimerTask;
@@ -57,7 +58,7 @@
private NetworkChannelServer channelServer = null;
private SessionFactory sessionFactory = null;
-
+
private SimChannelHandler simMessageHandler = new SimChannelHandler();
private ClientMessageHandler clientMessageHandler = new ClientMessageHandler();
@@ -68,13 +69,9 @@
private static final long HEARTBEAT_INTERVAL_TO_SIM = 5000;
- //needed because we are CLIENT of the sim servlet and need to know how to reach it
- private WebAPIDescriptor descriptor;
-
public MessageProxy(SessionFactory sessionFactory, WebAPIDescriptor descriptor) throws IOException {
ArgumentUtils.assertNotNull(sessionFactory);
this.sessionFactory = sessionFactory;
- this.descriptor = descriptor;
channelServer = new NetworkChannelServer(clientMessageHandler, descriptor.getCometProxyURI(), true, this);
Log.info("Started Message Proxy on port " + channelServer.getLocator().getPort() + " with target of " + descriptor.getCometSimURI());
outgoingHeartbeatTimer.schedule(new OutgoingHeartbeatTask(), 5000, 60000);
@@ -203,7 +200,7 @@
sendMessageToSpace(locatorAuth.uri, locatorAuth.spaceID, simMessage);
return;
} else if (request.getPayload() instanceof PayloadFactory.LogoutPayload) {
- request.getPayload(); //XXX is this necessary?
+ request.getPayload(); //XXX is this necessary? is there a side effect??
logout(request.getOrigin());
return;
} else if (request.getPayload() instanceof PayloadFactory.SpaceEventPayload) {
@@ -321,13 +318,16 @@
simChannel = (TCPChannel) simChannels.getForward(uri);
if (simChannel == null) {
try {
- /*Object selector=AsyncProtoFactory.getDefaultInfo().getSimSpecificSelector();
- AsyncProto proto=AsyncProtoFactory.getDefaultClient(uri.getHost(), selector);*/
- AsyncProto proto = AsyncProtoFactory.getDefaultClient(descriptor, false);
+ //the reason we don't use the service API descriptor here is that in the case
+ //of splitting across multiple machines (sims on different machine from the
+ //webapp) the sim isn't actually part of the service URI space, it has it's
+ //own location, communicated out of band (via the DB)
+ URI cometURI=representativeURIToCometUR(uri);
+ AsyncProto proto=AsyncProtoFactory.getDefaultClient(cometURI);
simChannel = new TCPChannel(proto, simMessageHandler, false, simMessageHandler, true, "sim-client");
} catch (IOException e) {
e.printStackTrace();
- throw new NoSuchDestinationException("Could not open a channel to " + uri);
+ throw new NoSuchDestinationException("Could not open a channel to " + uri + "(original exception was:"+e.getMessage());
}
simChannels.put(uri, simChannel);
}
@@ -336,6 +336,21 @@
simChannel.sendMessage(message);
}
+ private URI representativeURIToCometUR(URI representativeURI) throws IOException {
+ String cometPath="/og/comet/sim";
+
+ if (!representativeURI.getPath().endsWith("/og/sim/")) {
+ throw new IOException("Can't understand the path well enough to convert this to a comet URI:"+representativeURI.getPath());
+ }
+ try {
+ URI result=new URI(representativeURI.getScheme(),null,representativeURI.getHost(),representativeURI.getPort(),cometPath,null,null);
+ Log.warn("Somewhat dodgy: Converting the URI "+representativeURI+" to "+result+" for comet!");
+ return result;
+ } catch (URISyntaxException e) {
+ throw new IOException("Our converted URI was not well formed! Started with:"+representativeURI);
+ }
+ }
+
public void channelAdded(TCPChannel channel) {
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|