Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2bd81aa39661fc33332f5857e559e34cd758d0ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.frameworkadmin.equinox;

import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Utility class with static methods for logging to LogService, if available 
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class Log {
	static private ServiceTracker logTracker;
	static private boolean useLog = false;

	public static void dispose() {
		if (logTracker != null) {
			logTracker.close();
		}
		logTracker = null;
	}

	public static void init(BundleContext bc) {
		logTracker = new ServiceTracker(bc, LogService.class.getName(), null);
		logTracker.open();
	}

	public static void log(int level, Object obj, String method, String message) {
		log(level, obj, method, message, null);
	}

	public static void log(int level, Object obj, String method, String message, Throwable e) {
		LogService logService = null;
		String msg = ""; //$NON-NLS-1$
		if (method == null) {
			if (obj != null)
				msg = "(" + obj.getClass().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		} else if (obj == null)
			msg = "[" + method + "]" + message; //$NON-NLS-1$ //$NON-NLS-2$
		else
			msg = "[" + method + "](" + obj.getClass().getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		msg += message;
		if (logTracker != null)
			logService = (LogService) logTracker.getService();

		if (logService != null) {
			logService.log(level, msg, e);
		} else {
			String levelSt = null;
			if (level == LogService.LOG_DEBUG)
				levelSt = "DEBUG"; //$NON-NLS-1$
			else if (level == LogService.LOG_INFO)
				levelSt = "INFO"; //$NON-NLS-1$
			else if (level == LogService.LOG_WARNING)
				levelSt = "WARNING"; //$NON-NLS-1$
			else if (level == LogService.LOG_ERROR) {
				levelSt = "ERROR"; //$NON-NLS-1$
				useLog = true;
			}
			if (useLog) {
				System.err.println("[" + levelSt + "]" + msg); //$NON-NLS-1$ //$NON-NLS-2$
				if (e != null)
					e.printStackTrace();
			}
		}
	}

	public static void log(int level, Object obj, String method, Throwable e) {
		log(level, obj, method, null, e);
	}

	public static void log(int level, String message) {
		log(level, null, null, message, null);
	}

	public static void log(int level, String message, Throwable e) {
		log(level, null, null, message, e);
	}

	private Log() {
	}

}

Back to the top