comparison dwtx/core/internal/runtime/RuntimeLog.d @ 122:9d0585bcb7aa

Add core.jobs package
author Frank Benoit <benoit@tionex.de>
date Tue, 12 Aug 2008 02:34:21 +0200
parents
children 00f8787f4742
comparison
equal deleted inserted replaced
121:c0304616ea23 122:9d0585bcb7aa
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Julian Chen - fix for bug #92572, jclRM
11 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module dwtx.core.internal.runtime.RuntimeLog;
15
16 import dwt.dwthelper.utils;
17 import dwtx.dwtxhelper.Collection;
18
19 import dwtx.core.runtime.ILogListener;
20 import dwtx.core.runtime.IStatus;
21 import dwtx.core.runtime.OperationCanceledException;
22
23 /**
24 * NOT API!!! This log infrastructure was split from the InternalPlatform.
25 *
26 * @since dwtx.equinox.common 3.2
27 */
28 // XXX this must be removed and replaced with something more reasonable
29 public final class RuntimeLog {
30
31 private static ArrayList logListeners = new ArrayList(5);
32
33 /**
34 * Keep the messages until the first log listener is registered.
35 * Once first log listeners is registred, it is going to receive
36 * all status messages accumulated during the period when no log
37 * listener was available.
38 */
39 private static ArrayList queuedMessages = new ArrayList(5);
40
41 /**
42 * See dwtx.core.runtime.Platform#addLogListener(ILogListener)
43 */
44 public static void addLogListener(ILogListener listener) {
45 synchronized (logListeners) {
46 bool firstListener = (logListeners.size() is 0);
47 // replace if already exists (Set behaviour but we use an array
48 // since we want to retain order)
49 logListeners.remove(listener);
50 logListeners.add(listener);
51 if (firstListener) {
52 for (Iterator i = queuedMessages.iterator(); i.hasNext();) {
53 try {
54 IStatus recordedMessage = cast(IStatus) i.next();
55 listener.logging(recordedMessage, IRuntimeConstants.PI_RUNTIME);
56 } catch (Exception e) {
57 handleException(e);
58 } catch (LinkageError e) {
59 handleException(e);
60 }
61 }
62 queuedMessages.clear();
63 }
64 }
65 }
66
67 /**
68 * See dwtx.core.runtime.Platform#removeLogListener(ILogListener)
69 */
70 public static void removeLogListener(ILogListener listener) {
71 synchronized (logListeners) {
72 logListeners.remove(listener);
73 }
74 }
75
76 /**
77 * Checks if the given listener is present
78 */
79 public static bool contains(ILogListener listener) {
80 synchronized (logListeners) {
81 return logListeners.contains(listener);
82 }
83 }
84
85 /**
86 * Notifies all listeners of the platform log.
87 */
88 public static void log(IStatus status) {
89 // create array to avoid concurrent access
90 ILogListener[] listeners;
91 synchronized (logListeners) {
92 listeners = arraycast!(ILogListener)( logListeners.toArray());
93 if (listeners.length is 0) {
94 queuedMessages.add(status);
95 return;
96 }
97 }
98 for (int i = 0; i < listeners.length; i++) {
99 try {
100 listeners[i].logging(status, IRuntimeConstants.PI_RUNTIME);
101 } catch (Exception e) {
102 handleException(e);
103 } catch (LinkageError e) {
104 handleException(e);
105 }
106 }
107 }
108
109 private static void handleException(Exception e) {
110 if (!(cast(OperationCanceledException)e )) {
111 // Got a error while logging. Don't try to log again, just put it into stderr
112 ExceptionPrintStackTrace(e);
113 }
114 }
115
116 /**
117 * Helps determine if any listeners are registered with the logging mechanism.
118 * @return true if no listeners are registered
119 */
120 public static bool isEmpty() {
121 synchronized (logListeners) {
122 return (logListeners.size() is 0);
123 }
124 }
125
126 }