Search in sources :

Example 1 with SpyPosition

use of io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition in project zilla by aklivity.

the class LogCommand method main.

public static void main(String[] args) throws Exception {
    final CommandLineParser parser = new DefaultParser();
    Options options = new Options();
    options.addOption(builder("h").longOpt("help").desc("print this message").build());
    options.addOption(builder().longOpt("version").build());
    options.addOption(builder("t").hasArg().required(false).longOpt("type").desc("streams* | streams-[nowait|zero|head|tail] | counters | queues | routes").build());
    options.addOption(builder("f").hasArgs().required(false).longOpt("frameTypes").desc("log specific frame types only, e.g BEGIN").build());
    options.addOption(builder("d").longOpt("directory").hasArg().desc("configuration directory").build());
    options.addOption(builder("v").longOpt("verbose").desc("verbose").build());
    options.addOption(builder("e").hasArgs().required(false).longOpt("extensionTypes").desc("log specific extension types only, e.g. tcp").build());
    options.addOption(builder("i").hasArg().longOpt("interval").desc("run command continuously at interval").build());
    options.addOption(builder("s").longOpt("separator").desc("include thousands separator in integer values").build());
    options.addOption(builder("a").hasArg().longOpt("affinity").desc("affinity mask").build());
    final CommandLine cmdline = parser.parse(options, args);
    final Logger out = System.out::printf;
    final boolean hasVersion = cmdline.hasOption("version");
    final boolean hasDirectory = cmdline.hasOption("directory");
    final boolean hasHelp = cmdline.hasOption("help");
    if (hasVersion) {
        out.printf("version: %s\n", LogCommand.class.getPackage().getSpecificationVersion());
    }
    if (hasHelp || !hasDirectory) {
        if (hasHelp || !hasVersion) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("log", options);
        }
    } else {
        String directory = cmdline.getOptionValue("directory");
        boolean verbose = cmdline.hasOption("verbose");
        boolean separator = cmdline.hasOption("separator");
        String type = cmdline.getOptionValue("type", "streams");
        final int interval = Integer.parseInt(cmdline.getOptionValue("interval", "0"));
        final long affinity = Long.parseLong(cmdline.getOptionValue("affinity", Long.toString(0xffff_ffff_ffff_ffffL)));
        Properties properties = new Properties();
        properties.setProperty(ENGINE_DIRECTORY.name(), directory);
        final EngineConfiguration config = new EngineConfiguration(new Configuration(), properties);
        Runnable command = null;
        final Matcher matcher = Pattern.compile("streams(?:-(?<option>[a-z]+))?").matcher(type);
        if (matcher.matches()) {
            final String option = matcher.group("option");
            final boolean continuous = !"nowait".equals(option);
            final SpyPosition position = continuous && option != null ? SpyPosition.valueOf(option.toUpperCase()) : SpyPosition.ZERO;
            final String[] frameTypes = cmdline.getOptionValues("frameTypes");
            final String[] extensionTypes = cmdline.getOptionValues("extensionTypes");
            final Predicate<String> hasExtensionType = extensionTypes == null && !verbose ? t -> false : s -> verbose || Arrays.asList(extensionTypes).contains(s);
            final Predicate<String> hasFrameTypes = frameTypes == null ? t -> true : Arrays.asList(frameTypes)::contains;
            command = new LogStreamsCommand(config, out, hasFrameTypes, hasExtensionType, verbose, continuous, affinity, position);
        } else if ("buffers".equals(type)) {
            command = new LogBuffersCommand(config, out, verbose);
        } else if ("budgets".equals(type)) {
            command = new LogBudgetsCommand(config, out, verbose, affinity);
        } else if ("counters".equals(type)) {
            command = new LogCountersCommand(config, out, verbose, separator);
        } else if ("queues".equals(type)) {
            command = new LogQueueDepthCommand(config, out, verbose, separator);
        }
        do {
            command.run();
            Thread.sleep(TimeUnit.SECONDS.toMillis(interval));
        } while (interval > 0);
    }
}
Also used : Arrays(java.util.Arrays) Properties(java.util.Properties) SpyPosition(io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition) CommandLineParser(org.apache.commons.cli.CommandLineParser) Predicate(java.util.function.Predicate) Options(org.apache.commons.cli.Options) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) HelpFormatter(org.apache.commons.cli.HelpFormatter) DefaultParser(org.apache.commons.cli.DefaultParser) TimeUnit(java.util.concurrent.TimeUnit) Matcher(java.util.regex.Matcher) Configuration(io.aklivity.zilla.runtime.engine.Configuration) CommandLine(org.apache.commons.cli.CommandLine) Pattern(java.util.regex.Pattern) ENGINE_DIRECTORY(io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DIRECTORY) Option.builder(org.apache.commons.cli.Option.builder) Options(org.apache.commons.cli.Options) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) Configuration(io.aklivity.zilla.runtime.engine.Configuration) Matcher(java.util.regex.Matcher) Properties(java.util.Properties) HelpFormatter(org.apache.commons.cli.HelpFormatter) EngineConfiguration(io.aklivity.zilla.runtime.engine.EngineConfiguration) CommandLineParser(org.apache.commons.cli.CommandLineParser) DefaultParser(org.apache.commons.cli.DefaultParser) SpyPosition(io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition) CommandLine(org.apache.commons.cli.CommandLine)

Aggregations

SpyPosition (io.aklivity.zilla.runtime.command.log.internal.spy.RingBufferSpy.SpyPosition)1 Configuration (io.aklivity.zilla.runtime.engine.Configuration)1 EngineConfiguration (io.aklivity.zilla.runtime.engine.EngineConfiguration)1 ENGINE_DIRECTORY (io.aklivity.zilla.runtime.engine.EngineConfiguration.ENGINE_DIRECTORY)1 Arrays (java.util.Arrays)1 Properties (java.util.Properties)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Option.builder (org.apache.commons.cli.Option.builder)1 Options (org.apache.commons.cli.Options)1