In R there are two function to print a message in the console: cat and message. The message function is better because it is possible to turn off the message it displays (with the suppressMessages function). This is useful in a Rmarkdown or Quarto document, there’s a chunk option to suppress the messages.
I did a function which prints some messages with printstyled. Is it possible to turn off these messages? Obviously I could add an argument messages = true/false in my function but this is not what I want.
julia> macro noprint(expr)
quote
let so = stdout
redirect_stdout(devnull)
res = $(esc(expr))
redirect_stdout(so)
res
end
end
end
@noprint (macro with 1 method)
julia> output(x) = printstyled(x; color=1)
output (generic function with 1 method)
julia> output("Hello")
Hello
julia> @noprint output("Hello")
julia> using Logging, LoggingExtras
julia> log_fmtr = FormatLogger() do io, args
printstyled(io, "$(args.level): $(args.message)\n"; color=1)
end
FormatLogger(var"#186#187"(), Base.TTY(Base.Libc.WindowsRawSocket(0x0000000000000270) open, 0 bytes waiting), true)
julia> with_logger(log_fmtr) do; @info "This is in red." end
Info: This is in red.