FileDocCategorySizeDatePackage
Log.javaAPI DocAzureus 3.0.3.46058Thu Sep 22 14:50:36 BST 2005org.gudy.azureus2.ui.console.commands

Log

public class Log extends OptionsConsoleCommand
author
Tobias Minich

Fields Summary
private Map
channel_listener_map
Constructors Summary
public Log()

	
	 
	
		super( new String[] { "log", "l" });
	
Methods Summary
public static voidcommandLogtest(org.gudy.azureus2.ui.console.ConsoleInput ci, java.util.List args)

		Logger.getLogger("azureus2").fatal("Logging test" + (((args == null) || (args.isEmpty())) ? "" : ": " + args.get(0).toString()));
	
public voidexecute(java.lang.String commandName, org.gudy.azureus2.ui.console.ConsoleInput ci, org.apache.commons.cli.CommandLine commandLine)

		Appender con = Logger.getRootLogger().getAppender("ConsoleAppender");
		List args = commandLine.getArgList();
		if ((con != null) && (!args.isEmpty())) {
			String subcommand = (String) args.get(0);
			if ("off".equalsIgnoreCase(subcommand) ) {
				if ( args.size() == 1 ){
					con.addFilter(new DenyAllFilter());
					ci.out.println("> Console logging off");
				}else{
					
					String	name = (String)args.get(1);
					
					Object[]	entry  = (Object[])channel_listener_map.remove( name );
					
					if ( entry == null ){
						
						ci.out.println( "> Channel '" + name + "' not being logged" );
						
					}else{
						
						((LoggerChannel)entry[0]).removeListener((LoggerChannelListener)entry[1]);
						
						ci.out.println( "> Channel '" + name + "' logging off" );
					}
				}
			} else if ("on".equalsIgnoreCase(subcommand) ) {
				
				if ( args.size() == 1 ){

					if( commandLine.hasOption('f") )
					{
						// send log output to a file
						String filename = commandLine.getOptionValue('f");
						
						try
						{
							Appender newAppender = new FileAppender(new PatternLayout("%d{ISO8601} %c{1}-%p: %m%n"), filename, true);
							newAppender.setName("ConsoleAppender");
							Logger.getRootLogger().removeAppender(con);
							Logger.getRootLogger().addAppender(newAppender);
							ci.out.println("> Logging to filename: " + filename);
						} catch (IOException e)
						{
							ci.out.println("> Unable to log to file: " + filename + ": " + e);
						}					
					}
					else
					{
						if( ! (con instanceof ConsoleAppender) )
						{
							Logger.getRootLogger().removeAppender(con);
							con = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
							con.setName("ConsoleAppender");
						    Logger.getRootLogger().addAppender(con);
						}
						// switch back to console appender
						ci.out.println("> Console logging on");
					}
					
					con.clearFilters();
				}else{
					// hack - dunno how to do plugin-specific logging using these damn appenders..
					
					Map	channel_map = getChannelMap( ci );

					final String	name = (String)args.get(1);
					
					LoggerChannel	channel = (LoggerChannel)channel_map.get(name);
					
					if ( channel == null ){
						
						ci.out.println( "> Channel '" + name + "' not found" );
						
					}else if ( channel_listener_map.get(name) != null ){
						
						ci.out.println( "> Channel '" + name + "' already being logged" );

					}else{
						
						LoggerChannelListener	l = 
							new LoggerChannelListener()
							{
								public void
								messageLogged(
									int		type,
									String	content )
								{
									ci.out.println( "["+name+"] "+ content );
								}
								
								public void
								messageLogged(
									String		str,
									Throwable	error )
								{
									ci.out.println( "["+name+"] "+ str );
									
									error.printStackTrace( ci.out );
								}
							};
							
						channel.addListener( l );
						
						channel_listener_map.put( name, new Object[]{ channel, l });
						
						ci.out.println( "> Channel '" + name + "' on" );
					}
					
				}
			}else if ( subcommand.equalsIgnoreCase("list" )){
				
				Map	channel_map = getChannelMap( ci );
				
				Iterator it = channel_map.keySet().iterator();
				
				while( it.hasNext()){
					
					String	name = (String)it.next();
					
					ci.out.println( "  " + name + " [" + ( channel_listener_map.get( name ) == null?"off":"on") + "]" );
				}
			} else {
				
				ci.out.println("> Command 'log': Subcommand '" + subcommand + "' unknown.");
			}
		} else {
			ci.out.println("> Console logger not found or missing subcommand for 'log'\r\n> log syntax: log [-f filename] (on [name]|off [name]|list)");
		}
	
protected java.util.MapgetChannelMap(org.gudy.azureus2.ui.console.ConsoleInput ci)

		Map channel_map = new HashMap();
		
		PluginInterface[]	pis = ci.azureus_core.getPluginManager().getPluginInterfaces();
		
		for (int i=0;i<pis.length;i++){
		
			LoggerChannel[]	logs = pis[i].getLogger().getChannels();
		
			if ( logs.length > 0 ){
								
				if ( logs.length == 1 ){
					
					channel_map.put( pis[i].getPluginName(),logs[0] );

				}else{
					
					for (int j=0;j<logs.length;j++){
					
						channel_map.put( pis[i].getPluginName() + "." + logs[j].getName(), logs[j] );
					}
				}
			}
		}
		
		return( channel_map );
	
public java.lang.StringgetCommandDescriptions()

		return("log [-f filename] (on [name]|off [name]|list)\t\t\tl\tTurn on/off console logging");
	
protected org.apache.commons.cli.OptionsgetOptions()

		Options options = new Options();
		options.addOption( new Option("f", "filename", true, "filename to write log to"));
		return options;