Skip to content

Commit

Permalink
#531: prevent logging from context constructor (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Aug 29, 2024
1 parent d46fd0d commit f1b1801
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 206 deletions.
4 changes: 3 additions & 1 deletion cli/src/main/java/com/devonfw/tools/ide/cli/Ideasy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.devonfw.tools.ide.context.AbstractIdeContext;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeContextConsole;
import com.devonfw.tools.ide.context.IdeStartContextImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.property.FlagProperty;
import com.devonfw.tools.ide.property.Property;
Expand Down Expand Up @@ -110,7 +111,8 @@ private AbstractIdeContext initContext(CliArguments arguments) {
}
}
contextCommandlet.run();
return contextCommandlet.getIdeContext();
IdeStartContextImpl startContext = contextCommandlet.getStartContext();
return new IdeContextConsole(startContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.devonfw.tools.ide.context.AbstractIdeContext;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeContextConsole;
import com.devonfw.tools.ide.context.IdeStartContextImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLoggerOut;
import com.devonfw.tools.ide.property.FlagProperty;
import com.devonfw.tools.ide.property.LocaleProperty;

Expand All @@ -26,7 +27,7 @@ public class ContextCommandlet extends Commandlet {

private final LocaleProperty locale;

private AbstractIdeContext ideContext;
private IdeStartContextImpl startContext;

/**
* The constructor.
Expand Down Expand Up @@ -58,6 +59,22 @@ public boolean isIdeHomeRequired() {
@Override
public void run() {

IdeLogLevel logLevel = determineLogLevel();
if (this.startContext == null) {
this.startContext = new IdeStartContextImpl(logLevel, level -> new IdeSubLoggerOut(level, null, true, logLevel));
} else if (this.context != null) {
IdeStartContextImpl newStartContext = ((AbstractIdeContext) this.context).getStartContext();
assert (this.startContext == newStartContext);
this.startContext = newStartContext;
}
this.startContext.setBatchMode(this.batch.isTrue());
this.startContext.setForceMode(this.force.isTrue());
this.startContext.setQuietMode(this.quiet.isTrue());
this.startContext.setOfflineMode(this.offline.isTrue());
this.startContext.setLocale(this.locale.getValue());
}

private IdeLogLevel determineLogLevel() {
IdeLogLevel logLevel = IdeLogLevel.INFO;
if (this.trace.isTrue()) {
logLevel = IdeLogLevel.TRACE;
Expand All @@ -66,20 +83,14 @@ public void run() {
} else if (this.quiet.isTrue()) {
logLevel = IdeLogLevel.WARNING;
}

this.ideContext = new IdeContextConsole(logLevel, null, true);
this.ideContext.setBatchMode(this.batch.isTrue());
this.ideContext.setForceMode(this.force.isTrue());
this.ideContext.setQuietMode(this.quiet.isTrue());
this.ideContext.setOfflineMode(this.offline.isTrue());
this.ideContext.setLocale(this.locale.getValue());
return logLevel;
}

/**
* @return the {@link IdeContext} that has been created by {@link #run()}.
* @return the {@link IdeStartContextImpl} that has been created by {@link #run()}.
*/
public AbstractIdeContext getIdeContext() {
public IdeStartContextImpl getStartContext() {

return this.ideContext;
return this.startContext;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.devonfw.tools.ide.io.FileAccessImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeLogger;
import com.devonfw.tools.ide.log.IdeLoggerImpl;
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.merge.DirectoryMerger;
import com.devonfw.tools.ide.network.ProxyContext;
Expand All @@ -51,6 +50,7 @@
import com.devonfw.tools.ide.step.Step;
import com.devonfw.tools.ide.step.StepImpl;
import com.devonfw.tools.ide.url.model.UrlMetadata;
import com.devonfw.tools.ide.variable.IdeVariables;

/**
* Abstract base implementation of {@link IdeContext}.
Expand All @@ -59,7 +59,7 @@ public abstract class AbstractIdeContext implements IdeContext {

private static final String IDE_URLS_GIT = "https://github.com/devonfw/ide-urls.git";

private final IdeLoggerImpl logger;
private final IdeStartContextImpl startContext;

private Path ideHome;

Expand Down Expand Up @@ -115,16 +115,6 @@ public abstract class AbstractIdeContext implements IdeContext {

private DirectoryMerger workspaceMerger;

private boolean offlineMode;

private boolean forceMode;

private boolean batchMode;

private boolean quietMode;

private Locale locale;

private UrlMetadata urlMetadata;

private Path defaultExecutionDirectory;
Expand All @@ -134,15 +124,15 @@ public abstract class AbstractIdeContext implements IdeContext {
/**
* The constructor.
*
* @param logger the {@link IdeLogger}.
* @param startContext the {@link IdeLogger}.
* @param userDir the optional {@link Path} to current working directory.
* @param toolRepository @param toolRepository the {@link ToolRepository} of the context. If it is set to {@code null} {@link DefaultToolRepository} will
* be used.
*/
public AbstractIdeContext(IdeLoggerImpl logger, Path userDir, ToolRepository toolRepository) {
public AbstractIdeContext(IdeStartContextImpl startContext, Path userDir, ToolRepository toolRepository) {

super();
this.logger = logger;
this.startContext = startContext;
this.systemInfo = SystemInfoImpl.INSTANCE;
this.commandletManager = new CommandletManagerImpl(this);
this.fileAccess = new FileAccessImpl(this);
Expand Down Expand Up @@ -205,27 +195,25 @@ public AbstractIdeContext(IdeLoggerImpl logger, Path userDir, ToolRepository too
}

private Path findIdeRoot(Path ideHomePath) {
final Path ideRoot;

Path ideRootPath = null;
if (ideHomePath != null) {
ideRootPath = ideHomePath.getParent();
} else if (!isTest()) {
ideRootPath = getIdeRootPathFromEnv();
}
return ideRootPath;
}

if (!isTest()) {
String root = System.getenv("IDE_ROOT");
if (root != null) {
Path rootPath = Path.of(root);
if ((ideRootPath == null)) {
if (Files.isDirectory(rootPath)) {
ideRootPath = rootPath;
}
} else if (!ideRootPath.equals(rootPath)) {
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", rootPath,
(ideHomePath == null) ? "undefined" : ideHomePath.getFileName(), ideRootPath);
}
private static Path getIdeRootPathFromEnv() {
String root = System.getenv(IdeVariables.IDE_ROOT.getName());
if (root != null) {
Path rootPath = Path.of(root);
if (Files.isDirectory(rootPath)) {
return rootPath;
}
}
return ideRootPath;
return null;
}

@Override
Expand Down Expand Up @@ -546,57 +534,25 @@ public UrlMetadata getUrls() {
@Override
public boolean isQuietMode() {

return this.quietMode;
}

/**
* @param quietMode new value of {@link #isQuietMode()}.
*/
public void setQuietMode(boolean quietMode) {

this.quietMode = quietMode;
return this.startContext.isQuietMode();
}

@Override
public boolean isBatchMode() {

return this.batchMode;
}

/**
* @param batchMode new value of {@link #isBatchMode()}.
*/
public void setBatchMode(boolean batchMode) {

this.batchMode = batchMode;
return this.startContext.isBatchMode();
}

@Override
public boolean isForceMode() {

return this.forceMode;
}

/**
* @param forceMode new value of {@link #isForceMode()}.
*/
public void setForceMode(boolean forceMode) {

this.forceMode = forceMode;
return this.startContext.isForceMode();
}

@Override
public boolean isOfflineMode() {

return this.offlineMode;
}

/**
* @param offlineMode new value of {@link #isOfflineMode()}.
*/
public void setOfflineMode(boolean offlineMode) {

this.offlineMode = offlineMode;
return this.startContext.isOfflineMode();
}

@Override
Expand All @@ -621,18 +577,11 @@ public boolean isOnline() {
@Override
public Locale getLocale() {

if (this.locale == null) {
return Locale.getDefault();
Locale locale = this.startContext.getLocale();
if (locale == null) {
locale = Locale.getDefault();
}
return this.locale;
}

/**
* @param locale new value of {@link #getLocale()}.
*/
public void setLocale(Locale locale) {

this.locale = locale;
return locale;
}

@Override
Expand Down Expand Up @@ -694,7 +643,7 @@ protected ProcessContext createProcessContext() {
@Override
public IdeSubLogger level(IdeLogLevel level) {

return this.logger.level(level);
return this.startContext.level(level);
}

@Override
Expand Down Expand Up @@ -885,12 +834,26 @@ private boolean applyAndRun(CliArguments arguments, Commandlet cmd) {
throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
}
if (cmd.isProcessableOutput()) {
for (IdeLogLevel level : IdeLogLevel.values()) {
if (level != IdeLogLevel.INFO) {
this.logger.setLogLevel(level, false);
if (!debug().isEnabled()) {
// unless --debug or --trace was supplied, processable output commandlets will disable all log-levels except INFO to prevent other logs interfere
for (IdeLogLevel level : IdeLogLevel.values()) {
if (level != IdeLogLevel.INFO) {
this.startContext.setLogLevel(level, false);
}
}
}
} else {
if (!isTest()) {
if (this.ideRoot == null) {
warning("Variable IDE_ROOT is undefined. Please check your installation or run setup script again.");
} else if (this.ideHome != null) {
Path ideRootPath = getIdeRootPathFromEnv();
if (!this.ideRoot.equals(ideRootPath)) {
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", ideRootPath,
this.ideHome.getFileName(), this.ideRoot);
}
}
}
if (cmd.isIdeHomeRequired()) {
debug(getMessageIdeHomeFound());
}
Expand Down Expand Up @@ -1085,4 +1048,12 @@ public void setPathSyntax(WindowsPathSyntax pathSyntax) {

this.pathSyntax = pathSyntax;
}

/**
* @return the {@link IdeStartContextImpl}.
*/
public IdeStartContextImpl getStartContext() {

return startContext;
}
}
29 changes: 1 addition & 28 deletions cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;

import com.devonfw.tools.ide.cli.CliAbortException;
import com.devonfw.tools.ide.cli.CliException;
Expand All @@ -13,7 +12,6 @@
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.io.IdeProgressBar;
import com.devonfw.tools.ide.log.IdeLogger;
import com.devonfw.tools.ide.merge.DirectoryMerger;
import com.devonfw.tools.ide.network.ProxyContext;
import com.devonfw.tools.ide.os.SystemInfo;
Expand All @@ -29,7 +27,7 @@
/**
* Interface for interaction with the user allowing to input and output information.
*/
public interface IdeContext extends IdeLogger {
public interface IdeContext extends IdeStartContext {

/**
* The default settings URL.
Expand Down Expand Up @@ -137,26 +135,6 @@ public interface IdeContext extends IdeLogger {
/** Legacy folder name used as compatibility fallback if {@link #FOLDER_TEMPLATES} does not exist. */
String FOLDER_LEGACY_TEMPLATES = "devon";

/**
* @return {@code true} in case of quiet mode (reduced output), {@code false} otherwise.
*/
boolean isQuietMode();

/**
* @return {@code true} in case of batch mode (no {@link #question(String) user-interaction}), {@code false} otherwise.
*/
boolean isBatchMode();

/**
* @return {@code true} in case of force mode, {@code false} otherwise.
*/
boolean isForceMode();

/**
* @return {@code true} if offline mode is activated (-o/--offline), {@code false} otherwise.
*/
boolean isOfflineMode();

/**
* @return {@code true} if {@link #isOfflineMode() offline mode} is active or we are NOT {@link #isOnline() online}, {@code false} otherwise.
*/
Expand Down Expand Up @@ -414,11 +392,6 @@ default Path getSettingsTemplatePath() {
*/
SystemPath getPath();

/**
* @return the current {@link Locale}. Either configured via command-line option or {@link Locale#getDefault() default}.
*/
Locale getLocale();

/**
* @return a new {@link ProcessContext} to {@link ProcessContext#run() run} external commands.
*/
Expand Down
Loading

0 comments on commit f1b1801

Please sign in to comment.