Commit | Line | Data |
---|---|---|
4fb826b1 AM |
1 | /* |
2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to | |
6 | * deal in the Software without restriction, including without limitation the | |
7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
8 | * sell copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
20 | * IN THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | import java.io.IOException; | |
24 | import java.util.logging.Level; | |
25 | ||
26 | import org.lttng.ust.agent.ILttngHandler; | |
27 | import org.lttng.ust.agent.filter.FilterChangeNotifier; | |
28 | import org.lttng.ust.agent.filter.IFilterChangeListener; | |
29 | import org.lttng.ust.agent.jul.LttngLogHandler; | |
30 | import org.lttng.ust.agent.session.EventRule; | |
31 | import org.lttng.ust.agent.session.LogLevelSelector; | |
32 | ||
33 | /** | |
34 | * Example usage of a {@link IFilterChangeListener}. | |
35 | * | |
36 | * This listener will simply print to stdout the notifications it receives. To | |
37 | * try it, run the program, then issue "lttng enable-event" and | |
38 | * "lttng disable-event" commands for the JUL domain. | |
39 | * | |
40 | * @author Alexandre Montplaisir | |
41 | */ | |
a3454bfd | 42 | public class FilterChangeListenerExample { |
4fb826b1 AM |
43 | |
44 | private static class ExampleFilterChangeListener implements IFilterChangeListener { | |
45 | ||
46 | @Override | |
47 | public void eventRuleAdded(EventRule rule) { | |
48 | System.out.println(); | |
49 | System.out.println("New event rule enabled:"); | |
50 | System.out.println("Event name: " + rule.getEventName()); | |
51 | System.out.println(printLogLevel(rule.getLogLevelSelector())); | |
52 | System.out.println("Filter string: " + rule.getFilterString()); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public void eventRuleRemoved(EventRule rule) { | |
57 | System.out.println(); | |
58 | System.out.println("Event rule disabled:"); | |
59 | System.out.println("Event name: " + rule.getEventName()); | |
60 | System.out.println(printLogLevel(rule.getLogLevelSelector())); | |
61 | System.out.println("Filter string: " + rule.getFilterString()); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Convenience method to print integer log level values into their JUL | |
66 | * equivalent. | |
67 | */ | |
68 | private static String printLogLevel(LogLevelSelector lls) { | |
69 | String llname = Level.parse(String.valueOf(lls.getLogLevel())).getName(); | |
70 | return "Log level: " + llname + ", filter type: " + lls.getLogLevelType().toString(); | |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
75 | * Run the program. | |
76 | * | |
77 | * @param args | |
78 | * Command-line arguments (not used) | |
79 | * @throws IOException | |
80 | */ | |
81 | public static void main(String args[]) throws IOException { | |
82 | /* We need at least one log handler to activate the LTTng agent */ | |
83 | ILttngHandler handler = new LttngLogHandler(); | |
84 | ||
85 | /* Create a listener and register it to the manager */ | |
86 | IFilterChangeListener listener = new ExampleFilterChangeListener(); | |
87 | FilterChangeNotifier.getInstance().registerListener(listener); | |
88 | ||
89 | System.out.println("Press Enter to finish."); | |
90 | System.in.read(); | |
91 | ||
92 | /* Unregister the listener */ | |
93 | FilterChangeNotifier.getInstance().unregisterListener(listener); | |
94 | ||
95 | /* Cleanup the log handler we created */ | |
96 | handler.close(); | |
97 | } | |
a3454bfd | 98 | } |