public void attachLoggerChannel(LoggerChannel channel) { channel.addListener( new LoggerChannelListener() { public void messageLogged(String message, Throwable t) { messageLogged(LoggerChannel.LT_ERROR, message, t); } public void messageLogged(int logtype, String message) { messageLogged(logtype, message, null); } public void messageLogged(int logtype, String message, Throwable t) { String log_type_s = null; switch (logtype) { case LoggerChannel.LT_WARNING: log_type_s = "warning"; break; case LoggerChannel.LT_ERROR: log_type_s = "error"; break; } if (log_type_s != null) { String prefix = MessageText.getString("AlertMessageBox." + log_type_s); log.appendText("[" + prefix.toUpperCase() + "] "); } log.appendText(message + "\n"); if (t != null) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); log.appendText(sw.toString() + "\n"); } } }); }
public void execute(String commandName, final ConsoleInput ci, 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)"); } }
public void initialize(PluginInterface _plugin_interface) { plugin_interface = _plugin_interface; log = plugin_interface.getLogger().getTimeStampedChannel("UPnP"); log.setDiagnostic(); log.setForce(true); UIManager ui_manager = plugin_interface.getUIManager(); final BasicPluginViewModel model = ui_manager.createBasicPluginViewModel("UPnP"); model.setConfigSectionID(UPNP_PLUGIN_CONFIGSECTION_ID); BasicPluginConfigModel upnp_config = ui_manager.createBasicPluginConfigModel( ConfigSection.SECTION_PLUGINS, UPNP_PLUGIN_CONFIGSECTION_ID); // NATPMP BasicPluginConfigModel natpmp_config = ui_manager.createBasicPluginConfigModel( UPNP_PLUGIN_CONFIGSECTION_ID, NATPMP_PLUGIN_CONFIGSECTION_ID); natpmp_config.addLabelParameter2("natpmp.info"); ActionParameter natpmp_wiki = natpmp_config.addActionParameter2("Utils.link.visit", "MainWindow.about.internet.wiki"); natpmp_wiki.setStyle(ActionParameter.STYLE_LINK); natpmp_wiki.addListener( new ParameterListener() { public void parameterChanged(Parameter param) { try { plugin_interface.getUIManager().openURL(new URL("http://wiki.vuze.com/w/NATPMP")); } catch (Throwable e) { e.printStackTrace(); } } }); natpmp_enable_param = natpmp_config.addBooleanParameter2("natpmp.enable", "natpmp.enable", false); nat_pmp_router = natpmp_config.addStringParameter2("natpmp.routeraddress", "natpmp.routeraddress", ""); natpmp_enable_param.addListener( new ParameterListener() { public void parameterChanged(Parameter param) { setNATPMPEnableState(); } }); natpmp_enable_param.addEnabledOnSelection(nat_pmp_router); // UPNP upnp_config.addLabelParameter2("upnp.info"); upnp_config.addHyperlinkParameter2("upnp.wiki_link", "http://wiki.vuze.com/w/UPnP"); upnp_enable_param = upnp_config.addBooleanParameter2("upnp.enable", "upnp.enable", true); grab_ports_param = upnp_config.addBooleanParameter2("upnp.grabports", "upnp.grabports", false); release_mappings_param = upnp_config.addBooleanParameter2("upnp.releasemappings", "upnp.releasemappings", true); ActionParameter refresh_param = upnp_config.addActionParameter2("upnp.refresh.label", "upnp.refresh.button"); refresh_param.addListener( new ParameterListener() { public void parameterChanged(Parameter param) { UPnPPlugin.this.refreshMappings(); } }); // Auto-refresh mappings every minute when enabled. final BooleanParameter auto_refresh_on_bad_nat_param = upnp_config.addBooleanParameter2( "upnp.refresh_on_bad_nat", "upnp.refresh_mappings_on_bad_nat", false); plugin_interface .getUtilities() .createTimer("upnp mapping auto-refresh", true) .addPeriodicEvent( 1 * 60 * 1000, new UTTimerEventPerformer() { private long last_bad_nat = 0; public void perform(UTTimerEvent event) { if (upnp == null) { return; } if (!auto_refresh_on_bad_nat_param.getValue()) { return; } if (!upnp_enable_param.getValue()) { return; } int status = plugin_interface.getConnectionManager().getNATStatus(); if (status == ConnectionManager.NAT_BAD) { // Only try to refresh the mappings if this is the first bad NAT // message we've been given in the last 15 minutes - we don't want // to endlessly retry performing the mappings long now = plugin_interface.getUtilities().getCurrentSystemTime(); if (last_bad_nat + (15 * 60 * 1000) < now) { last_bad_nat = now; log.log( LoggerChannel.LT_WARNING, "NAT status is firewalled - trying to refresh UPnP mappings"); refreshMappings(true); } } } }); upnp_config.addLabelParameter2("blank.resource"); alert_success_param = upnp_config.addBooleanParameter2("upnp.alertsuccess", "upnp.alertsuccess", false); alert_other_port_param = upnp_config.addBooleanParameter2( "upnp.alertothermappings", "upnp.alertothermappings", true); alert_device_probs_param = upnp_config.addBooleanParameter2( "upnp.alertdeviceproblems", "upnp.alertdeviceproblems", true); selected_interfaces_param = upnp_config.addStringParameter2("upnp.selectedinterfaces", "upnp.selectedinterfaces", ""); selected_addresses_param = upnp_config.addStringParameter2("upnp.selectedaddresses", "upnp.selectedaddresses", ""); ignore_bad_devices = upnp_config.addBooleanParameter2("upnp.ignorebaddevices", "upnp.ignorebaddevices", true); ignored_devices_list = upnp_config.addLabelParameter2("upnp.ignorebaddevices.info"); ActionParameter reset_param = upnp_config.addActionParameter2( "upnp.ignorebaddevices.reset", "upnp.ignorebaddevices.reset.action"); reset_param.addListener( new ParameterListener() { public void parameterChanged(Parameter param) { PluginConfig pc = plugin_interface.getPluginconfig(); for (int i = 0; i < STATS_KEYS.length; i++) { String key = "upnp.device.stats." + STATS_KEYS[i]; pc.setPluginMapParameter(key, new HashMap()); } pc.setPluginMapParameter("upnp.device.ignorelist", new HashMap()); updateIgnoreList(); } }); trace_to_log = upnp_config.addBooleanParameter2("upnp.trace_to_log", "upnp.trace_to_log", false); final boolean enabled = upnp_enable_param.getValue(); upnp_enable_param.addEnabledOnSelection(alert_success_param); upnp_enable_param.addEnabledOnSelection(grab_ports_param); upnp_enable_param.addEnabledOnSelection(refresh_param); upnp_enable_param.addEnabledOnSelection(alert_other_port_param); upnp_enable_param.addEnabledOnSelection(alert_device_probs_param); upnp_enable_param.addEnabledOnSelection(release_mappings_param); upnp_enable_param.addEnabledOnSelection(selected_interfaces_param); upnp_enable_param.addEnabledOnSelection(selected_addresses_param); upnp_enable_param.addEnabledOnSelection(ignore_bad_devices); upnp_enable_param.addEnabledOnSelection(ignored_devices_list); upnp_enable_param.addEnabledOnSelection(reset_param); upnp_enable_param.addEnabledOnSelection(trace_to_log); natpmp_enable_param.setEnabled(enabled); model.getStatus().setText(enabled ? "Running" : "Disabled"); upnp_enable_param.addListener( new ParameterListener() { public void parameterChanged(Parameter p) { boolean e = upnp_enable_param.getValue(); natpmp_enable_param.setEnabled(e); model.getStatus().setText(e ? "Running" : "Disabled"); if (e) { startUp(); } else { closeDown(true); } setNATPMPEnableState(); } }); model.getActivity().setVisible(false); model.getProgress().setVisible(false); log.addListener( new LoggerChannelListener() { public void messageLogged(int type, String message) { model.getLogArea().appendText(message + "\n"); } public void messageLogged(String str, Throwable error) { model.getLogArea().appendText(error.toString() + "\n"); } }); // startup() used to be called on initializationComplete() // Moved to delayed task because rootDeviceFound can take // a lot of CPU cycle. Let's hope nothing breaks DelayedTask dt = plugin_interface .getUtilities() .createDelayedTask( new Runnable() { public void run() { if (enabled) { updateIgnoreList(); startUp(); } } }); dt.queue(); plugin_interface.addListener( new PluginListener() { public void initializationComplete() {} public void closedownInitiated() { if (services.size() == 0) { plugin_interface.getPluginconfig().setPluginParameter("plugin.info", ""); } } public void closedownComplete() { closeDown(true); } }); }