View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2011
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
17   * 1892103    2008-02-12  ebak         SLP improvements
18   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
19   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
20   * 3400209    2011-08-31  blaschke-oss Highlighted Static Analysis (PMD) issues
21   */
22  
23  package org.metricshub.wbem.sblim.slp.internal;
24  
25  /*-
26   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
27   * WBEM Java Client
28   * ჻჻჻჻჻჻
29   * Copyright 2023 - 2025 MetricsHub
30   * ჻჻჻჻჻჻
31   * Licensed under the Apache License, Version 2.0 (the "License");
32   * you may not use this file except in compliance with the License.
33   * You may obtain a copy of the License at
34   *
35   *      http://www.apache.org/licenses/LICENSE-2.0
36   *
37   * Unless required by applicable law or agreed to in writing, software
38   * distributed under the License is distributed on an "AS IS" BASIS,
39   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40   * See the License for the specific language governing permissions and
41   * limitations under the License.
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   * debug -> FINEST info -> INFO warning -> WARNING error -> SEVERE
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  	 * setLevel
75  	 *
76  	 * @param pLevel
77  	 */
78  	public static void setLevel(Level pLevel) {
79  		cLevel = pLevel;
80  	}
81  
82  	/**
83  	 * setConsolLog
84  	 *
85  	 * @param pOutStr
86  	 */
87  	public static void setOutput(OutputStream pOutStr) {
88  		setOutput(new PrintStream(pOutStr));
89  	}
90  
91  	/**
92  	 * setOutputStream
93  	 *
94  	 * @param pOutStr
95  	 */
96  	public static void setOutput(PrintStream pOutStr) {
97  		cOut = pOutStr;
98  	}
99  
100 	/**
101 	 * setPatterns
102 	 *
103 	 * @param pDenyPatterns
104 	 * @param pAllowPatterns
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 	 * debug
122 	 *
123 	 * @param pMsg
124 	 */
125 	public static void debug(String pMsg) {
126 		debug(pMsg, null);
127 	}
128 
129 	/**
130 	 * debug
131 	 *
132 	 * @param pMsg
133 	 * @param pEx
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 	 * info
142 	 *
143 	 * @param pMsg
144 	 */
145 	public static void info(String pMsg) {
146 		info(pMsg, null);
147 	}
148 
149 	/**
150 	 * info
151 	 *
152 	 * @param pMsg
153 	 * @param pEx
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 	 * warning
162 	 *
163 	 * @param pMsg
164 	 */
165 	public static void warning(String pMsg) {
166 		warning(pMsg, null);
167 	}
168 
169 	/**
170 	 * warning
171 	 *
172 	 * @param pMsg
173 	 * @param pEx
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 	 * error
182 	 *
183 	 * @param pMsg
184 	 */
185 	public static void error(String pMsg) {
186 		error(pMsg, null);
187 	}
188 
189 	/**
190 	 * error
191 	 *
192 	 * @param pEx
193 	 */
194 	public static void error(Exception pEx) {
195 		error(null, pEx);
196 	}
197 
198 	/**
199 	 * error
200 	 *
201 	 * @param pMsg
202 	 * @param pEx
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 	 * getDate
291 	 *
292 	 * @return String
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 }