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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*******************************************************************************
* Copyright (c) 2009, 2010 IBM Corporation and others.
* 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.osgi.framework.debug;
/**
* A framework trace entry is a bean containing all of the attributes for a single trace message.
*/
public class FrameworkDebugTraceEntry {
/** If a bundles symbolic name is not specified then the default value of /debug can be used */
public final static String DEFAULT_OPTION_PATH = "/debug"; //$NON-NLS-1$
/**
* The name of the thread executing the code
*/
private final String threadName;
/**
* The date and time when the trace occurred.
*
*/
private final long timestamp;
/**
* The trace option-path
*/
private final String optionPath;
/**
* The symbolic name of the bundle being traced
*/
private final String bundleSymbolicName;
/**
* The class being traced
*/
private final String className;
/**
* The method being traced
*/
private final String methodName;
/**
* The line number
*/
private final int lineNumber;
/**
* The trace message
*/
private String message;
/**
* The trace exception
*/
private final Throwable throwable;
/**
* Construct a new FrameworkTraceRecord object
*
* @param bundleSymbolicName
* The symbolic name of the bundle being traced
* @param optionPath
* The trace optionPath
* @param message
* The trace message
* @param traceClass
* The class that calls the trace API
*/
public FrameworkDebugTraceEntry(final String bundleSymbolicName, final String optionPath, final String message, final String traceClass) {
this(bundleSymbolicName, optionPath, message, null, traceClass);
}
/**
* Construct a new FrameworkTraceRecord object
*
* @param bundleSymbolicName
* The symbolic name of the bundle being traced
* @param optionPath
* The trace optionPath
* @param message
* The trace message
* @param error
* An exception to be traced
* @param traceClass
* The class that calls the trace API
*/
public FrameworkDebugTraceEntry(String bundleSymbolicName, final String optionPath, final String message, final Throwable error, final String traceClass) {
threadName = Thread.currentThread().getName();
if (optionPath == null) {
this.optionPath = FrameworkDebugTraceEntry.DEFAULT_OPTION_PATH;
} else {
this.optionPath = optionPath;
}
timestamp = System.currentTimeMillis();
this.bundleSymbolicName = bundleSymbolicName;
this.message = message;
throwable = error;
String determineClassName = null;
String determineMethodName = null;
int determineLineNumber = 0;
// dynamically determine the class name, method name, and line number of the method calling the trace framework
StackTraceElement[] stackElements = new Exception().getStackTrace();
int i = 0;
while (i < stackElements.length) {
String fullClassName = stackElements[i].getClassName();
if (!fullClassName.equals(Thread.class.getName()) && !fullClassName.equals(FrameworkDebugTraceEntry.class.getName()) && !fullClassName.equals(EclipseDebugTrace.class.getName())) {
/*
* The first class which is non-JDK or framework related has been hit.
* If a traceClass has been specified then this current stack element
* is likely that class so we should find out who called it. If a
* trace class has not been specified, or has been specified and this
* stack element is not that class, then we assume this stack element
* is the caller of the trace API.
*/
if ((traceClass == null) || !fullClassName.equals(traceClass)) {
determineClassName = stackElements[i].getClassName();
determineMethodName = stackElements[i].getMethodName();
determineLineNumber = stackElements[i].getLineNumber();
break; // only break when the right stack element has been found; Otherwise keep trying
}
}
i++;
}
className = determineClassName;
methodName = determineMethodName;
lineNumber = determineLineNumber;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
final StringBuffer buffer = new StringBuffer(threadName);
buffer.append(" "); //$NON-NLS-1$
buffer.append(timestamp);
buffer.append(" "); //$NON-NLS-1$
buffer.append(bundleSymbolicName);
buffer.append(" "); //$NON-NLS-1$
buffer.append(optionPath);
buffer.append(" "); //$NON-NLS-1$
buffer.append(className);
buffer.append(" "); //$NON-NLS-1$
buffer.append(methodName);
buffer.append(" "); //$NON-NLS-1$
buffer.append(lineNumber);
if (message != null) {
buffer.append(": "); //$NON-NLS-1$
buffer.append(message);
}
if (throwable != null) {
buffer.append(throwable);
}
return buffer.toString();
}
/**
* Accessor to the threads name
*
* @return the name of the thread
*/
public final String getThreadName() {
return threadName;
}
/**
* Accessor to the timestamp for this trace record
*
* @return the date
*/
public final long getTimestamp() {
return timestamp;
}
/**
* Accessor for the symbolic name of the bundle being traced
*
* @return The symbolic name of the bundle being traced
*/
public final String getBundleSymbolicName() {
return bundleSymbolicName;
}
/**
* Accessor for the trace message
*
* @return the trace message
*/
public final String getMessage() {
return message;
}
/**
* Accessor for the trace exception. This may be null if there is no exception.
*
* @return the trace exception or null if none was defined.
*/
public final Throwable getThrowable() {
return throwable;
}
/**
* Accessor for the name of the class being traced.
*
* @return The name of the class being traced.
*/
public final String getClassName() {
return className;
}
/**
* Accessor for the method being traced.
*
* @return The name of the method being traced.
*/
public final String getMethodName() {
return methodName;
}
/**
* Accessor for the option-path being traced. The <i><option-path></i> part of the debug option string
* required for the Eclipse debugging framework.
*
* <pre>
* Examples:
* 1) If a trace string com.ibm.myplugin.core/debug=true is specified then 'debug' is the option-path value.
* 2) If a trace string com.ibm.myplugin.core/debug/perf=true is specified then 'debug/perf' is the option-path value.
* </pre>
*
*
* @return The option-path being traced.
*/
public final String getOptionPath() {
return optionPath;
}
/**
* Return the line number in the class/method where the trace originator
*
* @return The line number from the class and method where the trace request originated
*/
public final int getLineNumber() {
return lineNumber;
}
/**
*
* @param newMessage
*/
void setMessage(final String newMessage) {
message = newMessage;
}
}
|