]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/internal/Messages.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / internal / Messages.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.internal;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
23
24 /**
25  * A logger for IStatus instances.
26  * 
27  * <p>
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.
31  * </p>
32  * 
33  * <p>
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>.
37  * </p>
38  * 
39  * <p>
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.
43  * </p>
44  * 
45  * @author Tuukka Lehtonen
46  */
47 final class Messages implements ILogger {
48
49     /**
50      * The log listeners.
51      */
52     ListenerList  listeners = new ListenerList();
53
54     /**
55      * A queue of messages that have been received before the first listener is
56      * added.
57      */
58     List<IStatus> queue = new ArrayList<IStatus>();
59
60     @Override
61     public void addLogListener(ILogListener listener) {
62         synchronized (listeners) {
63             boolean firstListener = listeners.isEmpty();
64             listeners.add(listener);
65             if (firstListener) {
66                 for (IStatus s : queue) {
67                     listener.logging(s, Activator.PLUGIN_ID);
68                 }
69                 queue.clear();
70             }
71         }
72     }
73
74     @Override
75     public String getName() {
76         return "Platform Log";
77     }
78
79     @Override
80     public void log(IStatus status) {
81         Object[] ls;
82         // This synchronizes properly with addLogListener.
83         synchronized (listeners) {
84             ls = listeners.getListeners();
85         }
86         if (ls.length == 0) {
87             queue.add(status);
88             return;
89         }
90         for (Object l : ls) {
91             try {
92                 ((ILogListener) l).logging(status, Activator.PLUGIN_ID);
93             } catch (Exception e) {
94                 handleException(e);
95             } catch (LinkageError e) {
96                 handleException(e);
97             }
98         }
99     }
100
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));
106         }
107     }
108
109     @Override
110     public void removeLogListener(ILogListener listener) {
111         listeners.remove(listener);
112     }
113
114 }