Skip to main content
summaryrefslogtreecommitdiffstats
blob: d5e64d8a36bf7df33d0c37060bfc3365a43fd40f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package org.eclipse.e4.tools.compat.internal;

import java.util.ArrayList;
import javax.inject.Inject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.osgi.framework.log.FrameworkLog;
import org.eclipse.osgi.framework.log.FrameworkLogEntry;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.debug.DebugTrace;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

/**
 * The workbench implementation of the logger service.
 */
public final class WorkbenchLogger extends Logger {
	protected DebugTrace trace;
	protected FrameworkLog log;
	private Bundle bundle = FrameworkUtil.getBundle(WorkbenchLogger.class);

	/**
	 * Creates a new workbench logger
	 */
	public WorkbenchLogger() {
		super();
	}

	public void debug(Throwable t) {
		debug(t, null);
	}

	public void debug(Throwable t, String message) {
		trace(t, message);
	}

	public void error(Throwable t, String message) {
		log(new Status(IStatus.ERROR, bundle.getSymbolicName(),
				message, t));
	}

	/**
	 * Copied from PlatformLogWriter in core runtime.
	 */
	private static FrameworkLogEntry getLog(IStatus status) {
		Throwable t = status.getException();
		ArrayList childlist = new ArrayList();

		int stackCode = t instanceof CoreException ? 1 : 0;
		// ensure a substatus inside a CoreException is properly logged
		if (stackCode == 1) {
			IStatus coreStatus = ((CoreException) t).getStatus();
			if (coreStatus != null) {
				childlist.add(getLog(coreStatus));
			}
		}

		if (status.isMultiStatus()) {
			IStatus[] children = status.getChildren();
			for (int i = 0; i < children.length; i++) {
				childlist.add(getLog(children[i]));
			}
		}

		FrameworkLogEntry[] children = (FrameworkLogEntry[]) (childlist.size() == 0 ? null
				: childlist.toArray(new FrameworkLogEntry[childlist.size()]));

		return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(),
				status.getMessage(), stackCode, t, children);
	}

	public void info(Throwable t, String message) {
		log(new Status(IStatus.INFO, bundle.getSymbolicName(), message,
				t));
	}

	public boolean isDebugEnabled() {
		return false;
	}

	public boolean isErrorEnabled() {
		return true;
	}

	public boolean isInfoEnabled() {
		return true;
	}

	public boolean isTraceEnabled() {
		return false;
	}

	public boolean isWarnEnabled() {
		return true;
	}

	private void log(IStatus status) {
		if (log != null) {
			log.log(getLog(status));
		} else {
			System.out.println(status.getMessage());
			if (status.getException() != null)
				status.getException().printStackTrace();
		}
	}

	/**
	 * Sets the debug options service for this logger.
	 * 
	 * @param options
	 *            The debug options to be used by this logger
	 */
	@Inject
	public void setDebugOptions(DebugOptions options) {
		if (options != null) {
			this.trace = options.newDebugTrace(bundle.getSymbolicName(), WorkbenchLogger.class);
		}
	}

	/**
	 * @param log
	 */
	@Inject
	public void setFrameworkLog(FrameworkLog log) {
		this.log = log;
	}

	public void trace(Throwable t, String message) {
		if (trace != null) {
			trace.trace(null, message, t);
		} else {
			System.out.println(message);
			t.printStackTrace();
		}
	}

	public void warn(Throwable t, String message) {
		log(new Status(IStatus.WARNING, bundle.getSymbolicName(),
				message, t));
	}
}

Back to the top