1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.message.internal;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.ListenerList;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.Status;
21 import org.simantics.message.ILogListener;
22 import org.simantics.message.ILogger;
25 * A logger for IStatus instances.
28 * Meant to be used by any Simantics UI's for logging messages that should be
29 * relevant to the user's normal workflow instead of the kinds of
30 * code-error-like messages that go into the normal eclipse Error Log.
34 * Use {@link #getDefault()} to get a hold of the singleton instance of Messages
35 * or even more directly, you can use {@link #defaultLog(IStatus)} to
36 * effectively perform <code>getDefault().log(status)</code>.
40 * Originally copied from <code>org.eclipse.core.internal.runtime.Log</code>
41 * since the code was internal to eclipse and not usable as such. Modifications
42 * have been made since.
45 * @author Tuukka Lehtonen
47 final class Messages implements ILogger {
52 ListenerList listeners = new ListenerList();
55 * A queue of messages that have been received before the first listener is
58 List<IStatus> queue = new ArrayList<IStatus>();
61 public void addLogListener(ILogListener listener) {
62 synchronized (listeners) {
63 boolean firstListener = listeners.isEmpty();
64 listeners.add(listener);
66 for (IStatus s : queue) {
67 listener.logging(s, Activator.PLUGIN_ID);
75 public String getName() {
76 return "Platform Log";
80 public void log(IStatus status) {
82 // This synchronizes properly with addLogListener.
83 synchronized (listeners) {
84 ls = listeners.getListeners();
92 ((ILogListener) l).logging(status, Activator.PLUGIN_ID);
93 } catch (Exception e) {
95 } catch (LinkageError e) {
101 private void handleException(Throwable e) {
102 if (!(e instanceof OperationCanceledException)) {
103 // Got an error while logging. Delegate to Error Log, which is meant
104 // for errors in the code instead of these user level notifications.
105 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Message logging failed, see exception for cause.", e));
110 public void removeLogListener(ILogListener listener) {
111 listeners.remove(listener);