-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from Roche-CSI/LoggingRunner
LoggingRunner & CommnadLogger simplify debug/trace logging of commands being run
- Loading branch information
Showing
3 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
gb-command/src/main/java/com/g2forge/gearbox/command/log/CommandLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.g2forge.gearbox.command.log; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import com.g2forge.alexandria.java.close.ICloseable; | ||
import com.g2forge.alexandria.java.function.IConsumer1; | ||
import com.g2forge.alexandria.java.io.RuntimeIOException; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class CommandLogger implements ICloseable, IConsumer1<String> { | ||
protected final Path logFile; | ||
|
||
protected final IConsumer1<String> tee; | ||
|
||
protected PrintStream stream = null; | ||
|
||
@Override | ||
public void accept(String t) { | ||
if (stream == null) try { | ||
Files.createDirectories(logFile.getParent()); | ||
stream = new PrintStream(Files.newOutputStream(logFile)); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
stream.println(t); | ||
if (tee != null) tee.accept(t); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (stream == null) { | ||
try { | ||
Files.deleteIfExists(logFile); | ||
} catch (IOException e) { | ||
throw new RuntimeIOException(e); | ||
} | ||
} else stream.close(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
gb-command/src/main/java/com/g2forge/gearbox/command/log/LoggingRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.g2forge.gearbox.command.log; | ||
|
||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Collectors; | ||
|
||
import com.g2forge.alexandria.command.invocation.CommandInvocation; | ||
import com.g2forge.alexandria.command.invocation.environment.IEnvironment; | ||
import com.g2forge.alexandria.command.invocation.environment.MapEnvironment; | ||
import com.g2forge.alexandria.command.invocation.environment.SystemEnvironment; | ||
import com.g2forge.alexandria.command.invocation.environment.modified.IEnvironmentModifier; | ||
import com.g2forge.alexandria.command.invocation.environment.modified.ModifiedEnvironment; | ||
import com.g2forge.alexandria.java.core.error.NotYetImplementedError; | ||
import com.g2forge.alexandria.java.core.helpers.HCollection; | ||
import com.g2forge.alexandria.java.function.IConsumer1; | ||
import com.g2forge.gearbox.command.process.IProcess; | ||
import com.g2forge.gearbox.command.process.IRunner; | ||
import com.g2forge.gearbox.command.process.redirect.IRedirect; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter(AccessLevel.PROTECTED) | ||
@RequiredArgsConstructor | ||
public class LoggingRunner implements IRunner { | ||
protected static String toString(IEnvironment environment, String prefix) { | ||
final StringBuilder retVal = new StringBuilder(); | ||
if (environment instanceof MapEnvironment) { | ||
for (Map.Entry<String, String> entry : ((MapEnvironment) environment).getVariables().entrySet()) { | ||
retVal.append(prefix).append(entry.getKey()).append('=').append(entry.getValue()).append("\n"); | ||
} | ||
} else if (environment instanceof ModifiedEnvironment) { | ||
final ModifiedEnvironment modified = ((ModifiedEnvironment) environment); | ||
|
||
final String base = toString(modified.getBase(), prefix + "\t"); | ||
final boolean multiLineBase = base.contains("\n"); | ||
final int modifierCount = modified.getModifiers().size(); | ||
|
||
if (multiLineBase || modifierCount > 1) { | ||
retVal.append(prefix).append("modified:").append(multiLineBase ? '\n' : ' ').append(multiLineBase ? base : base.strip()).append('\n'); | ||
retVal.append(prefix).append("modifiers:\n"); | ||
for (Map.Entry<String, IEnvironmentModifier> entry : modified.getModifiers().entrySet()) { | ||
retVal.append(prefix).append('\t').append(entry.getKey()).append(" <- ").append(entry.getValue()).append('\n'); | ||
} | ||
} else if (modifierCount < 1) retVal.append(base.strip()); | ||
else { | ||
final Entry<String, IEnvironmentModifier> entry = HCollection.getOne(modified.getModifiers().entrySet()); | ||
retVal.append(prefix).append("modified ").append(base.strip()).append(" with ").append(entry.getKey()).append(" <- ").append(entry.getValue()); | ||
} | ||
} else if (environment instanceof SystemEnvironment) { | ||
retVal.append(prefix).append("system environment\n"); | ||
} else throw new NotYetImplementedError("Environments of type " + environment.getClass() + " are not yet supported!"); | ||
|
||
return retVal.toString().stripTrailing(); | ||
} | ||
|
||
protected final IConsumer1<String> log; | ||
|
||
protected final IRunner runner; | ||
|
||
@Override | ||
public IProcess apply(CommandInvocation<IRedirect, IRedirect> commandInvocation) { | ||
final IConsumer1<String> log = getLog(); | ||
log.accept("Running: " + commandInvocation.getArguments().stream().collect(Collectors.joining(" "))); | ||
if (commandInvocation.getWorking() != null) log.accept("\tin " + commandInvocation.getWorking()); | ||
final IEnvironment environment = commandInvocation.getEnvironment(); | ||
if ((environment != null) && !(commandInvocation.getEnvironment() instanceof SystemEnvironment)) { | ||
final String string = toString(environment, "\t\t"); | ||
if (string.contains("\n")) { | ||
log.accept("\tenvironment:"); | ||
for (String line : string.split("\n")) { | ||
log.accept(line); | ||
} | ||
} else log.accept("\tenvironment: " + string.substring(2 /* Remove the tabs */)); | ||
} | ||
return runner.apply(commandInvocation); | ||
} | ||
} |