Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
89 user(s) are online (75 user(s) are browsing Forums)

Members: 0
Guests: 89

more...

Support us!

Headlines

 
  Register To Post  

Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
Is it possible to redirect STDERR to the same file as STDOUT.

The equivalent command in linux:
./myProgram 1>log 2>&1


I have looked at the Amiga wiki, but it does not seem to mention it. It seems that that STDOUT can be redirected to a file, of course, but STDERR cannot be redirected to the same file.

#include <stdio.h>
int main()
{
    
fprintf(stdout,"STDOUT\n");
    
fprintf(stderr,"STDERR\n");
    return 
0;
}

Then,
./myProgram out *>out

Results in:
main.exeunable to open redirection file


See also: https://eab.abime.net/showthread.php?t=100148

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Just can't stay away
Just can't stay away


See User information
@rjd324
Redirecting stdin and stdout to the same file is possible and often used ("run <>NIL: foobar"), should be possible with stdout and sdterr as well, and with all 3.
Try something like "foobar *>>out", but that might only redirect stderr but in appending mode like "foobar >>out" does for stdout, or "foobar >*>out".

Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
I still cannot figure out what syntax to actually use to do this. You are right, STDOUT,STDIN seems to work okay with that syntax and this is often done, but redirecting STDOUT and STDERR to the same file seems impossible through the shell.

But, this test program demonstrates that it is somehow possible:
#include <proto/dos.h>

BPTR _stdout,_stderr;
int ret=0;
int main()
{
    
_stdout=IDOS->Open("_stdout",MODE_NEWFILE);
    
_stderr=
#if 0
          
*IDOS->Open("_stderr",MODE_NEWFILE)
#else
          
_stdout
#endif
          
;
    if(!(
_stdout&&_stderr))
    {
        
ret=10;
        goto 
ENDER;
    }
    (
void)IDOS->SystemTags("dir NOTEXISTS",SYS_Output,_stdout,SYS_Error,_stderr,TAG_END);
ENDER:
    if(
_stdout
    { 
        
IDOS->FClose(_stdout);
        if(
_stdout==_stderr)
        {
            
_stderr=_stdout=(BPTR)0
        }
        else
        {
            
_stdout=(BPTR)0;
        }
    }
    if(
_stderr) { IDOS->FClose(_stderr); _stderr=(BPTR)0; }
    return 
ret;
}


This program invokes a DIR on some non-existent file. That always results in some text to both STDOUT and STDERR.

You will see that when you run this program both STDOUT and STDERR are in the same file.

I am surprised no one else has run into this issue since redirection from both of this FDs into a single location is fairly usual.

I guess - at the least - I could upload a similar "tool" to handle such a situation.

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Just can't stay away
Just can't stay away


See User information
@rjd324

Have you tried this?

dir xx >> RAM:p *>> RAM:p

Works for me.

Best regards,

Niels

Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
@nbache

Thanks, it "kind of" work. For some reason the STDOUT portion is truncated and says, for example:
ormation for bb

instead of:
Could not get information for bb


On top of which, I do not see why you would need that syntax.

Why append? This is not what I would have expected.

But, thanks - you almost got us there.

===

Perhaps only appending one of them would work. The test program I wrote above does it correctly - so why cannot Shell?

Is this some sort of bug?

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Just can't stay away
Just can't stay away


See User information
@rjd324

I can't reproduce your truncation:

Ny Shell-procesnr22
22.WB
-F0 0 [0,000.396Mandag 12/06-2023 00:36:10 
delete RAM:
22.WB
-F0 0 [0,000.585Mandag 12/06-2023 00:36:20 
dir xx >> RAM:*>> RAM:p
22.WB
-F20 205 [0,000.646Mandag 12/06-2023 00:36:30 
type RAM:p
DIR
Kunne ikke finde oplysninger om "xx".
DIRobjektet findes ikke
22.WB
-F0 0 [0,001.372Mandag 12/06-2023 00:36:36 
$


Quote:
Why append? This is not what I would have expected.
Perhaps because if you don't append, but just write, the first redirection (the STDOUT) locks the file so the second one (STDERR) can't write to it. And maybe they both try to verify that they can get their locks before either one starts to actually open (create) the file, because it seems to fail without even creating the file.

And that probably also means it wouldn't help using append only for one of them.

But I'm guessing.

Best regards,

Niels

Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
Perhaps you cannot reproduce it because you are not using the Enhancer version 54.5.

@cnicol
It looks like when I use the classic version 53.3 I see that the output looks reasonable. When I use 54.5 I get the truncation I talked about.

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
Truncation also seems to occur for version LIST 54.15 versus 53.10.

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Home away from home
Home away from home


See User information
The core problem with using the syntax

Command >ram:log *>>ram:log

is that you now have two streams writing to the same file. Neither stream knows what the other has written, so parts of the output will be overwritten.

To expand your simple test above.

#include    <stdio.h>
int main()
{
    
fprintf(stdout,"STDOUT1\n");
    
fflush(stdout);
    
fprintf(stderr,"STDERR1\n");
    
fflush(stderr);
    
fprintf(stdout,"STDOUT2\n");
    
fflush(stdout);
    
fprintf(stderr,"STDERR2\n");
    
fflush(stderr);
    
fprintf(stdout,"STDOUT3\n");
    
fflush(stdout);
    return 
0;
}


9.AmigaOS4:> gcc -o ram:testit test.
9.AmigaOS4
:> ram:testit  >RAM:LOG *>>RAM:LOG
9.AmigaOS4
:> type ram:LOG 
STDERR1
STDERR2
STDOUT3


You need to redirect stderr to stdouts stream. Not 100% sure if you can do that at the shell level.

Historicaly in AmigaDOS pre AmigaOS 4 shells STDERR and STDOUT were the same stream so no extra sysntax would be needed.

Go to top
Re: Redirect STDERR and STDOUT to the same file
Home away from home
Home away from home


See User information
@rjd324

I had a similar problem when automating the logs from scummvm compilations.

Some stuff still ended up in shell instead of the log file (or was partly overwritten too)

What I did was to write to two different log files and merge them after the compilation/command is done.

Command >ram:1st.log *>ram:2nd.log
Type ram:2nd.log >> ram:1st.log

Probably not exactly what you're looking for, but maybe enough to move on?

Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
@Raziel

Hi, yes I am aware that printing to separate files works but thank you for the suggestion; the issue being that interleaving of output is trashed in the sense that if you append the file containing only the STDERR to the file containing on the STDOUT (vice versa) then you no longer get the true time of which the information was output.

I am extending that test program written above and will need to test out some more complex use-cases. That program seems to be handling the situation but I have not tried more extensive testing.

If it does, then I guess I will can upload that program and I will probably name it "&>". As in:
&> <OUT_FILE> <COMMAND>

Where <COMMAND> can take any number of white spaced tokens etc.

If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Home away from home
Home away from home


See User information
"&>"

Don't name it that

Weirldy named commands will only cause issues.

Go to top
Re: Redirect STDERR and STDOUT to the same file
Home away from home
Home away from home


See User information
In the absense of a proper solution to redirection STderr to Stdout, what you really need is a custom PIPE: that has two inputs amd one output that can mux the output into a single stream.

Go to top
Re: Redirect STDERR and STDOUT to the same file
Home away from home
Home away from home


See User information
Instead of writing a program why not just wrap yout command in an abc-shel call

sh -c "testit 2>&1" >raM:log

Go to top
Re: Redirect STDERR and STDOUT to the same file
Quite a regular
Quite a regular


See User information
@broadblues

Of course the name was a joke. A reference to the shorthand &> that you can do in Linux, even though I always explicitly perform 1> 2>&1.

This program was just a little test anyway. You are right, I will mess around with PIPES and see what happens.

Using "sh" is not the point. I am talking about native DOS.

Anyway, thanks for the tips.

---

But then again AmigaOS is not Linux and not POSIX...


Edited by rjd324 on 2023/6/12 12:01:06
If liberty means anything at all, it means the right to tell people what they do not want to hear.
George Orwell.
Go to top
Re: Redirect STDERR and STDOUT to the same file
Just popping in
Just popping in


See User information
@rjd324

I have reproduced the truncation issue with both Dir 54.7 and List 54.18.

It shouldn't be hard to fix.

Thanks for pointing it out.

Costas.

Go to top
Re: Redirect STDERR and STDOUT to the same file
Just can't stay away
Just can't stay away


See User information
@rjd324

Quote:
Perhaps you cannot reproduce it because you are not using the Enhancer version 54.5.
That's correct, I don't.

Well, you know where to go with that one, then.

Edit: Ah, I see Costas is already on it .

Best regards,

Niels

Go to top
Re: Redirect STDERR and STDOUT to the same file
Just can't stay away
Just can't stay away


See User information
@broadblues

Quote:
sh -c "testit 2>&1" >raM:log


Doing this type of syntax in a shell sure works out. For simple cases.

This is a line from toolchain.prf in qmake :

Quote:

output = $$system("$$cmd_prefix $$QMAKE_CXX $$qtMakeExpand($$cxx_flags) -xc++ - 2>&1 $$cmd_suffix", lines, ec)


This proves to be intensely problematic, because of a) the environment prefix (cmd_prefix) and b) the way quotes are used. Or maybe I am just stupid.

Some way of emulating 2>&1 would be preferable.

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project