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

📄 logger.java

📁 BT JAVA LIBRARY,BTLib package is a J2ME Bluetooth library usefull when developing applications for J
💻 JAVA
字号:
/* The Bluetooth Library for client-server communication Copyright (C) 2006 Martin Vysny This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. */package net.sf.btw.tools;import java.io.IOException;import java.io.InputStream;import java.util.Calendar;import java.util.Enumeration;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;/** * Able to log application status. *  * @author Martin Vysny *  */public final class Logger extends Form implements CommandListener {	/**	 * Error log level.	 */	public static final int LEVEL_ERROR = 0;	/**	 * Warning log level.	 */	public static final int LEVEL_WARNING = 1;	/**	 * Info log level.	 */	public static final int LEVEL_INFO = 2;	/**	 * Debug log level.	 */	public static final int LEVEL_DEBUG = 3;	/**	 * Current log level. Log events below this level are ignored. Default value	 * is {@link #LEVEL_WARNING}.	 */	public static int CURRENT_LEVEL = LEVEL_WARNING;	/**	 * If <code>true</code> then the original System.out is used to print log	 * aswell.	 */	public static boolean ENABLE_CONSOLE = false;	/**	 * Checks if given level is loggable with	 * {@link #CURRENT_LEVEL current log level} setting.	 * 	 * @param level	 *            level to query	 * @return <code>true</code> if a message with given level is logged,	 *         <code>false</code> if it is thrown away.	 */	public static boolean isLoggable(final int level) {		return level <= CURRENT_LEVEL;	}	/**	 * Log messages. Old are removed as new are added.	 */	private static final Queue logMessages = new Queue(100);	private static boolean exists(final String resource) {		final InputStream in = logMessages.getClass().getResourceAsStream(resource);		final boolean result = (in != null);		if (in != null) {			try {				in.close();			} catch (IOException e) {				info("Logger.exists()", e); //$NON-NLS-1$			}		}		return result;	}	/**	 * Setup the class.	 */	static {		try {			ENABLE_CONSOLE = exists("/net/sf/btw/tools/LoggerEnableConsole"); //$NON-NLS-1$			if (exists("/net/sf/btw/tools/LoggerLevelDebug")) //$NON-NLS-1$				CURRENT_LEVEL = LEVEL_DEBUG;			debug("Logger initialized", null); //$NON-NLS-1$		} catch (Exception ex) {			ex.printStackTrace();		}	}	/**	 * Logs given event.	 * 	 * @param level	 *            the log level.	 * @param message	 *            the message to log. May be <code>null</code> - in this case	 *            exception message is used.	 * @param ex	 *            exception to log. May be <code>null</code> only if message	 *            is not <code>null</code>.	 */	public static void log(final int level, final String message,			final Throwable ex) {		if (!isLoggable(level))			return;		final StringBuffer msg = format(level);		if (message != null) {			msg.append(message);			if (ex != null)				msg.append(" : "); //$NON-NLS-1$		}		if (ex != null)			msg.append(ex);		final String msgStr = msg.toString();		if (ENABLE_CONSOLE) {			System.err.println(msgStr);			if (ex != null)				ex.printStackTrace();		}		synchronized (logMessages) {			if (!logMessages.offer(msgStr)) {				logMessages.poll();				logMessages.offer(msgStr);			}		}	}	/**	 * Formats start of the log and returns it as a buffer.	 * 	 * @param level	 *            the log level	 * @return buffer.	 */	private static StringBuffer format(final int level) {		final Calendar cal = Calendar.getInstance();		final int hours = cal.get(Calendar.HOUR_OF_DAY);		final int minutes = cal.get(Calendar.MINUTE);		final int second = cal.get(Calendar.SECOND);		final StringBuffer result = new StringBuffer();		if (hours < 10)			result.append('0');		result.append(hours);		result.append(':');		if (minutes < 10)			result.append('0');		result.append(minutes);		result.append(':');		if (second < 10)			result.append('0');		result.append(second);		result.append(" ["); //$NON-NLS-1$		switch (level) {		case LEVEL_ERROR:			result.append("ERR "); //$NON-NLS-1$			break;		case LEVEL_WARNING:			result.append("WARN"); //$NON-NLS-1$			break;		case LEVEL_INFO:			result.append("INFO"); //$NON-NLS-1$			break;		case LEVEL_DEBUG:			result.append("DBG "); //$NON-NLS-1$			break;		default: {			result.append(level);			result.append("   "); //$NON-NLS-1$		}		}		result.append("]: "); //$NON-NLS-1$		return result;	}	/**	 * Logs an info message.	 * 	 * @param message	 *            the message to log. May be <code>null</code> - in this case	 *            exception message is used.	 * @param ex	 *            exception to log. May be <code>null</code> only if message	 *            is not <code>null</code>.	 */	public static void info(final String message, final Throwable ex) {		log(LEVEL_INFO, message, ex);	}	/**	 * Logs an error message.	 * 	 * @param message	 *            the message to log. May be <code>null</code> - in this case	 *            exception message is used.	 * @param ex	 *            exception to log. May be <code>null</code> only if message	 *            is not <code>null</code>.	 */	public static void error(final String message, final Throwable ex) {		log(LEVEL_ERROR, message, ex);	}	/**	 * Logs an warning message.	 * 	 * @param message	 *            the message to log. May be <code>null</code> - in this case	 *            exception message is used.	 * @param ex	 *            exception to log. May be <code>null</code> only if message	 *            is not <code>null</code>.	 */	public static void warn(final String message, final Throwable ex) {		log(LEVEL_WARNING, message, ex);	}	/**	 * Logs an debug message.	 * 	 * @param message	 *            the message to log. May be <code>null</code> - in this case	 *            exception message is used.	 * @param ex	 *            exception to log. May be <code>null</code> only if message	 *            is not <code>null</code>.	 */	public static void debug(final String message, final Throwable ex) {		log(LEVEL_DEBUG, message, ex);	}	/**	 * Constructs new logger form, immediately switching to the form.	 * 	 * @param previous	 *            switch to this displayable when the log window is closed.	 * @param display	 *            the display instance	 * 	 */	public Logger(final Displayable previous, final Display display) {		super("Logger"); //$NON-NLS-1$		this.previous = previous;		this.display = display;		addCommand(refreshCommand);		addCommand(clearCommand);		addCommand(closeCommand);		setCommandListener(this);		refresh();		display.setCurrent(this);	}	private final Display display;	private final Command refreshCommand = new Command("Refresh", //$NON-NLS-1$			Command.SCREEN, 1);	private final Command clearCommand = new Command("Clear", Command.SCREEN, 2); //$NON-NLS-1$	private final Command closeCommand = new Command("Close", Command.BACK, 1); //$NON-NLS-1$	private final Displayable previous;	/*	 * (non-Javadoc)	 * 	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,	 *      javax.microedition.lcdui.Displayable)	 */	public void commandAction(Command arg0, Displayable arg1) {		if (arg0 == closeCommand) {			display.setCurrent(previous);			clearDisplay();		} else if (arg0 == refreshCommand) {			refresh();		} else if (arg0 == clearCommand) {			synchronized (logMessages) {				logMessages.clear();			}			refresh();		}	}	private void clearDisplay() {		// clear the display's text		while (size() > 0) {			delete(size() - 1);		}	}	private void refresh() {		clearDisplay();		synchronized (logMessages) {			for (final Enumeration e = logMessages.getEnumeration(); e					.hasMoreElements();) {				final Object msg = e.nextElement();				append((String) msg);			}		}	}}

⌨️ 快捷键说明

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