1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.metricshub.wbem.sblim.slp.internal;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 import java.io.OutputStream;
46 import java.io.PrintStream;
47 import java.io.PrintWriter;
48 import java.io.StringWriter;
49 import java.util.Calendar;
50 import java.util.Date;
51 import java.util.logging.Level;
52 import java.util.regex.Matcher;
53 import java.util.regex.Pattern;
54 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
55
56
57
58
59
60 public class TRC {
61 private static PrintStream cOut;
62
63 private static Level cLevel = SLPConfig.getGlobalCfg().getTraceLevel();
64
65 private static LogAndTraceBroker cLogger = LogAndTraceBroker.getBroker();
66
67 private static Pattern[] cDenyPatterns;
68
69 private static Pattern[] cAllowPatterns;
70
71 private static final String NAME = new TRC().getClass().getName();
72
73
74
75
76
77
78 public static void setLevel(Level pLevel) {
79 cLevel = pLevel;
80 }
81
82
83
84
85
86
87 public static void setOutput(OutputStream pOutStr) {
88 setOutput(new PrintStream(pOutStr));
89 }
90
91
92
93
94
95
96 public static void setOutput(PrintStream pOutStr) {
97 cOut = pOutStr;
98 }
99
100
101
102
103
104
105
106 public static void setPatterns(String[] pDenyPatterns, String[] pAllowPatterns) {
107 cDenyPatterns = compile(pDenyPatterns);
108 cAllowPatterns = compile(pAllowPatterns);
109 }
110
111 private static Pattern[] compile(String[] pStrs) {
112 if (pStrs == null) return null;
113 Pattern[] patterns = new Pattern[pStrs.length];
114 for (int i = 0; i < patterns.length; i++) {
115 patterns[i] = Pattern.compile(pStrs[i]);
116 }
117 return patterns;
118 }
119
120
121
122
123
124
125 public static void debug(String pMsg) {
126 debug(pMsg, null);
127 }
128
129
130
131
132
133
134
135 public static void debug(String pMsg, Exception pEx) {
136 if (cLevel.intValue() > Level.FINEST.intValue()) return;
137 trace(Level.FINEST, pMsg, pEx);
138 }
139
140
141
142
143
144
145 public static void info(String pMsg) {
146 info(pMsg, null);
147 }
148
149
150
151
152
153
154
155 public static void info(String pMsg, Exception pEx) {
156 if (cLevel.intValue() > Level.INFO.intValue()) return;
157 trace(Level.INFO, pMsg, pEx);
158 }
159
160
161
162
163
164
165 public static void warning(String pMsg) {
166 warning(pMsg, null);
167 }
168
169
170
171
172
173
174
175 public static void warning(String pMsg, Exception pEx) {
176 if (cLevel.intValue() > Level.WARNING.intValue()) return;
177 trace(Level.WARNING, pMsg, pEx);
178 }
179
180
181
182
183
184
185 public static void error(String pMsg) {
186 error(pMsg, null);
187 }
188
189
190
191
192
193
194 public static void error(Exception pEx) {
195 error(null, pEx);
196 }
197
198
199
200
201
202
203
204 public static void error(String pMsg, Exception pEx) {
205 if (cLevel.intValue() > Level.SEVERE.intValue()) return;
206 trace(Level.SEVERE, pMsg, pEx);
207 }
208
209 private static boolean hasMatch(Pattern[] pPatterns, String pStr) {
210 if (pPatterns == null) return false;
211 for (int i = 0; i < pPatterns.length; i++) {
212 Pattern pattern = pPatterns[i];
213 Matcher m = pattern.matcher(pStr);
214 if (m.matches()) return true;
215 }
216 return false;
217 }
218
219 private static boolean deny(String pFnStr) {
220 return hasMatch(cDenyPatterns, pFnStr);
221 }
222
223 private static boolean allow(String pFnStr) {
224 return hasMatch(cAllowPatterns, pFnStr);
225 }
226
227 private static void trace(Level pLevel, String pMsg, Exception pEx) {
228 StackTraceElement location = getLocation();
229 String fnStr = getFunctionStr(location);
230 if (deny(fnStr) && !allow(fnStr)) return;
231 StringBuffer buf = new StringBuffer(
232 '[' +
233 pLevel.toString() +
234 ' ' +
235 Thread.currentThread().getName() +
236 ' ' +
237 getDate() +
238 ' ' +
239 getLocationStr(location) +
240 "]\n"
241 );
242 if (pMsg != null) buf.append(pMsg + '\n');
243 if (pEx != null) {
244 StringWriter writer = new StringWriter();
245 pEx.printStackTrace(new PrintWriter(writer));
246 buf.append(writer.toString() + '\n');
247 }
248 synchronized (TRC.class) {
249 if (cOut != null) {
250 cOut.println(buf.toString());
251 System.out.flush();
252 } else {
253 cLogger.trace(pLevel, buf.toString());
254 }
255 }
256 }
257
258 private static StackTraceElement getLocation() {
259 Throwable thr = new Throwable();
260 StackTraceElement[] elements = thr.getStackTrace();
261 StackTraceElement e = null;
262 for (int i = 0; i < elements.length; i++) {
263 e = elements[i];
264 if (!e.getClassName().equals(NAME)) return e;
265 }
266 return null;
267 }
268
269 private static String getFunctionStr(StackTraceElement pSTE) {
270 return pSTE.getClassName() + '.' + pSTE.getMethodName();
271 }
272
273 private static String getLocationStr(StackTraceElement pSTE) {
274 return getFunctionStr(pSTE) + '(' + pSTE.getFileName() + ':' + pSTE.getLineNumber() + ')';
275 }
276
277 private static String pad(int pDigits, int pNum) {
278 String str = Integer.toString(pNum);
279 int len = Math.max(pDigits, str.length());
280 char[] cA = new char[len];
281 int paddingDigits = pDigits - str.length();
282 int dIdx = 0;
283 while (dIdx < paddingDigits) cA[dIdx++] = '0';
284 int sIdx = 0;
285 while (dIdx < len) cA[dIdx++] = str.charAt(sIdx++);
286 return new String(cA);
287 }
288
289
290
291
292
293
294 private static String getDate() {
295 long millis = new Date().getTime();
296 Calendar cal = Calendar.getInstance();
297 cal.setTimeInMillis(millis);
298 return (
299 Integer.toString(cal.get(Calendar.YEAR)) +
300 '.' +
301 pad(2, cal.get(Calendar.MONTH) + 1) +
302 '.' +
303 pad(2, cal.get(Calendar.DAY_OF_MONTH)) +
304 ' ' +
305 pad(2, cal.get(Calendar.HOUR_OF_DAY)) +
306 ':' +
307 pad(2, cal.get(Calendar.MINUTE)) +
308 ':' +
309 pad(2, cal.get(Calendar.SECOND)) +
310 ' ' +
311 pad(3, cal.get(Calendar.MILLISECOND))
312 );
313 }
314 }