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

📄 log.java

📁 JRed is a 100% Java implementation of the IrDA infrared communications protocols. JRed lets you beam
💻 JAVA
字号:
/*
**************************************************************************
** $Header: /cvsroot/jred/jred/src/com/synchrona/util/Log.java,v 1.1.1.1 2000/07/05 04:41:53 mpatters Exp $
**
** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
**
** This file is part of JRed, a 100% Java implementation of the IrDA
** infrared communications protocols.
**
** This file may be distributed under the terms of the Synchrona Public
** License as defined by Synchrona, Inc. and appearing in the file
** LICENSE included in the packaging of this file. The Synchrona Public
** License is based on the Q Public License as defined by Troll Tech AS
** of Norway; it differs only in its use of the courts of Florida, USA
** rather than those of Oslo, Norway.
**************************************************************************
*/
package com.synchrona.util;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Offers the usual logging capabilities. Can also cache a single instance
 * so it can be shared within a JVM. This is useful when logging output
 * should be directed to a single location.
 */
public class Log {
	public static final String DEFAULT_TIMESTAMP_FORMAT = "MM/dd/yy kk:mm:ss.SSS z";

	/**
	 * Name of the property from whose value Log instances determine
	 * whether or not debug messages should be printed. Debug messages
	 * are disabled by default.
	 */
	public static final String LOG_DEBUG_PROPERTY = "Log.debug";

	/**
	 * Name of the property from whose value Log instances determine
	 * whether or not error messages should be printed. Error messages
	 * are enabled by default.
	 */
	public static final String LOG_ERROR_PROPERTY = "Log.errors";

	/**
	 * Name of the property from whose value Log instances determine
	 * whether or not warning messages should be printed. Warnings are
	 * enabled by default.
	 */
	public static final String LOG_WARN_PROPERTY  = "Log.warnings";

	private boolean          _debug   = initState(LOG_DEBUG_PROPERTY, false);
	private boolean          _error   = initState(LOG_ERROR_PROPERTY, true);
	private boolean          _warn    = initState(LOG_WARN_PROPERTY,  true);
	private SimpleDateFormat _format  = new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT);
	private PrintWriter      _out     = null;

	/**
	 * Create a Log instance. All messages will be written to the
	 * given PrintWriter.
	 */
	public Log(PrintWriter out) {
		_out = out;
	}

	/**
	 * Print debugging messages, if debugging is enabled.
	 */
	public final void debug(String strCategory, String strMessage) {
		log(_debug, "debug", strCategory, strMessage);
	}

	public final void debugBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
		logBytes(_debug, "debug", strCategory, strMessage, ayBytes, nOffset, nLength);
	}

	/**
	 * Returns true if debugging messages are enabled.
	 */
	public boolean debugEnabled() {
		return _debug;
	}

	/**
	 * Print error messages, if errors is enabled. Errors are enabled by default.
	 */
	public final void error(String strCategory, String strMessage) {
		log(_error, "error", strCategory, strMessage);
	}

	public final void errorBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
		logBytes(_error, "error", strCategory, strMessage, ayBytes, nOffset, nLength);
	}

	public boolean errorsEnabled() {
		return _error;
	}

	public final void warn(String strCategory, String strMessage) {
		log(_warn, "warning", strCategory, strMessage);
	}

	public final void warnBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
		logBytes(_warn, "warning", strCategory, strMessage, ayBytes, nOffset, nLength);
	}

	/**
	 * Returns true if warning messages are enabled.
	 */
	public final boolean warningsEnabled() {
		return _warn;
	}

	/**
	*** Returns true if logging is enabled for this combination
	*** of type and category.
	***	
	*** @param type     Log type (debug, error, warn)
	*** @param category User-defined logging category
	**/
	private final boolean isEnabled(String type, boolean typeDefault, String category) {
		String  property = "Log." + type + "." + category;
		String  value    = System.getProperty(property);
		Boolean enabled;

		// If no override is defined for this property,
		// use the default value for this type of logging
		// message. This breaks if the default value changes
		// during program execution...how to handle this?
		if ( null == value ) {
			enabled = new Boolean(typeDefault);
		} else {
			enabled = new Boolean(value);
		}

		return enabled.booleanValue();
	}

	/**
	 * Looks for a property that defines the initial state (enabled/
	 * disabled) of a log level. If the property is not defined, 
	 * initState() returns the given default value.
	 */
	private final boolean initState(String strLevel, boolean bDefault) {
		String  strState = System.getProperty(strLevel);
		boolean bValue   = false;
		if ( null == strState ) {
			bValue = bDefault;
		} else {
			bValue = Boolean.getBoolean(strLevel);
		}
		return bValue;
	}

	/**
	 * If bFlag is true, this method formats its arguments and
	 * prints them to the log stream. This method is used by
	 * <a href="#debug">debug()</a>, <a href="#error">error()</a>,
	 * and <a href="#warn">warn().</a>
	 */
	private void log(boolean bFlag, String strType, String strCategory, String strMessage) {
		if ( isEnabled(strType, bFlag, strCategory) ) {
			StringBuffer buf = new StringBuffer(128);
			buf.append(strType);
			buf.append(" ");
			buf.append(_format.format(new Date()));
			buf.append(" [");
			buf.append(strCategory);
			buf.append("] ");
			buf.append(strMessage);
			_out.println(buf.toString());
		}
	}

	/**
	 * If bFlag is true, this method formats its arguments and
	 * prints them to the log stream. This method is used by
	 * <a href="#debug">debug()</a>, <a href="#error">error()</a>,
	 * and <a href="#warn">warn().</a>
	 */
	private void logBytes(boolean bFlag, String strType, String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
		if ( isEnabled(strType, bFlag, strCategory) ) {
			StringBuffer buf = new StringBuffer(128);
			buf.append(strType);
			buf.append(" ");
			buf.append(_format.format(new Date()));
			buf.append(" [");
			buf.append(strCategory);
			buf.append("] ");
			buf.append(strMessage);
			buf.append(" ");
			buf.append(Utilities.bytesToString(ayBytes, nOffset, nLength));
			_out.println(buf.toString());
		}
	}
}

⌨️ 快捷键说明

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