2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 import java
.io
.IOException
;
24 import java
.util
.logging
.Level
;
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
;
34 * Example usage of a {@link IFilterChangeListener}.
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.
40 * @author Alexandre Montplaisir
42 public class FilterChangeListenerExample
{
44 private static class ExampleFilterChangeListener
implements IFilterChangeListener
{
47 public void eventRuleAdded(EventRule rule
) {
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());
56 public void eventRuleRemoved(EventRule rule
) {
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());
65 * Convenience method to print integer log level values into their JUL
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();
78 * Command-line arguments (not used)
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();
85 /* Create a listener and register it to the manager */
86 IFilterChangeListener listener
= new ExampleFilterChangeListener();
87 FilterChangeNotifier
.getInstance().registerListener(listener
);
89 System
.out
.println("Press Enter to finish.");
92 /* Unregister the listener */
93 FilterChangeNotifier
.getInstance().unregisterListener(listener
);
95 /* Cleanup the log handler we created */