⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventlogger.java

📁 一个基于PlaceLab的室内和室外的智能导航系统
💻 JAVA
字号:
package org.placelab.midp;import java.util.Date;import java.util.Hashtable;import java.util.Vector;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Form;import javax.microedition.lcdui.List;import javax.microedition.lcdui.Spacer;import javax.microedition.lcdui.StringItem;import javax.microedition.rms.RecordComparator;import javax.microedition.rms.RecordEnumeration;import javax.microedition.rms.RecordFilter;import javax.microedition.rms.RecordStore;import javax.microedition.rms.RecordStoreException;import org.placelab.util.StringUtil;/** Event logging helper class.  In particular, use EventLogger.logError(String s) to log errors in your midlets for debugging */public class EventLogger { 	public static int JAVA_ERROR = 0;	public static String STORE_NAME = "placelab_eventlog";	public static EventLogger actInst = null;		private RecordStore rms;	private Hashtable eventTypes;		protected EventLogger() throws RecordStoreException {		rms = RecordStore.openRecordStore(STORE_NAME, true,				RecordStore.AUTHMODE_ANY, true);		eventTypes = new Hashtable();		Vector v = new Vector();				eventTypes.put(new Integer(0),"Java Error");	}		public static RecordEnumeration getRecords(RecordFilter rf, RecordComparator rc, boolean bool) throws RecordStoreException {		return getInst().rms.enumerateRecords(rf,rc,bool);	}		public static void setEventString(int eventNumber, String eventString) {		try {			EventLogger el = getInst();			el.eventTypes.put(new Integer(eventNumber),eventString);		} catch(Exception e) {		}	}		private static EventLogger getInst() throws RecordStoreException {		if (actInst == null) {			actInst = new EventLogger();		}		return actInst;	}		public static void add(int eventType,String message) {		try {			long time = System.currentTimeMillis();			String s = time + "|" + eventType + "|" + message;			// this outputs a line in the emulator			System.err.println("Event Logged: " + s);			byte[] ba = s.getBytes();			getInst().rms.addRecord(ba, 0, ba.length);		} catch (Exception ex) {			// No idea what to do. Can't print it or log it. Gotta swallow it...		}	}		public static void logError(Exception ex) {		add(JAVA_ERROR, ex.getClass().getName() + " : "				+ ex.getMessage());	}		public static void logError(String s) {	    add(JAVA_ERROR, s);	}		public static void deleteAll() throws RecordStoreException {		EventLogger inst = getInst();		if(inst != null) inst.deleteAll_impl();	}		public static void deleteType(int type) throws RecordStoreException {		EventLogger inst = getInst();		if(inst != null) inst.deleteType_impl(type);	}		private void deleteAll_impl() throws RecordStoreException {		rms.closeRecordStore();		RecordStore.deleteRecordStore(STORE_NAME);		rms = RecordStore.openRecordStore(STORE_NAME, true,				RecordStore.AUTHMODE_ANY, true);	}		private class TypeRecordFilter implements RecordFilter {		int type;		public TypeRecordFilter(int type) {			this.type=type;		}		public boolean matches(byte[] bytes) {			String s = new String(bytes);			String[] sarr = StringUtil.split(s, '|');			if(sarr == null || sarr.length < 2) return false;			return Integer.parseInt(sarr[1]) == type;		}	}		private void deleteType_impl(int type) {		try {			RecordEnumeration en = rms.enumerateRecords(new TypeRecordFilter(type), null, false);						while (en.hasNextElement()) {				rms.deleteRecord(en.nextRecordId());			}		} catch (RecordStoreException e) {		}	}	public static int getNumEventTypes() {		try {			return getInst().eventTypes.size();		} catch(RecordStoreException rse) {			rse.printStackTrace();		}		return -1;	}		public static Object getEventType(Object key) {		try {			return getInst().eventTypes.get(key);		} catch(RecordStoreException rse) {			rse.printStackTrace();		}		return null;	}	//	public static EventLoggerUI getEventLoggerUI(Display display, UIComponent back) {//		try {//			EventLoggerUI elui = getInst().new EventLoggerUI(display,back);//			return elui;//		} catch(RecordStoreException rse) {//		}//		return null;//	}			public static int numRecords() {		try {			return getInst().rms.getNumRecords();		} catch(RecordStoreException e) {			e.printStackTrace();		}		return -1;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -