Search in sources :

Example 1 with LoggerChannel

use of com.biglybt.pif.logging.LoggerChannel in project BiglyBT by BiglySoftware.

the class RPRequestHandler method processRequestSupport.

protected RPReply processRequestSupport(RPRequest request, RPRequestAccessController access_controller) {
    try {
        RPObject object = request.getObject();
        String method = request.getMethod();
        if (object == null && method.equals("getSingleton")) {
            RPObject pi = request.createRemotePluginInterface(plugin_interface);
            RPReply reply = new RPReply(pi);
            return (reply);
        } else if (object == null && method.equals("getDownloads")) {
            RPPluginInterface pi = request.createRemotePluginInterface(plugin_interface);
            // short cut method for quick access to downloads
            // used by GTS
            RPObject dm = (RPObject) pi._process(new RPRequest(null, "getDownloadManager", null)).getResponse();
            RPReply rep = dm._process(new RPRequest(null, "getDownloads", null));
            rep.setProperty("azureus_name", pi.azureus_name);
            rep.setProperty("azureus_version", pi.azureus_version);
            return (rep);
        } else if (object == null) {
            throw new RPNoObjectIDException();
        } else {
            // System.out.println( "Request: con = " + request.getConnectionId() + ", req = " + request.getRequestId() + ", client = " + request.getClientIP());
            object = RPObject._lookupLocal(object._getOID());
            // _setLocal synchronizes the RP objects with their underlying
            // plugin objects
            object._setLocal();
            if (method.equals("_refresh")) {
                RPReply reply = new RPReply(object);
                return (reply);
            } else {
                String name = object._getName();
                if (access_controller != null) {
                    access_controller.checkAccess(name, request);
                }
                RPReply reply = object._process(request);
                if (name.equals("IPFilter") && method.equals("setInRangeAddressesAreAllowed[boolean]") && request.getClientIP() != null) {
                    String client_ip = request.getClientIP();
                    // problem here, if someone changes the mode here they'll lose their
                    // connection coz they'll be denied access :)
                    boolean b = ((Boolean) request.getParams()[0]).booleanValue();
                    LoggerChannel[] channels = plugin_interface.getLogger().getChannels();
                    IPFilter filter = plugin_interface.getIPFilter();
                    if (b) {
                        if (filter.isInRange(client_ip)) {
                            for (int i = 0; i < channels.length; i++) {
                                channels[i].log(LoggerChannel.LT_INFORMATION, "Adding range for client '" + client_ip + "' as allow/deny flag changed to allow");
                            }
                            filter.createAndAddRange("auto-added for remote interface", client_ip, client_ip, false);
                            filter.save();
                            plugin_interface.getPluginconfig().save();
                        }
                    } else {
                        IPRange[] ranges = filter.getRanges();
                        for (int i = 0; i < ranges.length; i++) {
                            if (ranges[i].isInRange(client_ip)) {
                                for (int j = 0; j < channels.length; j++) {
                                    channels[j].log(LoggerChannel.LT_INFORMATION, "deleting range '" + ranges[i].getStartIP() + "-" + ranges[i].getEndIP() + "' for client '" + client_ip + "' as allow/deny flag changed to deny");
                                }
                                ranges[i].delete();
                            }
                        }
                        filter.save();
                        plugin_interface.getPluginconfig().save();
                    }
                }
                return (reply);
            }
        }
    } catch (RPException e) {
        return (new RPReply(e));
    } catch (Exception e) {
        throw new RPInternalProcessException(e);
    }
}
Also used : RPNoObjectIDException(com.biglybt.pifimpl.remote.rpexceptions.RPNoObjectIDException) IPFilter(com.biglybt.pif.ipfilter.IPFilter) RPInternalProcessException(com.biglybt.pifimpl.remote.rpexceptions.RPInternalProcessException) IPRange(com.biglybt.pif.ipfilter.IPRange) RPInternalProcessException(com.biglybt.pifimpl.remote.rpexceptions.RPInternalProcessException) RPNoObjectIDException(com.biglybt.pifimpl.remote.rpexceptions.RPNoObjectIDException) LoggerChannel(com.biglybt.pif.logging.LoggerChannel)

Example 2 with LoggerChannel

use of com.biglybt.pif.logging.LoggerChannel in project BiglyBT by BiglySoftware.

the class UPnPPlugin method startUp.

protected void startUp() {
    if (upnp != null) {
        // already started up, must have been re-enabled
        refreshMappings();
        return;
    }
    final LoggerChannel core_log = plugin_interface.getLogger().getChannel("UPnP Core");
    try {
        upnp = UPnPFactory.getSingleton(new UPnPAdapter() {

            Set exception_traces = new HashSet();

            @Override
            public SimpleXMLParserDocument parseXML(String data) throws SimpleXMLParserDocumentException {
                return (plugin_interface.getUtilities().getSimpleXMLParserDocumentFactory().create(data));
            }

            @Override
            public ResourceDownloaderFactory getResourceDownloaderFactory() {
                return (plugin_interface.getUtilities().getResourceDownloaderFactory());
            }

            @Override
            public UTTimer createTimer(String name) {
                return (plugin_interface.getUtilities().createTimer(name, true));
            }

            @Override
            public void createThread(String name, Runnable runnable) {
                plugin_interface.getUtilities().createThread(name, runnable);
            }

            @Override
            public Comparator getAlphanumericComparator() {
                return (plugin_interface.getUtilities().getFormatters().getAlphanumericComparator(true));
            }

            @Override
            public void trace(String str) {
                core_log.log(str);
                if (trace_to_log.getValue()) {
                    upnp_log_listener.log(str);
                }
            }

            @Override
            public void log(Throwable e) {
                String nested = Debug.getNestedExceptionMessage(e);
                if (!exception_traces.contains(nested)) {
                    exception_traces.add(nested);
                    if (exception_traces.size() > 128) {
                        exception_traces.clear();
                    }
                    core_log.log(e);
                } else {
                    core_log.log(nested);
                }
            }

            @Override
            public void log(String str) {
                log.log(str);
            }

            @Override
            public String getTraceDir() {
                return (plugin_interface.getUtilities().getUserDir());
            }
        }, getSelectedInterfaces());
        upnp.addRootDeviceListener(this);
        upnp_log_listener = new UPnPLogListener() {

            @Override
            public void log(String str) {
                log.log(str);
            }

            @Override
            public void logAlert(String str, boolean error, int type) {
                boolean logged = false;
                if (alert_device_probs_param.getValue()) {
                    if (type == UPnPLogListener.TYPE_ALWAYS) {
                        log.logAlertRepeatable(error ? LoggerChannel.LT_ERROR : LoggerChannel.LT_WARNING, str);
                        logged = true;
                    } else {
                        boolean do_it = false;
                        if (type == UPnPLogListener.TYPE_ONCE_EVER) {
                            byte[] fp = plugin_interface.getUtilities().getSecurityManager().calculateSHA1(str.getBytes());
                            String key = "upnp.alert.fp." + plugin_interface.getUtilities().getFormatters().encodeBytesToString(fp);
                            PluginConfig pc = plugin_interface.getPluginconfig();
                            if (!pc.getPluginBooleanParameter(key, false)) {
                                pc.setPluginParameter(key, true);
                                do_it = true;
                            }
                        } else {
                            do_it = true;
                        }
                        if (do_it) {
                            log.logAlert(error ? LoggerChannel.LT_ERROR : LoggerChannel.LT_WARNING, str);
                            logged = true;
                        }
                    }
                }
                if (!logged) {
                    log.log(str);
                }
            }
        };
        upnp.addLogListener(upnp_log_listener);
        mapping_manager.addListener(new UPnPMappingManagerListener() {

            @Override
            public void mappingAdded(UPnPMapping mapping) {
                addMapping(mapping);
            }
        });
        UPnPMapping[] upnp_mappings = mapping_manager.getMappings();
        for (int i = 0; i < upnp_mappings.length; i++) {
            addMapping(upnp_mappings[i]);
        }
        setNATPMPEnableState();
    } catch (Throwable e) {
        log.log(e);
    }
}
Also used : PluginConfig(com.biglybt.pif.PluginConfig) LoggerChannel(com.biglybt.pif.logging.LoggerChannel)

Example 3 with LoggerChannel

use of com.biglybt.pif.logging.LoggerChannel in project BiglyBT by BiglySoftware.

the class Log method execute.

@Override
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() {

                        @Override
                        public void messageLogged(int type, String content) {
                            ci.out.println("[" + name + "] " + content);
                        }

                        @Override
                        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)");
    }
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) DenyAllFilter(org.apache.log4j.varia.DenyAllFilter) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) LoggerChannel(com.biglybt.pif.logging.LoggerChannel)

Aggregations

LoggerChannel (com.biglybt.pif.logging.LoggerChannel)3 PluginConfig (com.biglybt.pif.PluginConfig)1 IPFilter (com.biglybt.pif.ipfilter.IPFilter)1 IPRange (com.biglybt.pif.ipfilter.IPRange)1 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)1 RPInternalProcessException (com.biglybt.pifimpl.remote.rpexceptions.RPInternalProcessException)1 RPNoObjectIDException (com.biglybt.pifimpl.remote.rpexceptions.RPNoObjectIDException)1 IOException (java.io.IOException)1 Appender (org.apache.log4j.Appender)1 ConsoleAppender (org.apache.log4j.ConsoleAppender)1 FileAppender (org.apache.log4j.FileAppender)1 PatternLayout (org.apache.log4j.PatternLayout)1 DenyAllFilter (org.apache.log4j.varia.DenyAllFilter)1