FileDocCategorySizeDatePackage
LoggerImpl.javaAPI DocAzureus 3.0.3.48124Wed Sep 05 14:37:28 BST 2007org.gudy.azureus2.core3.logging.impl

LoggerImpl

public class LoggerImpl extends Object
Logging tool
author
TuxPaper
since
2.3.0.7

Fields Summary
private final int
MAXHISTORY
private boolean
bLogToStdOut
private boolean
bEventLoggingEnabled
private PrintStream
psOldOut
private PrintStream
psOldErr
private PrintStream
psOut
private PrintStream
psErr
private List
logListeners
private org.gudy.azureus2.core3.util.AEDiagnosticsLogger
alertLogger
private List
alertListeners
private List
alertHistory
Constructors Summary
public LoggerImpl()
Initializes the Logger and sets up a file logger.


	         	 
	  
		doRedirects();
	
Methods Summary
public voidaddListener(org.gudy.azureus2.core3.logging.ILogAlertListener l)

		alertListeners.add(l);

		for (int i = 0; i < alertHistory.size(); i++) {
			LogAlert alert = (LogAlert) alertHistory.get(i);
			l.alertRaised(alert);
		}
	
public voidaddListener(org.gudy.azureus2.core3.logging.ILogEventListener aListener)

		logListeners.add(aListener);
	
public voiddoRedirects()
Set up stdout/stderr redirects

		try {
			if (System.out != psOut) {
				if (psOldOut == null)
					psOldOut = System.out;

				psOut = new PrintStream(new RedirectorStream(psOldOut, LogIDs.STDOUT,
						LogEvent.LT_INFORMATION));

				System.setOut(psOut);
			}

			if (System.err != psErr) {
				if (psOldErr == null)
					psOldErr = System.err;

				psErr = new PrintStream(new RedirectorStream(psOldErr, LogIDs.STDERR,
						LogEvent.LT_ERROR));

				System.setErr(psErr);
			}

		} catch (Throwable e) {
			Debug.printStackTrace(e);
		}
	
public java.io.PrintStreamgetOldStdErr()

		return psOldErr;
	
public voidinit()
Must be seperate from constructor, because the code may call a Logger.* method, which requires a loggerImpl to be not null.

		// temporarily set to true, to log any errors between now and setting
		// bEnabled properly.
		bEventLoggingEnabled = true;
		
		// Shorten from COConfigurationManager To make code more readable
		final ConfigurationManager config = ConfigurationManager.getInstance();

		boolean overrideLog = System.getProperty("azureus.overridelog") != null;
		if (overrideLog) {
			bEventLoggingEnabled = true;
		} else {
			bEventLoggingEnabled = config.getBooleanParameter("Logger.Enabled");

			config.addParameterListener("Logger.Enabled", new ParameterListener() {
				public void parameterChanged(String parameterName) {
					bEventLoggingEnabled = config.getBooleanParameter("Logger.Enabled");
				}
			});
		}

		alertLogger = AEDiagnostics.getLogger("alerts");
	
public booleanisEnabled()

		return bEventLoggingEnabled;
	
public voidlog(org.gudy.azureus2.core3.logging.LogAlert alert)

		String logText = "Alert:" + alert.entryType + ":" + alert.text;

		// Log all Alerts as Events
		LogEvent alertEvent = new LogEvent(LogIDs.ALERT, alert.entryType,
				logText);
		alertEvent.err = alert.err;
		Logger.log(alertEvent);

		if (alertLogger != null)
			alertLogger.log(logText);

		alertHistory.add(alert);

		if (alertHistory.size() > MAXHISTORY)
			alertHistory.remove(0);

		for (int i = 0; i < alertListeners.size(); i++) {
			try {
				Object listener = alertListeners.get(i);
				if (listener instanceof ILogAlertListener)
					((ILogAlertListener) listener).alertRaised(alert);
			} catch (Throwable f) {
				if (psOldErr != null) {
					psOldErr.println("Error while alerting: " + f.getMessage());
					f.printStackTrace(psOldErr);
				}
			}
		}
	
public voidlog(org.gudy.azureus2.core3.logging.LogEvent event)
Log an event

param
event event to log

		
		/**
		 * This highlights bits of code which log, but don't bother
		 * to check whether logging is enabled or not.
		 */ 
		//if (!bEventLoggingEnabled) {
		//	new Exception("No logging check done!").printStackTrace(psOldErr);
		//}
		
		if (bLogToStdOut && psOldOut != null)
			psOldOut.println(event.text);

		if (event.entryType == LogEvent.LT_ERROR) {
			if ( AEDiagnostics.isStartupComplete()){
					// more recursive horrors here if we try and log too early
				Debug.outDiagLoggerOnly("[" + event.logID + "] " + event.text);
			}
			if (psOldErr != null && event.logID != LogIDs.STDERR) {
				psOldErr.println("[" + event.logID + "] " + event.text);
			}
		}
		if (bEventLoggingEnabled)
			for (int i = 0; i < logListeners.size(); i++) {
				try {
					Object listener = logListeners.get(i);
					if (listener instanceof ILogEventListener)
						((ILogEventListener) listener).log(event);
				} catch (Throwable e) {
					if (psOldErr != null) {
						psOldErr.println("Error while logging: " + e.getMessage());
						e.printStackTrace(psOldErr);
					}
				}
			}

		// Write error to stderr, which will eventually get back here
		if (event.err != null && event.entryType == LogEvent.LT_ERROR){
			Debug.printStackTrace(event.err);
		}
	
public voidlogTextResource(org.gudy.azureus2.core3.logging.LogAlert alert)

		alert.text = MessageText.getString(alert.text);
		log(alert);
	
public voidlogTextResource(org.gudy.azureus2.core3.logging.LogAlert alert, java.lang.String[] params)

		alert.text = MessageText.getString(alert.text, params);
		log(alert);
	
public voidlogTextResource(org.gudy.azureus2.core3.logging.LogEvent event)

		event.text = MessageText.getString(event.text);
		log(event);
	
public voidlogTextResource(org.gudy.azureus2.core3.logging.LogEvent event, java.lang.String[] params)

		event.text = MessageText.getString(event.text, params);
		log(event);
	
public voidremoveListener(org.gudy.azureus2.core3.logging.ILogAlertListener l)

		alertListeners.remove(l);
	
public voidremoveListener(org.gudy.azureus2.core3.logging.ILogEventListener aListener)

		logListeners.remove(aListener);