Skip to main content
summaryrefslogtreecommitdiffstats
blob: 49f55c7532c1579b1294fd65b2bd6c5191f7902d (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
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 1998, 2005 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.device;

import java.io.PrintStream;
import java.text.DateFormat;
import java.util.Calendar;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

/**
 * LogTracker class. This class encapsulates the LogService
 * and handles all issues such as the service coming and going.
 */

public class LogTracker extends ServiceTracker implements LogService {
	/** LogService interface class name */
	protected final static String clazz = "org.osgi.service.log.LogService"; //$NON-NLS-1$

	/** PrintStream to use if LogService is unavailable */
	protected PrintStream out;

	/** Calendar and DateFormat to user if LogService is unavailable */
	private static Calendar calendar;
	private static DateFormat dateFormat;
	private String timestamp;

	/**
	 * Create new LogTracker.
	 *
	 * @param context BundleContext of parent bundle.
	 * @param out Default PrintStream to use if LogService is unavailable.
	 */
	public LogTracker(BundleContext context, PrintStream out) {
		super(context, clazz, null);
		this.out = out;
		calendar = Calendar.getInstance();
		dateFormat = DateFormat.getDateTimeInstance();
		open();
	}

	/*
	 * ----------------------------------------------------------------------
	 *      LogService Interface implementation
	 * ----------------------------------------------------------------------
	 */

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

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

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

	public synchronized void log(ServiceReference reference, int level, String message, Throwable exception) {
		ServiceReference[] references = getServiceReferences();

		if (references != null) {
			int size = references.length;

			for (int i = 0; i < size; i++) {
				LogService service = (LogService) getService(references[i]);
				if (service != null) {
					try {
						service.log(reference, level, message, exception);
					} catch (Exception e) {
						// do nothing
					}
				}
			}

			return;
		}

		noLogService(level, message, exception, reference);
	}

	/**
	 * The LogService is not available so we write the message to a PrintStream.
	 *
	 * @param level Logging level
	 * @param message Log message.
	 * @param throwable Log exception or null if none.
	 * @param reference ServiceReference associated with message or null if none.
	 */
	protected void noLogService(int level, String message, Throwable throwable, ServiceReference reference) {
		if (out != null) {
			synchronized (out) {
				// Bug #113286.  If no log service present and messages are being
				// printed to stdout, prepend message with a timestamp.
				timestamp = dateFormat.format(calendar.getTime());
				out.print(timestamp + " "); //$NON-NLS-1$

				switch (level) {
					case LOG_DEBUG : {
						out.print("Debug: "); //$NON-NLS-1$

						break;
					}
					case LOG_INFO : {
						out.print(LogTrackerMsg.Info); 

						break;
					}
					case LOG_WARNING : {
						out.print(LogTrackerMsg.Warning);

						break;
					}
					case LOG_ERROR : {
						out.print(LogTrackerMsg.Error);

						break;
					}
					default : {
						out.print("["); //$NON-NLS-1$
						out.print(LogTrackerMsg.Unknown_Log_level);         
						out.print("]: "); //$NON-NLS-1$

						break;
					}
				}

				out.println(message);

				if (reference != null) {
					out.println(reference);
				}

				if (throwable != null) {
					throwable.printStackTrace(out);
				}
			}
		}
	}
}

Back to the top