- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello everyone,
How can I redirect the STDOUT and STDERR outputs to a file?
The usual method of command line redirection is not an option for me. This is because CMD is often blocked by antivirus systems, making it impractical in my work environment.
I appreciate any insights or suggestions you might have. Thank you in advance for your help!
Best regards,
Leigh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The crash dump can be directed to a file with FOR_DIAGNOSTIC_LOG_FILE=<filename> environment variable. You can set the env var from within the Fortran of you wish. I use the sdk "SetEnvironmentVariable" but there is also an IFPORT from memory setenvqq
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I thought a simple approach like opening files to these unit numbers would work, but the results are very mixed.
Here is my attempt:
! redir.f90 --
! Redirecting stdout and stderr?
!
program redir
use iso_fortran_env
implicit none
open( output_unit, file = 'redir.out' )
open( error_unit, file = 'redir.err' )
write(*,*) 'Should appear in the file "redir.out"'
write(error_unit,*) 'Should appear in the file "redir.err"'
!
! This does not appear there ...
!
error stop 'Should appear in the file "redir.err"'
end program redir
gfortran has the program write to "redir.out" and "redir.err", but not the output from error stop, whereas ifx and ifort write to "redir.err" but not to "redir.out" (that appears on the screen).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
An alternative might be to write a small Fortran program that runs the program in question via execute_command_line and does the redirecting, thereby avoiding the direct use of CMD.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Arjen_Markus,
Many thanks for your sample code.
What I want to achieve is to handle a fatal error, for example, divide by zero.
And have the error details saved to the error file.
I have added a divide by zero to your code, so it's now:
program redir
use iso_fortran_env
implicit none
integer :: a, b, result
open( output_unit, file = 'redir.out' )
open( error_unit, file = 'redir.err' )
write(*,*) 'Should appear in the file "redir.out"'
write(error_unit,*) 'Should appear in the file "redir.err"'
!Throw a Divide by Zero error
a = 10
b = 0
result = a / b
!
! This does not appear there ...
!
error stop 'Also Should appear in the file "redir.err"'
end program redir
The divide by zero happens on this line:
result = a / b
Execution cannot progress past this line:
I was hoping that the error information:
Should appear in the file "redir.out"
forrtl: severe (164): Program Exception - integer divide by zero
Image PC Routine Line Source
redirect_stdout_s 00E91187 _MAIN__ 17 redirect_stdout_stderr.f90
redirect_stdout_s 00E9126F Unknown Unknown Unknown
redirect_stdout_s 00E93AB3 Unknown Unknown Unknown
redirect_stdout_s 00E93987 Unknown Unknown Unknown
redirect_stdout_s 00E9382D Unknown Unknown Unknown
redirect_stdout_s 00E93B18 Unknown Unknown Unknown
KERNEL32.DLL 770405C9 Unknown Unknown Unknown
ntdll.dll 775478BD Unknown Unknown Unknown
ntdll.dll 7754788D Unknown Unknown Unknown
would appear in the file = 'redir.err', but it does not.
Regards,
Leigh
PS I received this message:
Your post has been changed because invalid HTML was found in the message body. The invalid HTML has been removed. Please review the message and submit the message when you are satisfied.
I don't know what the invalid HTML was?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
int filefd = open("foo.txt", O_WRONLY|O_CREAT, 0666);
if (!fork()) {
close(1);//Close stdout
dup(filefd);
execlp("ls", "ls", NULL);
} else {
close(filefd);
wait(NULL);
}
return 0;
You will likely have to copy something from C and there are many examples on the net, not sure how to do it in Fortran, but there are plenty here who can tell you if it possible or do a C dll and link it in.
I found two C examples in ten seconds, the other one is better but I lost it.
The HTML message means you copied something from a web page or xml file and Intel discards it and then asks you to confirm, the old days with the old Intel Forum were so much easier, sad really.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Bring back 1984, that is my response to Intel and the three stage entry.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I implemented a straightforward and somewhat naïve program to do what I meant.
! runprogram.f90 --
! Run a program without directly using CMD
!
program runprogram
implicit none
character(len=200) :: string
integer :: k
call get_command( string )
k = index( string, ' ' )
string = string(k+1:)
call execute_command_line( trim(string) // ' 1> runprogram.out 2> runprogram.err' )
end program runprogram
This does the trick, even though it will redirect the output to fixed files.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The crash dump can be directed to a file with FOR_DIAGNOSTIC_LOG_FILE=<filename> environment variable. You can set the env var from within the Fortran of you wish. I use the sdk "SetEnvironmentVariable" but there is also an IFPORT from memory setenvqq
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, Andrew,
The FOR_DIAGNOSTIC_LOG_FILE environment variable works a treat.
I defined it with SETENVQQ.
Regards,
Leigh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just a postscript:
The FOR_PRINT environment variable will capture stdout.
I was hoping to combine stdout and stderr, by setting FOR_PRINT and FOR_DIAGNOSTIC_LOG_FILE to the same file, but that did not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you want to write to unit * with redirection from OPEN, add /standard-semantics or /assume:noold_unit_star
However, this controls only output initiated by WRITE statements in your program - it won’t do anything for error messages.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page