Commit | Line | Data |
---|---|---|
43e5396b DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | package org.lttng.ust.jul; | |
19 | ||
20 | import java.io.IOException; | |
43e5396b DG |
21 | import java.io.InputStream; |
22 | import java.io.BufferedReader; | |
23 | import java.io.FileReader; | |
24 | import java.util.concurrent.Semaphore; | |
f1fa0535 | 25 | import java.util.concurrent.TimeUnit; |
43e5396b DG |
26 | import java.util.logging.FileHandler; |
27 | import java.util.logging.Handler; | |
28 | import java.util.logging.Level; | |
29 | import java.util.logging.Logger; | |
30 | import java.util.logging.LogManager; | |
31 | import java.util.Enumeration; | |
32 | ||
33 | public class LTTngAgent { | |
9aabed2d DG |
34 | |
35 | /* Possible that we have to threads handling two sessiond. */ | |
36 | private static LTTngLogHandler lttngHandlerRoot; | |
37 | private static LTTngLogHandler lttngHandlerUser; | |
38 | private static LTTngThread lttngThreadRoot; | |
39 | private static LTTngThread lttngThreadUser; | |
40 | private static Thread sessiondThRoot; | |
41 | private static Thread sessiondThUser; | |
43e5396b DG |
42 | |
43 | /* Singleton agent object. */ | |
44 | private static LTTngAgent curAgent = null; | |
45 | ||
46 | /* Indicate if this object has been initialized. */ | |
47 | private static boolean initialized = false; | |
48 | ||
49 | private static Semaphore registerSem; | |
f1fa0535 | 50 | private final static int semTimeout = 3; /* Seconds */ |
43e5396b | 51 | |
f1fa0535 DG |
52 | /* |
53 | * Default value to connect to session daemon. Port number is dynamically | |
54 | * fetched from the port file that is created by a running session daemon. | |
55 | */ | |
43e5396b | 56 | private static final String sessiondAddr = "127.0.0.1"; |
43e5396b DG |
57 | |
58 | /* | |
59 | * Constructor is private. This is a singleton and a reference should be | |
60 | * acquired using getLTTngAgent(). | |
61 | */ | |
62 | private LTTngAgent() throws IOException { | |
63 | this.logManager = LogManager.getLogManager(); | |
501f6777 CB |
64 | this.lttngHandlerUser = new LTTngLogHandler(); |
65 | this.lttngHandlerRoot = new LTTngLogHandler(); | |
9aabed2d | 66 | this.lttngHandlerRoot.is_root = 1; |
43e5396b DG |
67 | this.registerSem = new Semaphore(0, true); |
68 | } | |
69 | ||
43e5396b DG |
70 | /* |
71 | * Public getter to acquire a reference to this singleton object. | |
72 | */ | |
73 | public static synchronized LTTngAgent getLTTngAgent() throws IOException { | |
74 | if (curAgent == null) { | |
75 | curAgent = new LTTngAgent(); | |
76 | curAgent.init(); | |
77 | } | |
78 | ||
79 | return curAgent; | |
80 | } | |
81 | ||
82 | /* | |
83 | * Initialize LTTngAgent. This will attach the log handler to all Logger | |
84 | * returned by the logManager. | |
85 | */ | |
86 | private synchronized void init() throws SecurityException, IOException { | |
9aabed2d DG |
87 | int nr_acquires = 0; |
88 | ||
43e5396b DG |
89 | if (this.initialized) { |
90 | return; | |
91 | } | |
92 | ||
9aabed2d | 93 | /* Handle user session daemon if any. */ |
f1fa0535 | 94 | this.lttngThreadUser = new LTTngThread(this.sessiondAddr, |
9aabed2d DG |
95 | this.lttngHandlerUser, this.registerSem); |
96 | this.sessiondThUser = new Thread(lttngThreadUser); | |
0c52a51d | 97 | this.sessiondThUser.setDaemon(true); |
9aabed2d DG |
98 | this.sessiondThUser.start(); |
99 | /* Wait for registration done of per-user sessiond */ | |
100 | nr_acquires++; | |
101 | ||
f1fa0535 DG |
102 | /* Handle root session daemon. */ |
103 | this.lttngThreadRoot = new LTTngThread(this.sessiondAddr, | |
104 | this.lttngHandlerRoot, this.registerSem); | |
105 | this.sessiondThRoot = new Thread(lttngThreadRoot); | |
0c52a51d | 106 | this.sessiondThRoot.setDaemon(true); |
f1fa0535 DG |
107 | this.sessiondThRoot.start(); |
108 | /* Wait for registration done of system-wide sessiond */ | |
109 | nr_acquires++; | |
43e5396b | 110 | |
9aabed2d | 111 | /* Wait for each registration to end. */ |
43e5396b | 112 | try { |
f1fa0535 DG |
113 | this.registerSem.tryAcquire(nr_acquires, semTimeout, |
114 | TimeUnit.SECONDS); | |
43e5396b DG |
115 | } catch (InterruptedException e) { |
116 | e.printStackTrace(); | |
117 | } | |
9aabed2d DG |
118 | |
119 | this.initialized = true; | |
43e5396b DG |
120 | } |
121 | ||
122 | public void dispose() throws IOException { | |
501f6777 CB |
123 | this.lttngJULThreadUser.dispose(); |
124 | this.lttngJULThreadRoot.dispose(); | |
43e5396b | 125 | |
501f6777 CB |
126 | /* Remove handlers from the root logger */ |
127 | Logger rootLogger = LogManager.getLogManager().getLogger(""); | |
128 | rootLogger.removeHandler(this.lttngHandlerUser); | |
129 | rootLogger.removeHandler(this.lttngHandlerRoot); | |
43e5396b DG |
130 | |
131 | try { | |
9aabed2d | 132 | this.sessiondThUser.join(); |
501f6777 | 133 | this.sessiondThRoot.join(); |
43e5396b DG |
134 | } catch (InterruptedException e) { |
135 | e.printStackTrace(); | |
136 | } | |
137 | } | |
138 | } |