Blame doc/examples/java-jul/FilterChangeListenerExample.java

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit c04fcb
 * of this software and associated documentation files (the "Software"), to
Packit c04fcb
 * deal in the Software without restriction, including without limitation the
Packit c04fcb
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit c04fcb
 * sell copies of the Software, and to permit persons to whom the Software is
Packit c04fcb
 * furnished to do so, subject to the following conditions:
Packit c04fcb
 *
Packit c04fcb
 * The above copyright notice and this permission notice shall be included in
Packit c04fcb
 * all copies or substantial portions of the Software.
Packit c04fcb
 *
Packit c04fcb
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit c04fcb
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit c04fcb
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit c04fcb
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit c04fcb
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit c04fcb
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit c04fcb
 * IN THE SOFTWARE.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
import java.io.IOException;
Packit c04fcb
import java.util.logging.Level;
Packit c04fcb
Packit c04fcb
import org.lttng.ust.agent.ILttngHandler;
Packit c04fcb
import org.lttng.ust.agent.filter.FilterChangeNotifier;
Packit c04fcb
import org.lttng.ust.agent.filter.IFilterChangeListener;
Packit c04fcb
import org.lttng.ust.agent.jul.LttngLogHandler;
Packit c04fcb
import org.lttng.ust.agent.session.EventRule;
Packit c04fcb
import org.lttng.ust.agent.session.LogLevelSelector;
Packit c04fcb
Packit c04fcb
/**
Packit c04fcb
 * Example usage of a {@link IFilterChangeListener}.
Packit c04fcb
 *
Packit c04fcb
 * This listener will simply print to stdout the notifications it receives. To
Packit c04fcb
 * try it, run the program, then issue "lttng enable-event" and
Packit c04fcb
 * "lttng disable-event" commands for the JUL domain.
Packit c04fcb
 *
Packit c04fcb
 * @author Alexandre Montplaisir
Packit c04fcb
 */
Packit c04fcb
public class FilterChangeListenerExample {
Packit c04fcb
Packit c04fcb
	private static class ExampleFilterChangeListener implements IFilterChangeListener {
Packit c04fcb
Packit c04fcb
		@Override
Packit c04fcb
		public void eventRuleAdded(EventRule rule) {
Packit c04fcb
			System.out.println();
Packit c04fcb
			System.out.println("New event rule enabled:");
Packit c04fcb
			System.out.println("Event name: " + rule.getEventName());
Packit c04fcb
			System.out.println(printLogLevel(rule.getLogLevelSelector()));
Packit c04fcb
			System.out.println("Filter string: " + rule.getFilterString());
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		@Override
Packit c04fcb
		public void eventRuleRemoved(EventRule rule) {
Packit c04fcb
			System.out.println();
Packit c04fcb
			System.out.println("Event rule disabled:");
Packit c04fcb
			System.out.println("Event name: " + rule.getEventName());
Packit c04fcb
			System.out.println(printLogLevel(rule.getLogLevelSelector()));
Packit c04fcb
			System.out.println("Filter string: " + rule.getFilterString());
Packit c04fcb
		}
Packit c04fcb
Packit c04fcb
		/**
Packit c04fcb
		 * Convenience method to print integer log level values into their JUL
Packit c04fcb
		 * equivalent.
Packit c04fcb
		 */
Packit c04fcb
		private static String printLogLevel(LogLevelSelector lls) {
Packit c04fcb
			String llname = Level.parse(String.valueOf(lls.getLogLevel())).getName();
Packit c04fcb
			return "Log level: " + llname + ", filter type: " + lls.getLogLevelType().toString();
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	/**
Packit c04fcb
	 * Run the program.
Packit c04fcb
	 *
Packit c04fcb
	 * @param args
Packit c04fcb
	 *            Command-line arguments (not used)
Packit c04fcb
	 * @throws IOException
Packit c04fcb
	 */
Packit c04fcb
	public static void main(String args[]) throws IOException {
Packit c04fcb
		/* We need at least one log handler to activate the LTTng agent */
Packit c04fcb
		ILttngHandler handler = new LttngLogHandler();
Packit c04fcb
Packit c04fcb
		/* Create a listener and register it to the manager */
Packit c04fcb
		IFilterChangeListener listener = new ExampleFilterChangeListener();
Packit c04fcb
		FilterChangeNotifier.getInstance().registerListener(listener);
Packit c04fcb
Packit c04fcb
		System.out.println("Press Enter to finish.");
Packit c04fcb
		System.in.read();
Packit c04fcb
Packit c04fcb
		/* Unregister the listener */
Packit c04fcb
		FilterChangeNotifier.getInstance().unregisterListener(listener);
Packit c04fcb
Packit c04fcb
		/* Cleanup the log handler we created */
Packit c04fcb
		handler.close();
Packit c04fcb
	}
Packit c04fcb
}