FileDocCategorySizeDatePackage
TorrentLog.javaAPI DocAzureus 3.0.3.44536Tue Dec 26 16:56:02 GMT 2006org.gudy.azureus2.ui.console.commands

TorrentLog

public class TorrentLog extends TorrentCommand implements org.gudy.azureus2.core3.logging.ILogEventListener
author
TuxPaper
created
Dec 21, 2006

Fields Summary
private static int
MODE_OFF
private static int
MODE_ON
private static int
MODE_FLIP
private static SimpleDateFormat
dateFormatter
private static FieldPosition
formatPos
private int
mode
private org.gudy.azureus2.core3.util.AEMonitor
dms_mon
private ArrayList
dms
private boolean
gm_listener_added
Constructors Summary
public TorrentLog()

param
commandNames
param
action

	
	 
		dateFormatter = new SimpleDateFormat("[h:mm:ss.SSS] ");
		formatPos = new FieldPosition(0);
	
		super(new String[] { "tlog", "tl"
		}, "Torrent Logging");
	
Methods Summary
public voidexecute(java.lang.String commandName, org.gudy.azureus2.ui.console.ConsoleInput ci, java.util.List args)

		mode = MODE_ON;
		Vector newargs = new Vector(args);
		if (newargs.isEmpty()) {
			mode = MODE_FLIP;
		} else if (newargs.contains("off")) {
			newargs.removeElement("off");
			mode = MODE_OFF;
		} else if (!newargs.contains("on")) {
			mode = MODE_FLIP;
		} 
		super.execute(commandName, ci, args);
	
public java.lang.StringgetCommandDescriptions()

		return "tl [on|off]\tTorrentLogging";
	
public voidlog(org.gudy.azureus2.core3.logging.LogEvent event)

		boolean bMatch = false;

		if (event.relatedTo == null) {
			return;
		}

		try {
			dms_mon.enter();

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

				if (obj == null)
					continue;

				for (int j = 0; !bMatch && j < dms.size(); j++) {
					if (obj instanceof LogRelation) {
						//System.err.println(obj.getClass().getSimpleName() + " is Logrelation");

						Object newObj = ((LogRelation) obj).queryForClass(DownloadManager.class);
						if (newObj != null)
							obj = newObj;
					}

					//System.err.println(obj.getClass().getName() + " matches " + filter[j].getClass().getSimpleName() + "?");

					if (obj == dms.get(j))
						bMatch = true;
				} // for filter
			} // for relatedTo

		} finally {
			dms_mon.exit();
		}

		if (bMatch) {
			final StringBuffer buf = new StringBuffer();
			dateFormatter.format(event.timeStamp, buf, formatPos);
			buf.append("{").append(event.logID).append("} ");

			buf.append(event.text);
			if (event.relatedTo != null) {
				buf.append("; \t| ");
				for (int j = 0; j < event.relatedTo.length; j++) {
					Object obj = event.relatedTo[j];
					if (j > 0)
						buf.append("; ");
					if (obj instanceof LogRelation) {
						buf.append(((LogRelation) obj).getRelationText());
					} else if (obj != null) {
						buf.append(obj.getClass().getName()).append(": '").append(
								obj.toString()).append("'");
					}
				}
			}
			System.out.println(buf.toString());
		}
	
protected booleanperformCommand(org.gudy.azureus2.ui.console.ConsoleInput ci, org.gudy.azureus2.core3.download.DownloadManager dm, java.util.List args)

		try {
			dms_mon.enter();
			
				// defer this so that a non-running core doesn't prevent console ui init
			
			if ( !gm_listener_added ){
				
				gm_listener_added = true;
				
				GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager();
				gm.addListener(new GlobalManagerAdapter() {
					public void downloadManagerRemoved(DownloadManager dm) {
						dms.remove(dm);
					}
				}, false);
			}
			
			boolean turnOn;
			if (mode == MODE_FLIP) {
				turnOn = !dms.contains(dm);
			} else {
				turnOn = mode == MODE_ON;
			}

			if (turnOn) {
				ci.out.print("->on] ");
				if (dms.contains(dm)) {
					return true;
				}
				dms.add(dm);
				if (dms.size() == 1) {
					Logger.addListener(this);
				}
			} else {
				ci.out.print("->off] ");
				dms.remove(dm);
				if (dms.size() == 0) {
					Logger.removeListener(this);
				}
			}
		} catch (Exception e) {
			e.printStackTrace(ci.out);
			return false;
		} finally {
			dms_mon.exit();
		}
		return true;