Skip to content

Commit

Permalink
Redirect stdio streams
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <fbricon@gmail.com>
  • Loading branch information
fbricon authored and angelozerr committed Sep 16, 2020
1 parent 7ea504d commit 044bef0
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 41 deletions.
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx;

import java.io.IOException;
import java.io.InputStream;

/**
* No-Op {@link InputStream}
*
* @author Fred Bricon
*/
class NoOpInputStream extends InputStream {

@Override
public int read() throws IOException {
return -1;
}

@Override
public int read(byte[] b) throws IOException {
return -1;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
return -1;
}

@Override
public int available() throws IOException {
return 0;
}

@Override
public synchronized void mark(int readlimit) {
}

@Override
public boolean markSupported() {
return true;
}

@Override
public synchronized void reset() throws IOException {
}

@Override
public long skip(long n) throws IOException {
return 0;
}

@Override
public void close() throws IOException {
}

}
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx;

import java.io.IOException;
import java.io.OutputStream;

/**
* No-Op {@link OutputStream}.
*
* @author Fred Bricon
*/
class NoOpOutputStream extends OutputStream {

@Override
public void write(int b) throws IOException {
}

@Override
public void write(byte[] b, int off, int len) {
}

@Override
public void write(byte[] b) throws IOException {
}

}
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx;

import java.io.PrintStream;

/**
* No-Op {@link PrintStream}.
*
* @author Fred Bricon
*/
class NoOpPrintStream extends PrintStream {

public NoOpPrintStream() {
super(new NoOpOutputStream());
}

}
Expand Up @@ -94,6 +94,12 @@ public XMLLanguageServer() {

@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
Object initOptions = InitializationOptionsSettings.getSettings(params);
Object xmlSettings = AllXMLSettings.getAllXMLSettings(initOptions);
XMLGeneralClientSettings settings = XMLGeneralClientSettings.getGeneralXMLSettings(xmlSettings);

LogHelper.initializeRootLogger(languageClient, settings == null? null : settings.getLogs());

LOGGER.info("Initializing XML Language server" + System.lineSeparator() + new ServerInfo().details());

this.parentProcessId = params.getProcessId();
Expand All @@ -108,7 +114,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
xmlTextDocumentService.updateClientCapabilities(capabilityManager.getClientCapabilities().capabilities,
capabilityManager.getClientCapabilities().getExtendedCapabilities());

updateSettings(InitializationOptionsSettings.getSettings(params));
updateSettings(initOptions, false /* already configured logging*/ );

ServerCapabilities nonDynamicServerCapabilities = ServerCapabilitiesInitializer.getNonDynamicServerCapabilities(
capabilityManager.getClientCapabilities(), xmlTextDocumentService.isIncrementalSupport());
Expand All @@ -133,22 +139,31 @@ public void initialized(InitializedParams params) {
/**
* Update XML settings configured from the client.
*
* @param initializationOptionsSettings the XML settings
* @param initOptions the XML settings
*/
public synchronized void updateSettings(Object initOptions) {
updateSettings(initOptions, true);
}

/**
* Update XML settings configured from the client.
*
* @param initOptions Settings the XML settings
* @param initLogs whether to initialize the log handlers
*/
public synchronized void updateSettings(Object initializationOptionsSettings) {
if (initializationOptionsSettings == null) {
private synchronized void updateSettings(Object initOptions, boolean initLogs) {
if (initOptions == null) {
return;
}
// Update client settings
initializationOptionsSettings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings);
XMLGeneralClientSettings xmlClientSettings = XMLGeneralClientSettings
.getGeneralXMLSettings(initializationOptionsSettings);
Object initSettings = AllXMLSettings.getAllXMLSettings(initOptions);
XMLGeneralClientSettings xmlClientSettings = XMLGeneralClientSettings.getGeneralXMLSettings(initSettings);
if (xmlClientSettings != null) {
// Update logs settings
LogsSettings logsSettings = xmlClientSettings.getLogs();
if (logsSettings != null) {
LogHelper.initializeRootLogger(languageClient, logsSettings);
if (initLogs) {
// Update logs settings
LogHelper.initializeRootLogger(languageClient, xmlClientSettings.getLogs());
}

// Update format settings
XMLFormattingOptions formatterSettings = xmlClientSettings.getFormat();
if (formatterSettings != null) {
Expand Down Expand Up @@ -181,15 +196,14 @@ public synchronized void updateSettings(Object initializationOptionsSettings) {
FilesUtils.setCachePathSetting(workDir);
}
}
ContentModelSettings cmSettings = ContentModelSettings
.getContentModelXMLSettings(initializationOptionsSettings);
ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initSettings);
if (cmSettings != null) {
XMLValidationSettings validationSettings = cmSettings.getValidation();
xmlTextDocumentService.getValidationSettings().merge(validationSettings);

}
// Update XML language service extensions
xmlTextDocumentService.updateSettings(initializationOptionsSettings);
xmlTextDocumentService.updateSettings(initSettings);
}

@Override
Expand Down
Expand Up @@ -14,6 +14,7 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -48,7 +49,12 @@ protected PasswordAuthentication getPasswordAuthentication() {

});
}
launch(System.in, System.out);
InputStream in = System.in;
PrintStream out = System.out;
System.setIn(new NoOpInputStream());
System.setOut(new NoOpPrintStream());
System.setErr(new NoOpPrintStream());
launch(in, out);
}

/**
Expand Down
Expand Up @@ -31,43 +31,44 @@
*/
public class LogHelper {

private static final String ROOT_LOGGER = "";

// This will apply to all child loggers
public static void initializeRootLogger(LanguageClient newLanguageClient, LogsSettings settings) {
if (newLanguageClient == null || settings == null) {
if (newLanguageClient == null) {
return;
}

Logger logger = Logger.getLogger("");
Logger logger = Logger.getLogger(ROOT_LOGGER);
unregisterAllHandlers(logger.getHandlers());
logger.setLevel(getLogLevel());
logger.setUseParentHandlers(false);// Stops output to console

// Configure logging LSP client handler
if (settings.getClient()) {
try {
logger.addHandler(LogHelper.getClientHandler(newLanguageClient));
} catch (Exception e) {
// TODO: handle exception
if (settings != null) {
if (settings.isClient()) {
try {
logger.addHandler(LogHelper.getClientHandler(newLanguageClient));
} catch (Exception e) {
// TODO: handle exception
}
}
}

// Configure logging for file
String path = settings.getFile();
if (path != null) {
createDirectoryPath(path);
try {
FileHandler fh = LogHelper.getFileHandler(path);
logger.addHandler(fh);

} catch (SecurityException | IOException e) {
logger.warning("Error at creation of FileHandler for logging");
// Configure logging for file
String path = settings.getFile();
if (path != null) {
createDirectoryPath(path);
try {
FileHandler fh = LogHelper.getFileHandler(path);
logger.addHandler(fh);
} catch (SecurityException | IOException e) {
logger.warning("Error at creation of FileHandler for logging");
}
} else {
logger.info("Log file could not be created, path not provided");
}
} else {
logger.info("Log file could not be created, path not provided");
}

}

private static Level getLogLevel() {
String logLevel = System.getProperty("log.level", "info").toLowerCase();
switch (logLevel) {
Expand Down Expand Up @@ -132,7 +133,7 @@ public static void unregisterHandler(Handler handler) {
return;
}
handler.close();
Logger.getLogger("").removeHandler(handler);
Logger.getLogger(ROOT_LOGGER).removeHandler(handler);
}

public static void unregisterAllHandlers(Handler[] handlers) {
Expand All @@ -141,7 +142,6 @@ public static void unregisterAllHandlers(Handler[] handlers) {
}
for (Handler h : handlers) {
unregisterHandler(h);
;
}
}
}
Expand Up @@ -42,7 +42,7 @@ public void setFile(String file) {
* @return <code>true</code> if LSP client is enabled and <code>false</code>
* otherwise.
*/
public boolean getClient() {
public boolean isClient() {
return client;
}

Expand Down

0 comments on commit 044bef0

Please sign in to comment.