changeset 214:8e4ef9134362

move time critical logging functions to java git-svn-id: https://luan-java.googlecode.com/svn/trunk@215 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 11 Jul 2014 06:06:31 +0000
parents 4a27f24ce2b5
children b2304de4579b
files logging/src/luan/modules/logging/Logging.luan logging/src/luan/modules/logging/LuanLogger.java
diffstat 2 files changed, 55 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
diff -r 4a27f24ce2b5 -r 8e4ef9134362 logging/src/luan/modules/logging/Logging.luan
--- a/logging/src/luan/modules/logging/Logging.luan	Fri Jul 11 04:19:25 2014 +0000
+++ b/logging/src/luan/modules/logging/Logging.luan	Fri Jul 11 06:06:31 2014 +0000
@@ -11,6 +11,7 @@
 import "org.apache.log4j.ConsoleAppender"
 import "org.apache.log4j.Level"
 import "org.apache.log4j.RollingFileAppender"
+import "luan.modules.logging.LuanLogger"
 
 
 layout = "%d %-5p %c - %m%n"
@@ -61,23 +62,7 @@
 
 
 local function to_luan_logger(log4j_logger)
-	local tbl = {}
-
-	function tbl.error(obj)
-		log4j_logger.error( to_string(obj) )
-	end
-
-	function tbl.warn(obj)
-		log4j_logger.warn( to_string(obj) )
-	end
-
-	function tbl.info(obj)
-		log4j_logger.info( to_string(obj) )
-	end
-
-	function tbl.debug(obj)
-		log4j_logger.debug( to_string(obj) )
-	end
+	local tbl = LuanLogger.new(log4j_logger).table()
 
 	function tbl.get_level()
 		local level = log4j_logger.getLevel()
diff -r 4a27f24ce2b5 -r 8e4ef9134362 logging/src/luan/modules/logging/LuanLogger.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/logging/src/luan/modules/logging/LuanLogger.java	Fri Jul 11 06:06:31 2014 +0000
@@ -0,0 +1,53 @@
+package luan.modules.logging;
+
+import org.apache.log4j.Logger;
+import luan.LuanState;
+import luan.LuanException;
+import luan.LuanTable;
+import luan.LuanJavaFunction;
+
+
+public final class LuanLogger {
+	private final Logger logger;
+
+	public LuanLogger(Logger logger) {
+		this.logger = logger;
+	}
+
+	public void error(LuanState luan,Object obj) throws LuanException {
+		logger.error( luan.toString(obj) );
+	}
+
+	public void warn(LuanState luan,Object obj) throws LuanException {
+		logger.warn( luan.toString(obj) );
+	}
+
+	public void info(LuanState luan,Object obj) throws LuanException {
+		logger.info( luan.toString(obj) );
+	}
+
+	public void debug(LuanState luan,Object obj) throws LuanException {
+		logger.debug( luan.toString(obj) );
+	}
+
+	public LuanTable table() {
+		LuanTable tbl = new LuanTable();
+		try {
+			tbl.put( "error", new LuanJavaFunction(
+				LuanLogger.class.getMethod( "error", LuanState.class, Object.class ), this
+			) );
+			tbl.put( "warn", new LuanJavaFunction(
+				LuanLogger.class.getMethod( "warn", LuanState.class, Object.class ), this
+			) );
+			tbl.put( "info", new LuanJavaFunction(
+				LuanLogger.class.getMethod( "info", LuanState.class, Object.class ), this
+			) );
+			tbl.put( "debug", new LuanJavaFunction(
+				LuanLogger.class.getMethod( "debug", LuanState.class, Object.class ), this
+			) );
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+		return tbl;
+	}
+}