FileDocCategorySizeDatePackage
FileLogging.javaAPI DocAzureus 3.0.3.48274Fri Mar 03 00:37:28 GMT 2006org.gudy.azureus2.core3.logging.impl

FileLogging

public class FileLogging extends Object implements ILogEventListener
Log events to a file.
author
TuxPaper

Fields Summary
public static final String
LOG_FILE_NAME
public static final String
BAK_FILE_NAME
public static LogIDs[]
configurableLOGIDs
private static final String
sTimeStampFormat
private static final String
CFG_ENABLELOGTOFILE
private boolean
bLogToFile
private String
sLogDir
private int
iLogFileMaxMB
private ArrayList[]
ignoredComponents
private ArrayList
listeners
private static final int
DEFPADDING
private int
lastWidth
Constructors Summary
Methods Summary
public voidaddListener(FileLoggingAdapter listener)

		if (!listeners.contains(listener))
			listeners.add(listener);
	
private voidcheckLoggingConfig()

		try {
			// Shorten from COConfigurationManager To make code more readable
			final ConfigurationManager config = ConfigurationManager.getInstance();

			boolean overrideLog = System.getProperty("azureus.overridelog") != null;
			if (overrideLog) {
				bLogToFile = true;
				sLogDir = ".";
				iLogFileMaxMB = 2;

				for (int i = 0; i < ignoredComponents.length; i++) {
					ignoredComponents[i].clear();
				}
			} else {
				reloadLogToFileParam();

				sLogDir = config.getStringParameter("Logging Dir", "");

				iLogFileMaxMB = config.getIntParameter("Logging Max Size");

				for (int i = 0; i < ignoredComponents.length; i++) {
					ignoredComponents[i].clear();
					int logType = indexToLogType(i);
					for (int j = 0; j < configurableLOGIDs.length; j++) {
						if (!config.getBooleanParameter("bLog." + logType + "."
								+ configurableLOGIDs[j], true))
							ignoredComponents[i].add(configurableLOGIDs[j]);
					}
				}
			}
		} catch (Throwable t) {
			Debug.printStackTrace(t);
		}
	
public java.util.ListgetListeners()

		return listeners;
	
private intindexToLogType(int index)

		switch (index) {
			case 0:
				return LogEvent.LT_INFORMATION;
			case 1:
				return LogEvent.LT_WARNING;
			case 2:
				return LogEvent.LT_ERROR;
		}
		return LogEvent.LT_INFORMATION;
	
public voidinitialize()


	   
		// Shorten from COConfigurationManager To make code more readable
		final ConfigurationManager config = ConfigurationManager.getInstance();
		boolean overrideLog = System.getProperty("azureus.overridelog") != null;

		for (int i = 0; i < ignoredComponents.length; i++) {
			ignoredComponents[i] = new ArrayList();
		}

		if (!overrideLog) {
			config.addListener(new COConfigurationListener() {
				public void configurationSaved() {
					checkLoggingConfig();
				}
			});
		}

		checkLoggingConfig();
		config.addParameterListener(CFG_ENABLELOGTOFILE, new ParameterListener() {
			public void parameterChanged(String parameterName) {
				FileLogging.this.reloadLogToFileParam();
			}
		});
	
public voidlog(LogEvent event)

	    
		if (ignoredComponents[logTypeToIndex(event.entryType)]
				.contains(event.logID))
			return;

		StringBuffer text = new StringBuffer();
		
		text.append(event.entryType).append(" ");

		padAndAppend(text, event.logID.toString(), 8, 1);

		//text.append("|");

		if (event.relatedTo != null) {
			lastWidth = padAndAppend(text, event.text, lastWidth, 1);
			if (lastWidth > 200)
				lastWidth = 200;

			for (int i = 0; i < event.relatedTo.length; i++) {
				Object obj = event.relatedTo[i];

				if (obj == null)
					continue;

				if (i > 0)
					text.append("; ");

				if (obj instanceof LogRelation) {
					text.append(((LogRelation) obj).getRelationText());
				} else {
					text.append("RelatedTo[")
					    .append(obj.toString())
					    .append("]");
				}
			}
		} else {
			text.append(event.text);

			lastWidth = DEFPADDING;
		}

		//text.append(event.text);

		if (event.text == null || !event.text.endsWith("\n"))
			text.append("\r\n");

		boolean okToLog = true;
		for (Iterator iter = listeners.iterator(); iter.hasNext() && okToLog;) {
			FileLoggingAdapter listener = (FileLoggingAdapter) iter.next();
			okToLog = listener.logToFile(event, text);
		}

		logToFile(text.toString());
	
private voidlogToFile(java.lang.String str)

		if (!bLogToFile)
			return;

		synchronized (Logger.class) {

			SimpleDateFormat format;
			format = new SimpleDateFormat(sTimeStampFormat);

			str = format.format(new Date()) + str;

			PrintWriter pw = null;

			File file_name = new File(sLogDir + File.separator + LOG_FILE_NAME);

			try {
				pw = new PrintWriter(new FileWriter(file_name, true));
				if (pw != null)
					pw.print(str);

			} catch (Throwable e) {

				// can't log this as go recursive!!!!

			} finally {

				if (pw != null) {
					try {
						pw.close();
					} catch (Throwable e) {
						// can't log as go recursive!!!!
					}

					// two files so half
					long lMaxBytes = (iLogFileMaxMB * 1024 * 1024) / 2;

					if (file_name.length() > lMaxBytes) {
						File back_name = new File(sLogDir + File.separator + BAK_FILE_NAME);

						if ((!back_name.exists()) || back_name.delete()) {
							if (!file_name.renameTo(back_name))
								file_name.delete();
						} else {
							file_name.delete();
						}
					}
				}
			} // finally
		} // sync
	
private intlogTypeToIndex(int entryType)

		switch (entryType) {
			case LogEvent.LT_INFORMATION:
				return 0;
			case LogEvent.LT_WARNING:
				return 1;
			case LogEvent.LT_ERROR:
				return 2;
		}
		return 0;
	
private intpadAndAppend(java.lang.StringBuffer appendTo, java.lang.String s, int width, int growBy)

		if (s == null)
			s = "null";
		appendTo.append(s);

		int sLen = s.length();
		int len = width - sLen;
		while (len <= 0)
			len += growBy;
		
		char[] padding = new char[len];
		if (len > 5) {
			for (int i = 0; i < len; i += 2)
				padding[i] = ' ";
			for (int i = 1; i < len; i += 2)
				padding[i] = '.";
		} else {
			for (int i = 0; i < len; i++)
				padding[i] = ' ";
		}

		appendTo.append(padding);
		
		return len + sLen;
	
protected voidreloadLogToFileParam()

		final ConfigurationManager config = ConfigurationManager.getInstance();
		boolean bNewLogToFile = config.getBooleanParameter(CFG_ENABLELOGTOFILE);
		if (bNewLogToFile != bLogToFile) {
			bLogToFile = bNewLogToFile;
			if (bLogToFile)
				Logger.addListener(this);
			else
				Logger.removeListener(this);
		}
	
public voidremoveListener(FileLoggingAdapter listener)

		listeners.remove(listener);