]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message/src/org/simantics/message/internal/LogWriter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.message / src / org / simantics / message / internal / LogWriter.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 /*******************************************************************************
13  * Copyright (c) 2000, 2006 IBM Corporation and others.
14  * All rights reserved. This program and the accompanying materials
15  * are made available under the terms of the Eclipse Public License v1.0
16  * which accompanies this distribution, and is available at
17  * http://www.eclipse.org/legal/epl-v10.html
18  *
19  * Contributors:
20  *     IBM Corporation - initial API and implementation
21  *******************************************************************************/
22 package org.simantics.message.internal;
23
24 import java.util.ArrayList;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.osgi.framework.log.FrameworkLog;
29 import org.eclipse.osgi.framework.log.FrameworkLogEntry;
30 import org.simantics.message.IDetailStatus;
31 import org.simantics.message.ILogListener;
32
33 /**
34  * A log writer that writes log entries. See PlatformLogReader for reading logs
35  * back into memory.
36  * <p>
37  * Note that this class just provides a bridge from the old ILog interface to
38  * the OSGi FrameworkLog interface.
39  * </p>
40  * 
41  * <p>
42  * Copied from <code>org.eclipse.core.internal.runtime.PlatformLogWriter</code>
43  * since the code was internal to eclipse. Support for {@link IDetailStatus} has
44  * been added.
45  * </p>
46  */
47 public class LogWriter implements ILogListener {
48     private final FrameworkLog frameworkLog;
49
50     public LogWriter(FrameworkLog frameworkLog) {
51         this.frameworkLog = frameworkLog;
52     }
53
54     /**
55      * @see ILogListener#logging(IStatus, String)
56      */
57     public synchronized void logging(IStatus status, String plugin) {
58         frameworkLog.log(getLog(status));
59     }
60
61     protected FrameworkLogEntry getLog(IStatus status) {
62         Throwable t = status.getException();
63         ArrayList<FrameworkLogEntry> childlist = new ArrayList<FrameworkLogEntry>();
64
65         int stackCode = t instanceof CoreException ? 1 : 0;
66         // ensure a substatus inside a CoreException is properly logged
67         if (stackCode == 1) {
68             IStatus coreStatus = ((CoreException) t).getStatus();
69             if (coreStatus != null) {
70                 childlist.add(getLog(coreStatus));
71             }
72         }
73
74         if (status.isMultiStatus()) {
75             IStatus[] children = status.getChildren();
76             for (int i = 0; i < children.length; i++) {
77                 childlist.add(getLog(children[i]));
78             }
79         }
80
81         FrameworkLogEntry[] children = (childlist.size() == 0 ? null : childlist.toArray(new FrameworkLogEntry[childlist.size()]));
82
83         String message = status.getMessage();
84         if (status instanceof IDetailStatus) {
85             IDetailStatus ds = (IDetailStatus) status;
86             message = message + "\n!DETAILS " + ds.getDetailedDescription();
87         }
88
89         return new FrameworkLogEntry(status.getPlugin(), status.getSeverity(), status.getCode(), message, stackCode, t, children);
90     }
91 }