Move to kernel style SPDX license identifiers
[lttng-ust.git] / doc / examples / java-jul / FilterChangeListenerExample.java
CommitLineData
4fb826b1 1/*
c0c0989a 2 * SPDX-License-Identifier: MIT
4fb826b1 3 *
c0c0989a
MJ
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
4fb826b1
AM
6 */
7
8import java.io.IOException;
9import java.util.logging.Level;
10
11import org.lttng.ust.agent.ILttngHandler;
12import org.lttng.ust.agent.filter.FilterChangeNotifier;
13import org.lttng.ust.agent.filter.IFilterChangeListener;
14import org.lttng.ust.agent.jul.LttngLogHandler;
15import org.lttng.ust.agent.session.EventRule;
16import org.lttng.ust.agent.session.LogLevelSelector;
17
18/**
19 * Example usage of a {@link IFilterChangeListener}.
20 *
21 * This listener will simply print to stdout the notifications it receives. To
22 * try it, run the program, then issue "lttng enable-event" and
23 * "lttng disable-event" commands for the JUL domain.
24 *
25 * @author Alexandre Montplaisir
26 */
a3454bfd 27public class FilterChangeListenerExample {
4fb826b1
AM
28
29 private static class ExampleFilterChangeListener implements IFilterChangeListener {
30
31 @Override
32 public void eventRuleAdded(EventRule rule) {
33 System.out.println();
34 System.out.println("New event rule enabled:");
35 System.out.println("Event name: " + rule.getEventName());
36 System.out.println(printLogLevel(rule.getLogLevelSelector()));
37 System.out.println("Filter string: " + rule.getFilterString());
38 }
39
40 @Override
41 public void eventRuleRemoved(EventRule rule) {
42 System.out.println();
43 System.out.println("Event rule disabled:");
44 System.out.println("Event name: " + rule.getEventName());
45 System.out.println(printLogLevel(rule.getLogLevelSelector()));
46 System.out.println("Filter string: " + rule.getFilterString());
47 }
48
49 /**
50 * Convenience method to print integer log level values into their JUL
51 * equivalent.
52 */
53 private static String printLogLevel(LogLevelSelector lls) {
54 String llname = Level.parse(String.valueOf(lls.getLogLevel())).getName();
55 return "Log level: " + llname + ", filter type: " + lls.getLogLevelType().toString();
56 }
57 }
58
59 /**
60 * Run the program.
61 *
62 * @param args
63 * Command-line arguments (not used)
64 * @throws IOException
65 */
66 public static void main(String args[]) throws IOException {
67 /* We need at least one log handler to activate the LTTng agent */
68 ILttngHandler handler = new LttngLogHandler();
69
70 /* Create a listener and register it to the manager */
71 IFilterChangeListener listener = new ExampleFilterChangeListener();
72 FilterChangeNotifier.getInstance().registerListener(listener);
73
74 System.out.println("Press Enter to finish.");
75 System.in.read();
76
77 /* Unregister the listener */
78 FilterChangeNotifier.getInstance().unregisterListener(listener);
79
80 /* Cleanup the log handler we created */
81 handler.close();
82 }
a3454bfd 83}
This page took 0.027394 seconds and 4 git commands to generate.