Skip to content

Commit f1b1801

Browse files
authored
#531: prevent logging from context constructor (#559)
1 parent d46fd0d commit f1b1801

File tree

12 files changed

+298
-206
lines changed

12 files changed

+298
-206
lines changed

cli/src/main/java/com/devonfw/tools/ide/cli/Ideasy.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.devonfw.tools.ide.context.AbstractIdeContext;
88
import com.devonfw.tools.ide.context.IdeContext;
99
import com.devonfw.tools.ide.context.IdeContextConsole;
10+
import com.devonfw.tools.ide.context.IdeStartContextImpl;
1011
import com.devonfw.tools.ide.log.IdeLogLevel;
1112
import com.devonfw.tools.ide.property.FlagProperty;
1213
import com.devonfw.tools.ide.property.Property;
@@ -110,7 +111,8 @@ private AbstractIdeContext initContext(CliArguments arguments) {
110111
}
111112
}
112113
contextCommandlet.run();
113-
return contextCommandlet.getIdeContext();
114+
IdeStartContextImpl startContext = contextCommandlet.getStartContext();
115+
return new IdeContextConsole(startContext);
114116
}
115117

116118
}

cli/src/main/java/com/devonfw/tools/ide/commandlet/ContextCommandlet.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import com.devonfw.tools.ide.context.AbstractIdeContext;
44
import com.devonfw.tools.ide.context.IdeContext;
5-
import com.devonfw.tools.ide.context.IdeContextConsole;
5+
import com.devonfw.tools.ide.context.IdeStartContextImpl;
66
import com.devonfw.tools.ide.log.IdeLogLevel;
7+
import com.devonfw.tools.ide.log.IdeSubLoggerOut;
78
import com.devonfw.tools.ide.property.FlagProperty;
89
import com.devonfw.tools.ide.property.LocaleProperty;
910

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

2728
private final LocaleProperty locale;
2829

29-
private AbstractIdeContext ideContext;
30+
private IdeStartContextImpl startContext;
3031

3132
/**
3233
* The constructor.
@@ -58,6 +59,22 @@ public boolean isIdeHomeRequired() {
5859
@Override
5960
public void run() {
6061

62+
IdeLogLevel logLevel = determineLogLevel();
63+
if (this.startContext == null) {
64+
this.startContext = new IdeStartContextImpl(logLevel, level -> new IdeSubLoggerOut(level, null, true, logLevel));
65+
} else if (this.context != null) {
66+
IdeStartContextImpl newStartContext = ((AbstractIdeContext) this.context).getStartContext();
67+
assert (this.startContext == newStartContext);
68+
this.startContext = newStartContext;
69+
}
70+
this.startContext.setBatchMode(this.batch.isTrue());
71+
this.startContext.setForceMode(this.force.isTrue());
72+
this.startContext.setQuietMode(this.quiet.isTrue());
73+
this.startContext.setOfflineMode(this.offline.isTrue());
74+
this.startContext.setLocale(this.locale.getValue());
75+
}
76+
77+
private IdeLogLevel determineLogLevel() {
6178
IdeLogLevel logLevel = IdeLogLevel.INFO;
6279
if (this.trace.isTrue()) {
6380
logLevel = IdeLogLevel.TRACE;
@@ -66,20 +83,14 @@ public void run() {
6683
} else if (this.quiet.isTrue()) {
6784
logLevel = IdeLogLevel.WARNING;
6885
}
69-
70-
this.ideContext = new IdeContextConsole(logLevel, null, true);
71-
this.ideContext.setBatchMode(this.batch.isTrue());
72-
this.ideContext.setForceMode(this.force.isTrue());
73-
this.ideContext.setQuietMode(this.quiet.isTrue());
74-
this.ideContext.setOfflineMode(this.offline.isTrue());
75-
this.ideContext.setLocale(this.locale.getValue());
86+
return logLevel;
7687
}
7788

7889
/**
79-
* @return the {@link IdeContext} that has been created by {@link #run()}.
90+
* @return the {@link IdeStartContextImpl} that has been created by {@link #run()}.
8091
*/
81-
public AbstractIdeContext getIdeContext() {
92+
public IdeStartContextImpl getStartContext() {
8293

83-
return this.ideContext;
94+
return this.startContext;
8495
}
8596
}

cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java

Lines changed: 51 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.devonfw.tools.ide.io.FileAccessImpl;
3434
import com.devonfw.tools.ide.log.IdeLogLevel;
3535
import com.devonfw.tools.ide.log.IdeLogger;
36-
import com.devonfw.tools.ide.log.IdeLoggerImpl;
3736
import com.devonfw.tools.ide.log.IdeSubLogger;
3837
import com.devonfw.tools.ide.merge.DirectoryMerger;
3938
import com.devonfw.tools.ide.network.ProxyContext;
@@ -51,6 +50,7 @@
5150
import com.devonfw.tools.ide.step.Step;
5251
import com.devonfw.tools.ide.step.StepImpl;
5352
import com.devonfw.tools.ide.url.model.UrlMetadata;
53+
import com.devonfw.tools.ide.variable.IdeVariables;
5454

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

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

62-
private final IdeLoggerImpl logger;
62+
private final IdeStartContextImpl startContext;
6363

6464
private Path ideHome;
6565

@@ -115,16 +115,6 @@ public abstract class AbstractIdeContext implements IdeContext {
115115

116116
private DirectoryMerger workspaceMerger;
117117

118-
private boolean offlineMode;
119-
120-
private boolean forceMode;
121-
122-
private boolean batchMode;
123-
124-
private boolean quietMode;
125-
126-
private Locale locale;
127-
128118
private UrlMetadata urlMetadata;
129119

130120
private Path defaultExecutionDirectory;
@@ -134,15 +124,15 @@ public abstract class AbstractIdeContext implements IdeContext {
134124
/**
135125
* The constructor.
136126
*
137-
* @param logger the {@link IdeLogger}.
127+
* @param startContext the {@link IdeLogger}.
138128
* @param userDir the optional {@link Path} to current working directory.
139129
* @param toolRepository @param toolRepository the {@link ToolRepository} of the context. If it is set to {@code null} {@link DefaultToolRepository} will
140130
* be used.
141131
*/
142-
public AbstractIdeContext(IdeLoggerImpl logger, Path userDir, ToolRepository toolRepository) {
132+
public AbstractIdeContext(IdeStartContextImpl startContext, Path userDir, ToolRepository toolRepository) {
143133

144134
super();
145-
this.logger = logger;
135+
this.startContext = startContext;
146136
this.systemInfo = SystemInfoImpl.INSTANCE;
147137
this.commandletManager = new CommandletManagerImpl(this);
148138
this.fileAccess = new FileAccessImpl(this);
@@ -205,27 +195,25 @@ public AbstractIdeContext(IdeLoggerImpl logger, Path userDir, ToolRepository too
205195
}
206196

207197
private Path findIdeRoot(Path ideHomePath) {
208-
final Path ideRoot;
198+
209199
Path ideRootPath = null;
210200
if (ideHomePath != null) {
211201
ideRootPath = ideHomePath.getParent();
202+
} else if (!isTest()) {
203+
ideRootPath = getIdeRootPathFromEnv();
212204
}
205+
return ideRootPath;
206+
}
213207

214-
if (!isTest()) {
215-
String root = System.getenv("IDE_ROOT");
216-
if (root != null) {
217-
Path rootPath = Path.of(root);
218-
if ((ideRootPath == null)) {
219-
if (Files.isDirectory(rootPath)) {
220-
ideRootPath = rootPath;
221-
}
222-
} else if (!ideRootPath.equals(rootPath)) {
223-
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", rootPath,
224-
(ideHomePath == null) ? "undefined" : ideHomePath.getFileName(), ideRootPath);
225-
}
208+
private static Path getIdeRootPathFromEnv() {
209+
String root = System.getenv(IdeVariables.IDE_ROOT.getName());
210+
if (root != null) {
211+
Path rootPath = Path.of(root);
212+
if (Files.isDirectory(rootPath)) {
213+
return rootPath;
226214
}
227215
}
228-
return ideRootPath;
216+
return null;
229217
}
230218

231219
@Override
@@ -546,57 +534,25 @@ public UrlMetadata getUrls() {
546534
@Override
547535
public boolean isQuietMode() {
548536

549-
return this.quietMode;
550-
}
551-
552-
/**
553-
* @param quietMode new value of {@link #isQuietMode()}.
554-
*/
555-
public void setQuietMode(boolean quietMode) {
556-
557-
this.quietMode = quietMode;
537+
return this.startContext.isQuietMode();
558538
}
559539

560540
@Override
561541
public boolean isBatchMode() {
562542

563-
return this.batchMode;
564-
}
565-
566-
/**
567-
* @param batchMode new value of {@link #isBatchMode()}.
568-
*/
569-
public void setBatchMode(boolean batchMode) {
570-
571-
this.batchMode = batchMode;
543+
return this.startContext.isBatchMode();
572544
}
573545

574546
@Override
575547
public boolean isForceMode() {
576548

577-
return this.forceMode;
578-
}
579-
580-
/**
581-
* @param forceMode new value of {@link #isForceMode()}.
582-
*/
583-
public void setForceMode(boolean forceMode) {
584-
585-
this.forceMode = forceMode;
549+
return this.startContext.isForceMode();
586550
}
587551

588552
@Override
589553
public boolean isOfflineMode() {
590554

591-
return this.offlineMode;
592-
}
593-
594-
/**
595-
* @param offlineMode new value of {@link #isOfflineMode()}.
596-
*/
597-
public void setOfflineMode(boolean offlineMode) {
598-
599-
this.offlineMode = offlineMode;
555+
return this.startContext.isOfflineMode();
600556
}
601557

602558
@Override
@@ -621,18 +577,11 @@ public boolean isOnline() {
621577
@Override
622578
public Locale getLocale() {
623579

624-
if (this.locale == null) {
625-
return Locale.getDefault();
580+
Locale locale = this.startContext.getLocale();
581+
if (locale == null) {
582+
locale = Locale.getDefault();
626583
}
627-
return this.locale;
628-
}
629-
630-
/**
631-
* @param locale new value of {@link #getLocale()}.
632-
*/
633-
public void setLocale(Locale locale) {
634-
635-
this.locale = locale;
584+
return locale;
636585
}
637586

638587
@Override
@@ -694,7 +643,7 @@ protected ProcessContext createProcessContext() {
694643
@Override
695644
public IdeSubLogger level(IdeLogLevel level) {
696645

697-
return this.logger.level(level);
646+
return this.startContext.level(level);
698647
}
699648

700649
@Override
@@ -885,12 +834,26 @@ private boolean applyAndRun(CliArguments arguments, Commandlet cmd) {
885834
throw new CliException(getMessageIdeRootNotFound(), ProcessResult.NO_IDE_ROOT);
886835
}
887836
if (cmd.isProcessableOutput()) {
888-
for (IdeLogLevel level : IdeLogLevel.values()) {
889-
if (level != IdeLogLevel.INFO) {
890-
this.logger.setLogLevel(level, false);
837+
if (!debug().isEnabled()) {
838+
// unless --debug or --trace was supplied, processable output commandlets will disable all log-levels except INFO to prevent other logs interfere
839+
for (IdeLogLevel level : IdeLogLevel.values()) {
840+
if (level != IdeLogLevel.INFO) {
841+
this.startContext.setLogLevel(level, false);
842+
}
891843
}
892844
}
893845
} else {
846+
if (!isTest()) {
847+
if (this.ideRoot == null) {
848+
warning("Variable IDE_ROOT is undefined. Please check your installation or run setup script again.");
849+
} else if (this.ideHome != null) {
850+
Path ideRootPath = getIdeRootPathFromEnv();
851+
if (!this.ideRoot.equals(ideRootPath)) {
852+
warning("Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", ideRootPath,
853+
this.ideHome.getFileName(), this.ideRoot);
854+
}
855+
}
856+
}
894857
if (cmd.isIdeHomeRequired()) {
895858
debug(getMessageIdeHomeFound());
896859
}
@@ -1085,4 +1048,12 @@ public void setPathSyntax(WindowsPathSyntax pathSyntax) {
10851048

10861049
this.pathSyntax = pathSyntax;
10871050
}
1051+
1052+
/**
1053+
* @return the {@link IdeStartContextImpl}.
1054+
*/
1055+
public IdeStartContextImpl getStartContext() {
1056+
1057+
return startContext;
1058+
}
10881059
}

cli/src/main/java/com/devonfw/tools/ide/context/IdeContext.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.nio.file.Files;
44
import java.nio.file.Path;
5-
import java.util.Locale;
65

76
import com.devonfw.tools.ide.cli.CliAbortException;
87
import com.devonfw.tools.ide.cli.CliException;
@@ -13,7 +12,6 @@
1312
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
1413
import com.devonfw.tools.ide.io.FileAccess;
1514
import com.devonfw.tools.ide.io.IdeProgressBar;
16-
import com.devonfw.tools.ide.log.IdeLogger;
1715
import com.devonfw.tools.ide.merge.DirectoryMerger;
1816
import com.devonfw.tools.ide.network.ProxyContext;
1917
import com.devonfw.tools.ide.os.SystemInfo;
@@ -29,7 +27,7 @@
2927
/**
3028
* Interface for interaction with the user allowing to input and output information.
3129
*/
32-
public interface IdeContext extends IdeLogger {
30+
public interface IdeContext extends IdeStartContext {
3331

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

140-
/**
141-
* @return {@code true} in case of quiet mode (reduced output), {@code false} otherwise.
142-
*/
143-
boolean isQuietMode();
144-
145-
/**
146-
* @return {@code true} in case of batch mode (no {@link #question(String) user-interaction}), {@code false} otherwise.
147-
*/
148-
boolean isBatchMode();
149-
150-
/**
151-
* @return {@code true} in case of force mode, {@code false} otherwise.
152-
*/
153-
boolean isForceMode();
154-
155-
/**
156-
* @return {@code true} if offline mode is activated (-o/--offline), {@code false} otherwise.
157-
*/
158-
boolean isOfflineMode();
159-
160138
/**
161139
* @return {@code true} if {@link #isOfflineMode() offline mode} is active or we are NOT {@link #isOnline() online}, {@code false} otherwise.
162140
*/
@@ -414,11 +392,6 @@ default Path getSettingsTemplatePath() {
414392
*/
415393
SystemPath getPath();
416394

417-
/**
418-
* @return the current {@link Locale}. Either configured via command-line option or {@link Locale#getDefault() default}.
419-
*/
420-
Locale getLocale();
421-
422395
/**
423396
* @return a new {@link ProcessContext} to {@link ProcessContext#run() run} external commands.
424397
*/

0 commit comments

Comments
 (0)