| 1 | /* |
| 2 | * Copyright (C) 2015 - Michael Jeanson <dgoulet@efficios.com> |
| 3 | * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com> |
| 4 | * Christian Babeux <christian.babeux@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License, version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.lang.Integer; |
| 22 | |
| 23 | import org.apache.log4j.Appender; |
| 24 | import org.apache.log4j.BasicConfigurator; |
| 25 | import org.apache.log4j.Logger; |
| 26 | import org.lttng.ust.agent.log4j.LttngLogAppender; |
| 27 | |
| 28 | public class JTestLTTng { |
| 29 | |
| 30 | /** |
| 31 | * Application start |
| 32 | * |
| 33 | * @param args |
| 34 | * Command-line arguments |
| 35 | * @throws IOException |
| 36 | * @throws InterruptedException |
| 37 | */ |
| 38 | public static void main(String args[]) throws IOException, InterruptedException { |
| 39 | |
| 40 | Logger lttng = Logger.getLogger("log4j-event"); |
| 41 | Logger lttng2 = Logger.getLogger("log4j-event-2"); |
| 42 | |
| 43 | int nrIter = Integer.parseInt(args[0]); |
| 44 | int waitTime = Integer.parseInt(args[1]); |
| 45 | int fire_debug_tp = 0; |
| 46 | int fire_second_tp = 0; |
| 47 | |
| 48 | if (args.length > 2) { |
| 49 | fire_debug_tp = Integer.parseInt(args[2]); |
| 50 | } |
| 51 | if (args.length > 3) { |
| 52 | fire_second_tp = Integer.parseInt(args[3]); |
| 53 | } |
| 54 | |
| 55 | /* Start with the default Log4j configuration, which logs to console */ |
| 56 | BasicConfigurator.configure(); |
| 57 | |
| 58 | /* |
| 59 | * Add a LTTng log appender to both loggers, which will also send the |
| 60 | * logged events to UST. |
| 61 | */ |
| 62 | Appender lttngAppender = new LttngLogAppender(); |
| 63 | lttng.addAppender(lttngAppender); |
| 64 | lttng2.addAppender(lttngAppender); |
| 65 | |
| 66 | for (int iter = 0; iter < nrIter; iter++) { |
| 67 | lttng.info("LOG4J tp fired!"); |
| 68 | if (fire_debug_tp == 1) { |
| 69 | /* Third arg, trigger debug TP. */ |
| 70 | lttng.debug("LOG4J DEBUG tp fired"); |
| 71 | } |
| 72 | Thread.sleep(waitTime); |
| 73 | } |
| 74 | |
| 75 | if (fire_second_tp == 1) { |
| 76 | lttng2.info("LOG4J second logger fired"); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Do not forget to close() all handlers so that the agent can shutdown |
| 81 | * and the session daemon socket gets cleaned up explicitly. |
| 82 | */ |
| 83 | lttngAppender.close(); |
| 84 | } |
| 85 | } |