Skip to content

Make logging customizable #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2019
Merged
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 @@ -13,6 +13,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.sun.jdi.ThreadReference;
import com.sun.jdi.VirtualMachine;
Expand All @@ -22,10 +23,11 @@

public class DebugSession implements IDebugSession {
private VirtualMachine vm;
private EventHub eventHub = new EventHub();
private EventHub eventHub;

public DebugSession(VirtualMachine virtualMachine) {
public DebugSession(VirtualMachine virtualMachine, Logger logger) {
vm = virtualMachine;
eventHub = new EventHub(logger);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@

package com.microsoft.java.debug.core;

import java.util.logging.Logger;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.microsoft.java.debug.core.protocol.JsonUtils;

public final class DebugSettings {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static DebugSettings current = new DebugSettings();

public int maxStringLength = 0;
Expand All @@ -41,12 +38,8 @@ public static DebugSettings getCurrent() {
* @param jsonSettings
* the new settings represents in json format.
*/
public void updateSettings(String jsonSettings) {
try {
current = JsonUtils.fromJson(jsonSettings, DebugSettings.class);
} catch (JsonSyntaxException ex) {
logger.severe(String.format("Invalid json for debugSettings: %s, %s", jsonSettings, ex.getMessage()));
}
public void updateSettings(String jsonSettings) throws JsonSyntaxException {
current = JsonUtils.fromJson(jsonSettings, DebugSettings.class);
}

private DebugSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;

import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -66,7 +67,8 @@ public static IDebugSession launch(VirtualMachineManager vmManager,
List<String> modulePaths,
List<String> classPaths,
String cwd,
String[] envVars)
String[] envVars,
Logger logger)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
return DebugUtility.launch(vmManager,
mainClass,
Expand All @@ -75,7 +77,8 @@ public static IDebugSession launch(VirtualMachineManager vmManager,
String.join(File.pathSeparator, modulePaths),
String.join(File.pathSeparator, classPaths),
cwd,
envVars);
envVars,
logger);
}

/**
Expand Down Expand Up @@ -114,7 +117,8 @@ public static IDebugSession launch(VirtualMachineManager vmManager,
String modulePaths,
String classPaths,
String cwd,
String[] envVars)
String[] envVars,
Logger logger)
throws IOException, IllegalConnectorArgumentsException, VMStartException {
List<LaunchingConnector> connectors = vmManager.launchingConnectors();
LaunchingConnector connector = connectors.get(0);
Expand Down Expand Up @@ -176,7 +180,7 @@ public static IDebugSession launch(VirtualMachineManager vmManager,
// Without this line, it throws ObjectCollectedException in ExceptionRequest.enable().
// See https://github.com/Microsoft/java-debug/issues/23
vm.version();
return new DebugSession(vm);
return new DebugSession(vm, logger);
}

/**
Expand All @@ -195,7 +199,7 @@ public static IDebugSession launch(VirtualMachineManager vmManager,
* @throws IllegalConnectorArgumentsException
* when one of the connector arguments is invalid.
*/
public static IDebugSession attach(VirtualMachineManager vmManager, String hostName, int port, int attachTimeout)
public static IDebugSession attach(VirtualMachineManager vmManager, String hostName, int port, int attachTimeout, Logger logger)
throws IOException, IllegalConnectorArgumentsException {
List<AttachingConnector> connectors = vmManager.attachingConnectors();
AttachingConnector connector = connectors.get(0);
Expand All @@ -211,7 +215,7 @@ public static IDebugSession attach(VirtualMachineManager vmManager, String hostN
arguments.get(HOSTNAME).setValue(hostName);
arguments.get(PORT).setValue(String.valueOf(port));
arguments.get(TIMEOUT).setValue(String.valueOf(attachTimeout));
return new DebugSession(connector.attach(arguments));
return new DebugSession(connector.attach(arguments), logger);
}

/**
Expand Down Expand Up @@ -543,8 +547,7 @@ private static List<String> parseArgumentsNonWindows(String args) {
* This piece of code is mainly copied from
* https://github.com/eclipse/eclipse.platform.debug/blob/master/org.eclipse.debug.core/core/org/eclipse/debug/core/DebugPlugin.java#L1264
*
* @param args
* the command line arguments as a single string.
* @param args the command line arguments as a single string.
* @return the individual arguments
*/
private static List<String> parseArgumentsWindows(String args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
import io.reactivex.subjects.PublishSubject;

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

public EventHub(Logger logger) {
this.logger = logger;
}

@Override
public Observable<DebugEvent> events() {
return subject;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* 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.Logger;

@FunctionalInterface
public interface LoggerFactory {
Logger create(String name);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*******************************************************************************
* 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
*******************************************************************************/
* 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;

Expand All @@ -28,8 +28,8 @@
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 final Logger logger;
private final Logger usageDataLogger;
private static final long RESPONSE_MAX_DELAY_MS = 1000;
private static final ThreadLocal<UsageDataSession> threadLocal = new InheritableThreadLocal<>();

Expand All @@ -48,7 +48,16 @@ public static String getSessionGuid() {
return threadLocal.get() == null ? "" : threadLocal.get().sessionGuid;
}

public UsageDataSession() {
public static UsageDataSession currentSession() {
return threadLocal.get();
}

/**
* Constructor.
*/
public UsageDataSession(Logger logger, LoggerFactory factory) {
this.logger = logger;
this.usageDataLogger = factory.create(Configuration.USAGE_DATA_LOGGER_NAME);
threadLocal.set(this);
}

Expand Down Expand Up @@ -153,16 +162,13 @@ public void submitUsageData() {
/**
* Record JDI event.
*/
public static void recordEvent(Event event) {
public void recordEvent(Event event) {
try {
UsageDataSession currentSession = threadLocal.get();
if (currentSession != null) {
Map<String, String> eventEntry = new HashMap<>();
eventEntry.put("timestamp", String.valueOf(System.currentTimeMillis()));
eventEntry.put("event", event.toString());
synchronized (currentSession.eventList) {
currentSession.eventList.add(JsonUtils.toJson(eventEntry));
}
Map<String, String> eventEntry = new HashMap<>();
eventEntry.put("timestamp", String.valueOf(System.currentTimeMillis()));
eventEntry.put("event", event.toString());
synchronized (eventList) {
eventList.add(JsonUtils.toJson(eventEntry));
}
} catch (Exception e) {
logger.log(Level.SEVERE, String.format("Exception on recording event: %s.", e.toString()), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import java.util.logging.Level;
import java.util.logging.Logger;

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

public class BreakpointManager {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private final Logger logger;
/**
* A collection of breakpoints registered with this manager.
*/
Expand All @@ -35,7 +34,8 @@ public class BreakpointManager {
/**
* Constructor.
*/
public BreakpointManager() {
public BreakpointManager(Logger logger) {
this.logger = logger;
this.breakpoints = Collections.synchronizedList(new ArrayList<>(5));
this.sourceToBreakpoints = new HashMap<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.adapter.handler.AttachRequestHandler;
import com.microsoft.java.debug.core.adapter.handler.CompletionsHandler;
import com.microsoft.java.debug.core.adapter.handler.ConfigurationDoneRequestHandler;
Expand Down Expand Up @@ -47,7 +46,7 @@
import com.microsoft.java.debug.core.protocol.Requests.Command;

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

private IDebugAdapterContext debugContext = null;
private Map<Command, List<IDebugRequestHandler>> requestHandlersForDebug = null;
Expand All @@ -56,7 +55,8 @@ public class DebugAdapter implements IDebugAdapter {
/**
* Constructor.
*/
public DebugAdapter(IProtocolServer server, IProviderContext providerContext) {
public DebugAdapter(IProtocolServer server, IProviderContext providerContext, Logger logger) {
this.logger = logger;
this.debugContext = new DebugAdapterContext(server, providerContext);
requestHandlersForDebug = new HashMap<>();
requestHandlersForNoDebug = new HashMap<>();
Expand Down Expand Up @@ -97,29 +97,29 @@ private void initialize() {
// Register request handlers.
// When there are multiple handlers registered for the same request, follow the rule "first register, first execute".
registerHandler(new InitializeRequestHandler());
registerHandler(new LaunchRequestHandler());
registerHandler(new LaunchRequestHandler(logger));

// DEBUG node only
registerHandlerForDebug(new AttachRequestHandler());
registerHandlerForDebug(new ConfigurationDoneRequestHandler());
registerHandlerForDebug(new DisconnectRequestHandler());
registerHandlerForDebug(new SetBreakpointsRequestHandler());
registerHandlerForDebug(new AttachRequestHandler(logger));
registerHandlerForDebug(new ConfigurationDoneRequestHandler(logger));
registerHandlerForDebug(new DisconnectRequestHandler(logger));
registerHandlerForDebug(new SetBreakpointsRequestHandler(logger));
registerHandlerForDebug(new SetExceptionBreakpointsRequestHandler());
registerHandlerForDebug(new SourceRequestHandler());
registerHandlerForDebug(new ThreadsRequestHandler());
registerHandlerForDebug(new StepRequestHandler());
registerHandlerForDebug(new StackTraceRequestHandler());
registerHandlerForDebug(new ScopesRequestHandler());
registerHandlerForDebug(new VariablesRequestHandler());
registerHandlerForDebug(new VariablesRequestHandler(logger));
registerHandlerForDebug(new SetVariableRequestHandler());
registerHandlerForDebug(new EvaluateRequestHandler());
registerHandlerForDebug(new EvaluateRequestHandler(logger));
registerHandlerForDebug(new HotCodeReplaceHandler());
registerHandlerForDebug(new RestartFrameHandler());
registerHandlerForDebug(new CompletionsHandler());
registerHandlerForDebug(new ExceptionInfoRequestHandler());
registerHandlerForDebug(new ExceptionInfoRequestHandler(logger));

// NO_DEBUG mode only
registerHandlerForNoDebug(new DisconnectRequestWithoutDebuggingHandler());
registerHandlerForNoDebug(new DisconnectRequestWithoutDebuggingHandler(logger));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.DebugException;
import com.microsoft.java.debug.core.LoggerFactory;
import com.microsoft.java.debug.core.UsageDataSession;
import com.microsoft.java.debug.core.protocol.AbstractProtocolServer;
import com.microsoft.java.debug.core.protocol.Events.DebugEvent;
Expand All @@ -29,15 +30,23 @@
import com.sun.jdi.VMDisconnectedException;

public class ProtocolServer extends AbstractProtocolServer {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);

private IDebugAdapter debugAdapter;
private UsageDataSession usageDataSession = new UsageDataSession();
private UsageDataSession usageDataSession;

private Object lock = new Object();
private boolean isDispatchingRequest = false;
private ConcurrentLinkedQueue<DebugEvent> eventQueue = new ConcurrentLinkedQueue<>();


/**
* Constructor.
*/
public ProtocolServer(InputStream input, OutputStream output, IProviderContext context, LoggerFactory factory) {
super(input, output, factory.create(Configuration.LOGGER_NAME));
debugAdapter = new DebugAdapter(this, context, logger);
usageDataSession = new UsageDataSession(logger, factory);
}

/**
* Constructs a protocol server instance based on the given input stream and output stream.
* @param input
Expand All @@ -48,8 +57,7 @@ public class ProtocolServer extends AbstractProtocolServer {
* provider context for a series of provider implementation
*/
public ProtocolServer(InputStream input, OutputStream output, IProviderContext context) {
super(input, output);
debugAdapter = new DebugAdapter(this, context);
this(input, output, context, Logger::getLogger);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.microsoft.java.debug.core.Configuration;
import com.microsoft.java.debug.core.adapter.IDebugAdapterContext;
import com.microsoft.java.debug.core.adapter.IDebugRequestHandler;
import com.microsoft.java.debug.core.adapter.LaunchMode;
Expand All @@ -29,7 +28,11 @@
import com.microsoft.java.debug.core.protocol.Requests.Command;

public abstract class AbstractDisconnectRequestHandler implements IDebugRequestHandler {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private final Logger logger;

public AbstractDisconnectRequestHandler(Logger logger) {
this.logger = logger;
}

@Override
public List<Command> getTargetCommands() {
Expand Down
Loading