Skip to content

Commit 724a416

Browse files
Copilotdahong67
andauthored
Add examples for caching stdout/stderr and log output (#30)
* Initial plan * Add example of caching printed output and logs to README Co-authored-by: dahong67 <[email protected]> * Improve example to use mktempdir for automatic cleanup Co-authored-by: dahong67 <[email protected]> * Replace file-based approach with IOCapture.jl for stdout/stderr capturing Co-authored-by: dahong67 <[email protected]> * Add installation note for IOCapture.jl in example Co-authored-by: dahong67 <[email protected]> * Split examples into separate stdout/stderr and logging sections Co-authored-by: dahong67 <[email protected]> * Review and revise new examples * Change order --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: dahong67 <[email protected]> Co-authored-by: David Hong <[email protected]> Co-authored-by: David Hong <[email protected]>
1 parent f4d8fc6 commit 724a416

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,85 @@ The next time this code is run, it simply
442442
loads and displays the saved HTML representation,
443443
which can be much faster!
444444
445+
## Example: Caching log messages
446+
447+
It can sometimes be useful to also save log messages (from `@info`, `@warn`, etc.).
448+
This can be accomplished by redirecting them to an `IOBuffer` with a `SimpleLogger`:
449+
450+
```julia
451+
using CacheVariables, Logging
452+
cache("simulation-with-logs.bson") do
453+
logs_io = IOBuffer()
454+
result = with_logger(SimpleLogger(logs_io)) do
455+
@info "something logged via @info"
456+
@warn "something logged via @warn"
457+
return "output of the computation"
458+
end
459+
return (; result=result, logs=String(take!(logs_io)))
460+
end
461+
```
462+
463+
## Example: Caching printed output (stdout/stderr)
464+
465+
It can sometimes be useful to also save things printed out to stdout/stderr.
466+
This can be accomplished by using [IOCapture](https://github.com/JuliaDocs/IOCapture.jl)
467+
as follows:
468+
469+
```julia
470+
using CacheVariables, IOCapture
471+
cache("simulation-with-output.bson") do
472+
IOCapture.capture(; color=true, passthrough=true) do
473+
println("something printed to stdout")
474+
println(stderr, "something printed to stderr")
475+
return "output of the computation"
476+
end
477+
end
478+
```
479+
480+
The `IOCapture.capture` block captures the output with everything printed to stdout/stderr
481+
and creates a `NamedTuple`. For example, the above block produces:
482+
483+
```julia
484+
(value = "output of the computation", output = "Something printed to stdout\nSomething printed to stderr\n", error = false, backtrace = Ptr{Nothing}[])
485+
```
486+
487+
The outer `cache` block then takes care of all the caching!
488+
489+
**Note:** IOCapture has some known limitations. Read their docs to learn more.
490+
491+
### Alternative approaches
492+
493+
Another package offering similar functionality is
494+
[Suppressor](https://github.com/JuliaIO/Suppressor.jl).
495+
496+
Another approach can be to directly call `redirect_stdio` and use temporary files, like so:
497+
498+
```julia
499+
cache("simulation-with-output.bson") do
500+
mktempdir() do tmpdir
501+
stdout_file = joinpath(tmpdir, "stdout.txt")
502+
stderr_file = joinpath(tmpdir, "stderr.txt")
503+
result = open(stdout_file, "w") do stdout_io
504+
open(stderr_file, "w") do stderr_io
505+
redirect_stdio(; stdout = stdout_io, stderr = stderr_io) do
506+
println("something printed to stdout")
507+
println(stderr, "something printed to stderr")
508+
return "output of the computation"
509+
end
510+
end
511+
end
512+
return (;
513+
result = result,
514+
stdout = read(stdout_file, String),
515+
stderr = read(stderr_file, String),
516+
)
517+
end
518+
end
519+
```
520+
521+
At the time of writing, `redirect_stdio` does not seem to support `IOBuffer`s directly yet:
522+
https://github.com/JuliaLang/julia/issues/12711
523+
445524
## Related packages
446525
447526
- [Memoization.jl](https://github.com/marius311/Memoization.jl)

0 commit comments

Comments
 (0)