Search in sources :

Example 11 with LoggerChannelListener

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

the class PluginLauncherImpl method launch.

public static void launch(String[] args) {
    if (Launcher.checkAndLaunch(PluginLauncherImpl.class, args))
        return;
    // This *has* to be done first as it sets system properties that are read and cached by Java
    COConfigurationManager.preInitialise();
    final LoggerChannelListener listener = new LoggerChannelListener() {

        @Override
        public void messageLogged(int type, String content) {
            log(content, false);
        }

        @Override
        public void messageLogged(String str, Throwable error) {
            log(str, true);
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            error.printStackTrace(pw);
            pw.flush();
            log(sw.toString(), true);
        }

        protected synchronized void log(String str, boolean stdout) {
            File log_file = getApplicationFile("launch.log");
            PrintWriter pw = null;
            try {
                pw = new PrintWriter(new FileWriter(log_file, true));
                if (str.endsWith("\n")) {
                    if (stdout) {
                        System.err.print("PluginLauncher: " + str);
                    }
                    pw.print(str);
                } else {
                    if (stdout) {
                        System.err.println("PluginLauncher: " + str);
                    }
                    pw.println(str);
                }
            } catch (Throwable e) {
            } finally {
                if (pw != null) {
                    pw.close();
                }
            }
        }
    };
    LaunchablePlugin[] launchables = findLaunchablePlugins(listener);
    if (launchables.length == 0) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "No launchable plugins found");
        return;
    } else if (launchables.length > 1) {
        listener.messageLogged(LoggerChannel.LT_ERROR, "Multiple launchable plugins found, running first");
    }
    try {
        // set default details for restarter
        SystemProperties.setApplicationEntryPoint("com.biglybt.pif.PluginLauncher");
        launchables[0].setDefaults(args);
        if (PluginSingleInstanceHandler.process(listener, args)) {
            return;
        }
        // we have to run the core startup on a separate thread and then effectively pass "this thread"
        // through to the launchable "process" method
        Thread core_thread = new Thread("PluginLauncher") {

            @Override
            public void run() {
                try {
                    // give 'process' call below some time to start up
                    Thread.sleep(500);
                    Core core = CoreFactory.create();
                    core.start();
                } catch (Throwable e) {
                    listener.messageLogged("PluginLauncher: launch fails", e);
                }
            }
        };
        core_thread.setDaemon(true);
        core_thread.start();
        boolean restart = false;
        boolean process_succeeded = false;
        try {
            restart = launchables[0].process();
            process_succeeded = true;
        } finally {
            try {
                if (restart) {
                    CoreFactory.getSingleton().restart();
                } else {
                    CoreFactory.getSingleton().stop();
                }
            } catch (Throwable e) {
                if (process_succeeded) {
                    throw (e);
                }
            }
        }
    } catch (Throwable e) {
        listener.messageLogged("PluginLauncher: launch fails", e);
    }
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) LaunchablePlugin(com.biglybt.pif.LaunchablePlugin) Core(com.biglybt.core.Core)

Example 12 with LoggerChannelListener

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

the class LoggerChannelImpl method notifyListeners.

private void notifyListeners(String listenersText, Throwable error) {
    for (int i = 0; i < listeners.size(); i++) {
        try {
            LoggerChannelListener l = (LoggerChannelListener) listeners.get(i);
            l.messageLogged(listenersText, error);
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    }
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener)

Example 13 with LoggerChannelListener

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

the class LoggerChannelImpl method logAlert.

// Alert Functions
// ===============
protected void logAlert(int alert_type, String message, boolean repeatable) {
    // output as log message to any listeners
    for (int i = 0; i < listeners.size(); i++) {
        try {
            ((LoggerChannelListener) listeners.get(i)).messageLogged(alert_type, addTimeStamp(message));
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
    }
    if (!no_output) {
        int at;
        switch(alert_type) {
            case LoggerChannel.LT_INFORMATION:
                {
                    at = LogAlert.AT_INFORMATION;
                    break;
                }
            case LoggerChannel.LT_WARNING:
                {
                    at = LogAlert.AT_WARNING;
                    break;
                }
            default:
                {
                    at = LogAlert.AT_ERROR;
                    break;
                }
        }
        com.biglybt.core.logging.Logger.log(new LogAlert(repeatable, at, message));
    }
}
Also used : LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) LogAlert(com.biglybt.core.logging.LogAlert)

Example 14 with LoggerChannelListener

use of com.biglybt.pif.logging.LoggerChannelListener 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

LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)14 UIManager (com.biglybt.pif.ui.UIManager)6 BasicPluginConfigModel (com.biglybt.pif.ui.model.BasicPluginConfigModel)5 BasicPluginViewModel (com.biglybt.pif.ui.model.BasicPluginViewModel)5 DelayedTask (com.biglybt.pif.utils.DelayedTask)3 PluginListener (com.biglybt.pif.PluginListener)2 UTTimerEvent (com.biglybt.pif.utils.UTTimerEvent)2 UTTimerEventPerformer (com.biglybt.pif.utils.UTTimerEventPerformer)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Core (com.biglybt.core.Core)1 DHT (com.biglybt.core.dht.DHT)1 DHTLogger (com.biglybt.core.dht.DHTLogger)1 DHTControlActivity (com.biglybt.core.dht.control.DHTControlActivity)1 DHTControlContact (com.biglybt.core.dht.control.DHTControlContact)1 DHTNATPuncher (com.biglybt.core.dht.nat.DHTNATPuncher)1 DHTRouterContact (com.biglybt.core.dht.router.DHTRouterContact)1 DHTTransportContact (com.biglybt.core.dht.transport.DHTTransportContact)1 DHTTransportFullStats (com.biglybt.core.dht.transport.DHTTransportFullStats)1 DHTTransportUDP (com.biglybt.core.dht.transport.udp.DHTTransportUDP)1