I must be doing something stupid. This is what I typed into the console:
~~~
function er(f::Function)
orig_stderr = STDERR
rd, wr = redirect_stderr()
f()
redirect_stderr(orig_stderr)
return readavailable(rd)
end
macro er(expr)
:(er(()->$expr)) |> esc
end
err = @er begin 1^-1 end
err
~~~
I only get:
ERROR: err not defined
Could it be some problem with the Windows console or the new REPL? (I coded
similar functions a couple of months ago, and they did work. Not anymore)
On Friday, June 20, 2014 3:40:03 PM UTC-6, Mike Innes wrote:
>
> The macro expression itself will return the output string. So if you type
> it into a repl you'll get something like
>
> julia> @with_err_str warn("foo")
> "\e[1m\e[31mWARNING: foo\n\e[0m"
>
> If you want to capture that string you'd just assign it to a variable:
>
> julia> err = @with_err_str warn("foo");
>
> One thing to be careful of is that those ANSI codes may not be included
> when running the tests.
>
>
> On 20 June 2014 22:29, Laszlo Hars <[email protected] <javascript:>>
> wrote:
>
>> I am confused: I can change all the "out"s to "err"s, but what variable
>> will contain the error message to be inspected? The error messages seem to
>> only appear in the console, nowhere else.
>>
>>
>> On Friday, June 20, 2014 2:47:28 PM UTC-6, Mike Innes wrote:
>>>
>>> function with_out_str(f::Function)
>>> orig_stdout = STDOUT
>>> rd, wr = redirect_stdout()
>>> f()
>>> redirect_stdout(orig_stdout)
>>> return readavailable(rd)
>>> end
>>>
>>> macro with_out_str(expr)
>>> :(with_out_str(()->$expr)) |> esc
>>> end
>>>
>>> You can use this as
>>>
>>> @with_out_str begin
>>> ... code ...
>>> end
>>>
>>> But I think you'll need to change "stdout" to "stderr" in the above
>>> definition to capture warnings.
>>>
>>> On Friday, 20 June 2014 21:35:51 UTC+1, Laszlo Hars wrote:
>>>>
>>>> Could someone help with redirecting stderr? For example, the following
>>>> code does not get the error message shown in the Julia console in Windows
>>>> 7, Julia Version 0.3.0-prerelease+3789:
>>>> ~~~
>>>> stderr_orig = STDERR
>>>> rd, wr = redirect_stderr()
>>>> 1^-1
>>>> close(wr)
>>>> eof(rd)
>>>> close(rd)
>>>> out = readall(rd)
>>>> redirect_stderr(stderr_orig)
>>>> ~~~
>>>>
>>>> On Friday, June 20, 2014 8:36:44 AM UTC-6, Jameson wrote:
>>>>>
>>>>> You could redirect_stderr and test for content writte
>>>>
>>>>
>