Skip to content

Commit f16b438

Browse files
committed
✨ --log to capture output to log file
1 parent 3b7420a commit f16b438

File tree

5 files changed

+64
-12
lines changed

5 files changed

+64
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ native-image-macOS-logs
6666
/javadoc.*
6767
/options
6868
/packages
69+
ttrpg-convert.out

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ The process is similar for other homebrew, including your own, so long as it is
330330
- There is a `#cli-support` thread in the `#tabletop-games` channel of the [Obsidian Discord](https://discord.gg/veuWUTm).
331331
- There is a `TTRPG-convert-help` post in the `obsidian-support` forum of the [Obsidian TTRPG Community Discord](https://discord.gg/Zpmr37Uv).
332332
- There is a TTRPG-convert tutorial (currently aimed at Windows users, but much of it is helpful no matter your OS) at [Obsidian TTRPG Tutorials](https://obsidianttrpgtutorials.com/Obsidian+TTRPG+Tutorials/Plugin+Tutorials/TTRPG-Convert-CLI/TTRPG-Convert-CLI+5e).
333+
- If you open an issue for an error, run with the `--debug` and `--log` options, and attach the log file to the issue.
333334

334335
### Want to help fix it?
335336

src/main/java/dev/ebullient/convert/RpgDataConvertCli.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ public class RpgDataConvertCli implements Callable<Integer>, QuarkusApplication
9393
@Option(names = { "-v", "--verbose" }, description = "Verbose output", defaultValue = "false", scope = ScopeType.INHERIT)
9494
boolean verbose;
9595

96+
@Option(names = { "-l",
97+
"--log" }, description = "Capture output in a log file", defaultValue = "false", scope = ScopeType.INHERIT)
98+
boolean log;
99+
96100
Datasource game;
97101

98102
@Option(names = { "-g",
@@ -264,7 +268,7 @@ public Integer call() {
264268

265269
private int executionStrategy(ParseResult parseResult) {
266270
try {
267-
tui.init(spec, debug, verbose);
271+
tui.init(spec, debug, verbose, log);
268272
return new CommandLine.RunLast().execute(parseResult);
269273
} finally {
270274
tui.close();

src/main/java/dev/ebullient/convert/io/Tui.java

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Stream;
2727

2828
import jakarta.enterprise.context.ApplicationScoped;
29+
import jakarta.enterprise.event.Observes;
2930

3031
import org.yaml.snakeyaml.DumperOptions;
3132
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
@@ -52,6 +53,7 @@
5253
import dev.ebullient.convert.qute.ImageRef;
5354
import dev.ebullient.convert.qute.QuteBase;
5455
import dev.ebullient.convert.qute.QuteNote;
56+
import io.quarkus.runtime.ShutdownEvent;
5557
import picocli.CommandLine;
5658
import picocli.CommandLine.Help;
5759
import picocli.CommandLine.Help.Ansi;
@@ -170,6 +172,7 @@ public static String toAnchorTag(String x) {
170172
Ansi ansi;
171173
ColorScheme colors;
172174

175+
PrintWriter log;
173176
PrintWriter out;
174177
PrintWriter err;
175178

@@ -193,6 +196,10 @@ public Tui() {
193196
}
194197

195198
public void init(CommandSpec spec, boolean debug, boolean verbose) {
199+
init(spec, debug, verbose, false);
200+
}
201+
202+
public void init(CommandSpec spec, boolean debug, boolean verbose, boolean log) {
196203
if (spec != null) {
197204
this.ansi = spec.commandLine().getHelp().ansi();
198205
this.colors = spec.commandLine().getHelp().colorScheme();
@@ -203,6 +210,18 @@ public void init(CommandSpec spec, boolean debug, boolean verbose) {
203210

204211
this.debug = debug;
205212
this.verbose = verbose;
213+
if (log) {
214+
Path p = Path.of("ttrpg-convert.out");
215+
try {
216+
this.log = new PrintWriter(Files.newOutputStream(p));
217+
} catch (IOException e) {
218+
errorf(e, "Unable to open log file %s: %s", p.toAbsolutePath(), e.toString());
219+
}
220+
}
221+
}
222+
223+
void onShutdown(@Observes ShutdownEvent event) {
224+
this.close();
206225
}
207226

208227
public void setOutputPath(Path output) {
@@ -214,8 +233,32 @@ public void setTemplates(Templates templates) {
214233
}
215234

216235
public void close() {
236+
flush();
237+
if (this.log != null) {
238+
log.close();
239+
}
240+
}
241+
242+
public void flush() {
217243
out.flush();
218244
err.flush();
245+
if (log != null) {
246+
log.flush();
247+
}
248+
}
249+
250+
private void outLine(Object line) {
251+
out.println(line);
252+
if (log != null) {
253+
log.println(line);
254+
}
255+
}
256+
257+
private void errLine(Object line) {
258+
err.println(line);
259+
if (log != null) {
260+
log.println(line);
261+
}
219262
}
220263

221264
public boolean isDebug() {
@@ -230,7 +273,7 @@ public void debugf(String format, Object... params) {
230273

231274
public void debug(String output) {
232275
if (isDebug()) {
233-
out.println(ansi.new Text("@|faint 🔧 " + output + "|@", colors));
276+
outLine(ansi.new Text("@|faint 🔧 " + output + "|@", colors));
234277
}
235278
}
236279

@@ -246,7 +289,7 @@ public void verbosef(String format, Object... params) {
246289

247290
public void verbose(String output) {
248291
if (isVerbose()) {
249-
out.println(ansi.new Text("@|faint 🔹 " + output + "|@", colors));
292+
outLine(ansi.new Text("@|faint 🔹 " + output + "|@", colors));
250293
}
251294
}
252295

@@ -255,29 +298,29 @@ public void warnf(String format, Object... params) {
255298
}
256299

257300
public void warn(String output) {
258-
out.println(ansi.new Text("[🔸 WARN] " + output));
301+
outLine(ansi.new Text("[🔸 WARN] " + output));
259302
}
260303

261304
public void donef(String format, Object... params) {
262305
done(String.format(format, params));
263306
}
264307

265308
public void done(String output) {
266-
out.println(ansi.new Text("[ ✅ OK] " + output));
309+
outLine(ansi.new Text("[ ✅ OK] " + output));
267310
}
268311

269312
public void printlnf(String format, Object... args) {
270313
println(String.format(format, args));
271314
}
272315

273316
public void println(String output) {
274-
out.println(ansi.new Text(output, colors));
275-
out.flush();
317+
outLine(ansi.new Text(output, colors));
318+
flush();
276319
}
277320

278321
public void println(String... output) {
279-
Arrays.stream(output).forEach(l -> out.println(ansi.new Text(l, colors)));
280-
out.flush();
322+
Arrays.stream(output).forEach(l -> outLine(ansi.new Text(l, colors)));
323+
flush();
281324
}
282325

283326
public void errorf(String format, Object... args) {
@@ -293,11 +336,14 @@ public void error(String errorMsg) {
293336
}
294337

295338
public void error(Throwable ex, String errorMsg) {
296-
err.println(colors.errorText("[ 🛑 ERR] " + errorMsg));
339+
errLine(colors.errorText("[ 🛑 ERR] " + errorMsg));
297340
if (ex != null && isDebug()) {
298341
ex.printStackTrace(err);
342+
if (log != null) {
343+
ex.printStackTrace(log);
344+
}
299345
}
300-
err.flush();
346+
flush();
301347
}
302348

303349
public void throwInvalidArgumentException(String message) {

src/test/java/dev/ebullient/convert/Tools5eDataConvertTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void testLiveData_5eHomebrew(QuarkusMainLauncher launcher) {
127127
Path target = testOutput.resolve("homebrew");
128128
TestUtils.deleteDir(target);
129129

130-
LaunchResult result = launcher.launch("--debug", "--index",
130+
LaunchResult result = launcher.launch("--debug", "--index", "--log",
131131
"-c", TestUtils.TEST_RESOURCES.resolve("sources-homebrew.json").toString(),
132132
"-o", target.toString(),
133133
TestUtils.TEST_RESOURCES.resolve("images-remote.json").toString(),

0 commit comments

Comments
 (0)