Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

package com.microsoft.java.debug.core;

import java.util.logging.Logger;

import com.sun.jdi.VMDisconnectedException;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.event.BreakpointEvent;
Expand All @@ -31,7 +29,6 @@
import io.reactivex.subjects.PublishSubject;

public class EventHub implements IEventHub {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private PublishSubject<DebugEvent> subject = PublishSubject.<DebugEvent>create();

@Override
Expand Down Expand Up @@ -68,7 +65,7 @@ public void start(VirtualMachine vm) {
boolean shouldResume = true;
for (Event event : set) {
try {
logger.fine("\nJDI Event: " + event + "\n");
Log.debug("\nJDI Event: %s\n", event.toString());
} catch (VMDisconnectedException e) {
// do nothing
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*******************************************************************************
* Copyright (c) 2017 Microsoft 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:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.debug.core;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.adapter.Messages;
import com.sun.jdi.event.Event;

/**
* This helper class provides static methods for a default instance of <code>java.util.logging.Logger</code>,a simple code like
* <code>Log.info("Sample trace: %d", 10)</code> is able to replace <code>logger.info(String.format("Sample trace: %d", 10))</code>.
* Using a very short name to better code fitness.
*/
public final class Log {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to wrap around the JDK logger? I didn't see much value added. Am I missing anything?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is mainly to focus on simplifying the logging code to better code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trace user data is also include in Log.java, Log will be the only class need to record logs and user datas.

private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);

/**
* Log an ERROR message.
*
* @param ex The exception object
* @param message The string message
* @param arguments Arguments referenced by the format specifiers in the message
*/
public static void error(Exception ex, String message, Object... arguments) {
logger.log(Level.SEVERE, String.format(message, arguments), ex);
}


/**
* Log an ERROR message.
*
* @param message The string message
* @param arguments Arguments referenced by the format specifiers in the message
*/
public static void error(String message, Object... arguments) {
logger.severe(String.format(message, arguments));
}

/**
* Log a INFO message.
*
* @param message The string message
* @param arguments Arguments referenced by the format specifiers in the message
* string.
*/
public static void info(String message, Object... arguments) {
logger.info(String.format(message, arguments));
}

/**
* Log a WARN message.
*
* @param message The string message
* @param arguments Arguments referenced by the format specifiers in the message
*/
public static void warn(String message, Object... arguments) {
logger.warning(String.format(message, arguments));
}

/**
* Log a DEBUG message.
*
* @param message The string message
* @param arguments Arguments referenced by the format specifiers in the message
*/
public static void debug(String message, Object... arguments) {
logger.fine(String.format(message, arguments));
}


/**
* Set the log level specifying which message levels will be
* logged by this logger. Message levels lower than this
* value will be discarded. The level value Level.OFF
* can be used to turn off logging.
*
* @param newLevel the new value for the log level (may be null)
*/
public static void setLevel(Level newLevel) {
logger.setLevel(newLevel);
}

/**
* Add a log Handler to receive logging messages.
* @param handler a logging Handler
*/
public static void addHandler(Handler handler) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any API corresponding to remove the handler?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

logger.addHandler(handler);
}

/**
* Remove a log Handler.
* Returns silently if the given Handler is not found or is null
*
* @param handler a logging Handler
*/
public static void removeHandler(Handler handler) throws SecurityException {
logger.removeHandler(handler);
}

/**
* Record a JDI event.
* @param event the JDI event.
*/
public static void traceEvent(Event event) {
UsageDataSession.recordEvent(event);
}


/**
* Begin an user data collection session to record events, requests and errors.
*
* @return a new user data collection session
*/
public static UsageDataSession beginSession() {
UsageDataSession session = new UsageDataSession();
session.reportStart();
return session;
}

/**
* Record the request from VSCode side.
*
* @param session user data collection session
* @param request from VSCode.
*/
public static void traceRequest(UsageDataSession session, Messages.Request request) {
if (session != null && request != null) {
session.recordRequest(request);
}
}

/**
* Record the response sends to VSCode.
*
* @param session user data collection session
* @param response to VSCode.
*/
public static void traceResponse(UsageDataSession session, Messages.Response response) {
if (session != null && response != null) {
session.recordResponse(response);
}
}

/**
* End an user data collection session, and report the usage datas.
*
* @param session user data collection session
*/
public static void endSession(UsageDataSession session) {
if (session != null) {
session.reportStop();
session.submitUsageData();
}
}


private Log() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.sun.jdi.event.Event;

public class UsageDataSession {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static final Logger usageDataLogger = Logger.getLogger(Configuration.USAGE_DATA_LOGGER_NAME);
private static final long RESPONSE_MAX_DELAY_MS = 1000;
private static final ThreadLocal<UsageDataSession> threadLocal = new InheritableThreadLocal<>();
Expand Down Expand Up @@ -155,7 +154,7 @@ public static void recordEvent(Event event) {
currentSession.eventList.add(JsonUtils.toJson(eventEntry));
}
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Exception on recording event: %s.", e.toString()), e);
Log.error(e, "Exception on recording event: %s.", e.toString());
}
}

Expand All @@ -169,7 +168,7 @@ public static void enableJdiEventSequence() {
currentSession.jdiEventSequenceEnabled = true;
}
} catch (Exception e) {
// ignore it
Log.error(e, "Exception on enableJdiEventSequence: %s.", e.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.IBreakpoint;
import com.microsoft.java.debug.core.Log;

public class BreakpointManager {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
/**
* A collection of breakpoints registered with this manager.
*/
Expand All @@ -36,8 +33,8 @@ public class BreakpointManager {
* Constructor.
*/
public BreakpointManager() {
this.breakpoints = Collections.synchronizedList(new ArrayList<>(5));
this.sourceToBreakpoints = new HashMap<>();
breakpoints = Collections.synchronizedList(new ArrayList<>(5));
sourceToBreakpoints = new HashMap<>();
}

/**
Expand Down Expand Up @@ -67,24 +64,24 @@ public IBreakpoint[] setBreakpoints(String source, IBreakpoint[] breakpoints) {
*/
public IBreakpoint[] setBreakpoints(String source, IBreakpoint[] breakpoints, boolean sourceModified) {
List<IBreakpoint> result = new ArrayList<>();
HashMap<String, IBreakpoint> breakpointMap = this.sourceToBreakpoints.get(source);
HashMap<String, IBreakpoint> breakpointMap = sourceToBreakpoints.get(source);
// When source file is modified, delete all previously added breakpoints.
if (sourceModified && breakpointMap != null) {
for (IBreakpoint bp : breakpointMap.values()) {
try {
// Destroy the breakpoint on the debugee VM.
bp.close();
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Remove breakpoint exception: %s", e.toString()), e);
Log.error(e, "Remove breakpoint exception: %s", e.toString());
}
this.breakpoints.remove(bp);
}
this.sourceToBreakpoints.put(source, null);
sourceToBreakpoints.put(source, null);
breakpointMap = null;
}
if (breakpointMap == null) {
breakpointMap = new HashMap<>();
this.sourceToBreakpoints.put(source, breakpointMap);
sourceToBreakpoints.put(source, breakpointMap);
}

// Compute the breakpoints that are newly added.
Expand Down Expand Up @@ -117,11 +114,11 @@ public IBreakpoint[] setBreakpoints(String source, IBreakpoint[] breakpoints, bo
}

private void addBreakpointsInternally(String source, IBreakpoint[] breakpoints) {
Map<String, IBreakpoint> breakpointMap = this.sourceToBreakpoints.computeIfAbsent(source, k -> new HashMap<>());
Map<String, IBreakpoint> breakpointMap = sourceToBreakpoints.computeIfAbsent(source, k -> new HashMap<>());

if (breakpoints != null && breakpoints.length > 0) {
for (IBreakpoint breakpoint : breakpoints) {
breakpoint.putProperty("id", this.nextBreakpointId.getAndIncrement());
breakpoint.putProperty("id", nextBreakpointId.getAndIncrement());
this.breakpoints.add(breakpoint);
breakpointMap.put(String.valueOf(breakpoint.lineNumber()), breakpoint);
}
Expand All @@ -132,7 +129,7 @@ private void addBreakpointsInternally(String source, IBreakpoint[] breakpoints)
* Removes the specified breakpoints from breakpoint manager.
*/
private void removeBreakpointsInternally(String source, IBreakpoint[] breakpoints) {
Map<String, IBreakpoint> breakpointMap = this.sourceToBreakpoints.get(source);
Map<String, IBreakpoint> breakpointMap = sourceToBreakpoints.get(source);
if (breakpointMap == null || breakpointMap.isEmpty() || breakpoints.length == 0) {
return;
}
Expand All @@ -145,21 +142,21 @@ private void removeBreakpointsInternally(String source, IBreakpoint[] breakpoint
this.breakpoints.remove(breakpoint);
breakpointMap.remove(String.valueOf(breakpoint.lineNumber()));
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Remove breakpoint exception: %s", e.toString()), e);
Log.error(e, "Remove breakpoint exception: %s", e.toString());
}
}
}
}

public IBreakpoint[] getBreakpoints() {
return this.breakpoints.toArray(new IBreakpoint[0]);
return breakpoints.toArray(new IBreakpoint[0]);
}

/**
* Gets the registered breakpoints at the source file.
*/
public IBreakpoint[] getBreakpoints(String source) {
HashMap<String, IBreakpoint> breakpointMap = this.sourceToBreakpoints.get(source);
HashMap<String, IBreakpoint> breakpointMap = sourceToBreakpoints.get(source);
if (breakpointMap == null) {
return new IBreakpoint[0];
}
Expand All @@ -170,8 +167,8 @@ public IBreakpoint[] getBreakpoints(String source) {
* Cleanup all breakpoints and reset the breakpoint id counter.
*/
public void reset() {
this.sourceToBreakpoints.clear();
this.breakpoints.clear();
this.nextBreakpointId.set(1);
sourceToBreakpoints.clear();
breakpoints.clear();
nextBreakpointId.set(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.Log;
import com.microsoft.java.debug.core.adapter.Requests.Arguments;
import com.microsoft.java.debug.core.adapter.Requests.Command;
import com.microsoft.java.debug.core.adapter.handler.AttachRequestHandler;
Expand All @@ -38,8 +36,6 @@
import com.microsoft.java.debug.core.adapter.handler.VariablesRequestHandler;

public class DebugAdapter implements IDebugAdapter {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);

private BiConsumer<Events.DebugEvent, Boolean> eventConsumer;
private IProviderContext providerContext;
private IDebugAdapterContext debugContext = null;
Expand Down Expand Up @@ -81,7 +77,7 @@ public Messages.Response dispatchRequest(Messages.Request request) {
String.format("Unrecognized request: { _request: %s }", request.command));
}
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("DebugSession dispatch exception: %s", e.toString()), e);
Log.error(e, "DebugSession dispatch exception: %s", e.toString());
AdapterUtils.setErrorResponse(response, ErrorCode.UNKNOWN_FAILURE,
e.getMessage() != null ? e.getMessage() : e.toString());
}
Expand Down
Loading