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 <laszloh...@gmail.com> 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
>>>
>>>

Reply via email to